-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_state.rl
More file actions
executable file
·159 lines (133 loc) · 2.83 KB
/
http_state.rl
File metadata and controls
executable file
·159 lines (133 loc) · 2.83 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
/**
* simplest http-header state machine (for JSCDebugger)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef DEBUG_HTTP
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
class HTTPParserDelegate
{
public:
virtual ~HTTPParserDelegate() {};
virtual void gotHeader(const char *headerName, const char *headerValue) = 0;
virtual void finishedParsingHeaders() = 0;
};
#endif
extern "C" {
int parseHeaders(int file, HTTPParserDelegate *delegate);
%%{
machine http_simple;
action copyKey {
if (key) {
free(key);
}
key = (char *)calloc(p - myTs + 1, 1);
memcpy(key, myTs, p - myTs);
}
action copyValue {
if (value) {
free(value);
}
value = (char *)calloc(p - myTs + 1, 1);
memcpy(value, myTs, p - myTs);
}
action markBegin {
myTs = p;
}
action errorHandler {
printf("parser error\n");
done = 1;
}
crlf = '\r\n';
key = (alpha | '-')+ % copyKey;
value = print+ % copyValue;
blank = ' ';
header = key >markBegin blank* ':' blank* value >markBegin crlf;
main := |*
header => {
if (delegate) delegate->gotHeader((const char *)key, (const char *)value);
};
crlf => {
if (delegate) delegate->finishedParsingHeaders();
done = 1;
fbreak;
};
*|;
}%%
%% write data;
int parseHeaders(int file, HTTPParserDelegate *delegate)
{
// machine state
int cs, act, done = 0;
char *ts, *te = 0;
// key and value storage
char *key = NULL, *value = NULL;
char *myTs;
%% write init;
#define READ_CHUNK_SIZE 128
int bufferSize = READ_CHUNK_SIZE;
char *buffer = (char *)calloc(bufferSize + 1, 1);
char *p = buffer;
while (!done) {
ssize_t readBytes = read(file, p, READ_CHUNK_SIZE);
if (readBytes <= 0) {
printf("EOF!?\n");
return -1;
}
char *pe = (p + readBytes);
%% write exec;
if (cs == http_simple_error) {
printf("error parsing\n");
}
if (!done) {
bufferSize *= 2;
char *newBuffer = (char *)realloc(buffer, bufferSize + 1);
if (!newBuffer) {
free(buffer);
printf("OUT OF MEMORY\n");
return 0;
}
if (newBuffer != buffer) {
myTs = newBuffer + (myTs - buffer);
p = newBuffer + (p - buffer);
buffer = newBuffer;
}
}
}
free(buffer);
// return how many bytes we actually parsed
return (p - buffer);
}
#ifdef DEBUG_HTTP
class SomeDelegate : public HTTPParserDelegate
{
void gotHeader(const char *name, const char *value)
{
printf("header: '%s' ~> '%s'\n", name, value);
}
void finishedParsingHeaders()
{
printf("we're done\n");
}
};
int main(int argc, char **argv) {
if (argc > 0) {
char *buff = (char *)calloc(2048, 1);
SomeDelegate *delegate = new SomeDelegate();
int f = open(argv[1], O_RDONLY);
int parsedBytes = parseHeaders(f, delegate);
if (parsedBytes > 0) {
read(f, buff, 2048);
printf("body:\n%s", buff);
}
close(f);
free(buff);
delete delegate;
}
}
#endif
}