diff --git a/src/http.c b/src/http.c index 7e99e12..b6a2077 100644 --- a/src/http.c +++ b/src/http.c @@ -19,14 +19,89 @@ // Standard library includes. #include #include +#include // Local includes #include "constants.h" +#define REQUEST_TYPE_BUFFER_SIZE 16 +#define REQUEST_PATH_BUFFER_SIZE 256 +#define REQUEST_PROTO_BUFFER_SIZE 16 + char *c_simple_http_request_response( const char *request, unsigned int size, const C_SIMPLE_HTTP_HTTPTemplates *templates) { + // parse first line. + unsigned int idx = 0; + char request_type[REQUEST_TYPE_BUFFER_SIZE] = {0}; + unsigned int request_type_idx = 0; + for (; idx < size + && request[idx] != ' ' + && request[idx] != '\n' + && request[idx] != '\r' + && request[idx] != '\t' + && request_type_idx < REQUEST_TYPE_BUFFER_SIZE - 1; + ++idx) { + request_type[request_type_idx++] = request[idx]; + } + if (request_type_idx == 0) { + return NULL; + } +#ifndef NDEBUG + fprintf(stderr, "Parsing request: got type \"%s\"\n", request_type); +#endif + // skip whitespace until next part. + for (; idx < size + && (request[idx] == ' ' || + request[idx] == '\n' || + request[idx] == '\r' || + request[idx] == '\t'); + ++idx) {} + // parse request path. + char request_path[REQUEST_PATH_BUFFER_SIZE] = {0}; + unsigned int request_path_idx = 0; + for (; idx < size + && request[idx] != ' ' + && request[idx] != '\n' + && request[idx] != '\r' + && request[idx] != '\t' + && request_path_idx < REQUEST_PATH_BUFFER_SIZE - 1; + ++idx) { + request_path[request_path_idx++] = request[idx]; + } + if (request_path_idx == 0) { + return NULL; + } +#ifndef NDEBUG + fprintf(stderr, "Parsing request: got path \"%s\"\n", request_path); +#endif + // skip whitespace until next part. + for (; idx < size + && (request[idx] == ' ' || + request[idx] == '\n' || + request[idx] == '\r' || + request[idx] == '\t'); + ++idx) {} + // parse request http protocol. + char request_proto[REQUEST_PROTO_BUFFER_SIZE] = {0}; + unsigned int request_proto_idx = 0; + for (; idx < size + && request[idx] != ' ' + && request[idx] != '\n' + && request[idx] != '\r' + && request[idx] != '\t' + && request_proto_idx < REQUEST_PROTO_BUFFER_SIZE - 1; + ++idx) { + request_proto[request_proto_idx++] = request[idx]; + } + if (request_proto_idx == 0) { + return NULL; + } +#ifndef NDEBUG + fprintf(stderr, "Parsing request: got http protocol \"%s\"\n", request_proto); +#endif + // TODO return NULL; } diff --git a/src/main.c b/src/main.c index 0f0730a..ae0e1dc 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ #include "signal_handling.h" #include "globals.h" #include "constants.h" +#include "http.h" int main(int argc, char **argv) { Args args = parse_args(argc, argv); @@ -83,17 +84,24 @@ int main(int argc, char **argv) { int connection_fd = ret; read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE); // DEBUG print received buf. - // TODO Validate request and send response. for (unsigned int idx = 0; idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret; ++idx) { - if (recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E) { + if ((recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E) + || recv_buf[idx] == '\n' || recv_buf[idx] == '\r') { printf("%c", recv_buf[idx]); } else { break; } } puts(""); + // TODO Validate request and send response. + // TODO WIP + const char *response = c_simple_http_request_response( + (const char*)recv_buf, read_ret, NULL); + if (response) { + free((void*)response); + } close(connection_fd); } else { printf("WARNING: accept: Unknown invalid state!\n");