-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
288 lines (258 loc) · 8.36 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <stdio.h>
#include <stdlib.h>
#include "server.h"
#define MAX_PORT_DIGITS 6
/*
* Function that manages the server functionality.
*
* Sets up server socket and runs an infinite loop to accept and handle incoming connections.
*
* Each connection is forked into a child process. To prevent forked child processes from becoming zombies
* when they exit, we loop over child processes and call `waitpid()`. The NOHANG option for waitpid results
* in a single zombie, as each finished child process is reaped and a new one forked. The final zombie is
* reaped when the parent process closes.
* */
int serve(uint16_t port)
{
// Create a socket, allow quick reuse
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
int reuse = 1;
if (setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
errorHandler(ERROR, "setsockopt() failed", "", 0);
}
// Set up a structure to hold the server address
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(port);
serverAddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int bound = bind(serverSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress));
if (bound != 0) {
char portString[5 + MAX_PORT_DIGITS];
sprintf(portString, "port %d", port);
errorHandler(ERROR, "Socket not bound", portString, 0);
}
int listening = listen(serverSocket, BACKLOG);
if (listening < 0) {
printf("Error: The server is not listening.\n");
return 1;
}
report(&serverAddress);
int clientSocket;
size_t childProcessCount = 0;
while(1) {
LogData *log = calloc(1, sizeof(*log));
log->clientAddr = calloc(1, sizeof(*log->clientAddr));
log->req = NULL;
clientSocket = acceptTCPConnection(serverSocket, log);
if (clientSocket == -1) {
// accept() failed, retry
continue;
}
pid_t pid = fork();
if (pid < 0) {
close(clientSocket);
continue;
} else if (pid == 0) {
// This is a child process.
close(serverSocket);
handleHTTPClient(clientSocket, log);
exit(EXIT_SUCCESS);
}
printf("Child process %d\n", pid);
childProcessCount++;
close(clientSocket);
while (childProcessCount) {
pid = waitpid((pid_t) -1, NULL, WNOHANG);
if (pid < 0) {
dieWithSystemMessage("waitpid() failed.");
} else if (pid == 0) {
break;
} else {
printf("Reaped pid %d\n", pid);
childProcessCount--;
}
}
freeLog(log);
}
return 0;
}
/**
* Accept connection from client, log client address.
*
* Returns a non-negative file descriptor of an accepted client socket. If accept() call fails
* returns -1.
*
* The accept() system call extracts the first connection on the queue of pending connections,
* creating a new socket with the same socket type protocol and address family as the specified socket,
* and allocating a new file descriptor for that socket.
* */
int acceptTCPConnection(int serverSocket, LogData *log) {
struct sockaddr_storage genericClientAddr;
socklen_t clientAddrLen = sizeof(genericClientAddr);
memset(&genericClientAddr, 0, clientAddrLen);
int clientSocket = accept(serverSocket, (struct sockaddr *)&genericClientAddr, &clientAddrLen);
if (clientSocket < 0) {
fprintf(stderr, "accept() failed");
return -1;
}
log->clientAddr->sa_family = ((struct sockaddr *)&genericClientAddr)->sa_family;
memcpy(log->clientAddr->sa_data, ((struct sockaddr *)&genericClientAddr)->sa_data, 14);
return clientSocket;
}
void handleHTTPClient(int clientSocket, LogData *log)
{
// @TODO What should the correct size of recvBuffer be?
char recvBuffer[INPUT_BUFFER_SIZE];
memset(recvBuffer, 0, sizeof(char) * INPUT_BUFFER_SIZE);
int nBytesReceived = recv(clientSocket, recvBuffer, sizeof(recvBuffer), 0);
if (nBytesReceived < 0) {
errorHandler(FORBIDDEN, "Failed to read request.", "", clientSocket);
}
// Terminate the recvBuffer after the received bytes
if (nBytesReceived < INPUT_BUFFER_SIZE) {
recvBuffer[nBytesReceived] = 0;
}
char *response = NULL;
char *filename = NULL;
char *mimeType = NULL;
char *req = NULL;
firstLine(recvBuffer, &req);
char *tmpReq = realloc(log->req, (strlen(req) * sizeof(*(log->req))) + 1);
if (tmpReq == NULL) {
dieWithSystemMessage("Memory allocation: realloc() failure.");
}
log->req = tmpReq;
strcpy(log->req, req);
free(req);
router(recvBuffer, clientSocket, &filename);
ssize_t mimeTypeIndex = -1;
if ((mimeTypeIndex = fileTypeAllowed(filename)) == -1) {
free(filename);
errorHandler(FORBIDDEN, "mime type", "Requested file type not allowed.", clientSocket);
}
// @TODO Check the return from router()...
if (setResponse(filename, &response, OK, mimeTypeIndex, clientSocket, log) != 0) {
errorHandler(ERROR, "Error setting the response.", "", clientSocket);
}
send(clientSocket, response, strlen(response) + 1, 0);
free(response);
free(filename);
free(mimeType);
logConnection(log);
close(clientSocket);
}
/**
*
* @TODO We should check for 404s here...
* */
int router(char *request, int clientSocket, char **filename) {
if(strncmp(request, "GET ", 4) && strncmp(request, "get ", 4)) {
errorHandler(FORBIDDEN, "Only simple GET operation supported", request, clientSocket);
}
// Limit the request. Include the resource requested only.
// NUL terminate after the second space to end the request string. Because we've ruled out anything
// but GET requests, we know that the resource being requested starts after index 4 of the
// request e.g. `GET / HTTP/1.1`
size_t requestLen = strlen(request);
for (size_t i = 4; i < requestLen; i++) {
if (request[i] == ' ') {
request[i] = 0;
break;
}
}
// Check for illegal access to parent directory "../"
// Difficult to test as most clients don't allow requests like this.
if (strstr(request, "..")) {
errorHandler(FORBIDDEN, "Parent directory (..) path names are forbidden.", request, clientSocket);
}
// Convert / to index.html, HTTP request verb to uppercase.
if(!strncmp(&request[0], "GET /\0", 6) || !strncmp(&request[0], "get /\0", 6)) {
(void)strcpy(request, "GET /index.html");
}
// Set filename - the content that has been requested.
size_t filenameLen = strlen(request) - 5;
*filename = calloc(filenameLen + 1, sizeof(**filename));
for (size_t i = 5, j = 0; j < filenameLen; i++, j++) {
(*filename)[j] = request[i];
}
return 0;
}
void report(struct sockaddr_in *serverAddress)
{
char hostBuffer[INET6_ADDRSTRLEN];
char serviceBuffer[NI_MAXSERV]; // defined in `<netdb.h>`
socklen_t addr_len = sizeof(*serverAddress);
int err = getnameinfo(
(struct sockaddr *) serverAddress,
addr_len,
hostBuffer,
sizeof(hostBuffer),
serviceBuffer,
sizeof(serviceBuffer),
NI_NUMERICHOST
);
if (err != 0) {
printf("It's not working!!\n");
}
printf("Server listening on http://%s:%s\n", hostBuffer, serviceBuffer);
}
void setHostServiceFromSocket(struct sockaddr_in *socketAddress, char *hostBuffer, char *serviceBuffer)
{
socklen_t addr_len = sizeof(*socketAddress);
int err = getnameinfo(
(struct sockaddr *) socketAddress,
addr_len,
hostBuffer,
INET6_ADDRSTRLEN,
serviceBuffer,
NI_MAXSERV,
NI_NUMERICHOST
);
if (err != 0) {
errorHandler(ERROR, "getnameinfo()", "", 0);
}
}
/**
* Use Common Log Format:
* - IP address of client
* - RFC 1413 identity of the client (not reliable, hyphen as placeholder)
* - userid, HTTP authentication @TODO: hyphen as placeholder
* - Time the request was received
* - The request line from the client in double quotes
* - The returned status code
* - Size of object returned, not including headers (body size in bytes)
*
* */
void logConnection(LogData *log)
{
char *IPString = NULL;
switch(log->clientAddr->sa_family) {
case AF_INET: {
struct sockaddr_in *addr_in = (struct sockaddr_in *)log->clientAddr;
IPString = calloc(INET_ADDRSTRLEN + 1, sizeof(*IPString));
inet_ntop(AF_INET, &(addr_in->sin_addr), IPString, INET_ADDRSTRLEN);
break;
}
case AF_INET6: {
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)log->clientAddr;
IPString = calloc(INET6_ADDRSTRLEN + 1, sizeof(*IPString));
inet_ntop(AF_INET6, &(addr_in6->sin6_addr), IPString, INET6_ADDRSTRLEN);
break;
}
default:
break;
}
char *timeString = NULL;
char *status = "200";
timestamp(&timeString);
printf("%s - - [%s] \"%s\" %s %lu\n", IPString, timeString, log->req, status, log->size);
free(timeString);
free(IPString);
freeLog(log);
}
void freeLog(LogData *log)
{
free(log->clientAddr);
free(log->req);
free(log);
}