diff --git a/include/server/server.h b/include/server/server.h index 913021d..11509fd 100644 --- a/include/server/server.h +++ b/include/server/server.h @@ -15,6 +15,9 @@ /* Wait forever (epoll_wait()) */ #define CWS_SERVER_EPOLL_TIMEOUT -1 +/* Main server loop */ +extern volatile bool cws_server_run; + /** * @brief Runs the server * @@ -40,12 +43,6 @@ void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *host */ void cws_server_loop(int sockfd); -// @TODO -/** - * @brief Cleanup server's resources - */ -void cws_server_cleanup(); - /** * @brief Adds a file descriptor to the interest list * diff --git a/include/utils/hashmap.h b/include/utils/hashmap.h index a8f045d..e110ec1 100644 --- a/include/utils/hashmap.h +++ b/include/utils/hashmap.h @@ -8,7 +8,7 @@ #define CWS_HASHMAP_MAX_CLIENTS 1024 /** - * @brief Hash map struct + * @brief Client Hashmap struct * */ typedef struct cws_bucket_t { diff --git a/src/http/http.c b/src/http/http.c index 8008699..60a2994 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -33,7 +33,6 @@ cws_http *cws_http_parse(char *request_str, int sockfd) { strncpy(request->location, pch, CWS_HTTP_LOCATION_LEN); /* Parse location path */ - /* TODO: Prevent Path Traversal */ if (strcmp(request->location, "/") == 0) { snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s/index.html", CWS_WWW); } else { @@ -50,6 +49,9 @@ cws_http *cws_http_parse(char *request_str, int sockfd) { strncpy(request->http_version, pch, CWS_HTTP_VERSION_LEN); /* Parse other stuff... */ + /* Parse until a \r\n and store the header with its value + * into a hashmap + */ return request; } @@ -122,7 +124,6 @@ void cws_http_get_content_type(cws_http *request, char *content_type) { char ct[32]; /* TODO: Improve content_type (used to test) */ - if (strcmp(ptr, "html") == 0 || strcmp(ptr, "css") == 0 || strcmp(ptr, "javascript") == 0) { strncpy(ct, "text", sizeof ct); } @@ -153,11 +154,7 @@ void cws_http_send_not_implemented(cws_http *request) { } void cws_http_send_not_found(cws_http *request) { - const char response[1024] = - "HTTP/1.1 404 Not Found\r\n" - "Content-Type: text/html\r\n" - "Content-Length: 216\r\n" - "\r\n" + const char html_body[] = "\n" "
\n" "404 Not Found.
\n" "\n" ""; - const size_t response_len = strlen(response); + int html_body_len = strlen(html_body); + + char response[1024]; + int response_len = snprintf(response, sizeof(response), + "HTTP/1.1 404 Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s", + html_body_len, html_body); + send(request->sockfd, response, response_len, 0); } diff --git a/src/main.c b/src/main.c index fe481eb..6b8bcec 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include