-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpstream.c
356 lines (291 loc) · 9.74 KB
/
httpstream.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <stddef.h>
#include "gatling.h"
#ifdef SUPPORT_PROXY
void httpstream_initstate(struct httpstreamstate* hss) {
hss->state=HSS_HEADER;
hss->bytesleft=0;
hss->type=DONTKNOW;
}
int httpstream_update(struct httpstreamstate* hss,char c) {
switch (hss->state) {
case HSS_HEADER_CRLF:
if (c=='\r') {
hss->state=HSS_HEADER_CRLFCR;
break;
}
goto new_header_line;
// else fall through
case HSS_HEADER:
if (hss->type == DONTKNOW) {
if (c=='H') hss->type=RESPONSE_MAYBE;
else if (c=='P') hss->type=POSTREQUEST;
else hss->type=REQUEST;
}
else if (hss->type == RESPONSE_MAYBE) {
if (c=='T')
hss->type=RESPONSE; /* "HTTP/2.0 OK" */
else
hss->type=REQUEST; /* "HEAD /foo HTTP/1.1" */
}
new_header_line:
if ((c|0x20)=='c') hss->state=HSS_HEADER_C;
else if ((c|0x20)=='t') hss->state=HSS_HEADER_T;
else {
shared:
if (c=='\r') hss->state=HSS_HEADER_CR;
else hss->state=HSS_HEADER_OTHER;
}
break;
case HSS_HEADER_C:
if ((c|0x20)=='o') { hss->state=HSS_HEADER_CO; break; }
else goto shared;
case HSS_HEADER_CO:
if ((c|0x20)=='n') { hss->state=HSS_HEADER_CON; break; }
else goto shared;
case HSS_HEADER_CON:
if ((c|0x20)=='t') { hss->state=HSS_HEADER_CONT; break; }
else goto shared;
case HSS_HEADER_CONT:
if ((c|0x20)=='e') { hss->state=HSS_HEADER_CONTE; break; }
else goto shared;
case HSS_HEADER_CONTE:
if ((c|0x20)=='n') { hss->state=HSS_HEADER_CONTEN; break; }
else goto shared;
case HSS_HEADER_CONTEN:
if ((c|0x20)=='t') { hss->state=HSS_HEADER_CONTENT; break; }
else goto shared;
case HSS_HEADER_CONTENT:
if (c=='-') { hss->state=HSS_HEADER_CONTENT_; break; }
else goto shared;
case HSS_HEADER_CONTENT_:
if ((c|0x20)=='l') { hss->state=HSS_HEADER_CONTENT_L; break; }
else goto shared;
case HSS_HEADER_CONTENT_L:
if ((c|0x20)=='e') { hss->state=HSS_HEADER_CONTENT_LE; break; }
else goto shared;
case HSS_HEADER_CONTENT_LE:
if ((c|0x20)=='n') { hss->state=HSS_HEADER_CONTENT_LEN; break; }
else goto shared;
case HSS_HEADER_CONTENT_LEN:
if ((c|0x20)=='g') { hss->state=HSS_HEADER_CONTENT_LENG; break; }
else goto shared;
case HSS_HEADER_CONTENT_LENG:
if ((c|0x20)=='t') { hss->state=HSS_HEADER_CONTENT_LENGT; break; }
else goto shared;
case HSS_HEADER_CONTENT_LENGT:
if ((c|0x20)=='h') { hss->state=HSS_HEADER_CONTENT_LENGTH; break; }
else goto shared;
case HSS_HEADER_CONTENT_LENGTH:
if (c==':') { hss->state=HSS_HEADER_CONTENT_LENGTH_; break; }
else goto shared;
case HSS_HEADER_CONTENT_LENGTH_:
if (c==' '||c=='\t') break; // skip whitespace without changing state
if (c>='0' && c<='9') { hss->bytesleft=c-'0'; hss->state=HSS_HEADER_CONTENT_LENGTH_NUM; break; }
else goto shared;
case HSS_HEADER_CONTENT_LENGTH_NUM:
// printf("<bs=%ld, c=%c, bs=%ld>",hss->bytesleft,c,hss->bytesleft*10+c-'0');
if (c>='0' && c<='9') { hss->bytesleft=hss->bytesleft*10+c-'0'; break; }
else if (c!='\r') hss->bytesleft=UNKNOWN;
goto shared;
case HSS_HEADER_T:
if ((c|0x20)=='r') { hss->state=HSS_HEADER_TR; break; }
else goto shared;
case HSS_HEADER_TR:
if ((c|0x20)=='a') { hss->state=HSS_HEADER_TRA; break; }
else goto shared;
case HSS_HEADER_TRA:
if ((c|0x20)=='n') { hss->state=HSS_HEADER_TRAN; break; }
else goto shared;
case HSS_HEADER_TRAN:
if ((c|0x20)=='s') { hss->state=HSS_HEADER_TRANS; break; }
else goto shared;
case HSS_HEADER_TRANS:
if ((c|0x20)=='f') { hss->state=HSS_HEADER_TRANSF; break; }
else goto shared;
case HSS_HEADER_TRANSF:
if ((c|0x20)=='e') { hss->state=HSS_HEADER_TRANSFE; break; }
else goto shared;
case HSS_HEADER_TRANSFE:
if ((c|0x20)=='r') { hss->state=HSS_HEADER_TRANSFER; break; }
else goto shared;
case HSS_HEADER_TRANSFER:
if (c=='-') { hss->state=HSS_HEADER_TRANSFER_; break; }
else goto shared;
case HSS_HEADER_TRANSFER_:
if ((c|0x20)=='e') { hss->state=HSS_HEADER_TRANSFER_E; break; }
else goto shared;
case HSS_HEADER_TRANSFER_E:
if ((c|0x20)=='n') { hss->state=HSS_HEADER_TRANSFER_EN; break; }
else goto shared;
case HSS_HEADER_TRANSFER_EN:
if ((c|0x20)=='c') { hss->state=HSS_HEADER_TRANSFER_ENC; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENC:
if ((c|0x20)=='o') { hss->state=HSS_HEADER_TRANSFER_ENCO; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCO:
if ((c|0x20)=='d') { hss->state=HSS_HEADER_TRANSFER_ENCOD; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCOD:
if ((c|0x20)=='i') { hss->state=HSS_HEADER_TRANSFER_ENCODI; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODI:
if ((c|0x20)=='n') { hss->state=HSS_HEADER_TRANSFER_ENCODIN; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODIN:
if ((c|0x20)=='g') { hss->state=HSS_HEADER_TRANSFER_ENCODING; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING:
if (c==':') { hss->state=HSS_HEADER_TRANSFER_ENCODING_; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_:
if (c==' ' || c=='\t') break; /* skip whitespace */
if ((c|0x20)=='c') { hss->state=HSS_HEADER_TRANSFER_ENCODING_C; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_C:
if ((c|0x20)=='h') { hss->state=HSS_HEADER_TRANSFER_ENCODING_CH; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_CH:
if ((c|0x20)=='u') { hss->state=HSS_HEADER_TRANSFER_ENCODING_CHU; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_CHU:
if ((c|0x20)=='n') { hss->state=HSS_HEADER_TRANSFER_ENCODING_CHUN; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_CHUN:
if ((c|0x20)=='k') { hss->state=HSS_HEADER_TRANSFER_ENCODING_CHUNK; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_CHUNK:
if ((c|0x20)=='e') { hss->state=HSS_HEADER_TRANSFER_ENCODING_CHUNKE; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_CHUNKE:
if ((c|0x20)=='d') { hss->state=HSS_HEADER_TRANSFER_ENCODING_CHUNKED; break; }
else goto shared;
case HSS_HEADER_TRANSFER_ENCODING_CHUNKED:
hss->state=HSS_HEADER_OTHER;
if (c==' ' || c=='\t' || c==';' || c=='\r' || c=='\n')
hss->bytesleft=CHUNKED;
goto shared;
case HSS_HEADER_OTHER:
goto shared;
case HSS_HEADER_CR:
if (c=='\n') { hss->state=HSS_HEADER_CRLF; break; }
else goto shared;
case HSS_HEADER_CRLFCR:
if (c=='\n') {
// end of header found.
if (hss->type==REQUEST) {
// if it's a GET request, ignore content-length and chunked
// encoding.
hss->bytesleft=0;
hss->state=HSS_DONE;
return 1;
}
// Now we either have found a content-length or the chunked
// encoding marker or neither.
if (hss->bytesleft==CHUNKED) {
// chunked encoding; read chunked header next.
hss->state=HSS_HEADER_CHUNKED_CRLF;
} else if (hss->bytesleft==UNKNOWN || hss->bytesleft==0) {
// neither chunked encoding nor content length
// only way to end the stream is to drop the connection
hss->state=HSS_INFINITE;
} else {
// found positive content length
hss->state=HSS_KNOWLENGTH;
}
break;
}
else goto shared;
case HSS_KNOWLENGTH:
if (--hss->bytesleft==0) {
hss->state=HSS_DONE;
return 1;
}
break;
case HSS_HEADER_CHUNKED:
if (c=='\r')
hss->state=HSS_HEADER_CHUNKED_CR;
break;
case HSS_HEADER_CHUNKED_CR:
if (c=='\n')
hss->state=HSS_HEADER_CHUNKED_CRLF;
break;
case HSS_HEADER_CHUNKED_CRLF:
hss->bytesleft=0;
// fall through
case HSS_HEADER_CHUNKED_CRLF_NUM:
if (c>='0' && c<='9')
hss->bytesleft=hss->bytesleft*10+c-'0';
else if (c>='a' && c<='f')
hss->bytesleft=hss->bytesleft*10+c-'a'+10;
else if (c>='A' && c<='F')
hss->bytesleft=hss->bytesleft*10+c-'A'+10;
else if (c=='\r' && hss->state==HSS_HEADER_CHUNKED_CRLF_NUM) {
hss->state=HSS_HEADER_CHUNKED_CRLF_NUM_CR;
break;
}
// printf("[bl=%d]",hss->bytesleft);
hss->state=HSS_HEADER_CHUNKED_CRLF_NUM;
break;
case HSS_HEADER_CHUNKED_CRLF_NUM_CR:
if (c=='\n') {
if (hss->bytesleft==0) {
hss->state=HSS_DONE;
return 1;
}
hss->state=HSS_KNOWLENGTH_CHUNKED;
}
break;
case HSS_KNOWLENGTH_CHUNKED:
if (--hss->bytesleft==0)
/* chunk is done; read next one */
hss->state=HSS_HEADER_CHUNKED;
break;
case HSS_INFINITE:
break;
case HSS_DONE:
return 1;
}
return 0;
}
size_t httpstream(struct httpstreamstate* hss,
const char* buf, size_t len) {
// buf is part of a http request or reply
// this is meant to be called from a proxy loop that keeps reading
// data from a socket and writing it to another socket. For each
// chunk of data that is coming in, the code calls this function on
// each chunk. This function then parses the chunk and says how many
// bytes in it belong to the http stream. Basically, it will return
// len until the stream is finished (request + POST data received or
// reply + content data received).
size_t i;
if (hss->state==HSS_DONE)
return 0;
for (i=0; i<len; ++i)
if (httpstream_update(hss,buf[i]))
return i+1;
return len;
}
#if NEED_MAIN
#include <stdio.h>
int main() {
struct httpstreamstate hss;
// char buf[]="POST / HTTP/1.0\r\nHost: localhost:80\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nfnord\r\n0\r\nhalali";
char buf[10000];
size_t i,l;
FILE* f;
f=fopen("/tmp/a/blubber","r");
l=fread(buf,1,sizeof(buf),f);
httpstream_initstate(&hss);
for (i=0; i<l; ++i) {
printf("%c",buf[i]);
if (httpstream_update(&hss,buf[i]))
break;
printf("[%d,bl=%ld]",hss.state,hss.bytesleft);
}
httpstream_initstate(&hss);
printf("%zu\n",httpstream(&hss,buf,l));
return 0;
}
#endif
#endif