-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpRequest.c
More file actions
176 lines (145 loc) · 4.58 KB
/
httpRequest.c
File metadata and controls
176 lines (145 loc) · 4.58 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
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
//
// Created by rose on 2/7/19.
//
#include <memory.h>
#include <stdbool.h>
#include "uri.h"
#include "response.h"
#include "httpRequest.h"
#include "result.h"
#include "input.h"
result_t readHeaders(int fd, httpHeaders_t *headers);
result_t readHttpVersion(int fd, string_t *pString);
result_t readTarget(int fd, uri_t *uri);
result_t readMethod(int fd, string_t *methodPtr);
void destroyHttpRequest(httpRequest_t self) {
destroyString(self->method);
destroyUri(self->target);
destroyString(self->httpVersion);
destroyHttpHeaders(self->headers);
free(self);
}
httpRequest_t createHttpRequest(string_t method, uri_t target, string_t httpVersion, httpHeaders_t headers) {
httpRequest_t ret = malloc(sizeof(struct httpRequest));
ret->method = method;
ret->target = target;
ret->httpVersion = httpVersion;
ret->headers = headers;
return ret;
}
httpResponse_t readRequest(int fd, httpRequest_t *request) {
// First read method
string_t method;
result_t r = readMethod(fd, &method);
if (r != OK) {
httpResponse_t response = createHttpResponse();
httpResponseStatus(response, HTTP_STATUS_CODE_INVALID_METHOD);
return response;
}
// and then target
uri_t target;
r = readTarget(fd, &target);
if (target == NULL || r != OK) {
httpResponse_t response = createHttpResponse();
httpResponseStatus(response, HTTP_STATUS_CODE_INVALID_REQUEST);
return response;
}
// and then version
string_t version;
r = readHttpVersion(fd, &version);
if (r == INVALID_VERSION) {
httpResponse_t response = createHttpResponse();
httpResponseStatus(response, HTTP_STATUS_CODE_INVALID_VERSION);
return response;
} else if (r != OK) {
httpResponse_t response = createHttpResponse();
httpResponseStatus(response, HTTP_STATUS_CODE_SERVER_ERROR);
return response;
}
// and then headers
httpHeaders_t headers;
r = readHeaders(fd, &headers);
if (r == INVALID_HEADER) {
httpResponse_t response = createHttpResponse();
httpResponseStatus(response, HTTP_STATUS_CODE_INVALID_REQUEST);
return response;
}
*request = createHttpRequest(method, target, version, headers);
return NULL;
}
result_t readHeaders(int fd, httpHeaders_t *headers) {
*headers = createHttpHeaders();
while (true) {
// first read the line
string_t line;
readUntilChars(fd, &line, "\r\n");
// if line length is zero, that means we've reached
// the newline indicating the headers are over.
if (stringLength(line) == 0) {
destroyString(line);
return OK;
}
// Read the key
string_t key = createString();
size_t i = 0;
for (;; i++) {
if (i == stringLength(line)) {
destroyString(key);
destroyString(line);
destroyHttpHeaders(*headers);
return INVALID_HEADER;
}
if (*charAt(line, i) == ':') {
i++;
break;
}
append(key, *charAt(line, i));
}
// Skip past spaces
for (; i < stringLength(line) && *charAt(line, i) == ' '; i++);
// read the value
string_t value = createString();
size_t spaces = 0; // keep track of trailing spaces.
for (; i < stringLength(line); i++) {
char c = *charAt(line, i);
append(value, c);
if (c == ' ') {
spaces++;
} else {
spaces = 0;
}
}
//Trim trailing spaces.
removeLastChars(value, spaces);
destroyString(line);
httpHeadersAppend(*headers, key, value);
}
}
result_t readHttpVersion(int fd, string_t *pString) {
result_t r = readUntilChars(fd, pString, "\r\n");
if (r != OK) return r;
// Version must be HTTP/1.1
if (strcmp("HTTP/1.1", stringData(*pString)) != 0) {
destroyString(*pString);
return INVALID_VERSION;
}
return OK;
}
result_t readTarget(int fd, uri_t *uri) {
string_t s;
result_t ret;
if ((ret = readUntil(fd, &s, ' ')) != OK) return ret;
*uri = parseUri(s);
destroyString(s);
return OK;
}
enum result readMethod(int fd, string_t *methodPtr) {
enum result ret = readUntil(fd, methodPtr, ' ');
if (ret != OK) return ret;
// We only allow a GET method for now
if (strcmp(charAt(*methodPtr, 0), "GET") != 0) {
destroyString(*methodPtr);
return INVALID_METHOD;
}
return OK;
}