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