add response test

This commit is contained in:
2024-11-14 22:42:29 +01:00
parent e5e40c795c
commit 718dbd50c0
14 changed files with 109 additions and 19 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -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

7
.idea/codeStyles/Project.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<clangFormatSettings>
<option name="ENABLED" value="true" />
</clangFormatSettings>
</code_scheme>
</component>

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

26
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakePythonSetting">
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MesonSettings">
<option name="linkedExternalProjectsSettings">
<MesonProjectSettings>
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
<option name="profiles">
<MesonProfile>
<option name="buildDir" value="build" />
<option name="toolchainName" value="WSL" />
</MesonProfile>
</option>
</MesonProjectSettings>
</option>
</component>
<component name="MesonWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

7
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/doxygen-awesome-css" vcs="Git" />
</component>
</project>

View File

@@ -1,5 +1,5 @@
#ifndef __CLIENT_H__ #ifndef CWS_CLIENT_H
#define __CLIENT_H__ #define CWS_CLIENT_H
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>

View File

@@ -1,5 +1,5 @@
#ifndef __HTTP_H__ #ifndef CWS_HTTP_H
#define __HTTP_H__ #define CWS_HTTP_H
#include <stdio.h> /* Debug */ #include <stdio.h> /* Debug */
#include <stdlib.h> #include <stdlib.h>

View File

@@ -1,5 +1,5 @@
#ifndef __MAIN_H__ #ifndef CWS_MAIN_H
#define __MAIN_H__ #define CWS_MAIN_H
#include <stdio.h> #include <stdio.h>

View File

@@ -1,5 +1,5 @@
#ifndef __SERVER_H__ #ifndef CWS_SERVER_H
#define __SERVER_H__ #define CWS_SERVER_H
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
@@ -94,4 +94,6 @@ int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *
*/ */
void close_fds(bucket_t *bucket); void close_fds(bucket_t *bucket);
void send_html_test(int sockfd);
#endif #endif

View File

@@ -1,5 +1,5 @@
#ifndef __COLORS_H__ #ifndef CWS_COLORS_H
#define __COLORS_H__ #define CWS_COLORS_H
/* ANSI color escape sequences */ /* ANSI color escape sequences */
#define RED "\033[31m" #define RED "\033[31m"

View File

@@ -1,5 +1,5 @@
#ifndef __HASHMAP_C__ #ifndef CWS_HASHMAP_H
#define __HASHMAP_C__ #define CWS_HASHMAP_H
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>

View File

@@ -1,5 +1,5 @@
#ifndef __UTILS_H__ #ifndef CWS_UTILS_H
#define __UTILS_H__ #define CWS_UTILS_H
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>

View File

@@ -23,4 +23,6 @@ Content-Length: 88\r\n
Connection: Closed\r\n Connection: Closed\r\n
\r\n \r\n
<HTML> <HTML>
``` ```
`Content-Length` is the length of the body (in the response case is the length of html page)

View File

@@ -73,8 +73,8 @@ void handle_clients(int sockfd) {
struct epoll_event *revents = malloc(EPOLL_MAXEVENTS * sizeof(struct epoll_event)); struct epoll_event *revents = malloc(EPOLL_MAXEVENTS * sizeof(struct epoll_event));
int nfds; int nfds;
char *msg = "Hello there!"; // char *msg = "Hello there!";
size_t msg_len = strlen(msg); // size_t msg_len = strlen(msg);
char data[4096]; char data[4096];
int client_fd; int client_fd;
int run = 1; int run = 1;
@@ -94,8 +94,8 @@ void handle_clients(int sockfd) {
epoll_ctl_add(epfd, client_fd, EPOLLIN); epoll_ctl_add(epfd, client_fd, EPOLLIN);
hm_push(clients, client_fd, &their_sa); hm_push(clients, client_fd, &their_sa);
int bytes_sent = send(client_fd, msg, msg_len, 0); // int bytes_sent = send(client_fd, msg, msg_len, 0);
fprintf(stdout, "[server] Sent %d bytes\n", bytes_sent); // fprintf(stdout, "[server] Sent %d bytes\n", bytes_sent);
} else { } else {
/* Incoming data */ /* Incoming data */
client_fd = revents[i].data.fd; client_fd = revents[i].data.fd;
@@ -113,6 +113,8 @@ void handle_clients(int sockfd) {
continue; continue;
} }
send_html_test(client_fd);
// fprintf(stdout, "[server] Bytes read (%d):\n%s\n", bytes_read, data); // fprintf(stdout, "[server] Bytes read (%d):\n%s\n", bytes_read, data);
if (strcmp(data, "stop") == 0) { if (strcmp(data, "stop") == 0) {
fprintf(stdout, GREEN BOLD "[server] Stopping...\n" RESET); 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[] =
"<!DOCTYPE html>\n"
"<html lang=\"en\">\n"
"\n"
"<head>\n"
" <meta charset=\"UTF-8\">\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
" <title>cws</title>\n"
"</head>\n"
"\n"
"<body>\n"
" <h1>Hello from cws!</h1>\n"
"</body>\n"
"\n"
"</html>";
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);
}