diff --git a/src/main.c b/src/main.c index 2e44525..f74a773 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -10,8 +11,9 @@ #include #include -#define MAX_CLIENTS 1024 -#define BUFFER_SIZE 1024 +#define MAX_CLIENTS 1000 +#define BUFFER_SIZE 1000 +#define MAX_BUFFER_SIZE 1000000 #define WORKER_THREADS 8 #define DEFAULT_HOST "0.0.0.0" @@ -35,6 +37,12 @@ struct client { int sock; char *addr_str; pthread_mutex_t lock; + char *req_buffer; + size_t req_buffer_sz; + size_t req_buffer_len; + char *res_buffer; + size_t res_buffer_sz; + size_t res_buffer_len; }; struct client clients[MAX_CLIENTS]; @@ -69,6 +77,14 @@ int accept_connection(struct app_state *app) { client->active = 1; client->sock = sock; + client->req_buffer = malloc(sizeof(char) * BUFFER_SIZE); + client->req_buffer_len = 0; + client->req_buffer_sz = BUFFER_SIZE; + + client->res_buffer = malloc(sizeof(char) * BUFFER_SIZE); + client->res_buffer_len = 0; + client->res_buffer_sz = BUFFER_SIZE; + char *addr_str = inet_ntoa(addr.sin_addr); size_t addr_strlen = snprintf(NULL, 0, "%s:%d", addr_str, addr.sin_port) + 1; client->addr_str = malloc(addr_strlen * sizeof(char)); @@ -76,7 +92,7 @@ int accept_connection(struct app_state *app) { pthread_mutex_unlock(&client->lock); - printf("Connected: %s\n", client->addr_str); + // printf("Connected: %s\n", client->addr_str); return 0; } @@ -87,7 +103,10 @@ void close_client(struct app_state *app, struct client *client, int read) { client->addr_str, strerror(errno)); } - printf("Disonnected: %s\n", client->addr_str); + if (client->req_buffer_len > 300) + printf("%s\n", client->req_buffer); + + // printf("Disonnected: %s\n", client->addr_str); pthread_mutex_lock(&app->epoll_lock); if (epoll_ctl(app->epoll, EPOLL_CTL_DEL, client->sock, NULL) == -1) @@ -129,8 +148,24 @@ void handle_client(struct app_state *app, struct client *client) { memset(buffer, 0, BUFFER_SIZE); int read = recv(client->sock, buffer, BUFFER_SIZE, 0); - if (read > 0) { - if (str_has_suffix(buffer, "\r\n\r\n")) { + if (read > 0 && client->req_buffer_len + read < MAX_BUFFER_SIZE) { + // increase buffer size if running out of space + if (client->req_buffer_len + read > client->req_buffer_sz) { + char *new_buffer = malloc(client->req_buffer_sz + BUFFER_SIZE); + memset(new_buffer, 0, client->req_buffer_sz + BUFFER_SIZE); + strncpy(new_buffer, client->req_buffer, client->req_buffer_sz); + client->req_buffer_sz += BUFFER_SIZE; + client->req_buffer = new_buffer; + } + + // copy buffer to client state + strncpy(client->req_buffer + client->req_buffer_len, buffer, read); + client->req_buffer_len += read; + + // printf("Read %d bytes from %s (%ld total)\n", + // read, client->addr_str, client->req_buffer_len); + + if (str_has_suffix(client->req_buffer, "\r\n\r\n")) { if (send(client->sock, HTTP_RESPONSE, strlen(HTTP_RESPONSE), 0) == -1) fprintf(stderr, "Failed to send [%s]: %s\n", client->addr_str, strerror(errno)); @@ -141,7 +176,6 @@ void handle_client(struct app_state *app, struct client *client) { } pthread_mutex_unlock(&client->lock); - return; } @@ -161,6 +195,9 @@ int main(int argc, char *argv[]) { perror("Failed to create socket"); return 1; } + int optval = 1; + setsockopt(app.sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); + setsockopt(app.sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); memset(&app.addr, 0, sizeof(struct sockaddr_in)); app.addr.sin_family = AF_INET; @@ -207,8 +244,6 @@ int main(int argc, char *argv[]) { for (size_t i = 0; i < MAX_CLIENTS; i++) pthread_mutex_init(&clients[i].lock, NULL); - app.page_buffer = malloc(1024 * sizeof(struct app_state)); - printf("Now listening on %s:%d\n", DEFAULT_HOST, port); char running = 1;