From 718dbd50c0e078eba1d35ac1355d516f08d8527b Mon Sep 17 00:00:00 2001 From: Francesco Date: Thu, 14 Nov 2024 22:42:29 +0100 Subject: [PATCH] add response test --- .idea/.gitignore | 8 ++++++ .idea/codeStyles/Project.xml | 7 +++++ .idea/codeStyles/codeStyleConfig.xml | 5 ++++ .idea/misc.xml | 26 ++++++++++++++++++ .idea/vcs.xml | 7 +++++ include/client/client.h | 4 +-- include/http/http.h | 4 +-- include/main.h | 4 +-- include/server/server.h | 6 ++-- include/utils/colors.h | 4 +-- include/utils/hashmap.h | 4 +-- include/utils/utils.h | 4 +-- notes/http-request.md | 4 ++- src/server/server.c | 41 +++++++++++++++++++++++++--- 14 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..f603881 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..683c996 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9497029 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/include/client/client.h b/include/client/client.h index f404866..4782124 100644 --- a/include/client/client.h +++ b/include/client/client.h @@ -1,5 +1,5 @@ -#ifndef __CLIENT_H__ -#define __CLIENT_H__ +#ifndef CWS_CLIENT_H +#define CWS_CLIENT_H #include #include diff --git a/include/http/http.h b/include/http/http.h index 0c56a78..eb69d8b 100644 --- a/include/http/http.h +++ b/include/http/http.h @@ -1,5 +1,5 @@ -#ifndef __HTTP_H__ -#define __HTTP_H__ +#ifndef CWS_HTTP_H +#define CWS_HTTP_H #include /* Debug */ #include diff --git a/include/main.h b/include/main.h index 91284c3..a75e5dd 100644 --- a/include/main.h +++ b/include/main.h @@ -1,5 +1,5 @@ -#ifndef __MAIN_H__ -#define __MAIN_H__ +#ifndef CWS_MAIN_H +#define CWS_MAIN_H #include diff --git a/include/server/server.h b/include/server/server.h index 0fd2171..7b77c3c 100644 --- a/include/server/server.h +++ b/include/server/server.h @@ -1,5 +1,5 @@ -#ifndef __SERVER_H__ -#define __SERVER_H__ +#ifndef CWS_SERVER_H +#define CWS_SERVER_H #include #include @@ -94,4 +94,6 @@ int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t * */ void close_fds(bucket_t *bucket); +void send_html_test(int sockfd); + #endif diff --git a/include/utils/colors.h b/include/utils/colors.h index 2f6bd2d..c48e679 100644 --- a/include/utils/colors.h +++ b/include/utils/colors.h @@ -1,5 +1,5 @@ -#ifndef __COLORS_H__ -#define __COLORS_H__ +#ifndef CWS_COLORS_H +#define CWS_COLORS_H /* ANSI color escape sequences */ #define RED "\033[31m" diff --git a/include/utils/hashmap.h b/include/utils/hashmap.h index ff541ec..5a13287 100644 --- a/include/utils/hashmap.h +++ b/include/utils/hashmap.h @@ -1,5 +1,5 @@ -#ifndef __HASHMAP_C__ -#define __HASHMAP_C__ +#ifndef CWS_HASHMAP_H +#define CWS_HASHMAP_H #include #include diff --git a/include/utils/utils.h b/include/utils/utils.h index 78f395a..9d6710e 100644 --- a/include/utils/utils.h +++ b/include/utils/utils.h @@ -1,5 +1,5 @@ -#ifndef __UTILS_H__ -#define __UTILS_H__ +#ifndef CWS_UTILS_H +#define CWS_UTILS_H #include #include diff --git a/notes/http-request.md b/notes/http-request.md index 6fd352b..66fcd10 100644 --- a/notes/http-request.md +++ b/notes/http-request.md @@ -23,4 +23,6 @@ Content-Length: 88\r\n Connection: Closed\r\n \r\n -``` \ No newline at end of file +``` + +`Content-Length` is the length of the body (in the response case is the length of html page) \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index f7b54ba..fc2e44a 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -73,8 +73,8 @@ void handle_clients(int sockfd) { struct epoll_event *revents = malloc(EPOLL_MAXEVENTS * sizeof(struct epoll_event)); int nfds; - char *msg = "Hello there!"; - size_t msg_len = strlen(msg); + // char *msg = "Hello there!"; + // size_t msg_len = strlen(msg); char data[4096]; int client_fd; int run = 1; @@ -94,8 +94,8 @@ void handle_clients(int sockfd) { epoll_ctl_add(epfd, client_fd, EPOLLIN); hm_push(clients, client_fd, &their_sa); - int bytes_sent = send(client_fd, msg, msg_len, 0); - fprintf(stdout, "[server] Sent %d bytes\n", bytes_sent); + // int bytes_sent = send(client_fd, msg, msg_len, 0); + // fprintf(stdout, "[server] Sent %d bytes\n", bytes_sent); } else { /* Incoming data */ client_fd = revents[i].data.fd; @@ -113,6 +113,8 @@ void handle_clients(int sockfd) { continue; } + send_html_test(client_fd); + // 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); @@ -199,3 +201,34 @@ void close_fds(bucket_t *bucket) { } } } + +void send_html_test(int sockfd) { + char html[] = + "\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " cws\n" + "\n" + "\n" + "\n" + "

Hello from cws!

\n" + "\n" + "\n" + ""; + char len[4096]; + size_t content_length = strlen(html); + sprintf(len, "Content-Length: %zu\r\n", content_length); + fprintf(stdout, "Content-length: %zu\n", content_length); + char response[65535]; + strcat(response, "HTTP/1.1 200 OK\r\n"); + strcat(response, "Content-Type: text/html\r\n"); + strcat(response, len); + strcat(response, "Connection: closed\r\n"); + strcat(response, "\r\n"); + strcat(response, html); + + send(sockfd, response, strlen(response), 0); +}