Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_slice_filter_module.c @ 6321:bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
With main request buffered, it's possible, that a slice subrequest will send
output before it. For example, while main request is waiting for aio read to
complete, a slice subrequest can start an aio operation as well. The order
in which aio callbacks are called is undetermined.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Tue, 08 Dec 2015 17:39:56 +0300 |
parents | 29f35e60840b |
children | 4f0f4f02c98f |
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; | |
225 cl->buf->sync = 1; | |
226 ctx->last = 1; | |
227 } | |
228 } | |
229 | |
230 rc = ngx_http_next_body_filter(r, in); | |
231 | |
232 if (rc == NGX_ERROR || !ctx->last) { | |
233 return rc; | |
234 } | |
235 | |
236 if (ctx->start >= ctx->end) { | |
237 ngx_http_set_ctx(r, NULL, ngx_http_slice_filter_module); | |
238 ngx_http_send_special(r, NGX_HTTP_LAST); | |
239 return rc; | |
240 } | |
241 | |
6321
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
242 if (r->buffered) { |
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
243 return rc; |
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
244 } |
bc9ea464e354
Slice filter: never run subrequests when main request is buffered.
Roman Arutyunyan <arut@nginx.com>
parents:
6317
diff
changeset
|
245 |
6317 | 246 if (ngx_http_subrequest(r, &r->uri, &r->args, &sr, NULL, 0) != NGX_OK) { |
247 return NGX_ERROR; | |
248 } | |
249 | |
250 ngx_http_set_ctx(sr, ctx, ngx_http_slice_filter_module); | |
251 | |
252 slcf = ngx_http_get_module_loc_conf(r, ngx_http_slice_filter_module); | |
253 | |
254 ctx->range.len = ngx_sprintf(ctx->range.data, "bytes=%O-%O", ctx->start, | |
255 ctx->start + (off_t) slcf->size - 1) | |
256 - ctx->range.data; | |
257 | |
258 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
259 "http slice subrequest: \"%V\"", &ctx->range); | |
260 | |
261 return rc; | |
262 } | |
263 | |
264 | |
265 static ngx_int_t | |
266 ngx_http_slice_parse_content_range(ngx_http_request_t *r, | |
267 ngx_http_slice_content_range_t *cr) | |
268 { | |
269 off_t start, end, complete_length, cutoff, cutlim; | |
270 u_char *p; | |
271 ngx_table_elt_t *h; | |
272 | |
273 h = r->headers_out.content_range; | |
274 | |
275 if (h == NULL | |
276 || h->value.len < 7 | |
277 || ngx_strncmp(h->value.data, "bytes ", 6) != 0) | |
278 { | |
279 return NGX_ERROR; | |
280 } | |
281 | |
282 p = h->value.data + 6; | |
283 | |
284 cutoff = NGX_MAX_OFF_T_VALUE / 10; | |
285 cutlim = NGX_MAX_OFF_T_VALUE % 10; | |
286 | |
287 start = 0; | |
288 end = 0; | |
289 complete_length = 0; | |
290 | |
291 while (*p == ' ') { p++; } | |
292 | |
293 if (*p < '0' || *p > '9') { | |
294 return NGX_ERROR; | |
295 } | |
296 | |
297 while (*p >= '0' && *p <= '9') { | |
298 if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) { | |
299 return NGX_ERROR; | |
300 } | |
301 | |
302 start = start * 10 + *p++ - '0'; | |
303 } | |
304 | |
305 while (*p == ' ') { p++; } | |
306 | |
307 if (*p++ != '-') { | |
308 return NGX_ERROR; | |
309 } | |
310 | |
311 while (*p == ' ') { p++; } | |
312 | |
313 if (*p < '0' || *p > '9') { | |
314 return NGX_ERROR; | |
315 } | |
316 | |
317 while (*p >= '0' && *p <= '9') { | |
318 if (end >= cutoff && (end > cutoff || *p - '0' > cutlim)) { | |
319 return NGX_ERROR; | |
320 } | |
321 | |
322 end = end * 10 + *p++ - '0'; | |
323 } | |
324 | |
325 end++; | |
326 | |
327 while (*p == ' ') { p++; } | |
328 | |
329 if (*p++ != '/') { | |
330 return NGX_ERROR; | |
331 } | |
332 | |
333 while (*p == ' ') { p++; } | |
334 | |
335 if (*p != '*') { | |
336 if (*p < '0' || *p > '9') { | |
337 return NGX_ERROR; | |
338 } | |
339 | |
340 while (*p >= '0' && *p <= '9') { | |
341 if (complete_length >= cutoff | |
342 && (complete_length > cutoff || *p - '0' > cutlim)) | |
343 { | |
344 return NGX_ERROR; | |
345 } | |
346 | |
347 complete_length = complete_length * 10 + *p++ - '0'; | |
348 } | |
349 | |
350 } else { | |
351 complete_length = -1; | |
352 p++; | |
353 } | |
354 | |
355 while (*p == ' ') { p++; } | |
356 | |
357 if (*p != '\0') { | |
358 return NGX_ERROR; | |
359 } | |
360 | |
361 cr->start = start; | |
362 cr->end = end; | |
363 cr->complete_length = complete_length; | |
364 | |
365 return NGX_OK; | |
366 } | |
367 | |
368 | |
369 static ngx_int_t | |
370 ngx_http_slice_range_variable(ngx_http_request_t *r, | |
371 ngx_http_variable_value_t *v, uintptr_t data) | |
372 { | |
373 u_char *p; | |
374 ngx_http_slice_ctx_t *ctx; | |
375 ngx_http_slice_loc_conf_t *slcf; | |
376 | |
377 ctx = ngx_http_get_module_ctx(r, ngx_http_slice_filter_module); | |
378 | |
379 if (ctx == NULL) { | |
380 if (r != r->main || r->headers_out.status) { | |
381 v->not_found = 1; | |
382 return NGX_OK; | |
383 } | |
384 | |
385 slcf = ngx_http_get_module_loc_conf(r, ngx_http_slice_filter_module); | |
386 | |
387 if (slcf->size == 0) { | |
388 v->not_found = 1; | |
389 return NGX_OK; | |
390 } | |
391 | |
392 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_slice_ctx_t)); | |
393 if (ctx == NULL) { | |
394 return NGX_ERROR; | |
395 } | |
396 | |
397 ngx_http_set_ctx(r, ctx, ngx_http_slice_filter_module); | |
398 | |
399 p = ngx_pnalloc(r->pool, sizeof("bytes=-") - 1 + 2 * NGX_OFF_T_LEN); | |
400 if (p == NULL) { | |
401 return NGX_ERROR; | |
402 } | |
403 | |
404 ctx->start = slcf->size * (ngx_http_slice_get_start(r) / slcf->size); | |
405 | |
406 ctx->range.data = p; | |
407 ctx->range.len = ngx_sprintf(p, "bytes=%O-%O", ctx->start, | |
408 ctx->start + (off_t) slcf->size - 1) | |
409 - p; | |
410 } | |
411 | |
412 v->data = ctx->range.data; | |
413 v->valid = 1; | |
414 v->not_found = 0; | |
415 v->no_cacheable = 1; | |
416 v->len = ctx->range.len; | |
417 | |
418 return NGX_OK; | |
419 } | |
420 | |
421 | |
422 static off_t | |
423 ngx_http_slice_get_start(ngx_http_request_t *r) | |
424 { | |
425 off_t start, cutoff, cutlim; | |
426 u_char *p; | |
427 ngx_table_elt_t *h; | |
428 | |
429 if (r->headers_in.if_range) { | |
430 return 0; | |
431 } | |
432 | |
433 h = r->headers_in.range; | |
434 | |
435 if (h == NULL | |
436 || h->value.len < 7 | |
437 || ngx_strncasecmp(h->value.data, (u_char *) "bytes=", 6) != 0) | |
438 { | |
439 return 0; | |
440 } | |
441 | |
442 p = h->value.data + 6; | |
443 | |
444 if (ngx_strchr(p, ',')) { | |
445 return 0; | |
446 } | |
447 | |
448 while (*p == ' ') { p++; } | |
449 | |
450 if (*p == '-') { | |
451 return 0; | |
452 } | |
453 | |
454 cutoff = NGX_MAX_OFF_T_VALUE / 10; | |
455 cutlim = NGX_MAX_OFF_T_VALUE % 10; | |
456 | |
457 start = 0; | |
458 | |
459 while (*p >= '0' && *p <= '9') { | |
460 if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) { | |
461 return 0; | |
462 } | |
463 | |
464 start = start * 10 + *p++ - '0'; | |
465 } | |
466 | |
467 return start; | |
468 } | |
469 | |
470 | |
471 static void * | |
472 ngx_http_slice_create_loc_conf(ngx_conf_t *cf) | |
473 { | |
474 ngx_http_slice_loc_conf_t *slcf; | |
475 | |
476 slcf = ngx_palloc(cf->pool, sizeof(ngx_http_slice_loc_conf_t)); | |
477 if (slcf == NULL) { | |
478 return NULL; | |
479 } | |
480 | |
481 slcf->size = NGX_CONF_UNSET_SIZE; | |
482 | |
483 return slcf; | |
484 } | |
485 | |
486 | |
487 static char * | |
488 ngx_http_slice_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
489 { | |
490 ngx_http_slice_loc_conf_t *prev = parent; | |
491 ngx_http_slice_loc_conf_t *conf = child; | |
492 | |
493 ngx_conf_merge_size_value(conf->size, prev->size, 0); | |
494 | |
495 return NGX_CONF_OK; | |
496 } | |
497 | |
498 | |
499 static ngx_int_t | |
500 ngx_http_slice_add_variables(ngx_conf_t *cf) | |
501 { | |
502 ngx_http_variable_t *var; | |
503 | |
504 var = ngx_http_add_variable(cf, &ngx_http_slice_range_name, 0); | |
505 if (var == NULL) { | |
506 return NGX_ERROR; | |
507 } | |
508 | |
509 var->get_handler = ngx_http_slice_range_variable; | |
510 | |
511 return NGX_OK; | |
512 } | |
513 | |
514 | |
515 static ngx_int_t | |
516 ngx_http_slice_init(ngx_conf_t *cf) | |
517 { | |
518 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
519 ngx_http_top_header_filter = ngx_http_slice_header_filter; | |
520 | |
521 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
522 ngx_http_top_body_filter = ngx_http_slice_body_filter; | |
523 | |
524 return NGX_OK; | |
525 } |