-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_parser.c
More file actions
162 lines (128 loc) · 4.43 KB
/
http_parser.c
File metadata and controls
162 lines (128 loc) · 4.43 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
#include "http_parser.h"
#define _BSD_SOURCE
#include <string.h>
#include <stdlib.h>
#define curchar buffer[p]
#define nextchar buffer[p+1]
#define next() \
buffer[p] = 0; \
buffer += (p+1); \
p = 0;
#define if_eof(r) if (curchar == 0) return r;
#define check_method(m) \
if (!p && !strcmp(m, message->method)) p = sizeof(m);
#define check_protocol(p) \
if (!(strcmp("HTTP/1.0", p) || strcmp("HTTP/1.1", p))) {\
return 400;\
}
#define next_at_newline(r) \
buffer = strchr(buffer, '\n'); \
if (!buffer) return r; \
next(); \
if (curchar == '\r') {\
next(); \
}
int http_parser(char *buffer, struct http_message *message, int type) {
if (type != HTTP_REQUEST && type != HTTP_RESPONSE) return -1;
int p = 0;
memset(message, 0, sizeof(struct http_message));
message->type = type;
message->method = type == HTTP_REQUEST ? buffer : NULL;
message->protocol = type == HTTP_RESPONSE ? buffer : NULL;
buffer = strchr(buffer, ' ');
if (!buffer) return 400;
next();
if (type == HTTP_REQUEST) {
check_method("GET");
check_method("HEAD");
check_method("POST");
check_method("PUT");
check_method("DELETE");
check_method("TRACE");
check_method("OPTIONS");
check_method("CONNECT");
check_method("PATCH");
if (!p) return 400;
p = 0;
} else {
check_protocol(message->protocol);
}
message->url = type == HTTP_REQUEST ? buffer : NULL;
message->status = type == HTTP_RESPONSE ? buffer : NULL;
buffer = strchr(buffer, ' ');
if (!buffer) return 414;
next();
if (type == HTTP_REQUEST && strstr(message->url, "/") != message->url)
return 400;
else if (type == HTTP_RESPONSE && atoi(message->status) == 0)
return 400;
message->protocol = type == HTTP_REQUEST ? buffer : message->protocol;
message->line = type == HTTP_RESPONSE ? buffer : NULL;
next_at_newline(400);
if (type == HTTP_REQUEST) {
check_protocol(message->protocol);
}
int contains_host_header = 0;
int h_index = 0;
while (1) {
if (curchar != 0 && (curchar == '\n' || (curchar == '\r' && nextchar == '\n'))) {
next();
if (curchar == '\n') {
next();
}
break;
}
if (h_index < HTTP_HEADER_MAX)
message->headers[h_index].name = buffer;
buffer = strchr(buffer, ':');
if (!buffer) return 413;
next();
while (curchar != 0 && (curchar == ' ' || curchar == '\r' || curchar == '\n' || curchar == '\t')) {
next();
}
if_eof(413);
if (h_index < HTTP_HEADER_MAX)
message->headers[h_index].value = buffer;
next_at_newline(413);
struct http_header *header = &message->headers[h_index];
if (type == HTTP_REQUEST) {
// is connection keep-alive? phase 1
if (!strcasecmp("Connection", header->name)) {
if (message->protocol[7] == '0' && !strcasecmp("Keep-Alive", header->value)) {
message->connection_keepalive = 1;
} else if (message->protocol[7] == '1' && !strcasecmp("Close", header->value)) {
message->connection_keepalive = 0;
}
message->connection_close = ! message->connection_keepalive;
}
// is gzip supported?
if (!strcasecmp("Accept-Encoding", header->name)) {
if (strstr(header->value, "gzip")) {
message->gzip_supported = 1;
}
}
// check Host header presence, required in HTTP/1.1
if (!contains_host_header && !strcasecmp("Host", header->name)) {
contains_host_header = 1;
}
}
h_index++;
}
if (type == HTTP_REQUEST) {
// is connection keep-alive? phase 2
if (! message->connection_keepalive && ! message->connection_close) {
if (message->protocol[7] == '0') {
message->connection_keepalive = 0;
} else {
message->connection_keepalive = 1;
}
message->connection_close = ! message->connection_keepalive;
}
// required Host header
if (message->protocol[7] == '1' && !contains_host_header) {
return 400;
}
}
message->body = buffer;
return 0;
}