From e5e40c795cb63e460157b111c0f9fe00e2879fb3 Mon Sep 17 00:00:00 2001 From: Francesco Date: Mon, 11 Nov 2024 00:50:39 +0100 Subject: [PATCH] initial http parsing --- .clang-format | 4 +-- include/{ => client}/client.h | 0 include/http/http.h | 48 ++++++++++++++++++++++++++++++++++ include/{ => server}/server.h | 9 +++---- include/{ => utils}/colors.h | 0 include/{ => utils}/hashmap.h | 14 ++++++---- include/{ => utils}/utils.h | 0 notes/http-request.md | 15 +++++++++++ src/{ => client}/client.c | 7 +++-- src/{mainc.c => client/main.c} | 5 ++-- src/http/http.c | 40 ++++++++++++++++++++++++++++ src/main.c | 5 ++-- src/meson.build | 7 +++-- src/{ => server}/server.c | 34 +++++++++++++++++++----- src/{ => utils}/hashmap.c | 2 +- src/{ => utils}/utils.c | 4 +-- www/index.html | 14 ++++++++++ 17 files changed, 176 insertions(+), 32 deletions(-) rename include/{ => client}/client.h (100%) create mode 100644 include/http/http.h rename include/{ => server}/server.h (90%) rename include/{ => utils}/colors.h (100%) rename include/{ => utils}/hashmap.h (86%) rename include/{ => utils}/utils.h (100%) rename src/{ => client}/client.c (91%) rename src/{mainc.c => client/main.c} (82%) create mode 100644 src/http/http.c rename src/{ => server}/server.c (82%) rename src/{ => utils}/hashmap.c (98%) rename src/{ => utils}/utils.c (95%) create mode 100644 www/index.html diff --git a/.clang-format b/.clang-format index 3fb3e14..3d4d9d7 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,5 @@ BasedOnStyle: Google ColumnLimit: 120 UseTab: Always -IndentWidth: 8 -TabWidth: 8 +IndentWidth: 4 +TabWidth: 4 diff --git a/include/client.h b/include/client/client.h similarity index 100% rename from include/client.h rename to include/client/client.h diff --git a/include/http/http.h b/include/http/http.h new file mode 100644 index 0000000..0c56a78 --- /dev/null +++ b/include/http/http.h @@ -0,0 +1,48 @@ +#ifndef __HTTP_H__ +#define __HTTP_H__ + +#include /* Debug */ +#include +#include + +#define WWW "../www/" /**< Directory used to get html files */ +/** In the future I'll move conf stuff under a server struct, I can skip just because I want something that works */ +#define LOCATION_LEN 1024 +#define HTTP_VERSION_LEN 8 +#define USER_AGENT_LEN 1024 +#define HOST_LEN 1024 + +enum http_method { + GET, /**< GET method */ + POST, /**< POST method */ +}; +/* In the future I'll add HEAD, PUT, DELETE */ + +/** + * @brief HTTP request struct + * + */ +typedef struct http { + enum http_method method; /**< HTTP request method */ + char location[LOCATION_LEN]; /**< Resource requested */ + char http_version[HTTP_VERSION_LEN]; /**< HTTP version */ + char user_agent[USER_AGENT_LEN]; /**< User-Agent */ + char host[HOST_LEN]; /**< Host */ +} http_t; +/* Connection */ +/* Accept-Encoding */ +/* Accept-Language */ + +/** + * @brief Parses a HTTP request + * + * @param request[in] The http request sent to the server + * @return Returns a http_t pointer to the request + */ +http_t *http_parse(char *request_str); + +void http_parse_method(http_t *request, const char *method); +void http_free(http_t *request); +void http_send_response(http_t *request); + +#endif \ No newline at end of file diff --git a/include/server.h b/include/server/server.h similarity index 90% rename from include/server.h rename to include/server/server.h index cfe3afc..0fd2171 100644 --- a/include/server.h +++ b/include/server/server.h @@ -14,7 +14,7 @@ #include #include -#include "hashmap.h" +#include "utils/hashmap.h" /* On which port the server will run */ #define PORT 3030 @@ -33,12 +33,12 @@ * * @param hostname[in] The hostname of the server (default localhost, it could be NULL) * @param service[in] The service (found in /etc/services) or the port where to run - * @return int 0 on success, -1 on error + * @return 0 on success, -1 on error */ int start_server(const char *hostname, const char *service); /** - * @brief Sets the up hints object + * @brief Setups hints object * * @param hints[out] The hints addrinfo * @param len[in] The length of hints @@ -62,7 +62,6 @@ void handle_clients(int sockfd); */ void epoll_ctl_add(int epfd, int sockfd, uint32_t events); -/* Remove a file descriptor from the interest list */ /** * @brief Removes a file descriptor from the interest list * @@ -84,7 +83,7 @@ void setnonblocking(int sockfd); * @param sockfd[in] Server's file descriptor * @param their_sa[out] Populates the struct with client's information * @param theirsa_size[in] Size of the struct - * @return int Returns -1 on error or the file descriptor on success + * @return Returns -1 on error or the file descriptor on success */ int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size); diff --git a/include/colors.h b/include/utils/colors.h similarity index 100% rename from include/colors.h rename to include/utils/colors.h diff --git a/include/hashmap.h b/include/utils/hashmap.h similarity index 86% rename from include/hashmap.h rename to include/utils/hashmap.h index 6849d2d..ff541ec 100644 --- a/include/hashmap.h +++ b/include/utils/hashmap.h @@ -9,18 +9,22 @@ /* Each process on Linux can have a maximum of 1024 open file descriptors */ #define HASHMAP_MAX_CLIENTS 1024 +/** + * @brief Hash map struct + * + */ typedef struct bucket { - int sockfd; /**< Client socket descriptor */ + int sockfd; /**< Client socket descriptor */ struct sockaddr_storage sas; /**< Associated socket address */ - struct bucket *next; /**< Next node in case of collision */ - struct bucket *prev; /**< Previous node in case of collision */ + struct bucket *next; /**< Next node in case of collision */ + struct bucket *prev; /**< Previous node in case of collision */ } bucket_t; /** * @brief Calculates the hash code of a given file descriptor * * @param sockfd[in] The file descriptor - * @return int Returns the hash code + * @return Returns the hash code */ int hash(int sockfd); @@ -53,7 +57,7 @@ void hm_remove(bucket_t *bucket, int sockfd); * * @param bucket[in] The hash map * @param sockfd[in] The file descriptor (key) - * @return struct hashmap* Returns NULL or the key pointer + * @return Returns NULL or the key pointer */ bucket_t *hm_lookup(bucket_t *bucket, int sockfd); diff --git a/include/utils.h b/include/utils/utils.h similarity index 100% rename from include/utils.h rename to include/utils/utils.h diff --git a/notes/http-request.md b/notes/http-request.md index 84d7a34..6fd352b 100644 --- a/notes/http-request.md +++ b/notes/http-request.md @@ -9,3 +9,18 @@ Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive ``` + +The first line is a *request line*. It has: +- Method (GET, POST, HEAD, ...) +- Location (the request resource, file) +- HTTP version + +# HTTP Response +```bash +HTTP/1.1 200 OK\r\n +Content-Type: text/html\r\n +Content-Length: 88\r\n +Connection: Closed\r\n +\r\n + +``` \ No newline at end of file diff --git a/src/client.c b/src/client/client.c similarity index 91% rename from src/client.c rename to src/client/client.c index b9e8b18..1f933dd 100644 --- a/src/client.c +++ b/src/client/client.c @@ -1,8 +1,6 @@ -// THIS FILE IS ONLY A TEST FOR THE BASIC STUFF +#include "client/client.h" -#include "client.h" - -#include "colors.h" +#include "utils/colors.h" int test_client_connection(const char *hostname, const char *service) { struct addrinfo hints; @@ -32,6 +30,7 @@ int test_client_connection(const char *hostname, const char *service) { fprintf(stdout, "[client] => "); fgets(buf, sizeof buf, stdin); + buf[strcspn(buf, "\n")] = '\0'; send(sockfd, buf, strlen(buf), 0); freeaddrinfo(res); diff --git a/src/mainc.c b/src/client/main.c similarity index 82% rename from src/mainc.c rename to src/client/main.c index 0228b95..7013401 100644 --- a/src/mainc.c +++ b/src/client/main.c @@ -1,7 +1,8 @@ -#include "client.h" -#include "colors.h" #include "main.h" +#include "client/client.h" +#include "utils/colors.h" + int main(int argc, char **argv) { fprintf(stdout, BOLD GREEN "[client] Running client...\n" RESET); diff --git a/src/http/http.c b/src/http/http.c new file mode 100644 index 0000000..5e28aee --- /dev/null +++ b/src/http/http.c @@ -0,0 +1,40 @@ +#include "http/http.h" + +#include "utils/colors.h" + +http_t *http_parse(char *request_str) { + http_t *request = malloc(sizeof(http_t)); + fprintf(stdout, YELLOW "[http] REQUEST:\n%s\n" RESET, request_str); + + /* Parse HTTP method */ + char *pch = strtok(request_str, " "); + printf("%s\n", pch); + http_parse_method(request, pch); + + /* Parse location */ + pch = strtok(NULL, " "); + printf("%s\n", pch); + strncpy(request->location, pch, LOCATION_LEN); + + /* Parse HTTP version */ + pch = strtok(NULL, " \r\n"); + printf("%s\n", pch); + strncpy(request->http_version, pch, HTTP_VERSION_LEN); + + /* Parse other stuff... */ + + return request; +} + +void http_parse_method(http_t *request, const char *method) { + if (strcmp(method, "GET") == 0) { + request->method = GET; + } + if (strcmp(method, "POST") == 0) { + request->method = POST; + } +} + +void http_send_response(http_t *request) { /* TODO */ } + +void http_free(http_t *request) { free(request); } diff --git a/src/main.c b/src/main.c index ab0e21b..6c88d86 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,8 @@ #include "main.h" -#include "colors.h" -#include "server.h" +#include "server/server.h" +#include "utils/colors.h" + int main(int argc, char **argv) { fprintf(stdout, BOLD GREEN "[server] Running cws...\n" RESET); diff --git a/src/meson.build b/src/meson.build index f26f12d..12c70d7 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,2 +1,5 @@ -server = files('main.c', 'server.c', 'utils.c', 'hashmap.c') -client = files('mainc.c', 'client.c') \ No newline at end of file +server = files('main.c', 'server/server.c') +server += files('utils/utils.c', 'utils/hashmap.c') +server += files('http/http.c') + +client = files('client/main.c', 'client/client.c') \ No newline at end of file diff --git a/src/server.c b/src/server/server.c similarity index 82% rename from src/server.c rename to src/server/server.c index 57a8e89..f7b54ba 100644 --- a/src/server.c +++ b/src/server/server.c @@ -1,8 +1,9 @@ -#include "server.h" +#include "server/server.h" -#include "colors.h" -#include "hashmap.h" -#include "utils.h" +#include "http/http.h" +#include "utils/colors.h" +#include "utils/hashmap.h" +#include "utils/utils.h" int start_server(const char *hostname, const char *service) { struct addrinfo hints; @@ -19,9 +20,16 @@ int start_server(const char *hostname, const char *service) { int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); fprintf(stdout, YELLOW "[server] sockfd: %d\n" RESET, sockfd); + int opt = 1; + status = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt); + if (status != 0) { + fprintf(stderr, RED BOLD "[server] setsockopt(): %s\n" RESET, strerror(errno)); + exit(EXIT_FAILURE); + } + status = bind(sockfd, res->ai_addr, res->ai_addrlen); if (status != 0) { - fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET, gai_strerror(status)); + fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET, strerror(errno)); exit(EXIT_FAILURE); } @@ -77,7 +85,10 @@ void handle_clients(int sockfd) { for (int i = 0; i < nfds; ++i) { if (revents[i].data.fd == sockfd) { /* New client */ + char ip[INET_ADDRSTRLEN]; client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size); + get_client_ip(&their_sa, ip); + fprintf(stdout, BLUE "[server] Client (%s) connected\n" RESET, ip); setnonblocking(client_fd); epoll_ctl_add(epfd, client_fd, EPOLLIN); @@ -102,17 +113,26 @@ void handle_clients(int sockfd) { continue; } - data[strcspn(data, "\n")] = '\0'; - fprintf(stdout, "[server] Bytes read (%d): %s\n", bytes_read, data); + // fprintf(stdout, "[server] Bytes read (%d):\n%s\n", bytes_read, data); if (strcmp(data, "stop") == 0) { fprintf(stdout, GREEN BOLD "[server] Stopping...\n" RESET); run = 0; break; } + + /* Parse HTTP request */ + http_t *request = http_parse(data); + fprintf(stdout, "[server] request location: %s\n", request->location); + http_send_response(request); + http_free(request); + + /* Clear str */ + memset(data, 0, sizeof data); } } } + /* Clean up everything */ free(revents); close(epfd); close_fds(clients); diff --git a/src/hashmap.c b/src/utils/hashmap.c similarity index 98% rename from src/hashmap.c rename to src/utils/hashmap.c index 99ff8ea..c25718e 100644 --- a/src/hashmap.c +++ b/src/utils/hashmap.c @@ -1,4 +1,4 @@ -#include "hashmap.h" +#include "utils/hashmap.h" int hash(int sockfd) { return sockfd % HASHMAP_MAX_CLIENTS; } diff --git a/src/utils.c b/src/utils/utils.c similarity index 95% rename from src/utils.c rename to src/utils/utils.c index 9769943..d222732 100644 --- a/src/utils.c +++ b/src/utils/utils.c @@ -1,6 +1,6 @@ -#include "utils.h" +#include "utils/utils.h" -#include "colors.h" +#include "utils/colors.h" void print_ips(const char *hostname, const char *port) { struct addrinfo ai; diff --git a/www/index.html b/www/index.html new file mode 100644 index 0000000..2ff87dc --- /dev/null +++ b/www/index.html @@ -0,0 +1,14 @@ + + + + + + + cws + + + +

Hello from cws!

+ + + \ No newline at end of file