add response test
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal 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
7
.idea/codeStyles/Project.xml
generated
Normal 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
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal 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
26
.idea/misc.xml
generated
Normal 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
7
.idea/vcs.xml
generated
Normal 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>
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef __CLIENT_H__
|
||||
#define __CLIENT_H__
|
||||
#ifndef CWS_CLIENT_H
|
||||
#define CWS_CLIENT_H
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef __HTTP_H__
|
||||
#define __HTTP_H__
|
||||
#ifndef CWS_HTTP_H
|
||||
#define CWS_HTTP_H
|
||||
|
||||
#include <stdio.h> /* Debug */
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef __MAIN_H__
|
||||
#define __MAIN_H__
|
||||
#ifndef CWS_MAIN_H
|
||||
#define CWS_MAIN_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef __SERVER_H__
|
||||
#define __SERVER_H__
|
||||
#ifndef CWS_SERVER_H
|
||||
#define CWS_SERVER_H
|
||||
|
||||
#include <arpa/inet.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 send_html_test(int sockfd);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef __HASHMAP_C__
|
||||
#define __HASHMAP_C__
|
||||
#ifndef CWS_HASHMAP_H
|
||||
#define CWS_HASHMAP_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef __UTILS_H__
|
||||
#define __UTILS_H__
|
||||
#ifndef CWS_UTILS_H
|
||||
#define CWS_UTILS_H
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
@@ -24,3 +24,5 @@ Connection: Closed\r\n
|
||||
\r\n
|
||||
<HTML>
|
||||
```
|
||||
|
||||
`Content-Length` is the length of the body (in the response case is the length of html page)
|
||||
@@ -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[] =
|
||||
"<!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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user