0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 /*
|
|
13 * the single part format:
|
|
14 *
|
|
15 * "HTTP/1.0 206 Partial Content" CRLF
|
|
16 * ... header ...
|
|
17 * "Content-Type: image/jpeg" CRLF
|
|
18 * "Content-Length: SIZE" CRLF
|
|
19 * "Content-Range: bytes START-END/SIZE" CRLF
|
|
20 * CRLF
|
|
21 * ... data ...
|
|
22 *
|
|
23 *
|
|
24 * the mutlipart format:
|
|
25 *
|
|
26 * "HTTP/1.0 206 Partial Content" CRLF
|
|
27 * ... header ...
|
|
28 * "Content-Type: multipart/byteranges; boundary=0123456789" CRLF
|
|
29 * CRLF
|
|
30 * CRLF
|
|
31 * "--0123456789" CRLF
|
|
32 * "Content-Type: image/jpeg" CRLF
|
|
33 * "Content-Range: bytes START0-END0/SIZE" CRLF
|
|
34 * CRLF
|
|
35 * ... data ...
|
|
36 * CRLF
|
|
37 * "--0123456789" CRLF
|
|
38 * "Content-Type: image/jpeg" CRLF
|
|
39 * "Content-Range: bytes START1-END1/SIZE" CRLF
|
|
40 * CRLF
|
|
41 * ... data ...
|
|
42 * CRLF
|
|
43 * "--0123456789--" CRLF
|
|
44 */
|
|
45
|
|
46
|
|
47 typedef struct {
|
|
48 ngx_str_t boundary_header;
|
|
49 } ngx_http_range_filter_ctx_t;
|
|
50
|
|
51
|
|
52 static ngx_int_t ngx_http_range_header_filter_init(ngx_cycle_t *cycle);
|
|
53 static ngx_int_t ngx_http_range_body_filter_init(ngx_cycle_t *cycle);
|
|
54
|
|
55
|
|
56 static ngx_http_module_t ngx_http_range_header_filter_module_ctx = {
|
|
57 NULL, /* pre conf */
|
|
58
|
|
59 NULL, /* create main configuration */
|
|
60 NULL, /* init main configuration */
|
|
61
|
|
62 NULL, /* create server configuration */
|
|
63 NULL, /* merge server configuration */
|
|
64
|
|
65 NULL, /* create location configuration */
|
|
66 NULL, /* merge location configuration */
|
|
67 };
|
|
68
|
|
69
|
|
70 ngx_module_t ngx_http_range_header_filter_module = {
|
|
71 NGX_MODULE,
|
|
72 &ngx_http_range_header_filter_module_ctx, /* module context */
|
|
73 NULL, /* module directives */
|
|
74 NGX_HTTP_MODULE, /* module type */
|
|
75 ngx_http_range_header_filter_init, /* init module */
|
|
76 NULL /* init child */
|
|
77 };
|
|
78
|
|
79
|
|
80 static ngx_http_module_t ngx_http_range_body_filter_module_ctx = {
|
|
81 NULL, /* pre conf */
|
|
82
|
|
83 NULL, /* create main configuration */
|
|
84 NULL, /* init main configuration */
|
|
85
|
|
86 NULL, /* create server configuration */
|
|
87 NULL, /* merge server configuration */
|
|
88
|
|
89 NULL, /* create location configuration */
|
|
90 NULL, /* merge location configuration */
|
|
91 };
|
|
92
|
|
93
|
|
94 ngx_module_t ngx_http_range_body_filter_module = {
|
|
95 NGX_MODULE,
|
|
96 &ngx_http_range_body_filter_module_ctx, /* module context */
|
|
97 NULL, /* module directives */
|
|
98 NGX_HTTP_MODULE, /* module type */
|
|
99 ngx_http_range_body_filter_init, /* init module */
|
|
100 NULL /* init child */
|
|
101 };
|
|
102
|
|
103
|
|
104 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
105 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
106
|
|
107
|
42
|
108 static ngx_int_t
|
|
109 ngx_http_range_header_filter(ngx_http_request_t *r)
|
0
|
110 {
|
|
111 u_char *p;
|
|
112 size_t len;
|
|
113 off_t start, end;
|
10
|
114 ngx_int_t rc;
|
|
115 ngx_uint_t suffix, i;
|
42
|
116 ngx_atomic_int_t boundary;
|
10
|
117 ngx_table_elt_t *content_range;
|
0
|
118 ngx_http_range_t *range;
|
|
119 ngx_http_range_filter_ctx_t *ctx;
|
|
120
|
|
121 if (r->http_version < NGX_HTTP_VERSION_10
|
|
122 || r->headers_out.status != NGX_HTTP_OK
|
|
123 || r->headers_out.content_length_n == -1
|
|
124 || !r->filter_allow_ranges)
|
|
125 {
|
|
126 return ngx_http_next_header_filter(r);
|
|
127 }
|
|
128
|
|
129 if (r->headers_in.range == NULL
|
|
130 || r->headers_in.range->value.len < 7
|
|
131 || ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
|
|
132 {
|
|
133
|
|
134 r->headers_out.accept_ranges = ngx_list_push(&r->headers_out.headers);
|
|
135 if (r->headers_out.accept_ranges == NULL) {
|
|
136 return NGX_ERROR;
|
|
137 }
|
|
138
|
|
139 r->headers_out.accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
|
|
140 r->headers_out.accept_ranges->key.data = (u_char *) "Accept-Ranges";
|
|
141 r->headers_out.accept_ranges->value.len = sizeof("bytes") - 1;
|
|
142 r->headers_out.accept_ranges->value.data = (u_char *) "bytes";
|
|
143
|
|
144 return ngx_http_next_header_filter(r);
|
|
145 }
|
|
146
|
10
|
147 if (ngx_array_init(&r->headers_out.ranges, r->pool, 5,
|
|
148 sizeof(ngx_http_range_t)) == NGX_ERROR)
|
|
149 {
|
|
150 return NGX_ERROR;
|
|
151 }
|
0
|
152
|
|
153 rc = 0;
|
|
154 range = NULL;
|
|
155 p = r->headers_in.range->value.data + 6;
|
|
156
|
|
157 for ( ;; ) {
|
|
158 start = 0;
|
|
159 end = 0;
|
|
160 suffix = 0;
|
|
161
|
|
162 while (*p == ' ') { p++; }
|
|
163
|
|
164 if (*p != '-') {
|
|
165 if (*p < '0' || *p > '9') {
|
|
166 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
167 break;
|
|
168 }
|
|
169
|
|
170 while (*p >= '0' && *p <= '9') {
|
|
171 start = start * 10 + *p++ - '0';
|
|
172 }
|
|
173
|
|
174 while (*p == ' ') { p++; }
|
|
175
|
|
176 if (*p++ != '-') {
|
|
177 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
178 break;
|
|
179 }
|
|
180
|
|
181 if (start >= r->headers_out.content_length_n) {
|
|
182 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
183 break;
|
|
184 }
|
|
185
|
|
186 while (*p == ' ') { p++; }
|
|
187
|
|
188 if (*p == ',' || *p == '\0') {
|
10
|
189 if (!(range = ngx_array_push(&r->headers_out.ranges))) {
|
|
190 return NGX_ERROR;
|
|
191 }
|
|
192
|
0
|
193 range->start = start;
|
|
194 range->end = r->headers_out.content_length_n;
|
|
195
|
|
196 if (*p++ != ',') {
|
|
197 break;
|
|
198 }
|
|
199
|
|
200 continue;
|
|
201 }
|
|
202
|
|
203 } else {
|
|
204 suffix = 1;
|
|
205 p++;
|
|
206 }
|
|
207
|
|
208 if (*p < '0' || *p > '9') {
|
|
209 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
210 break;
|
|
211 }
|
|
212
|
|
213 while (*p >= '0' && *p <= '9') {
|
|
214 end = end * 10 + *p++ - '0';
|
|
215 }
|
|
216
|
|
217 while (*p == ' ') { p++; }
|
|
218
|
|
219 if (*p != ',' && *p != '\0') {
|
|
220 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
221 break;
|
|
222 }
|
|
223
|
|
224 if (suffix) {
|
|
225 start = r->headers_out.content_length_n - end;
|
|
226 end = r->headers_out.content_length_n - 1;
|
|
227 }
|
|
228
|
|
229 if (start > end) {
|
|
230 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
231 break;
|
|
232 }
|
|
233
|
10
|
234 if (!(range = ngx_array_push(&r->headers_out.ranges))) {
|
|
235 return NGX_ERROR;
|
|
236 }
|
|
237
|
0
|
238 range->start = start;
|
|
239
|
|
240 if (end >= r->headers_out.content_length_n) {
|
|
241 /*
|
|
242 * Download Accelerator sends the last byte position
|
|
243 * that equals to the file length
|
|
244 */
|
|
245 range->end = r->headers_out.content_length_n;
|
|
246
|
|
247 } else {
|
|
248 range->end = end + 1;
|
|
249 }
|
|
250
|
|
251 if (*p++ != ',') {
|
|
252 break;
|
|
253 }
|
|
254 }
|
|
255
|
|
256 if (rc) {
|
|
257
|
|
258 /* rc == NGX_HTTP_RANGE_NOT_SATISFIABLE */
|
|
259
|
|
260 r->headers_out.status = rc;
|
|
261 r->headers_out.ranges.nelts = 0;
|
|
262
|
10
|
263 if (!(content_range = ngx_list_push(&r->headers_out.headers))) {
|
0
|
264 return NGX_ERROR;
|
|
265 }
|
|
266
|
10
|
267 r->headers_out.content_range = content_range;
|
|
268
|
|
269 content_range->key.len = sizeof("Content-Range") - 1;
|
|
270 content_range->key.data = (u_char *) "Content-Range";
|
0
|
271
|
10
|
272 content_range->value.data =
|
|
273 ngx_palloc(r->pool, sizeof("bytes */") - 1 + NGX_OFF_T_LEN);
|
|
274
|
|
275 if (content_range->value.data == NULL) {
|
0
|
276 return NGX_ERROR;
|
|
277 }
|
|
278
|
10
|
279 content_range->value.len = ngx_sprintf(content_range->value.data,
|
|
280 "bytes */%O",
|
|
281 r->headers_out.content_length_n)
|
|
282 - content_range->value.data;
|
0
|
283
|
|
284 r->headers_out.content_length_n = -1;
|
|
285 if (r->headers_out.content_length) {
|
|
286 r->headers_out.content_length->key.len = 0;
|
|
287 r->headers_out.content_length = NULL;
|
|
288 }
|
|
289
|
|
290 return rc;
|
10
|
291 }
|
|
292
|
|
293 r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
|
|
294
|
|
295 if (r->headers_out.ranges.nelts == 1) {
|
|
296
|
|
297 if (!(content_range = ngx_list_push(&r->headers_out.headers))) {
|
|
298 return NGX_ERROR;
|
|
299 }
|
|
300
|
|
301 r->headers_out.content_range = content_range;
|
|
302
|
|
303 content_range->key.len = sizeof("Content-Range") - 1;
|
|
304 content_range->key.data = (u_char *) "Content-Range";
|
|
305
|
|
306 content_range->value.data =
|
|
307 ngx_palloc(r->pool, sizeof("bytes -/") - 1 + 3 * NGX_OFF_T_LEN);
|
|
308 if (content_range->value.data == NULL) {
|
|
309 return NGX_ERROR;
|
|
310 }
|
|
311
|
|
312 /* "Content-Range: bytes SSSS-EEEE/TTTT" header */
|
|
313
|
|
314 content_range->value.len = ngx_sprintf(content_range->value.data,
|
|
315 "bytes %O-%O/%O",
|
|
316 range->start, range->end - 1,
|
|
317 r->headers_out.content_length_n)
|
|
318 - content_range->value.data;
|
|
319
|
|
320 r->headers_out.content_length_n = range->end - range->start;
|
|
321
|
|
322 return ngx_http_next_header_filter(r);
|
|
323 }
|
|
324
|
|
325
|
|
326 /* TODO: what if no content_type ?? */
|
|
327
|
|
328 ngx_http_create_ctx(r, ctx, ngx_http_range_body_filter_module,
|
|
329 sizeof(ngx_http_range_filter_ctx_t), NGX_ERROR);
|
|
330
|
|
331
|
42
|
332 len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
|
|
333 + sizeof(CRLF "Content-Type: ") - 1
|
10
|
334 + r->headers_out.content_type->value.len
|
|
335 + sizeof(CRLF "Content-Range: bytes ") - 1;
|
|
336
|
|
337 if (r->headers_out.charset.len) {
|
|
338 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
|
|
339 }
|
|
340
|
|
341 if (!(ctx->boundary_header.data = ngx_palloc(r->pool, len))) {
|
|
342 return NGX_ERROR;
|
|
343 }
|
|
344
|
42
|
345 boundary = ngx_next_temp_number(0);
|
10
|
346
|
|
347 /*
|
|
348 * The boundary header of the range:
|
|
349 * CRLF
|
|
350 * "--0123456789" CRLF
|
|
351 * "Content-Type: image/jpeg" CRLF
|
|
352 * "Content-Range: bytes "
|
|
353 */
|
|
354
|
|
355 if (r->headers_out.charset.len) {
|
|
356 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
|
42
|
357 CRLF "--%0muA" CRLF
|
10
|
358 "Content-Type: %V; charset=%V" CRLF
|
|
359 "Content-Range: bytes ",
|
|
360 boundary,
|
|
361 &r->headers_out.content_type->value,
|
|
362 &r->headers_out.charset)
|
|
363 - ctx->boundary_header.data;
|
|
364
|
|
365 r->headers_out.charset.len = 0;
|
0
|
366
|
|
367 } else {
|
10
|
368 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
|
42
|
369 CRLF "--%0muA" CRLF
|
10
|
370 "Content-Type: %V" CRLF
|
|
371 "Content-Range: bytes ",
|
|
372 boundary,
|
|
373 &r->headers_out.content_type->value)
|
|
374 - ctx->boundary_header.data;
|
|
375 }
|
0
|
376
|
10
|
377 r->headers_out.content_type->value.data =
|
42
|
378 ngx_palloc(r->pool,
|
|
379 sizeof("Content-Type: multipart/byteranges; boundary=") - 1
|
|
380 + NGX_ATOMIC_T_LEN);
|
0
|
381
|
10
|
382 if (r->headers_out.content_type->value.data == NULL) {
|
|
383 return NGX_ERROR;
|
|
384 }
|
0
|
385
|
10
|
386 /* "Content-Type: multipart/byteranges; boundary=0123456789" */
|
0
|
387
|
10
|
388 r->headers_out.content_type->value.len =
|
|
389 ngx_sprintf(r->headers_out.content_type->value.data,
|
42
|
390 "multipart/byteranges; boundary=%0muA",
|
10
|
391 boundary)
|
|
392 - r->headers_out.content_type->value.data;
|
0
|
393
|
42
|
394
|
10
|
395 /* the size of the last boundary CRLF "--0123456789--" CRLF */
|
42
|
396
|
|
397 len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN + sizeof("--" CRLF) - 1;
|
0
|
398
|
10
|
399 range = r->headers_out.ranges.elts;
|
|
400 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
|
4
|
401
|
10
|
402 /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */
|
4
|
403
|
10
|
404 range[i].content_range.data =
|
|
405 ngx_palloc(r->pool, 3 * NGX_OFF_T_LEN + 2 + 4);
|
0
|
406
|
10
|
407 if (range[i].content_range.data == NULL) {
|
|
408 return NGX_ERROR;
|
|
409 }
|
0
|
410
|
10
|
411 range[i].content_range.len = ngx_sprintf(range[i].content_range.data,
|
|
412 "%O-%O/%O" CRLF CRLF,
|
|
413 range[i].start, range[i].end - 1,
|
|
414 r->headers_out.content_length_n)
|
|
415 - range[i].content_range.data;
|
0
|
416
|
10
|
417 len += ctx->boundary_header.len + range[i].content_range.len
|
|
418 + (size_t) (range[i].end - range[i].start);
|
|
419 }
|
0
|
420
|
10
|
421 r->headers_out.content_length_n = len;
|
|
422 r->headers_out.content_length = NULL;
|
0
|
423
|
|
424 return ngx_http_next_header_filter(r);
|
|
425 }
|
|
426
|
|
427
|
42
|
428 static ngx_int_t
|
|
429 ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
0
|
430 {
|
|
431 ngx_uint_t i;
|
|
432 ngx_buf_t *b;
|
|
433 ngx_chain_t *out, *hcl, *rcl, *dcl, **ll;
|
|
434 ngx_http_range_t *range;
|
|
435 ngx_http_range_filter_ctx_t *ctx;
|
|
436
|
|
437 if (r->headers_out.ranges.nelts == 0) {
|
|
438 return ngx_http_next_body_filter(r, in);
|
|
439 }
|
|
440
|
|
441 /*
|
|
442 * the optimized version for the static files only
|
|
443 * that are passed in the single file buf
|
|
444 */
|
|
445
|
|
446 if (in && in->buf->in_file && in->buf->last_buf) {
|
|
447 range = r->headers_out.ranges.elts;
|
|
448
|
|
449 if (r->headers_out.ranges.nelts == 1) {
|
|
450 in->buf->file_pos = range->start;
|
|
451 in->buf->file_last = range->end;
|
|
452
|
|
453 return ngx_http_next_body_filter(r, in);
|
|
454 }
|
|
455
|
|
456 ctx = ngx_http_get_module_ctx(r, ngx_http_range_body_filter_module);
|
|
457 ll = &out;
|
|
458
|
|
459 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
|
|
460
|
|
461 /*
|
|
462 * The boundary header of the range:
|
|
463 * CRLF
|
|
464 * "--0123456789" CRLF
|
|
465 * "Content-Type: image/jpeg" CRLF
|
|
466 * "Content-Range: bytes "
|
|
467 */
|
|
468
|
10
|
469 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
470 return NGX_ERROR;
|
|
471 }
|
|
472
|
0
|
473 b->memory = 1;
|
|
474 b->pos = ctx->boundary_header.data;
|
|
475 b->last = ctx->boundary_header.data + ctx->boundary_header.len;
|
|
476
|
10
|
477 if (!(hcl = ngx_alloc_chain_link(r->pool))) {
|
|
478 return NGX_ERROR;
|
|
479 }
|
|
480
|
0
|
481 hcl->buf = b;
|
|
482
|
10
|
483
|
0
|
484 /* "SSSS-EEEE/TTTT" CRLF CRLF */
|
|
485
|
10
|
486 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
487 return NGX_ERROR;
|
|
488 }
|
|
489
|
0
|
490 b->temporary = 1;
|
|
491 b->pos = range[i].content_range.data;
|
|
492 b->last = range[i].content_range.data + range[i].content_range.len;
|
|
493
|
10
|
494 if (!(rcl = ngx_alloc_chain_link(r->pool))) {
|
|
495 return NGX_ERROR;
|
|
496 }
|
|
497
|
0
|
498 rcl->buf = b;
|
|
499
|
10
|
500
|
0
|
501 /* the range data */
|
|
502
|
10
|
503 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
504 return NGX_ERROR;
|
|
505 }
|
|
506
|
0
|
507 b->in_file = 1;
|
|
508 b->file_pos = range[i].start;
|
|
509 b->file_last = range[i].end;
|
|
510 b->file = in->buf->file;
|
|
511
|
10
|
512 if (!(dcl = ngx_alloc_chain_link(r->pool))) {
|
|
513 return NGX_ERROR;
|
|
514 }
|
|
515
|
|
516 dcl->buf = b;
|
0
|
517
|
|
518 *ll = hcl;
|
|
519 hcl->next = rcl;
|
|
520 rcl->next = dcl;
|
|
521 ll = &dcl->next;
|
|
522 }
|
|
523
|
|
524 /* the last boundary CRLF "--0123456789--" CRLF */
|
|
525
|
10
|
526 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
527 return NGX_ERROR;
|
|
528 }
|
|
529
|
0
|
530 b->temporary = 1;
|
|
531 b->last_buf = 1;
|
10
|
532
|
42
|
533 b->pos = ngx_palloc(r->pool, sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
|
|
534 + sizeof("--" CRLF) - 1);
|
10
|
535 if (b->pos == NULL) {
|
|
536 return NGX_ERROR;
|
|
537 }
|
|
538
|
0
|
539 b->last = ngx_cpymem(b->pos, ctx->boundary_header.data, 4 + 10);
|
|
540 *b->last++ = '-'; *b->last++ = '-';
|
|
541 *b->last++ = CR; *b->last++ = LF;
|
|
542
|
10
|
543 if (!(hcl = ngx_alloc_chain_link(r->pool))) {
|
|
544 return NGX_ERROR;
|
|
545 }
|
|
546
|
|
547 hcl->buf = b;
|
|
548 hcl->next = NULL;
|
|
549
|
0
|
550 *ll = hcl;
|
|
551
|
|
552 return ngx_http_next_body_filter(r, out);
|
|
553 }
|
|
554
|
|
555 /* TODO: alert */
|
|
556
|
|
557 return ngx_http_next_body_filter(r, in);
|
|
558 }
|
|
559
|
|
560
|
42
|
561 static ngx_int_t
|
|
562 ngx_http_range_header_filter_init(ngx_cycle_t *cycle)
|
0
|
563 {
|
|
564 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
565 ngx_http_top_header_filter = ngx_http_range_header_filter;
|
|
566
|
|
567 return NGX_OK;
|
|
568 }
|
|
569
|
|
570
|
42
|
571 static ngx_int_t
|
|
572 ngx_http_range_body_filter_init(ngx_cycle_t *cycle)
|
0
|
573 {
|
|
574 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
575 ngx_http_top_body_filter = ngx_http_range_body_filter;
|
|
576
|
|
577 return NGX_OK;
|
|
578 }
|