Mercurial > hg > nginx
comparison src/http/modules/ngx_http_slice_filter_module.c @ 6317:29f35e60840b
Slice filter.
Splits a request into subrequests, each providing a specific range of response.
The variable "$slice_range" must be used to set subrequest range and proper
cache key. The directive "slice" sets slice size.
The following example splits requests into 1-megabyte cacheable subrequests.
server {
listen 8000;
location / {
slice 1m;
proxy_cache cache;
proxy_cache_key $uri$is_args$args$slice_range;
proxy_set_header Range $slice_range;
proxy_cache_valid 200 206 1h;
proxy_pass http://127.0.0.1:9000;
}
}
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Mon, 07 Dec 2015 16:30:48 +0300 |
parents | |
children | bc9ea464e354 |
comparison
equal
deleted
inserted
replaced
6316:f44de0d12143 | 6317:29f35e60840b |
---|---|
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 | |
242 if (ngx_http_subrequest(r, &r->uri, &r->args, &sr, NULL, 0) != NGX_OK) { | |
243 return NGX_ERROR; | |
244 } | |
245 | |
246 ngx_http_set_ctx(sr, ctx, ngx_http_slice_filter_module); | |
247 | |
248 slcf = ngx_http_get_module_loc_conf(r, ngx_http_slice_filter_module); | |
249 | |
250 ctx->range.len = ngx_sprintf(ctx->range.data, "bytes=%O-%O", ctx->start, | |
251 ctx->start + (off_t) slcf->size - 1) | |
252 - ctx->range.data; | |
253 | |
254 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
255 "http slice subrequest: \"%V\"", &ctx->range); | |
256 | |
257 return rc; | |
258 } | |
259 | |
260 | |
261 static ngx_int_t | |
262 ngx_http_slice_parse_content_range(ngx_http_request_t *r, | |
263 ngx_http_slice_content_range_t *cr) | |
264 { | |
265 off_t start, end, complete_length, cutoff, cutlim; | |
266 u_char *p; | |
267 ngx_table_elt_t *h; | |
268 | |
269 h = r->headers_out.content_range; | |
270 | |
271 if (h == NULL | |
272 || h->value.len < 7 | |
273 || ngx_strncmp(h->value.data, "bytes ", 6) != 0) | |
274 { | |
275 return NGX_ERROR; | |
276 } | |
277 | |
278 p = h->value.data + 6; | |
279 | |
280 cutoff = NGX_MAX_OFF_T_VALUE / 10; | |
281 cutlim = NGX_MAX_OFF_T_VALUE % 10; | |
282 | |
283 start = 0; | |
284 end = 0; | |
285 complete_length = 0; | |
286 | |
287 while (*p == ' ') { p++; } | |
288 | |
289 if (*p < '0' || *p > '9') { | |
290 return NGX_ERROR; | |
291 } | |
292 | |
293 while (*p >= '0' && *p <= '9') { | |
294 if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) { | |
295 return NGX_ERROR; | |
296 } | |
297 | |
298 start = start * 10 + *p++ - '0'; | |
299 } | |
300 | |
301 while (*p == ' ') { p++; } | |
302 | |
303 if (*p++ != '-') { | |
304 return NGX_ERROR; | |
305 } | |
306 | |
307 while (*p == ' ') { p++; } | |
308 | |
309 if (*p < '0' || *p > '9') { | |
310 return NGX_ERROR; | |
311 } | |
312 | |
313 while (*p >= '0' && *p <= '9') { | |
314 if (end >= cutoff && (end > cutoff || *p - '0' > cutlim)) { | |
315 return NGX_ERROR; | |
316 } | |
317 | |
318 end = end * 10 + *p++ - '0'; | |
319 } | |
320 | |
321 end++; | |
322 | |
323 while (*p == ' ') { p++; } | |
324 | |
325 if (*p++ != '/') { | |
326 return NGX_ERROR; | |
327 } | |
328 | |
329 while (*p == ' ') { p++; } | |
330 | |
331 if (*p != '*') { | |
332 if (*p < '0' || *p > '9') { | |
333 return NGX_ERROR; | |
334 } | |
335 | |
336 while (*p >= '0' && *p <= '9') { | |
337 if (complete_length >= cutoff | |
338 && (complete_length > cutoff || *p - '0' > cutlim)) | |
339 { | |
340 return NGX_ERROR; | |
341 } | |
342 | |
343 complete_length = complete_length * 10 + *p++ - '0'; | |
344 } | |
345 | |
346 } else { | |
347 complete_length = -1; | |
348 p++; | |
349 } | |
350 | |
351 while (*p == ' ') { p++; } | |
352 | |
353 if (*p != '\0') { | |
354 return NGX_ERROR; | |
355 } | |
356 | |
357 cr->start = start; | |
358 cr->end = end; | |
359 cr->complete_length = complete_length; | |
360 | |
361 return NGX_OK; | |
362 } | |
363 | |
364 | |
365 static ngx_int_t | |
366 ngx_http_slice_range_variable(ngx_http_request_t *r, | |
367 ngx_http_variable_value_t *v, uintptr_t data) | |
368 { | |
369 u_char *p; | |
370 ngx_http_slice_ctx_t *ctx; | |
371 ngx_http_slice_loc_conf_t *slcf; | |
372 | |
373 ctx = ngx_http_get_module_ctx(r, ngx_http_slice_filter_module); | |
374 | |
375 if (ctx == NULL) { | |
376 if (r != r->main || r->headers_out.status) { | |
377 v->not_found = 1; | |
378 return NGX_OK; | |
379 } | |
380 | |
381 slcf = ngx_http_get_module_loc_conf(r, ngx_http_slice_filter_module); | |
382 | |
383 if (slcf->size == 0) { | |
384 v->not_found = 1; | |
385 return NGX_OK; | |
386 } | |
387 | |
388 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_slice_ctx_t)); | |
389 if (ctx == NULL) { | |
390 return NGX_ERROR; | |
391 } | |
392 | |
393 ngx_http_set_ctx(r, ctx, ngx_http_slice_filter_module); | |
394 | |
395 p = ngx_pnalloc(r->pool, sizeof("bytes=-") - 1 + 2 * NGX_OFF_T_LEN); | |
396 if (p == NULL) { | |
397 return NGX_ERROR; | |
398 } | |
399 | |
400 ctx->start = slcf->size * (ngx_http_slice_get_start(r) / slcf->size); | |
401 | |
402 ctx->range.data = p; | |
403 ctx->range.len = ngx_sprintf(p, "bytes=%O-%O", ctx->start, | |
404 ctx->start + (off_t) slcf->size - 1) | |
405 - p; | |
406 } | |
407 | |
408 v->data = ctx->range.data; | |
409 v->valid = 1; | |
410 v->not_found = 0; | |
411 v->no_cacheable = 1; | |
412 v->len = ctx->range.len; | |
413 | |
414 return NGX_OK; | |
415 } | |
416 | |
417 | |
418 static off_t | |
419 ngx_http_slice_get_start(ngx_http_request_t *r) | |
420 { | |
421 off_t start, cutoff, cutlim; | |
422 u_char *p; | |
423 ngx_table_elt_t *h; | |
424 | |
425 if (r->headers_in.if_range) { | |
426 return 0; | |
427 } | |
428 | |
429 h = r->headers_in.range; | |
430 | |
431 if (h == NULL | |
432 || h->value.len < 7 | |
433 || ngx_strncasecmp(h->value.data, (u_char *) "bytes=", 6) != 0) | |
434 { | |
435 return 0; | |
436 } | |
437 | |
438 p = h->value.data + 6; | |
439 | |
440 if (ngx_strchr(p, ',')) { | |
441 return 0; | |
442 } | |
443 | |
444 while (*p == ' ') { p++; } | |
445 | |
446 if (*p == '-') { | |
447 return 0; | |
448 } | |
449 | |
450 cutoff = NGX_MAX_OFF_T_VALUE / 10; | |
451 cutlim = NGX_MAX_OFF_T_VALUE % 10; | |
452 | |
453 start = 0; | |
454 | |
455 while (*p >= '0' && *p <= '9') { | |
456 if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) { | |
457 return 0; | |
458 } | |
459 | |
460 start = start * 10 + *p++ - '0'; | |
461 } | |
462 | |
463 return start; | |
464 } | |
465 | |
466 | |
467 static void * | |
468 ngx_http_slice_create_loc_conf(ngx_conf_t *cf) | |
469 { | |
470 ngx_http_slice_loc_conf_t *slcf; | |
471 | |
472 slcf = ngx_palloc(cf->pool, sizeof(ngx_http_slice_loc_conf_t)); | |
473 if (slcf == NULL) { | |
474 return NULL; | |
475 } | |
476 | |
477 slcf->size = NGX_CONF_UNSET_SIZE; | |
478 | |
479 return slcf; | |
480 } | |
481 | |
482 | |
483 static char * | |
484 ngx_http_slice_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
485 { | |
486 ngx_http_slice_loc_conf_t *prev = parent; | |
487 ngx_http_slice_loc_conf_t *conf = child; | |
488 | |
489 ngx_conf_merge_size_value(conf->size, prev->size, 0); | |
490 | |
491 return NGX_CONF_OK; | |
492 } | |
493 | |
494 | |
495 static ngx_int_t | |
496 ngx_http_slice_add_variables(ngx_conf_t *cf) | |
497 { | |
498 ngx_http_variable_t *var; | |
499 | |
500 var = ngx_http_add_variable(cf, &ngx_http_slice_range_name, 0); | |
501 if (var == NULL) { | |
502 return NGX_ERROR; | |
503 } | |
504 | |
505 var->get_handler = ngx_http_slice_range_variable; | |
506 | |
507 return NGX_OK; | |
508 } | |
509 | |
510 | |
511 static ngx_int_t | |
512 ngx_http_slice_init(ngx_conf_t *cf) | |
513 { | |
514 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
515 ngx_http_top_header_filter = ngx_http_slice_header_filter; | |
516 | |
517 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
518 ngx_http_top_body_filter = ngx_http_slice_body_filter; | |
519 | |
520 return NGX_OK; | |
521 } |