Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 200 lines (167 sloc) 5.212 kB
72f60ce First commit.
Evan Miller authored
1 /* Parser definitions for mod_zip */
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;
17
18 parsing_file->header_sent = 0;
19 parsing_file->trailer_sent = 0;
20
21 parsing_file->crc32 = 0;
22 parsing_file->size = 0;
23 }
24
25 static ngx_int_t
26 ngx_http_zip_clean_range(ngx_http_zip_range_t *range,
27 int prefix, int suffix, ngx_http_zip_ctx_t *ctx)
28 {
29 if (suffix) {
30 range->end = ctx->archive_size;
31 range->start = ctx->archive_size - range->start;
32 } else if (prefix) {
33 range->end = ctx->archive_size;
34 } else {
35 range->end++;
36 /*
37 * Download Accelerator sends the last byte position
38 * that equals to the file length
39 */
40 if (range->end >= ctx->archive_size) {
41 range->end = ctx->archive_size;
42 }
43 }
44 if (range->start < 0) {
45 return NGX_ERROR;
46 }
47 if (range->start >= ctx->archive_size) {
48 return NGX_ERROR;
49 }
50 return NGX_OK;
51 }
52
53 %%{
54 machine request;
55 write data;
56 }%%
57
58 ngx_int_t
59 ngx_http_zip_parse_request(ngx_http_zip_ctx_t *ctx)
60 {
61 int cs;
62 u_char *p = ctx->unparsed_request->data;
63 u_char *pe = ctx->unparsed_request->data + ctx->unparsed_request->len;
64 ngx_http_zip_file_t *parsing_file = NULL;
65
66 %%{
67
68 action start_file {
69 parsing_file = ngx_array_push(&ctx->files);
70 ngx_http_zip_file_init(parsing_file);
71
72 parsing_file->index = ctx->files.nelts - 1;
73 }
74
75 action start_uri {
76 parsing_file->uri.data = fpc;
77 parsing_file->uri.len = 1;
78 }
79
80 action end_uri {
81 parsing_file->uri.len = fpc - parsing_file->uri.data;
82 }
83 action start_args {
84 parsing_file->args.data = fpc;
85 }
86 action end_args {
87 parsing_file->args.len = fpc - parsing_file->args.data;
88 }
89 action size_incr {
90 parsing_file->size = parsing_file->size * 10 + (fc - '0');
91 }
92 action crc_incr {
93 if (fc == '-') {
94 ctx->missing_crc32 = 1;
95 } else {
96 parsing_file->crc32 *= 16;
97 if (fc >= 'a' && fc <= 'f') {
98 parsing_file->crc32 += fc - 'a' + 10;
99 }
100 else if (fc >= 'A' && fc <= 'F') {
101 parsing_file->crc32 += fc - 'A' + 10;
102 } else { /* 0-9 */
103 parsing_file->crc32 += fc - '0';
104 }
105 }
106 }
107 action start_filename {
108 parsing_file->filename.data = fpc;
109 }
110 action end_filename {
111 parsing_file->filename.len = fpc - parsing_file->filename.data;
112 }
113
114 main := (
115 ( [0-9a-fA-F]+ | "-" ) >start_file $crc_incr
116 " "+
117 [0-9]+ $size_incr
118 " "+
119 [^? ]+ >start_uri %end_uri
120 ( "?" [^ ]+ >start_args %end_args )?
121 " "+
122 [^ ] >start_filename
123 [^\r\n\0]+ %end_filename
124 [\r\n]+
125 )+;
126
127 write init;
128 write exec;
129 }%%
130
131 if (cs < request_first_final) {
132 return NGX_ERROR;
133 }
134
135 ctx->parsed = 1;
136
137 return NGX_OK;
138 }
139
140 %%{
141 machine range;
142 write data;
143 }%%
144
145 ngx_int_t
146 ngx_http_zip_parse_range(ngx_http_request_t *r, ngx_str_t *range_str, ngx_http_zip_ctx_t *ctx)
147 {
148 int cs, prefix = 0, suffix = 0;
149
150 ngx_http_zip_range_t *range = NULL;
151 u_char *p = range_str->data;
152 u_char *pe = range_str->data + range_str->len;
153
154 %%{
155 action new_range {
156 if (range) {
157 if (ngx_http_zip_clean_range(range, prefix, suffix, ctx) == NGX_ERROR) {
158 return NGX_ERROR;
159 }
160 }
161 if ((range = ngx_array_push(&ctx->ranges)) == NULL) {
162 return NGX_ERROR;
163 }
164 range->start = 0; range->end = 0; range->boundary_sent = 0;
165 suffix = 0;
166 prefix = 1;
167 }
168
169 action start_incr { range->start = range->start * 10 + (fc - '0'); }
170
171 action end_incr { range->end = range->end * 10 + (fc - '0'); prefix = 0; }
172
173 action suffix { suffix = 1; }
174
175 suffix_byte_range_spec = "-" [0-9]+ $start_incr >suffix;
176 byte_range_spec = [0-9]+ $start_incr
177 "-"
178 [0-9]* $end_incr;
179 byte_range_specs = ( byte_range_spec | suffix_byte_range_spec ) >new_range;
180 byte_range_set = byte_range_specs ( "," byte_range_specs )*;
181
182 main := "bytes=" byte_range_set;
183
184 write init;
185 write exec;
186 }%%
187
188 if (cs < range_first_final) {
189 return NGX_ERROR;
190 }
191
192 if (range) {
193 if (ngx_http_zip_clean_range(range, prefix, suffix, ctx) == NGX_ERROR) {
194 return NGX_ERROR;
195 }
196 }
197
198 return NGX_OK;
199 }
Something went wrong with that request. Please try again.