adds some file stuff and cleans up a bit
This commit is contained in:
parent
97d970c0cd
commit
18579c7981
@ -1,7 +1,7 @@
|
|||||||
TARGET=http
|
TARGET=http
|
||||||
CC=gcc
|
CC=gcc
|
||||||
|
|
||||||
OBJ = main.o tcp.o http.o
|
OBJ = main.o tcp.o http.o file.o
|
||||||
|
|
||||||
$(TARGET): $(OBJ)
|
$(TARGET): $(OBJ)
|
||||||
mkdir -p ../build
|
mkdir -p ../build
|
||||||
@ -11,6 +11,7 @@ $(TARGET): $(OBJ)
|
|||||||
main.o: include/tcp.h
|
main.o: include/tcp.h
|
||||||
tcp.o: include/tcp.h include/http.h
|
tcp.o: include/tcp.h include/http.h
|
||||||
http.o: include/http.h
|
http.o: include/http.h
|
||||||
|
file.o: include/file.h
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
|
62
src/file.c
Normal file
62
src/file.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "include/file.h"
|
||||||
|
|
||||||
|
void get_file_from_uri(char** uri, char** buffer)
|
||||||
|
{
|
||||||
|
char* tok;
|
||||||
|
FILE* f;
|
||||||
|
int err;
|
||||||
|
size_t bufsize, newlen;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// this is assuming absPath, NOT abs uri
|
||||||
|
//
|
||||||
|
// absURI is ONLY generate them in requests to proxies, otherwise
|
||||||
|
// will be an abs path
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: if no file is specified, append index.html
|
||||||
|
*/
|
||||||
|
f = fopen(*uri, "r");
|
||||||
|
if (f == NULL) {
|
||||||
|
perror("fopen");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fseek(f, 0L, SEEK_END); // go to the end of the file
|
||||||
|
if (err != 0) {
|
||||||
|
perror("fseek");
|
||||||
|
fclose(f);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
bufsize = ftell(f); // lets get the length of the file here
|
||||||
|
if (bufsize == -1) {
|
||||||
|
perror("ftell");
|
||||||
|
fclose(f);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
(*buffer) = malloc(sizeof(char) * (bufsize + 1));
|
||||||
|
if (*buffer == NULL) {
|
||||||
|
perror("malloc");
|
||||||
|
fclose(f);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
rewind(f); // go back to the beginning of the file
|
||||||
|
|
||||||
|
newlen = fread(*buffer, sizeof(char), bufsize, f);
|
||||||
|
if (ferror(f) != 0) {
|
||||||
|
perror("fread");
|
||||||
|
fclose(f);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
} else {
|
||||||
|
(*buffer)[newlen++] = '\0'; // this is just to be safe
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
}
|
12
src/http.c
12
src/http.c
@ -132,6 +132,11 @@ const char* http_status_code_to_str(http_status_code_t code)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_http_request(http_request* request)
|
||||||
|
{
|
||||||
|
free(request->request_line.request_uri);
|
||||||
|
}
|
||||||
|
|
||||||
void parse_request_line(char** line, http_request_line* request)
|
void parse_request_line(char** line, http_request_line* request)
|
||||||
{
|
{
|
||||||
char *tok, *ver, *num;
|
char *tok, *ver, *num;
|
||||||
@ -185,12 +190,13 @@ void parse_request_line(char** line, http_request_line* request)
|
|||||||
request->version.minor = atoi(num);
|
request->version.minor = atoi(num);
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_http_request(char** buffer, http_request_line* request)
|
void parse_http_request(char** buffer, http_request* request)
|
||||||
{
|
{
|
||||||
// going to be using strtok here to split up the request
|
// going to be using strtok here to split up the request
|
||||||
|
|
||||||
// first lets split each line
|
// first lets split each line
|
||||||
char* line;
|
char* line;
|
||||||
|
http_request_line r_line;
|
||||||
|
|
||||||
// parse the first line
|
// parse the first line
|
||||||
line = strtok(*buffer, "\n");
|
line = strtok(*buffer, "\n");
|
||||||
@ -199,7 +205,9 @@ void parse_http_request(char** buffer, http_request_line* request)
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_request_line(&line, request);
|
parse_request_line(&line, &r_line);
|
||||||
|
|
||||||
|
request->request_line = r_line;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: start accepting the headers
|
* TODO: start accepting the headers
|
||||||
|
6
src/include/file.h
Normal file
6
src/include/file.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef FILE_H
|
||||||
|
#define FILE_H
|
||||||
|
|
||||||
|
void get_file_from_uri(char** uri, char** buffer);
|
||||||
|
|
||||||
|
#endif
|
@ -49,15 +49,17 @@ typedef struct {
|
|||||||
} http_status_line;
|
} http_status_line;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
struct http_request_line;
|
http_request_line request_line;
|
||||||
struct general_headers;
|
struct general_headers general_headers;
|
||||||
struct requests_headers;
|
struct request_headers request_headers;
|
||||||
} http_request;
|
} http_request;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
http_status_line status;
|
http_status_line status;
|
||||||
struct general_headers general;
|
struct general_headers general;
|
||||||
struct response_headers response;
|
struct response_headers response;
|
||||||
|
size_t message_len;
|
||||||
|
char* message_body;
|
||||||
} http_response;
|
} http_response;
|
||||||
|
|
||||||
|
|
||||||
@ -68,6 +70,6 @@ const char* http_status_code_to_str(http_status_code_t code);
|
|||||||
|
|
||||||
void parse_request_line(char** line, http_request_line* request);
|
void parse_request_line(char** line, http_request_line* request);
|
||||||
|
|
||||||
void parse_http_request(char** buffer, http_request_line* request);
|
void parse_http_request(char** buffer, http_request* request);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
14
src/main.c
14
src/main.c
@ -5,6 +5,20 @@
|
|||||||
|
|
||||||
#include "include/tcp.h"
|
#include "include/tcp.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* we need to parse the request headers and put them into a struct
|
||||||
|
*
|
||||||
|
* also need to implement the features the headers are requesting
|
||||||
|
*
|
||||||
|
* for now we're just using the request line and the status line for basic info
|
||||||
|
* but eventually i would like to start implementing headers
|
||||||
|
*
|
||||||
|
* file config for port, file location, etc
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
server_conn_t server;
|
server_conn_t server;
|
||||||
|
20
src/tcp.c
20
src/tcp.c
@ -92,9 +92,11 @@ void* connection_handler(void* arg)
|
|||||||
{
|
{
|
||||||
int client_socket = *(int*) arg;
|
int client_socket = *(int*) arg;
|
||||||
http_request request;
|
http_request request;
|
||||||
|
//http_request_line request;
|
||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
// recv message
|
||||||
err = recv_message(&buffer, client_socket);
|
err = recv_message(&buffer, client_socket);
|
||||||
if (err == -1) { // if we had a recv error lets throw away the result
|
if (err == -1) { // if we had a recv error lets throw away the result
|
||||||
free(buffer);
|
free(buffer);
|
||||||
@ -102,20 +104,24 @@ void* connection_handler(void* arg)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
puts(buffer);
|
// puts(buffer);
|
||||||
|
|
||||||
parse_http_request(&buffer, &request);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle HTTP request here :)
|
* Handle HTTP request here :)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// recv message
|
|
||||||
|
|
||||||
// parse
|
parse_http_request(&buffer, &request);
|
||||||
//
|
|
||||||
|
printf("%s %s %d.%d\n", http_method_to_str(request.request_line.method),
|
||||||
|
request.request_line.request_uri, request.request_line.version.major,
|
||||||
|
request.request_line.version.minor);
|
||||||
|
|
||||||
// create payload
|
// create payload
|
||||||
//
|
|
||||||
|
// get the file
|
||||||
|
|
||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user