Skip to content
This repository
Newer
Older
100644 239 lines (199 sloc) 6.291 kb
911b447c »
2010-01-26 Update from Google Code SVN
1 /* Ragel Parser definitions for mod_zip64 */
72f60ce1 »
2009-10-19 First commit.
2
3 #include "ngx_http_zip_module.h"
4 #include "ngx_http_zip_parsers.h"
5
6 static void
7 ngx_http_zip_file_init(ngx_http_zip_file_t *parsing_file)
8 {
9 parsing_file->uri.data = NULL;
10 parsing_file->uri.len = 0;
11
12 parsing_file->args.data = NULL;
13 parsing_file->args.len = 0;
14
15 parsing_file->filename.data = NULL;
16 parsing_file->filename.len = 0;
46efeae0 »
2010-11-14 add X-Archive-Charset and Unicode Path extra field
17
18 parsing_file->filename_utf8.data = NULL;
19 parsing_file->filename_utf8.len = 0;
72f60ce1 »
2009-10-19 First commit.
20
21 parsing_file->header_sent = 0;
22 parsing_file->trailer_sent = 0;
23
24 parsing_file->crc32 = 0;
25 parsing_file->size = 0;
911b447c »
2010-01-26 Update from Google Code SVN
26
27 parsing_file->missing_crc32 = 0;
28 parsing_file->need_zip64 = 0;
29 parsing_file->need_zip64_offset = 0;
30 }
31
32 inline char hex_char_value(unsigned char ch) {
33 if ('0' <= ch && ch <= '9')
34 return ch - '0';
35 if ('A' <= ch && ch <= 'F')
36 return ch - 'A' + 10;
37 if ('a' <= ch && ch <= 'f')
38 return ch - 'A' + 10;
39 return 0;
40 }
41
42 size_t destructive_url_decode_len(unsigned char* start, unsigned char* end)
43 {
44 unsigned char *read_pos = start, *write_pos = start;
45
46 for (; read_pos < end; read_pos++) {
47 unsigned char ch = *read_pos;
48 if (ch == '%' && (read_pos+2 < end)) {
49 ch = 16 * hex_char_value(*(read_pos+1)) + hex_char_value(*(read_pos+2));
50 read_pos += 2;
51 }
52 if (ch == '+')
53 ch = ' ';
54 *(write_pos++) = ch;
55 }
56
57 return write_pos - start;
72f60ce1 »
2009-10-19 First commit.
58 }
59
911b447c »
2010-01-26 Update from Google Code SVN
60
72f60ce1 »
2009-10-19 First commit.
61 static ngx_int_t
62 ngx_http_zip_clean_range(ngx_http_zip_range_t *range,
63 int prefix, int suffix, ngx_http_zip_ctx_t *ctx)
64 {
65 if (suffix) {
66 range->end = ctx->archive_size;
67 range->start = ctx->archive_size - range->start;
68 } else if (prefix) {
69 range->end = ctx->archive_size;
70 } else {
71 range->end++;
72 /*
73 * Download Accelerator sends the last byte position
74 * that equals to the file length
75 */
76 if (range->end >= ctx->archive_size) {
77 range->end = ctx->archive_size;
78 }
79 }
80 if (range->start < 0) {
81 return NGX_ERROR;
82 }
83 if (range->start >= ctx->archive_size) {
84 return NGX_ERROR;
85 }
86 return NGX_OK;
87 }
88
89 %%{
90 machine request;
91 write data;
92 }%%
93
94 ngx_int_t
95 ngx_http_zip_parse_request(ngx_http_zip_ctx_t *ctx)
96 {
97 int cs;
98 u_char *p = ctx->unparsed_request->data;
99 u_char *pe = ctx->unparsed_request->data + ctx->unparsed_request->len;
100 ngx_http_zip_file_t *parsing_file = NULL;
101
102 %%{
103
104 action start_file {
105 parsing_file = ngx_array_push(&ctx->files);
106 ngx_http_zip_file_init(parsing_file);
107
108 parsing_file->index = ctx->files.nelts - 1;
109 }
110
111 action start_uri {
112 parsing_file->uri.data = fpc;
113 parsing_file->uri.len = 1;
114 }
115
116 action end_uri {
911b447c »
2010-01-26 Update from Google Code SVN
117 //parsing_file->uri.len = fpc - parsing_file->uri.data;
118 parsing_file->uri.len = destructive_url_decode_len(parsing_file->uri.data, fpc);
72f60ce1 »
2009-10-19 First commit.
119 }
120 action start_args {
121 parsing_file->args.data = fpc;
122 }
123 action end_args {
124 parsing_file->args.len = fpc - parsing_file->args.data;
125 }
126 action size_incr {
127 parsing_file->size = parsing_file->size * 10 + (fc - '0');
128 }
129 action crc_incr {
130 if (fc == '-') {
131 ctx->missing_crc32 = 1;
911b447c »
2010-01-26 Update from Google Code SVN
132 parsing_file->missing_crc32 = 1;
133 parsing_file->crc32 = 0xffffffff;
72f60ce1 »
2009-10-19 First commit.
134 } else {
135 parsing_file->crc32 *= 16;
136 if (fc >= 'a' && fc <= 'f') {
137 parsing_file->crc32 += fc - 'a' + 10;
138 }
139 else if (fc >= 'A' && fc <= 'F') {
140 parsing_file->crc32 += fc - 'A' + 10;
141 } else { /* 0-9 */
142 parsing_file->crc32 += fc - '0';
143 }
144 }
145 }
146 action start_filename {
147 parsing_file->filename.data = fpc;
148 }
149 action end_filename {
150 parsing_file->filename.len = fpc - parsing_file->filename.data;
151 }
152
153 main := (
154 ( [0-9a-fA-F]+ | "-" ) >start_file $crc_incr
155 " "+
156 [0-9]+ $size_incr
157 " "+
158 [^? ]+ >start_uri %end_uri
159 ( "?" [^ ]+ >start_args %end_args )?
160 " "+
161 [^ ] >start_filename
162 [^\r\n\0]+ %end_filename
163 [\r\n]+
164 )+;
165
166 write init;
167 write exec;
168 }%%
169
170 if (cs < request_first_final) {
171 return NGX_ERROR;
172 }
173
174 ctx->parsed = 1;
175
176 return NGX_OK;
177 }
178
179 %%{
180 machine range;
181 write data;
182 }%%
183
184 ngx_int_t
185 ngx_http_zip_parse_range(ngx_http_request_t *r, ngx_str_t *range_str, ngx_http_zip_ctx_t *ctx)
186 {
187 int cs, prefix = 0, suffix = 0;
188
189 ngx_http_zip_range_t *range = NULL;
190 u_char *p = range_str->data;
191 u_char *pe = range_str->data + range_str->len;
192
193 %%{
194 action new_range {
195 if (range) {
196 if (ngx_http_zip_clean_range(range, prefix, suffix, ctx) == NGX_ERROR) {
197 return NGX_ERROR;
198 }
199 }
200 if ((range = ngx_array_push(&ctx->ranges)) == NULL) {
201 return NGX_ERROR;
202 }
203 range->start = 0; range->end = 0; range->boundary_sent = 0;
204 suffix = 0;
205 prefix = 1;
206 }
207
208 action start_incr { range->start = range->start * 10 + (fc - '0'); }
209
210 action end_incr { range->end = range->end * 10 + (fc - '0'); prefix = 0; }
211
212 action suffix { suffix = 1; }
213
214 suffix_byte_range_spec = "-" [0-9]+ $start_incr >suffix;
215 byte_range_spec = [0-9]+ $start_incr
216 "-"
217 [0-9]* $end_incr;
218 byte_range_specs = ( byte_range_spec | suffix_byte_range_spec ) >new_range;
219 byte_range_set = byte_range_specs ( "," byte_range_specs )*;
220
221 main := "bytes=" byte_range_set;
222
223 write init;
224 write exec;
225 }%%
226
227 if (cs < range_first_final) {
228 return NGX_ERROR;
229 }
230
231 if (range) {
232 if (ngx_http_zip_clean_range(range, prefix, suffix, ctx) == NGX_ERROR) {
233 return NGX_ERROR;
234 }
235 }
236
237 return NGX_OK;
238 }
Something went wrong with that request. Please try again.