-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.c
More file actions
57 lines (48 loc) · 1.76 KB
/
http.c
File metadata and controls
57 lines (48 loc) · 1.76 KB
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
#include "http.h"
#include "response.h"
#include "uri.h"
#include "httpRequest.h"
#include "mimeTypes.h"
#include "loggerWorkerThread.h"
void handleHttpConnection(int socketFd, channel_t logger, fileCache_t cache) {
httpRequest_t request;
// First read response
httpResponse_t response = readRequest(socketFd, &request);
if (response != NULL) {
logMessageWithIpC(logger, socketFd, "Invalid request.");
writeResponse(socketFd, response);
close(socketFd);
destroyHttpResponse(response);
return;
}
// Then read the target
char *path = uriToFilePath(request->target);
string_t s = path == NULL ? NULL : fileCacheGetFile(cache, path);
if (path != NULL) free(path);
// if file doesn't exist return proper error
if (s == NULL) {
string_t message = stringFromCString("File not found: ");
string_t file = uriToString(request->target);
plusEqual(message, file);
destroyString(file);
logMessageWithIp(logger, socketFd, message);
response = createHttpResponse();
httpResponseStatus(response, HTTP_STATUS_CODE_NOT_FOUND);
writeResponse(socketFd, response);
destroyHttpResponse(response);
destroyHttpRequest(request);
close(socketFd);
return;
}
// return the file
logMessageWithIpC(logger, socketFd, "Successful request.");
response = createHttpResponse();
httpResponseStatus(response, HTTP_STATUS_CODE_OK);
setHttpContent(response, s);
addContentLengthHeader(response);
addHeader(response, stringFromCString("Content-Type"), getFileContentType(request->target));
writeResponse(socketFd, response);
destroyHttpResponse(response);
destroyHttpRequest(request);
close(socketFd);
}