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