-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
64 lines (48 loc) · 1.34 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <winsock2.h>
#include <string.h>
#include "src/server.h"
#include "src/content.h"
#include "src/request.h"
int main(int argc, char **argv) {
char *publicDir;
if (argc == 1) {
publicDir = "./public";
} else {
publicDir = argv[1];
}
char *hostAddress = "0.0.0.0";
int portNumber = 3000;
SOCKET serverSocket = createServer(hostAddress, portNumber);
SOCKET clientSocket;
while (1) {
clientSocket = getConnection(serverSocket);
REQUEST req = getRequest(clientSocket);
char publicFile[512];
strcpy(publicFile, publicDir);
if (strcmp(req.path, "/") == 0) {
strcat(publicFile, "/index.html");
} else {
strcat(publicFile, req.path);
}
// req.method --- req.path
printf(
"\x1b[37m%s\x1b[0m --- \x1b[37m%s\x1b[0m",
req.method, req.path
);
RESPONSE response = getResponse(publicFile);
int bytes;
bytes = sendData(clientSocket, response.content);
// --- Bytes: int
printf(" --- Bytes: %d", bytes);
char *status = "\x1b[32m200\x1b[0m";
if (response.status == 404) {
status = "\x1b[31m404\x1b[0m";
}
printf(" --- %s\n", status);
free(response.content);
}
closesocket(clientSocket);
stopServer(serverSocket);
return 0;
}