-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
65 lines (55 loc) · 1.28 KB
/
parse.c
File metadata and controls
65 lines (55 loc) · 1.28 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
#include "parse.h"
/**
* Given a char buffer returns the parsed request headers
*/
Request * parse(char *buffer, int size, HTTPContext *context) {
//Differant states in the state machine
enum {
STATE_START = 0, STATE_CR, STATE_CRLF, STATE_CRLFCR, STATE_CRLFCRLF
};
int i = 0, state;
size_t offset = 0;
char ch;
char buf[8192];
memset(buf, 0, 8192);
state = STATE_START;
while (state != STATE_CRLFCRLF)
{
char expected = 0;
if (i == size)
break;
ch = buffer[i++];
buf[offset++] = ch;
switch (state) {
case STATE_START:
case STATE_CRLF:
expected = '\r';
break;
case STATE_CR:
case STATE_CRLFCR:
expected = '\n';
break;
default:
state = STATE_START;
continue;
}
if (ch == expected)
state++;
else
state = STATE_START;
}
//Valid End State
if (state == STATE_CRLFCRLF)
{
Request *request = (Request *) malloc(sizeof(Request));
request->header_count=0;
request->headers = (Request_header *) malloc(sizeof(Request_header)*1);
set_parsing_options(buf, i, request);
if (yyparse() == SUCCESS)
{
return request;
}
}
/* Handle Malformed Requests : set is valid to 0 and handle it in outer logic */
context->is_valid = 0;
}