diff --git a/src/Makefile b/src/Makefile index 266d5f3..c58bff4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ TARGET=http CC=gcc -OBJ = main.o tcp.o http.o +OBJ = main.o tcp.o http.o file.o $(TARGET): $(OBJ) mkdir -p ../build @@ -11,6 +11,7 @@ $(TARGET): $(OBJ) main.o: include/tcp.h tcp.o: include/tcp.h include/http.h http.o: include/http.h +file.o: include/file.h .PHONY: clean clean: diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..1574153 --- /dev/null +++ b/src/file.c @@ -0,0 +1,62 @@ +#include +#include + +#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); +} diff --git a/src/http.c b/src/http.c index bf1006d..253061b 100644 --- a/src/http.c +++ b/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) { char *tok, *ver, *num; @@ -185,12 +190,13 @@ void parse_request_line(char** line, http_request_line* request) 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 // first lets split each line char* line; + http_request_line r_line; // parse the first line line = strtok(*buffer, "\n"); @@ -199,7 +205,9 @@ void parse_http_request(char** buffer, http_request_line* request) exit(EXIT_FAILURE); } - parse_request_line(&line, request); + parse_request_line(&line, &r_line); + + request->request_line = r_line; /** * TODO: start accepting the headers diff --git a/src/include/file.h b/src/include/file.h new file mode 100644 index 0000000..91e3957 --- /dev/null +++ b/src/include/file.h @@ -0,0 +1,6 @@ +#ifndef FILE_H +#define FILE_H + +void get_file_from_uri(char** uri, char** buffer); + +#endif diff --git a/src/include/http.h b/src/include/http.h index 063ecda..274d9af 100644 --- a/src/include/http.h +++ b/src/include/http.h @@ -49,15 +49,17 @@ typedef struct { } http_status_line; typedef struct { - struct http_request_line; - struct general_headers; - struct requests_headers; + http_request_line request_line; + struct general_headers general_headers; + struct request_headers request_headers; } http_request; typedef struct { http_status_line status; struct general_headers general; struct response_headers response; + size_t message_len; + char* message_body; } 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_http_request(char** buffer, http_request_line* request); +void parse_http_request(char** buffer, http_request* request); #endif diff --git a/src/main.c b/src/main.c index 72762d9..829bda1 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,20 @@ #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) { server_conn_t server; diff --git a/src/tcp.c b/src/tcp.c index 84ef4fa..1a53838 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -92,9 +92,11 @@ void* connection_handler(void* arg) { int client_socket = *(int*) arg; http_request request; + //http_request_line request; char* buffer = NULL; int err; + // recv message err = recv_message(&buffer, client_socket); if (err == -1) { // if we had a recv error lets throw away the result free(buffer); @@ -102,20 +104,24 @@ void* connection_handler(void* arg) return NULL; } - puts(buffer); - - parse_http_request(&buffer, &request); + // puts(buffer); /** * 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 - // + + // get the file + + // send payload free(buffer);