Mercurial > hg > nginx
annotate src/http/modules/ngx_http_slice_filter_module.c @ 6940:39ff6939266e
Unified error messages about duplicate directives.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Wed, 22 Mar 2017 22:49:52 +0300 |
parents | d16ba0ea3434 |
children | a97ad1663ef4 |
rev | line source |
---|---|
6317 | 1 |
2 /* | |
3 * Copyright (C) Roman Arutyunyan | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 typedef struct { | |
14 size_t size; | |
15 } ngx_http_slice_loc_conf_t; | |
16 | |
17 | |
18 typedef struct { | |
19 off_t start; | |
20 off_t end; | |
21 ngx_str_t range; | |
22 ngx_str_t etag; | |
23 ngx_uint_t last; /* unsigned last:1; */ | |
24 } ngx_http_slice_ctx_t; | |
25 | |
26 | |
27 typedef struct { | |
28 off_t start; | |
29 off_t end; | |
30 off_t complete_length; | |
31 } ngx_http_slice_content_range_t; | |
32 | |
33 | |
34 static ngx_int_t ngx_http_slice_header_filter(ngx_http_request_t *r); | |
35 static ngx_int_t ngx_http_slice_body_filter(ngx_http_request_t *r, | |
36 ngx_chain_t *in); | |
37 static ngx_int_t ngx_http_slice_parse_content_range(ngx_http_request_t *r, | |
38 ngx_http_slice_content_range_t *cr); | |
39 static ngx_int_t ngx_http_slice_range_variable(ngx_http_request_t *r, | |
40 ngx_http_variable_value_t *v, uintptr_t data); | |
41 static off_t ngx_http_slice_get_start(ngx_http_request_t *r); | |
42 static void *ngx_http_slice_create_loc_conf(ngx_conf_t *cf); | |
43 static char *ngx_http_slice_merge_loc_conf(ngx_conf_t *cf, void *parent, | |
44 void *child); | |
45 static ngx_int_t ngx_http_slice_add_variables(ngx_conf_t *cf); | |
46 static ngx_int_t ngx_http_slice_init(ngx_conf_t *cf); | |
47 | |
48 | |
49 static ngx_command_t ngx_http_slice_filter_commands[] = { | |
50 | |
51 { ngx_string("slice"), | |
52 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
53 ngx_conf_set_size_slot, | |
54 NGX_HTTP_LOC_CONF_OFFSET, | |
55 offsetof(ngx_http_slice_loc_conf_t, size), | |
56 NULL }, | |
57 | |
58 ngx_null_command | |
59 }; | |
60 | |
61 | |
62 static ngx_http_module_t ngx_http_slice_filter_module_ctx = { | |
63 ngx_http_slice_add_variables, /* preconfiguration */ | |
64 ngx_http_slice_init, /* postconfiguration */ | |
65 | |
66 NULL, /* create main configuration */ | |
67 NULL, /* init main configuration */ | |
68 | |
69 NULL, /* create server configuration */ | |
70 NULL, /* merge server configuration */ | |
71 | |
72 ngx_http_slice_create_loc_conf, /* create location configuration */ | |
73 ngx_http_slice_merge_loc_conf /* merge location configuration */ | |
74 }; | |
75 | |
76 | |
77 ngx_module_t ngx_http_slice_filter_module = { | |
78 NGX_MODULE_V1, | |
79 &ngx_http_slice_filter_module_ctx, /* module context */ | |
80 ngx_http_slice_filter_commands, /* module directives */ | |
81 NGX_HTTP_MODULE, /* module type */ | |
82 NULL, /* init master */ | |
83 NULL, /* init module */ | |
84 NULL, /* init process */ | |
85 NULL, /* init thread */ | |
86 NULL, /* exit thread */ | |
87 NULL, /* exit process */ | |
88 NULL, /* exit master */ | |
89 NGX_MODULE_V1_PADDING | |
90 }; | |
91 | |
92 | |
93 static ngx_str_t ngx_http_slice_range_name = ngx_string("slice_range"); | |
94 | |
95 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
96 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
97 | |
98 | |
99 static ngx_int_t | |
100 ngx_http_slice_header_filter(ngx_http_request_t *r) | |
101 { | |
102 off_t end; | |
103 ngx_int_t rc; | |
104 ngx_table_elt_t *h; | |
105 ngx_http_slice_ctx_t *ctx; | |
106 ngx_http_slice_loc_conf_t *slcf; | |
107 ngx_http_slice_content_range_t cr; | |
108 | |
109 ctx = ngx_http_get_module_ctx(r, ngx_http_slice_filter_module); | |
110 if (ctx == NULL) { | |
111 return ngx_http_next_header_filter(r); | |
112 } | |
113 | |
114 if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT) { | |
115 if (r == r->main) { | |
116 ngx_http_set_ctx(r, NULL, ngx_http_slice_filter_module); | |
117 return ngx_http_next_header_filter(r); | |
118 } | |
119 | |
120 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
121 "unexpected status code %ui in slice response", | |
122 r->headers_out.status); | |
123 return NGX_ERROR; | |
124 } | |
125 | |
126 h = r->headers_out.etag; | |
127 | |
128 if (ctx->etag.len) { | |
129 if (h == NULL | |
130 || h->value.len != ctx->etag.len | |
131 || ngx_strncmp(h->value.data, ctx->etag.data, ctx->etag.len) | |
132 != 0) | |
133 { | |
134 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
135 "etag mismatch in slice response"); | |
136 return NGX_ERROR; | |
137 } | |
138 } | |
139 | |
140 if (h) { | |
141 ctx->etag = h->value; | |
142 } | |
143 | |
144 if (ngx_http_slice_parse_content_range(r, &cr) != NGX_OK) { | |
145 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
146 "invalid range in slice response"); | |
147 return NGX_ERROR; | |
148 } | |
149 | |
150 if (cr.complete_length == -1) { | |
151 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
152 "no complete length in slice response"); | |
153 return NGX_ERROR; | |
154 } | |
155 | |
156 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
157 "http slice response range: %O-%O/%O", | |
158 cr.start, cr.end, cr.complete_length); | |
159 | |
160 slcf = ngx_http_get_module_loc_conf(r, ngx_http_slice_filter_module); | |
161 | |
162 end = ngx_min(cr.start + (off_t) slcf->size, cr.complete_length); | |
163 | |
164 if (cr.start != ctx->start || cr.end != end) { | |
165 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
166 "unexpected range in slice response: %O-%O", | |
167 cr.start, cr.end); | |
168 return NGX_ERROR; | |
169 } | |
170 | |
171 ctx->start = end; | |
172 | |
173 r->headers_out.status = NGX_HTTP_OK; | |
174 r->headers_out.status_line.len = 0; | |
175 r->headers_out.content_length_n = cr.complete_length; | |
176 r->headers_out.content_offset = cr.start; | |
177 r->headers_out.content_range->hash = 0; | |
178 r->headers_out.content_range = NULL; | |
179 | |
180 r->allow_ranges = 1; | |
181 r->subrequest_ranges = 1; | |
182 r->single_range = 1; | |
183 | |
184 rc = ngx_http_next_header_filter(r); | |
185 | |
186 if (r != r->main) { | |
187 return rc; | |
188 } | |
189 | |
190 if (r->headers_out.status == NGX_HTTP_PARTIAL_CONTENT) { | |
191 if (ctx->start + (off_t) slcf->size <= r->headers_out.content_offset) { | |
192 ctx->start = slcf->size | |
193 * (r->headers_out.content_offset / slcf->size); | |
194 } | |
195 | |
196 ctx->end = r->headers_out.content_offset | |
197 + r->headers_out.content_length_n; | |
198 | |
199 } else { | |
200 ctx->end = cr.complete_length; | |
201 } | |
202 | |
203 return rc; | |
204 } | |
205 | |
206 | |
207 static ngx_int_t | |
208 ngx_http_slice_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
209 { | |
210 ngx_int_t rc; | |
211 ngx_chain_t *cl; | |
212 ngx_http_request_t *sr; | |
213 ngx_http_slice_ctx_t *ctx; | |
214 ngx_http_slice_loc_conf_t *slcf; | |
215 | |
216 ctx = ngx_http_get_module_ctx(r, ngx_http_slice_filter_module); | |
217 | |
218 if (ctx == NULL || r != r->main) { | |
219 return ngx_http_next_body_filter(r, in); | |
220 } | |
221 | |
222 for (cl = in; cl; cl = cl->next) { | |
223 if (cl->buf->last_buf) { | |
224 cl->buf->last_buf = 0; | |
6322
4f0f4f02c98f
Slice filter: terminate first slice with last_in_chain flag.
Roman Arutyunyan <arut@nginx.com>
parents:
6321
diff
changeset
|
225 cl->buf->last_in_chain = 1; |
6317 | 226 cl->buf->sync = 1; |
227 ctx->last = 1; | |
228 } | |
229 } | |
230 | |
231 rc = ngx_http_next_body_filter(r, in); | |
232 | |
233 if (rc == NGX_ERROR || !ctx->last) { | |
234 return rc; | |
235 } | |
236 | |
237 if (ctx->start >= ctx->end) { | |
238 ngx_http_set_ctx(r, NULL, ngx_http_slice_filter_module); | |
239 ngx_http_send_special(r, NGX_HTTP_LAST); | |
240 return rc; | |
241 } | |
242 | |
6321
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
243 if (r->buffered) { |
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
244 return rc; |
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
245 } |
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
246 |
6907
d16ba0ea3434
Slice filter: fetch slices in cloned subrequests.
Roman Arutyunyan <arut@nginx.com>
parents:
6322
diff
changeset
|
247 if (ngx_http_subrequest(r, &r->uri, &r->args, &sr, NULL, |
d16ba0ea3434
Slice filter: fetch slices in cloned subrequests.
Roman Arutyunyan <arut@nginx.com>
parents:
6322
diff
changeset
|
248 NGX_HTTP_SUBREQUEST_CLONE) |
d16ba0ea3434
Slice filter: fetch slices in cloned subrequests.
Roman Arutyunyan <arut@nginx.com>
parents:
6322
diff
changeset
|
249 != NGX_OK) |
d16ba0ea3434
Slice filter: fetch slices in cloned subrequests.
Roman Arutyunyan <arut@nginx.com>
parents:
6322
diff
changeset
|
250 { |
6317 | 251 return NGX_ERROR; |
252 } | |
253 | |
254 ngx_http_set_ctx(sr, ctx, ngx_http_slice_filter_module); | |
255 | |
256 slcf = ngx_http_get_module_loc_conf(r, ngx_http_slice_filter_module); | |
257 | |
258 ctx->range.len = ngx_sprintf(ctx->range.data, "bytes=%O-%O", ctx->start, | |
259 ctx->start + (off_t) slcf->size - 1) | |
260 - ctx->range.data; | |
261 | |
262 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
263 "http slice subrequest: \"%V\"", &ctx->range); | |
264 | |
265 return rc; | |
266 } | |
267 | |
268 | |
269 static ngx_int_t | |
270 ngx_http_slice_parse_content_range(ngx_http_request_t *r, | |
271 ngx_http_slice_content_range_t *cr) | |
272 { | |
273 off_t start, end, complete_length, cutoff, cutlim; | |
274 u_char *p; | |
275 ngx_table_elt_t *h; | |
276 | |
277 h = r->headers_out.content_range; | |
278 | |
279 if (h == NULL | |
280 || h->value.len < 7 | |
281 || ngx_strncmp(h->value.data, "bytes ", 6) != 0) | |
282 { | |
283 return NGX_ERROR; | |
284 } | |
285 | |
286 p = h->value.data + 6; | |
287 | |
288 cutoff = NGX_MAX_OFF_T_VALUE / 10; | |
289 cutlim = NGX_MAX_OFF_T_VALUE % 10; | |
290 | |
291 start = 0; | |
292 end = 0; | |
293 complete_length = 0; | |
294 | |
295 while (*p == ' ') { p++; } | |
296 | |
297 if (*p < '0' || *p > '9') { | |
298 return NGX_ERROR; | |
299 } | |
300 | |
301 while (*p >= '0' && *p <= '9') { | |
302 if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) { | |
303 return NGX_ERROR; | |
304 } | |
305 | |
306 start = start * 10 + *p++ - '0'; | |
307 } | |
308 | |
309 while (*p == ' ') { p++; } | |
310 | |
311 if (*p++ != '-') { | |
312 return NGX_ERROR; | |
313 } | |
314 | |
315 while (*p == ' ') { p++; } | |
316 | |
317 if (*p < '0' || *p > '9') { | |
318 return NGX_ERROR; | |
319 } | |
320 | |
321 while (*p >= '0' && *p <= '9') { | |
322 if (end >= cutoff && (end > cutoff || *p - '0' > cutlim)) { | |
323 return NGX_ERROR; | |
324 } | |
325 | |
326 end = end * 10 + *p++ - '0'; | |
327 } | |
328 | |
329 end++; | |
330 | |
331 while (*p == ' ') { p++; } | |
332 | |
333 if (*p++ != '/') { | |
334 return NGX_ERROR; | |
335 } | |
336 | |
337 while (*p == ' ') { p++; } | |
338 | |
339 if (*p != '*') { | |
340 if (*p < '0' || *p > '9') { | |
341 return NGX_ERROR; | |
342 } | |
343 | |
344 while (*p >= '0' && *p <= '9') { | |
345 if (complete_length >= cutoff | |
346 && (complete_length > cutoff || *p - '0' > cutlim)) | |
347 { | |
348 return NGX_ERROR; | |
349 } | |
350 | |
351 complete_length = complete_length * 10 + *p++ - '0'; | |
352 } | |
353 | |
354 } else { | |
355 complete_length = -1; | |
356 p++; | |
357 } | |
358 | |
359 while (*p == ' ') { p++; } | |
360 | |
361 if (*p != '\0') { | |
362 return NGX_ERROR; | |
363 } | |
364 | |
365 cr->start = start; | |
366 cr->end = end; | |
367 cr->complete_length = complete_length; | |
368 | |
369 return NGX_OK; | |
370 } | |
371 | |
372 | |
373 static ngx_int_t | |
374 ngx_http_slice_range_variable(ngx_http_request_t *r, | |
375 ngx_http_variable_value_t *v, uintptr_t data) | |
376 { | |
377 u_char *p; | |
378 ngx_http_slice_ctx_t *ctx; | |
379 ngx_http_slice_loc_conf_t *slcf; | |
380 | |
381 ctx = ngx_http_get_module_ctx(r, ngx_http_slice_filter_module); | |
382 | |
383 if (ctx == NULL) { | |
384 if (r != r->main || r->headers_out.status) { | |
385 v->not_found = 1; | |
386 return NGX_OK; | |
387 } | |
388 | |
389 slcf = ngx_http_get_module_loc_conf(r, ngx_http_slice_filter_module); | |
390 | |
391 if (slcf->size == 0) { | |
392 v->not_found = 1; | |
393 return NGX_OK; | |
394 } | |
395 | |
396 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_slice_ctx_t)); | |
397 if (ctx == NULL) { | |
398 return NGX_ERROR; | |
399 } | |
400 | |
401 ngx_http_set_ctx(r, ctx, ngx_http_slice_filter_module); | |
402 | |
403 p = ngx_pnalloc(r->pool, sizeof("bytes=-") - 1 + 2 * NGX_OFF_T_LEN); | |
404 if (p == NULL) { | |
405 return NGX_ERROR; | |
406 } | |
407 | |
408 ctx->start = slcf->size * (ngx_http_slice_get_start(r) / slcf->size); | |
409 | |
410 ctx->range.data = p; | |
411 ctx->range.len = ngx_sprintf(p, "bytes=%O-%O", ctx->start, | |
412 ctx->start + (off_t) slcf->size - 1) | |
413 - p; | |
414 } | |
415 | |
416 v->data = ctx->range.data; | |
417 v->valid = 1; | |
418 v->not_found = 0; | |
419 v->no_cacheable = 1; | |
420 v->len = ctx->range.len; | |
421 | |
422 return NGX_OK; | |
423 } | |
424 | |
425 | |
426 static off_t | |
427 ngx_http_slice_get_start(ngx_http_request_t *r) | |
428 { | |
429 off_t start, cutoff, cutlim; | |
430 u_char *p; | |
431 ngx_table_elt_t *h; | |
432 | |
433 if (r->headers_in.if_range) { | |
434 return 0; | |
435 } | |
436 | |
437 h = r->headers_in.range; | |
438 | |
439 if (h == NULL | |
440 || h->value.len < 7 | |
441 || ngx_strncasecmp(h->value.data, (u_char *) "bytes=", 6) != 0) | |
442 { | |
443 return 0; | |
444 } | |
445 | |
446 p = h->value.data + 6; | |
447 | |
448 if (ngx_strchr(p, ',')) { | |
449 return 0; | |
450 } | |
451 | |
452 while (*p == ' ') { p++; } | |
453 | |
454 if (*p == '-') { | |
455 return 0; | |
456 } | |
457 | |
458 cutoff = NGX_MAX_OFF_T_VALUE / 10; | |
459 cutlim = NGX_MAX_OFF_T_VALUE % 10; | |
460 | |
461 start = 0; | |
462 | |
463 while (*p >= '0' && *p <= '9') { | |
464 if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) { | |
465 return 0; | |
466 } | |
467 | |
468 start = start * 10 + *p++ - '0'; | |
469 } | |
470 | |
471 return start; | |
472 } | |
473 | |
474 | |
475 static void * | |
476 ngx_http_slice_create_loc_conf(ngx_conf_t *cf) | |
477 { | |
478 ngx_http_slice_loc_conf_t *slcf; | |
479 | |
480 slcf = ngx_palloc(cf->pool, sizeof(ngx_http_slice_loc_conf_t)); | |
481 if (slcf == NULL) { | |
482 return NULL; | |
483 } | |
484 | |
485 slcf->size = NGX_CONF_UNSET_SIZE; | |
486 | |
487 return slcf; | |
488 } | |
489 | |
490 | |
491 static char * | |
492 ngx_http_slice_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
493 { | |
494 ngx_http_slice_loc_conf_t *prev = parent; | |
495 ngx_http_slice_loc_conf_t *conf = child; | |
496 | |
497 ngx_conf_merge_size_value(conf->size, prev->size, 0); | |
498 | |
499 return NGX_CONF_OK; | |
500 } | |
501 | |
502 | |
503 static ngx_int_t | |
504 ngx_http_slice_add_variables(ngx_conf_t *cf) | |
505 { | |
506 ngx_http_variable_t *var; | |
507 | |
508 var = ngx_http_add_variable(cf, &ngx_http_slice_range_name, 0); | |
509 if (var == NULL) { | |
510 return NGX_ERROR; | |
511 } | |
512 | |
513 var->get_handler = ngx_http_slice_range_variable; | |
514 | |
515 return NGX_OK; | |
516 } | |
517 | |
518 | |
519 static ngx_int_t | |
520 ngx_http_slice_init(ngx_conf_t *cf) | |
521 { | |
522 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
523 ngx_http_top_header_filter = ngx_http_slice_header_filter; | |
524 | |
525 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
526 ngx_http_top_body_filter = ngx_http_slice_body_filter; | |
527 | |
528 return NGX_OK; | |
529 } |