Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 543 lines (461 sloc) 11.53 kb
911b447 Update from Google Code SVN
Evan Miller authored
1
72f60ce First commit.
Evan Miller authored
2 #line 1 "ngx_http_zip_parsers.rl"
911b447 Update from Google Code SVN
Evan Miller authored
3 /* Ragel Parser definitions for mod_zip64 */
72f60ce First commit.
Evan Miller authored
4
5 #include "ngx_http_zip_module.h"
6 #include "ngx_http_zip_parsers.h"
7
8 static void
9 ngx_http_zip_file_init(ngx_http_zip_file_t *parsing_file)
10 {
11 parsing_file->uri.data = NULL;
12 parsing_file->uri.len = 0;
13
14 parsing_file->args.data = NULL;
15 parsing_file->args.len = 0;
16
17 parsing_file->filename.data = NULL;
18 parsing_file->filename.len = 0;
19
20 parsing_file->header_sent = 0;
21 parsing_file->trailer_sent = 0;
22
23 parsing_file->crc32 = 0;
24 parsing_file->size = 0;
911b447 Update from Google Code SVN
Evan Miller authored
25
26 parsing_file->missing_crc32 = 0;
27 parsing_file->need_zip64 = 0;
28 parsing_file->need_zip64_offset = 0;
29 }
30
31 inline char hex_char_value(unsigned char ch) {
32 if ('0' <= ch && ch <= '9')
33 return ch - '0';
34 if ('A' <= ch && ch <= 'F')
35 return ch - 'A' + 10;
36 if ('a' <= ch && ch <= 'f')
37 return ch - 'A' + 10;
38 return 0;
39 }
40
41 size_t destructive_url_decode_len(unsigned char* start, unsigned char* end)
42 {
43 unsigned char *read_pos = start, *write_pos = start;
44
45 for (; read_pos < end; read_pos++) {
46 unsigned char ch = *read_pos;
47 if (ch == '%' && (read_pos+2 < end)) {
48 ch = 16 * hex_char_value(*(read_pos+1)) + hex_char_value(*(read_pos+2));
49 read_pos += 2;
50 }
51 if (ch == '+')
52 ch = ' ';
53 *(write_pos++) = ch;
54 }
55
56 return write_pos - start;
72f60ce First commit.
Evan Miller authored
57 }
58
911b447 Update from Google Code SVN
Evan Miller authored
59
72f60ce First commit.
Evan Miller authored
60 static ngx_int_t
61 ngx_http_zip_clean_range(ngx_http_zip_range_t *range,
62 int prefix, int suffix, ngx_http_zip_ctx_t *ctx)
63 {
64 if (suffix) {
65 range->end = ctx->archive_size;
66 range->start = ctx->archive_size - range->start;
67 } else if (prefix) {
68 range->end = ctx->archive_size;
69 } else {
70 range->end++;
71 /*
72 * Download Accelerator sends the last byte position
73 * that equals to the file length
74 */
75 if (range->end >= ctx->archive_size) {
76 range->end = ctx->archive_size;
77 }
78 }
79 if (range->start < 0) {
80 return NGX_ERROR;
81 }
82 if (range->start >= ctx->archive_size) {
83 return NGX_ERROR;
84 }
85 return NGX_OK;
86 }
87
88
911b447 Update from Google Code SVN
Evan Miller authored
89 #line 90 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
90 static const char _request_actions[] = {
91 0, 1, 1, 1, 2, 1, 3, 1,
92 4, 1, 5, 1, 6, 1, 7, 1,
93 8, 2, 0, 6
94 };
95
96 static const char _request_key_offsets[] = {
97 0, 0, 7, 8, 11, 14, 16, 18,
98 19, 22, 25, 32, 33, 34
99 };
100
101 static const char _request_trans_keys[] = {
102 45, 48, 57, 65, 70, 97, 102, 32,
103 32, 48, 57, 32, 48, 57, 32, 63,
104 32, 63, 32, 0, 10, 13, 0, 10,
105 13, 32, 48, 57, 65, 70, 97, 102,
106 32, 32, 10, 13, 45, 48, 57, 65,
107 70, 97, 102, 0
108 };
109
110 static const char _request_single_lengths[] = {
111 0, 1, 1, 1, 1, 2, 2, 1,
112 3, 3, 1, 1, 1, 3
113 };
114
115 static const char _request_range_lengths[] = {
116 0, 3, 0, 1, 1, 0, 0, 0,
117 0, 0, 3, 0, 0, 3
118 };
119
120 static const char _request_index_offsets[] = {
121 0, 0, 5, 7, 10, 13, 16, 19,
122 21, 25, 29, 34, 36, 38
123 };
124
125 static const char _request_indicies[] = {
126 0, 2, 2, 2, 1, 3, 1, 3,
127 4, 1, 5, 4, 1, 5, 1, 6,
128 8, 9, 7, 11, 10, 1, 1, 1,
129 12, 1, 13, 13, 12, 3, 14, 14,
130 14, 1, 1, 15, 17, 16, 18, 18,
131 0, 2, 2, 2, 1, 0
132 };
133
134 static const char _request_trans_targs[] = {
135 2, 0, 10, 3, 4, 5, 6, 6,
136 7, 11, 8, 7, 9, 13, 10, 12,
137 12, 7, 13
138 };
139
140 static const char _request_trans_actions[] = {
141 17, 0, 17, 0, 9, 0, 1, 0,
142 3, 3, 13, 0, 0, 15, 11, 5,
143 0, 7, 0
144 };
145
146 static const int request_start = 1;
147 static const int request_first_final = 13;
148 static const int request_error = 0;
149
150 static const int request_en_main = 1;
151
911b447 Update from Google Code SVN
Evan Miller authored
152
153 #line 89 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
154
155
156 ngx_int_t
157 ngx_http_zip_parse_request(ngx_http_zip_ctx_t *ctx)
158 {
159 int cs;
160 u_char *p = ctx->unparsed_request->data;
161 u_char *pe = ctx->unparsed_request->data + ctx->unparsed_request->len;
162 ngx_http_zip_file_t *parsing_file = NULL;
163
164
911b447 Update from Google Code SVN
Evan Miller authored
165 #line 166 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
166 {
167 cs = request_start;
168 }
169
911b447 Update from Google Code SVN
Evan Miller authored
170 #line 171 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
171 {
172 int _klen;
173 unsigned int _trans;
174 const char *_acts;
175 unsigned int _nacts;
176 const char *_keys;
177
178 if ( p == pe )
179 goto _test_eof;
180 if ( cs == 0 )
181 goto _out;
182 _resume:
183 _keys = _request_trans_keys + _request_key_offsets[cs];
184 _trans = _request_index_offsets[cs];
185
186 _klen = _request_single_lengths[cs];
187 if ( _klen > 0 ) {
188 const char *_lower = _keys;
189 const char *_mid;
190 const char *_upper = _keys + _klen - 1;
191 while (1) {
192 if ( _upper < _lower )
193 break;
194
195 _mid = _lower + ((_upper-_lower) >> 1);
196 if ( (*p) < *_mid )
197 _upper = _mid - 1;
198 else if ( (*p) > *_mid )
199 _lower = _mid + 1;
200 else {
201 _trans += (_mid - _keys);
202 goto _match;
203 }
204 }
205 _keys += _klen;
206 _trans += _klen;
207 }
208
209 _klen = _request_range_lengths[cs];
210 if ( _klen > 0 ) {
211 const char *_lower = _keys;
212 const char *_mid;
213 const char *_upper = _keys + (_klen<<1) - 2;
214 while (1) {
215 if ( _upper < _lower )
216 break;
217
218 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
219 if ( (*p) < _mid[0] )
220 _upper = _mid - 2;
221 else if ( (*p) > _mid[1] )
222 _lower = _mid + 2;
223 else {
224 _trans += ((_mid - _keys)>>1);
225 goto _match;
226 }
227 }
228 _trans += _klen;
229 }
230
231 _match:
232 _trans = _request_indicies[_trans];
233 cs = _request_trans_targs[_trans];
234
235 if ( _request_trans_actions[_trans] == 0 )
236 goto _again;
237
238 _acts = _request_actions + _request_trans_actions[_trans];
239 _nacts = (unsigned int) *_acts++;
240 while ( _nacts-- > 0 )
241 {
242 switch ( *_acts++ )
243 {
244 case 0:
911b447 Update from Google Code SVN
Evan Miller authored
245 #line 101 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
246 {
247 parsing_file = ngx_array_push(&ctx->files);
248 ngx_http_zip_file_init(parsing_file);
249
250 parsing_file->index = ctx->files.nelts - 1;
251 }
252 break;
253 case 1:
911b447 Update from Google Code SVN
Evan Miller authored
254 #line 108 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
255 {
256 parsing_file->uri.data = p;
257 parsing_file->uri.len = 1;
258 }
259 break;
260 case 2:
911b447 Update from Google Code SVN
Evan Miller authored
261 #line 113 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
262 {
911b447 Update from Google Code SVN
Evan Miller authored
263 //parsing_file->uri.len = fpc - parsing_file->uri.data;
264 parsing_file->uri.len = destructive_url_decode_len(parsing_file->uri.data, p);
72f60ce First commit.
Evan Miller authored
265 }
266 break;
267 case 3:
911b447 Update from Google Code SVN
Evan Miller authored
268 #line 117 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
269 {
270 parsing_file->args.data = p;
271 }
272 break;
273 case 4:
911b447 Update from Google Code SVN
Evan Miller authored
274 #line 120 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
275 {
276 parsing_file->args.len = p - parsing_file->args.data;
277 }
278 break;
279 case 5:
911b447 Update from Google Code SVN
Evan Miller authored
280 #line 123 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
281 {
282 parsing_file->size = parsing_file->size * 10 + ((*p) - '0');
283 }
284 break;
285 case 6:
911b447 Update from Google Code SVN
Evan Miller authored
286 #line 126 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
287 {
288 if ((*p) == '-') {
289 ctx->missing_crc32 = 1;
911b447 Update from Google Code SVN
Evan Miller authored
290 parsing_file->missing_crc32 = 1;
291 parsing_file->crc32 = 0xffffffff;
72f60ce First commit.
Evan Miller authored
292 } else {
293 parsing_file->crc32 *= 16;
294 if ((*p) >= 'a' && (*p) <= 'f') {
295 parsing_file->crc32 += (*p) - 'a' + 10;
296 }
297 else if ((*p) >= 'A' && (*p) <= 'F') {
298 parsing_file->crc32 += (*p) - 'A' + 10;
299 } else { /* 0-9 */
300 parsing_file->crc32 += (*p) - '0';
301 }
302 }
303 }
304 break;
305 case 7:
911b447 Update from Google Code SVN
Evan Miller authored
306 #line 143 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
307 {
308 parsing_file->filename.data = p;
309 }
310 break;
311 case 8:
911b447 Update from Google Code SVN
Evan Miller authored
312 #line 146 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
313 {
314 parsing_file->filename.len = p - parsing_file->filename.data;
315 }
316 break;
911b447 Update from Google Code SVN
Evan Miller authored
317 #line 318 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
318 }
319 }
320
321 _again:
322 if ( cs == 0 )
323 goto _out;
324 if ( ++p != pe )
325 goto _resume;
326 _test_eof: {}
327 _out: {}
328 }
911b447 Update from Google Code SVN
Evan Miller authored
329
330 #line 165 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
331
332
333 if (cs < request_first_final) {
334 return NGX_ERROR;
335 }
336
337 ctx->parsed = 1;
338
339 return NGX_OK;
340 }
341
342
911b447 Update from Google Code SVN
Evan Miller authored
343 #line 344 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
344 static const char _range_actions[] = {
345 0, 1, 0, 1, 1, 1, 2, 2,
346 0, 1, 2, 3, 1
347 };
348
349 static const char _range_key_offsets[] = {
350 0, 0, 1, 2, 3, 4, 5, 6,
351 9, 11, 14, 17
352 };
353
354 static const char _range_trans_keys[] = {
355 98, 121, 116, 101, 115, 61, 45, 48,
356 57, 48, 57, 45, 48, 57, 44, 48,
357 57, 44, 48, 57, 0
358 };
359
360 static const char _range_single_lengths[] = {
361 0, 1, 1, 1, 1, 1, 1, 1,
362 0, 1, 1, 1
363 };
364
365 static const char _range_range_lengths[] = {
366 0, 0, 0, 0, 0, 0, 0, 1,
367 1, 1, 1, 1
368 };
369
370 static const char _range_index_offsets[] = {
371 0, 0, 2, 4, 6, 8, 10, 12,
372 15, 17, 20, 23
373 };
374
375 static const char _range_trans_targs[] = {
376 2, 0, 3, 0, 4, 0, 5, 0,
377 6, 0, 7, 0, 8, 9, 0, 10,
378 0, 11, 9, 0, 7, 10, 0, 7,
379 11, 0, 0
380 };
381
382 static const char _range_trans_actions[] = {
383 0, 0, 0, 0, 0, 0, 0, 0,
384 0, 0, 0, 0, 1, 7, 0, 10,
385 0, 0, 3, 0, 0, 3, 0, 0,
386 5, 0, 0
387 };
388
389 static const int range_start = 1;
390 static const int range_first_final = 10;
391 static const int range_error = 0;
392
393 static const int range_en_main = 1;
394
911b447 Update from Google Code SVN
Evan Miller authored
395
396 #line 179 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
397
398
399 ngx_int_t
400 ngx_http_zip_parse_range(ngx_http_request_t *r, ngx_str_t *range_str, ngx_http_zip_ctx_t *ctx)
401 {
402 int cs, prefix = 0, suffix = 0;
403
404 ngx_http_zip_range_t *range = NULL;
405 u_char *p = range_str->data;
406 u_char *pe = range_str->data + range_str->len;
407
408
911b447 Update from Google Code SVN
Evan Miller authored
409 #line 410 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
410 {
411 cs = range_start;
412 }
413
911b447 Update from Google Code SVN
Evan Miller authored
414 #line 415 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
415 {
416 int _klen;
417 unsigned int _trans;
418 const char *_acts;
419 unsigned int _nacts;
420 const char *_keys;
421
422 if ( p == pe )
423 goto _test_eof;
424 if ( cs == 0 )
425 goto _out;
426 _resume:
427 _keys = _range_trans_keys + _range_key_offsets[cs];
428 _trans = _range_index_offsets[cs];
429
430 _klen = _range_single_lengths[cs];
431 if ( _klen > 0 ) {
432 const char *_lower = _keys;
433 const char *_mid;
434 const char *_upper = _keys + _klen - 1;
435 while (1) {
436 if ( _upper < _lower )
437 break;
438
439 _mid = _lower + ((_upper-_lower) >> 1);
440 if ( (*p) < *_mid )
441 _upper = _mid - 1;
442 else if ( (*p) > *_mid )
443 _lower = _mid + 1;
444 else {
445 _trans += (_mid - _keys);
446 goto _match;
447 }
448 }
449 _keys += _klen;
450 _trans += _klen;
451 }
452
453 _klen = _range_range_lengths[cs];
454 if ( _klen > 0 ) {
455 const char *_lower = _keys;
456 const char *_mid;
457 const char *_upper = _keys + (_klen<<1) - 2;
458 while (1) {
459 if ( _upper < _lower )
460 break;
461
462 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
463 if ( (*p) < _mid[0] )
464 _upper = _mid - 2;
465 else if ( (*p) > _mid[1] )
466 _lower = _mid + 2;
467 else {
468 _trans += ((_mid - _keys)>>1);
469 goto _match;
470 }
471 }
472 _trans += _klen;
473 }
474
475 _match:
476 cs = _range_trans_targs[_trans];
477
478 if ( _range_trans_actions[_trans] == 0 )
479 goto _again;
480
481 _acts = _range_actions + _range_trans_actions[_trans];
482 _nacts = (unsigned int) *_acts++;
483 while ( _nacts-- > 0 )
484 {
485 switch ( *_acts++ )
486 {
487 case 0:
911b447 Update from Google Code SVN
Evan Miller authored
488 #line 191 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
489 {
490 if (range) {
491 if (ngx_http_zip_clean_range(range, prefix, suffix, ctx) == NGX_ERROR) {
492 return NGX_ERROR;
493 }
494 }
495 if ((range = ngx_array_push(&ctx->ranges)) == NULL) {
496 return NGX_ERROR;
497 }
498 range->start = 0; range->end = 0; range->boundary_sent = 0;
499 suffix = 0;
500 prefix = 1;
501 }
502 break;
503 case 1:
911b447 Update from Google Code SVN
Evan Miller authored
504 #line 205 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
505 { range->start = range->start * 10 + ((*p) - '0'); }
506 break;
507 case 2:
911b447 Update from Google Code SVN
Evan Miller authored
508 #line 207 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
509 { range->end = range->end * 10 + ((*p) - '0'); prefix = 0; }
510 break;
511 case 3:
911b447 Update from Google Code SVN
Evan Miller authored
512 #line 209 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
513 { suffix = 1; }
514 break;
911b447 Update from Google Code SVN
Evan Miller authored
515 #line 516 "ngx_http_zip_parsers.c"
72f60ce First commit.
Evan Miller authored
516 }
517 }
518
519 _again:
520 if ( cs == 0 )
521 goto _out;
522 if ( ++p != pe )
523 goto _resume;
524 _test_eof: {}
525 _out: {}
526 }
911b447 Update from Google Code SVN
Evan Miller authored
527
528 #line 222 "ngx_http_zip_parsers.rl"
72f60ce First commit.
Evan Miller authored
529
530
531 if (cs < range_first_final) {
532 return NGX_ERROR;
533 }
534
535 if (range) {
536 if (ngx_http_zip_clean_range(range, prefix, suffix, ctx) == NGX_ERROR) {
537 return NGX_ERROR;
538 }
539 }
540
541 return NGX_OK;
542 }
Something went wrong with that request. Please try again.