Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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