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
|
|
108 static ngx_int_t ngx_http_range_header_filter(ngx_http_request_t *r)
|
|
109 {
|
|
110 u_char *p;
|
|
111 size_t len;
|
|
112 off_t start, end;
|
10
|
113 ngx_int_t rc;
|
|
114 uint32_t boundary;
|
|
115 ngx_uint_t suffix, i;
|
|
116 ngx_table_elt_t *content_range;
|
0
|
117 ngx_http_range_t *range;
|
|
118 ngx_http_range_filter_ctx_t *ctx;
|
|
119
|
|
120 if (r->http_version < NGX_HTTP_VERSION_10
|
|
121 || r->headers_out.status != NGX_HTTP_OK
|
|
122 || r->headers_out.content_length_n == -1
|
|
123 || !r->filter_allow_ranges)
|
|
124 {
|
|
125 return ngx_http_next_header_filter(r);
|
|
126 }
|
|
127
|
|
128 if (r->headers_in.range == NULL
|
|
129 || r->headers_in.range->value.len < 7
|
|
130 || ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
|
|
131 {
|
|
132
|
|
133 r->headers_out.accept_ranges = ngx_list_push(&r->headers_out.headers);
|
|
134 if (r->headers_out.accept_ranges == NULL) {
|
|
135 return NGX_ERROR;
|
|
136 }
|
|
137
|
|
138 r->headers_out.accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
|
|
139 r->headers_out.accept_ranges->key.data = (u_char *) "Accept-Ranges";
|
|
140 r->headers_out.accept_ranges->value.len = sizeof("bytes") - 1;
|
|
141 r->headers_out.accept_ranges->value.data = (u_char *) "bytes";
|
|
142
|
|
143 return ngx_http_next_header_filter(r);
|
|
144 }
|
|
145
|
10
|
146 if (ngx_array_init(&r->headers_out.ranges, r->pool, 5,
|
|
147 sizeof(ngx_http_range_t)) == NGX_ERROR)
|
|
148 {
|
|
149 return NGX_ERROR;
|
|
150 }
|
0
|
151
|
|
152 rc = 0;
|
|
153 range = NULL;
|
|
154 p = r->headers_in.range->value.data + 6;
|
|
155
|
|
156 for ( ;; ) {
|
|
157 start = 0;
|
|
158 end = 0;
|
|
159 suffix = 0;
|
|
160
|
|
161 while (*p == ' ') { p++; }
|
|
162
|
|
163 if (*p != '-') {
|
|
164 if (*p < '0' || *p > '9') {
|
|
165 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
166 break;
|
|
167 }
|
|
168
|
|
169 while (*p >= '0' && *p <= '9') {
|
|
170 start = start * 10 + *p++ - '0';
|
|
171 }
|
|
172
|
|
173 while (*p == ' ') { p++; }
|
|
174
|
|
175 if (*p++ != '-') {
|
|
176 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
177 break;
|
|
178 }
|
|
179
|
|
180 if (start >= r->headers_out.content_length_n) {
|
|
181 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
182 break;
|
|
183 }
|
|
184
|
|
185 while (*p == ' ') { p++; }
|
|
186
|
|
187 if (*p == ',' || *p == '\0') {
|
10
|
188 if (!(range = ngx_array_push(&r->headers_out.ranges))) {
|
|
189 return NGX_ERROR;
|
|
190 }
|
|
191
|
0
|
192 range->start = start;
|
|
193 range->end = r->headers_out.content_length_n;
|
|
194
|
|
195 if (*p++ != ',') {
|
|
196 break;
|
|
197 }
|
|
198
|
|
199 continue;
|
|
200 }
|
|
201
|
|
202 } else {
|
|
203 suffix = 1;
|
|
204 p++;
|
|
205 }
|
|
206
|
|
207 if (*p < '0' || *p > '9') {
|
|
208 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
209 break;
|
|
210 }
|
|
211
|
|
212 while (*p >= '0' && *p <= '9') {
|
|
213 end = end * 10 + *p++ - '0';
|
|
214 }
|
|
215
|
|
216 while (*p == ' ') { p++; }
|
|
217
|
|
218 if (*p != ',' && *p != '\0') {
|
|
219 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
220 break;
|
|
221 }
|
|
222
|
|
223 if (suffix) {
|
|
224 start = r->headers_out.content_length_n - end;
|
|
225 end = r->headers_out.content_length_n - 1;
|
|
226 }
|
|
227
|
|
228 if (start > end) {
|
|
229 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
|
|
230 break;
|
|
231 }
|
|
232
|
10
|
233 if (!(range = ngx_array_push(&r->headers_out.ranges))) {
|
|
234 return NGX_ERROR;
|
|
235 }
|
|
236
|
0
|
237 range->start = start;
|
|
238
|
|
239 if (end >= r->headers_out.content_length_n) {
|
|
240 /*
|
|
241 * Download Accelerator sends the last byte position
|
|
242 * that equals to the file length
|
|
243 */
|
|
244 range->end = r->headers_out.content_length_n;
|
|
245
|
|
246 } else {
|
|
247 range->end = end + 1;
|
|
248 }
|
|
249
|
|
250 if (*p++ != ',') {
|
|
251 break;
|
|
252 }
|
|
253 }
|
|
254
|
|
255 if (rc) {
|
|
256
|
|
257 /* rc == NGX_HTTP_RANGE_NOT_SATISFIABLE */
|
|
258
|
|
259 r->headers_out.status = rc;
|
|
260 r->headers_out.ranges.nelts = 0;
|
|
261
|
10
|
262 if (!(content_range = ngx_list_push(&r->headers_out.headers))) {
|
0
|
263 return NGX_ERROR;
|
|
264 }
|
|
265
|
10
|
266 r->headers_out.content_range = content_range;
|
|
267
|
|
268 content_range->key.len = sizeof("Content-Range") - 1;
|
|
269 content_range->key.data = (u_char *) "Content-Range";
|
0
|
270
|
10
|
271 content_range->value.data =
|
|
272 ngx_palloc(r->pool, sizeof("bytes */") - 1 + NGX_OFF_T_LEN);
|
|
273
|
|
274 if (content_range->value.data == NULL) {
|
0
|
275 return NGX_ERROR;
|
|
276 }
|
|
277
|
10
|
278 content_range->value.len = ngx_sprintf(content_range->value.data,
|
|
279 "bytes */%O",
|
|
280 r->headers_out.content_length_n)
|
|
281 - content_range->value.data;
|
0
|
282
|
|
283 r->headers_out.content_length_n = -1;
|
|
284 if (r->headers_out.content_length) {
|
|
285 r->headers_out.content_length->key.len = 0;
|
|
286 r->headers_out.content_length = NULL;
|
|
287 }
|
|
288
|
|
289 return rc;
|
10
|
290 }
|
|
291
|
|
292 r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
|
|
293
|
|
294 if (r->headers_out.ranges.nelts == 1) {
|
|
295
|
|
296 if (!(content_range = ngx_list_push(&r->headers_out.headers))) {
|
|
297 return NGX_ERROR;
|
|
298 }
|
|
299
|
|
300 r->headers_out.content_range = content_range;
|
|
301
|
|
302 content_range->key.len = sizeof("Content-Range") - 1;
|
|
303 content_range->key.data = (u_char *) "Content-Range";
|
|
304
|
|
305 content_range->value.data =
|
|
306 ngx_palloc(r->pool, sizeof("bytes -/") - 1 + 3 * NGX_OFF_T_LEN);
|
|
307 if (content_range->value.data == NULL) {
|
|
308 return NGX_ERROR;
|
|
309 }
|
|
310
|
|
311 /* "Content-Range: bytes SSSS-EEEE/TTTT" header */
|
|
312
|
|
313 content_range->value.len = ngx_sprintf(content_range->value.data,
|
|
314 "bytes %O-%O/%O",
|
|
315 range->start, range->end - 1,
|
|
316 r->headers_out.content_length_n)
|
|
317 - content_range->value.data;
|
|
318
|
|
319 r->headers_out.content_length_n = range->end - range->start;
|
|
320
|
|
321 return ngx_http_next_header_filter(r);
|
|
322 }
|
|
323
|
|
324
|
|
325 /* TODO: what if no content_type ?? */
|
|
326
|
|
327 ngx_http_create_ctx(r, ctx, ngx_http_range_body_filter_module,
|
|
328 sizeof(ngx_http_range_filter_ctx_t), NGX_ERROR);
|
|
329
|
|
330
|
|
331 len = sizeof(CRLF "--0123456789" CRLF "Content-Type: ") - 1
|
|
332 + r->headers_out.content_type->value.len
|
|
333 + sizeof(CRLF "Content-Range: bytes ") - 1;
|
|
334
|
|
335 if (r->headers_out.charset.len) {
|
|
336 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
|
|
337 }
|
|
338
|
|
339 if (!(ctx->boundary_header.data = ngx_palloc(r->pool, len))) {
|
|
340 return NGX_ERROR;
|
|
341 }
|
|
342
|
|
343 boundary = ngx_next_temp_number(0);
|
|
344
|
|
345 /*
|
|
346 * The boundary header of the range:
|
|
347 * CRLF
|
|
348 * "--0123456789" CRLF
|
|
349 * "Content-Type: image/jpeg" CRLF
|
|
350 * "Content-Range: bytes "
|
|
351 */
|
|
352
|
|
353 if (r->headers_out.charset.len) {
|
|
354 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
|
|
355 CRLF "--%010ud" CRLF
|
|
356 "Content-Type: %V; charset=%V" CRLF
|
|
357 "Content-Range: bytes ",
|
|
358 boundary,
|
|
359 &r->headers_out.content_type->value,
|
|
360 &r->headers_out.charset)
|
|
361 - ctx->boundary_header.data;
|
|
362
|
|
363 r->headers_out.charset.len = 0;
|
0
|
364
|
|
365 } else {
|
10
|
366 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
|
|
367 CRLF "--%010ud" CRLF
|
|
368 "Content-Type: %V" CRLF
|
|
369 "Content-Range: bytes ",
|
|
370 boundary,
|
|
371 &r->headers_out.content_type->value)
|
|
372 - ctx->boundary_header.data;
|
|
373 }
|
0
|
374
|
10
|
375 r->headers_out.content_type->value.data =
|
|
376 ngx_palloc(r->pool, sizeof("Content-Type: multipart/byteranges; "
|
|
377 "boundary=0123456789") - 1);
|
0
|
378
|
10
|
379 if (r->headers_out.content_type->value.data == NULL) {
|
|
380 return NGX_ERROR;
|
|
381 }
|
0
|
382
|
10
|
383 /* "Content-Type: multipart/byteranges; boundary=0123456789" */
|
0
|
384
|
10
|
385 r->headers_out.content_type->value.len =
|
|
386 ngx_sprintf(r->headers_out.content_type->value.data,
|
|
387 "multipart/byteranges; boundary=%010ud",
|
|
388 boundary)
|
|
389 - r->headers_out.content_type->value.data;
|
0
|
390
|
10
|
391 /* the size of the last boundary CRLF "--0123456789--" CRLF */
|
|
392 len = sizeof(CRLF "--0123456789--" CRLF) - 1;
|
0
|
393
|
10
|
394 range = r->headers_out.ranges.elts;
|
|
395 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
|
4
|
396
|
10
|
397 /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */
|
4
|
398
|
10
|
399 range[i].content_range.data =
|
|
400 ngx_palloc(r->pool, 3 * NGX_OFF_T_LEN + 2 + 4);
|
0
|
401
|
10
|
402 if (range[i].content_range.data == NULL) {
|
|
403 return NGX_ERROR;
|
|
404 }
|
0
|
405
|
10
|
406 range[i].content_range.len = ngx_sprintf(range[i].content_range.data,
|
|
407 "%O-%O/%O" CRLF CRLF,
|
|
408 range[i].start, range[i].end - 1,
|
|
409 r->headers_out.content_length_n)
|
|
410 - range[i].content_range.data;
|
0
|
411
|
10
|
412 len += ctx->boundary_header.len + range[i].content_range.len
|
|
413 + (size_t) (range[i].end - range[i].start);
|
|
414 }
|
0
|
415
|
10
|
416 r->headers_out.content_length_n = len;
|
|
417 r->headers_out.content_length = NULL;
|
0
|
418
|
|
419 return ngx_http_next_header_filter(r);
|
|
420 }
|
|
421
|
|
422
|
|
423 static ngx_int_t ngx_http_range_body_filter(ngx_http_request_t *r,
|
|
424 ngx_chain_t *in)
|
|
425 {
|
|
426 ngx_uint_t i;
|
|
427 ngx_buf_t *b;
|
|
428 ngx_chain_t *out, *hcl, *rcl, *dcl, **ll;
|
|
429 ngx_http_range_t *range;
|
|
430 ngx_http_range_filter_ctx_t *ctx;
|
|
431
|
|
432 if (r->headers_out.ranges.nelts == 0) {
|
|
433 return ngx_http_next_body_filter(r, in);
|
|
434 }
|
|
435
|
|
436 /*
|
|
437 * the optimized version for the static files only
|
|
438 * that are passed in the single file buf
|
|
439 */
|
|
440
|
|
441 if (in && in->buf->in_file && in->buf->last_buf) {
|
|
442 range = r->headers_out.ranges.elts;
|
|
443
|
|
444 if (r->headers_out.ranges.nelts == 1) {
|
|
445 in->buf->file_pos = range->start;
|
|
446 in->buf->file_last = range->end;
|
|
447
|
|
448 return ngx_http_next_body_filter(r, in);
|
|
449 }
|
|
450
|
|
451 ctx = ngx_http_get_module_ctx(r, ngx_http_range_body_filter_module);
|
|
452 ll = &out;
|
|
453
|
|
454 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
|
|
455
|
|
456 /*
|
|
457 * The boundary header of the range:
|
|
458 * CRLF
|
|
459 * "--0123456789" CRLF
|
|
460 * "Content-Type: image/jpeg" CRLF
|
|
461 * "Content-Range: bytes "
|
|
462 */
|
|
463
|
10
|
464 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
465 return NGX_ERROR;
|
|
466 }
|
|
467
|
0
|
468 b->memory = 1;
|
|
469 b->pos = ctx->boundary_header.data;
|
|
470 b->last = ctx->boundary_header.data + ctx->boundary_header.len;
|
|
471
|
10
|
472 if (!(hcl = ngx_alloc_chain_link(r->pool))) {
|
|
473 return NGX_ERROR;
|
|
474 }
|
|
475
|
0
|
476 hcl->buf = b;
|
|
477
|
10
|
478
|
0
|
479 /* "SSSS-EEEE/TTTT" CRLF CRLF */
|
|
480
|
10
|
481 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
482 return NGX_ERROR;
|
|
483 }
|
|
484
|
0
|
485 b->temporary = 1;
|
|
486 b->pos = range[i].content_range.data;
|
|
487 b->last = range[i].content_range.data + range[i].content_range.len;
|
|
488
|
10
|
489 if (!(rcl = ngx_alloc_chain_link(r->pool))) {
|
|
490 return NGX_ERROR;
|
|
491 }
|
|
492
|
0
|
493 rcl->buf = b;
|
|
494
|
10
|
495
|
0
|
496 /* the range data */
|
|
497
|
10
|
498 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
499 return NGX_ERROR;
|
|
500 }
|
|
501
|
0
|
502 b->in_file = 1;
|
|
503 b->file_pos = range[i].start;
|
|
504 b->file_last = range[i].end;
|
|
505 b->file = in->buf->file;
|
|
506
|
10
|
507 if (!(dcl = ngx_alloc_chain_link(r->pool))) {
|
|
508 return NGX_ERROR;
|
|
509 }
|
|
510
|
|
511 dcl->buf = b;
|
0
|
512
|
|
513 *ll = hcl;
|
|
514 hcl->next = rcl;
|
|
515 rcl->next = dcl;
|
|
516 ll = &dcl->next;
|
|
517 }
|
|
518
|
|
519 /* the last boundary CRLF "--0123456789--" CRLF */
|
|
520
|
10
|
521 if (!(b = ngx_calloc_buf(r->pool))) {
|
|
522 return NGX_ERROR;
|
|
523 }
|
|
524
|
0
|
525 b->temporary = 1;
|
|
526 b->last_buf = 1;
|
10
|
527
|
|
528 b->pos = ngx_palloc(r->pool, sizeof(CRLF "--0123456789--" CRLF) - 1);
|
|
529 if (b->pos == NULL) {
|
|
530 return NGX_ERROR;
|
|
531 }
|
|
532
|
0
|
533 b->last = ngx_cpymem(b->pos, ctx->boundary_header.data, 4 + 10);
|
|
534 *b->last++ = '-'; *b->last++ = '-';
|
|
535 *b->last++ = CR; *b->last++ = LF;
|
|
536
|
10
|
537 if (!(hcl = ngx_alloc_chain_link(r->pool))) {
|
|
538 return NGX_ERROR;
|
|
539 }
|
|
540
|
|
541 hcl->buf = b;
|
|
542 hcl->next = NULL;
|
|
543
|
0
|
544 *ll = hcl;
|
|
545
|
|
546 return ngx_http_next_body_filter(r, out);
|
|
547 }
|
|
548
|
|
549 /* TODO: alert */
|
|
550
|
|
551 return ngx_http_next_body_filter(r, in);
|
|
552 }
|
|
553
|
|
554
|
|
555 static ngx_int_t ngx_http_range_header_filter_init(ngx_cycle_t *cycle)
|
|
556 {
|
|
557 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
558 ngx_http_top_header_filter = ngx_http_range_header_filter;
|
|
559
|
|
560 return NGX_OK;
|
|
561 }
|
|
562
|
|
563
|
|
564 static ngx_int_t ngx_http_range_body_filter_init(ngx_cycle_t *cycle)
|
|
565 {
|
|
566 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
567 ngx_http_top_body_filter = ngx_http_range_body_filter;
|
|
568
|
|
569 return NGX_OK;
|
|
570 }
|