-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.c
208 lines (177 loc) · 4.19 KB
/
request.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
#include "request.h"
#include <signal.h>
#include "log.h"
const char
*lal_method_to_string(enum lal_http_method m)
{
return m == GET ? "GET"
: m == POST ? "POST"
: m == PUT ? "PUT"
: m == PATCH ? "PATCH"
: m == DELETE ? "DELETE"
: m == HEAD ? "HEAD"
: m == OPTIONS ? "OPTIONS"
: "ANY";
}
const char
*lal_header_error_msg(LAL_HEADER_ERROR error) {
return error == RECV_FAILED ?
"`recv` failed"
: error == NOBYTES ?
"No bytes received"
: error == DISCONNECTED ?
"Peer closed socket prematurely"
: NULL;
}
struct lal_entry
*lal_get_header (struct lal_request *request, const char *key)
{
struct lal_entry *entry = request->header;
do {
if (
strlen(key) == entry->keylen &&
!strncmp(key, entry->key, entry->keylen)
) return entry;
entry = entry->next;
} while (entry);
return NULL;
}
LAL_HEADER_ERROR
lal_parse_header (int sock, char **header)
{
int nbytes = 0, len = 0;
char buf[MAXHEADERSIZE], *ptr = buf;
memset(buf, 0, MAXHEADERSIZE);
while (
ptr - buf < MAXHEADERSIZE - 1 &&
(ptr - buf < 4 || strncmp(ptr - 4, "\r\n\r\n", 4) != 0) &&
(nbytes = recv(sock, ptr, 1, 0)) != 0
) {
if (nbytes < 0) {
fprintf(stderr, "recv() failed: %s\n", strerror(errno));
return RECV_FAILED;
}
ptr++;
len += nbytes;
}
if (ptr - buf == MAXHEADERSIZE && strstr(buf, "\r\n\r\n") == NULL)
return MAXHEADERSIZE_EXCEEDED;
if (!len) {
//log_trace(
// "request.c: sock: %i; len = %i; buf = \"%s\"",
// sock, len, buf
//);
return NOBYTES;
}
if (!nbytes)
return DISCONNECTED;
*ptr = '\0';
*header = ptr = (char *)calloc(++len, sizeof(char));
memcpy(*header, buf, len);
return LAL_SUCCESS;
}
void
lal_set_headers (struct lal_request *request, const char *src)
{
struct lal_entry *entry = (struct lal_entry *)malloc(sizeof(struct lal_entry));
request->header = entry;
entry->key = entry->val = NULL;
entry->next = NULL;
while (*src != '\0') {
entry->keylen = entry->vallen = 0;
entry->key = src;
while (*src != '\0' && *src++ != ':')
entry->keylen++;
if (*src == ' ')
src++;
entry->val = src;
while (*src != '\0' && *src++ != '\n')
entry->vallen++;
if (*src == '\0')
entry->next = NULL;
else
entry = entry->next = (struct lal_entry *)malloc(
sizeof(struct lal_entry));
}
}
enum lal_http_method
lal_method_from_string(const char *src)
{
enum lal_http_method rc = strstr(src, "GET") == src ? GET
: strstr(src, "POST") == src ? POST
: strstr(src, "PUT") == src ? PUT
: strstr(src, "DELETE") == src ? DELETE
: strstr(src, "OPTIONS") == src ? OPTIONS
: strstr(src, "HEAD") == src ? HEAD
: (enum lal_http_method)-1;
//if (!~rc) {
// for (int i = 0; i < 30; i++) {
// fprintf(stderr, "%02x ", *src++);
// }
// puts("");
//}
return rc;
}
struct lal_request
*lal_create_request(const char *src)
{
const char *header = src, *path;
int pathlen = 0;
enum lal_http_method method = lal_method_from_string(src);
if (!~method) {
log_error("No `method` found");
return NULL;
}
/* skip over the method */
while (*src++ != ' ')
;
path = src;
while (*src++ != ' ')
pathlen++;
struct lal_request *request, r = {
.method = method,
._raw_header = header,
.path = path,
.pathlen = pathlen,
.header = NULL,
.content = NULL,
.content_length = 0
};
request = malloc(sizeof (struct lal_request));
memcpy(request, &r, sizeof(struct lal_request));
/* fast forward to the next line */
while (*src != '\0' && *src++ != '\n')
;
lal_set_headers(request, src);
return request;
}
void
lal_set_content(int sock, struct lal_request *request)
{
struct lal_entry *e = lal_get_header(request, "Content-Length");
if (e) {
char buf[e->vallen + 1];
memcpy(buf, e->val, e->vallen); buf[e->vallen] = 0;
int content_length = strtol(
buf, NULL, 10
);
uint8_t *content = malloc(sizeof(uint8_t) * content_length);
recv(sock, content, content_length, 0);
request->content = content;
request->content_length = content_length;
}
}
void
lal_destroy_request (struct lal_request *request)
{
struct lal_entry *prev, *entry = request->header;
while (entry) {
prev = entry;
entry = entry->next;
free(prev);
}
if (request->content != NULL)
free((void *)request->content);
free((void *)request->_raw_header);
free(request);
}