3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 * the single part format:
15 * "HTTP/1.0 206 Partial Content" CRLF
17 * "Content-Type: image/jpeg" CRLF
18 * "Content-Length: SIZE" CRLF
19 * "Content-Range: bytes START-END/SIZE" CRLF
24 * the mutlipart format:
26 * "HTTP/1.0 206 Partial Content" CRLF
28 * "Content-Type: multipart/byteranges; boundary=0123456789" CRLF
32 * "Content-Type: image/jpeg" CRLF
33 * "Content-Range: bytes START0-END0/SIZE" CRLF
38 * "Content-Type: image/jpeg" CRLF
39 * "Content-Range: bytes START1-END1/SIZE" CRLF
43 * "--0123456789--" CRLF
50 ngx_str_t content_range;
56 ngx_str_t boundary_header;
58 } ngx_http_range_filter_ctx_t;
61 ngx_int_t ngx_http_range_parse(ngx_http_request_t *r,
62 ngx_http_range_filter_ctx_t *ctx);
63 static ngx_int_t ngx_http_range_singlepart_header(ngx_http_request_t *r,
64 ngx_http_range_filter_ctx_t *ctx);
65 static ngx_int_t ngx_http_range_multipart_header(ngx_http_request_t *r,
66 ngx_http_range_filter_ctx_t *ctx);
67 static ngx_int_t ngx_http_range_not_satisfiable(ngx_http_request_t *r);
68 static ngx_int_t ngx_http_range_test_overlapped(ngx_http_request_t *r,
69 ngx_http_range_filter_ctx_t *ctx, ngx_chain_t *in);
70 static ngx_int_t ngx_http_range_singlepart_body(ngx_http_request_t *r,
71 ngx_http_range_filter_ctx_t *ctx, ngx_chain_t *in);
72 static ngx_int_t ngx_http_range_multipart_body(ngx_http_request_t *r,
73 ngx_http_range_filter_ctx_t *ctx, ngx_chain_t *in);
75 static ngx_int_t ngx_http_range_header_filter_init(ngx_conf_t *cf);
76 static ngx_int_t ngx_http_range_body_filter_init(ngx_conf_t *cf);
79 static ngx_http_module_t ngx_http_range_header_filter_module_ctx = {
80 NULL, /* preconfiguration */
81 ngx_http_range_header_filter_init, /* postconfiguration */
83 NULL, /* create main configuration */
84 NULL, /* init main configuration */
86 NULL, /* create server configuration */
87 NULL, /* merge server configuration */
89 NULL, /* create location configuration */
90 NULL, /* merge location configuration */
94 ngx_module_t ngx_http_range_header_filter_module = {
96 &ngx_http_range_header_filter_module_ctx, /* module context */
97 NULL, /* module directives */
98 NGX_HTTP_MODULE, /* module type */
99 NULL, /* init master */
100 NULL, /* init module */
101 NULL, /* init process */
102 NULL, /* init thread */
103 NULL, /* exit thread */
104 NULL, /* exit process */
105 NULL, /* exit master */
106 NGX_MODULE_V1_PADDING
110 static ngx_http_module_t ngx_http_range_body_filter_module_ctx = {
111 NULL, /* preconfiguration */
112 ngx_http_range_body_filter_init, /* postconfiguration */
114 NULL, /* create main configuration */
115 NULL, /* init main configuration */
117 NULL, /* create server configuration */
118 NULL, /* merge server configuration */
120 NULL, /* create location configuration */
121 NULL, /* merge location configuration */
125 ngx_module_t ngx_http_range_body_filter_module = {
127 &ngx_http_range_body_filter_module_ctx, /* module context */
128 NULL, /* module directives */
129 NGX_HTTP_MODULE, /* module type */
130 NULL, /* init master */
131 NULL, /* init module */
132 NULL, /* init process */
133 NULL, /* init thread */
134 NULL, /* exit thread */
135 NULL, /* exit process */
136 NULL, /* exit master */
137 NGX_MODULE_V1_PADDING
141 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
142 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
146 ngx_http_range_header_filter(ngx_http_request_t *r)
150 ngx_http_range_filter_ctx_t *ctx;
152 if (r->http_version < NGX_HTTP_VERSION_10
153 || r->headers_out.status != NGX_HTTP_OK
155 || r->headers_out.content_length_n == -1
158 return ngx_http_next_header_filter(r);
161 if (r->headers_in.range == NULL
162 || r->headers_in.range->value.len < 7
163 || ngx_strncasecmp(r->headers_in.range->value.data,
164 (u_char *) "bytes=", 6)
170 if (r->headers_in.if_range && r->headers_out.last_modified_time != -1) {
172 if_range = ngx_http_parse_time(r->headers_in.if_range->value.data,
173 r->headers_in.if_range->value.len);
175 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
177 if_range, r->headers_out.last_modified_time);
179 if (if_range != r->headers_out.last_modified_time) {
184 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_range_filter_ctx_t));
189 if (ngx_array_init(&ctx->ranges, r->pool, 1, sizeof(ngx_http_range_t))
195 rc = ngx_http_range_parse(r, ctx);
199 ngx_http_set_ctx(r, ctx, ngx_http_range_body_filter_module);
201 r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
203 if (ctx->ranges.nelts == 1) {
204 return ngx_http_range_singlepart_header(r, ctx);
207 return ngx_http_range_multipart_header(r, ctx);
210 if (rc == NGX_HTTP_RANGE_NOT_SATISFIABLE) {
211 return ngx_http_range_not_satisfiable(r);
214 /* rc == NGX_ERROR */
220 r->headers_out.accept_ranges = ngx_list_push(&r->headers_out.headers);
221 if (r->headers_out.accept_ranges == NULL) {
225 r->headers_out.accept_ranges->hash = 1;
226 r->headers_out.accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
227 r->headers_out.accept_ranges->key.data = (u_char *) "Accept-Ranges";
228 r->headers_out.accept_ranges->value.len = sizeof("bytes") - 1;
229 r->headers_out.accept_ranges->value.data = (u_char *) "bytes";
231 return ngx_http_next_header_filter(r);
236 ngx_http_range_parse(ngx_http_request_t *r, ngx_http_range_filter_ctx_t *ctx)
241 ngx_http_range_t *range;
243 p = r->headers_in.range->value.data + 6;
250 while (*p == ' ') { p++; }
253 if (*p < '0' || *p > '9') {
254 return NGX_HTTP_RANGE_NOT_SATISFIABLE;
257 while (*p >= '0' && *p <= '9') {
258 start = start * 10 + *p++ - '0';
261 while (*p == ' ') { p++; }
264 return NGX_HTTP_RANGE_NOT_SATISFIABLE;
267 if (start >= r->headers_out.content_length_n) {
268 return NGX_HTTP_RANGE_NOT_SATISFIABLE;
271 while (*p == ' ') { p++; }
273 if (*p == ',' || *p == '\0') {
274 range = ngx_array_push(&ctx->ranges);
279 range->start = start;
280 range->end = r->headers_out.content_length_n;
294 if (*p < '0' || *p > '9') {
295 return NGX_HTTP_RANGE_NOT_SATISFIABLE;
298 while (*p >= '0' && *p <= '9') {
299 end = end * 10 + *p++ - '0';
302 while (*p == ' ') { p++; }
304 if (*p != ',' && *p != '\0') {
305 return NGX_HTTP_RANGE_NOT_SATISFIABLE;
309 start = r->headers_out.content_length_n - end;
310 end = r->headers_out.content_length_n - 1;
314 return NGX_HTTP_RANGE_NOT_SATISFIABLE;
317 range = ngx_array_push(&ctx->ranges);
322 range->start = start;
324 if (end >= r->headers_out.content_length_n) {
326 * Download Accelerator sends the last byte position
327 * that equals to the file length
329 range->end = r->headers_out.content_length_n;
332 range->end = end + 1;
343 ngx_http_range_singlepart_header(ngx_http_request_t *r,
344 ngx_http_range_filter_ctx_t *ctx)
346 ngx_table_elt_t *content_range;
347 ngx_http_range_t *range;
349 content_range = ngx_list_push(&r->headers_out.headers);
350 if (content_range == NULL) {
354 r->headers_out.content_range = content_range;
356 content_range->hash = 1;
357 content_range->key.len = sizeof("Content-Range") - 1;
358 content_range->key.data = (u_char *) "Content-Range";
360 content_range->value.data = ngx_pnalloc(r->pool,
361 sizeof("bytes -/") - 1 + 3 * NGX_OFF_T_LEN);
362 if (content_range->value.data == NULL) {
366 /* "Content-Range: bytes SSSS-EEEE/TTTT" header */
368 range = ctx->ranges.elts;
370 content_range->value.len = ngx_sprintf(content_range->value.data,
372 range->start, range->end - 1,
373 r->headers_out.content_length_n)
374 - content_range->value.data;
376 r->headers_out.content_length_n = range->end - range->start;
378 if (r->headers_out.content_length) {
379 r->headers_out.content_length->hash = 0;
380 r->headers_out.content_length = NULL;
383 return ngx_http_next_header_filter(r);
388 ngx_http_range_multipart_header(ngx_http_request_t *r,
389 ngx_http_range_filter_ctx_t *ctx)
393 ngx_http_range_t *range;
394 ngx_atomic_uint_t boundary;
396 len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
397 + sizeof(CRLF "Content-Type: ") - 1
398 + r->headers_out.content_type.len
399 + sizeof(CRLF "Content-Range: bytes ") - 1;
401 if (r->headers_out.charset.len) {
402 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
405 ctx->boundary_header.data = ngx_pnalloc(r->pool, len);
406 if (ctx->boundary_header.data == NULL) {
410 boundary = ngx_next_temp_number(0);
413 * The boundary header of the range:
415 * "--0123456789" CRLF
416 * "Content-Type: image/jpeg" CRLF
417 * "Content-Range: bytes "
420 if (r->headers_out.charset.len) {
421 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
423 "Content-Type: %V; charset=%V" CRLF
424 "Content-Range: bytes ",
426 &r->headers_out.content_type,
427 &r->headers_out.charset)
428 - ctx->boundary_header.data;
430 r->headers_out.charset.len = 0;
432 } else if (r->headers_out.content_type.len) {
433 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
435 "Content-Type: %V" CRLF
436 "Content-Range: bytes ",
438 &r->headers_out.content_type)
439 - ctx->boundary_header.data;
442 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
444 "Content-Range: bytes ",
446 - ctx->boundary_header.data;
449 r->headers_out.content_type.data =
451 sizeof("Content-Type: multipart/byteranges; boundary=") - 1
454 if (r->headers_out.content_type.data == NULL) {
458 /* "Content-Type: multipart/byteranges; boundary=0123456789" */
460 r->headers_out.content_type.len =
461 ngx_sprintf(r->headers_out.content_type.data,
462 "multipart/byteranges; boundary=%0muA",
464 - r->headers_out.content_type.data;
467 /* the size of the last boundary CRLF "--0123456789--" CRLF */
469 len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN + sizeof("--" CRLF) - 1;
471 range = ctx->ranges.elts;
472 for (i = 0; i < ctx->ranges.nelts; i++) {
474 /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */
476 range[i].content_range.data =
477 ngx_pnalloc(r->pool, 3 * NGX_OFF_T_LEN + 2 + 4);
479 if (range[i].content_range.data == NULL) {
483 range[i].content_range.len = ngx_sprintf(range[i].content_range.data,
484 "%O-%O/%O" CRLF CRLF,
485 range[i].start, range[i].end - 1,
486 r->headers_out.content_length_n)
487 - range[i].content_range.data;
489 len += ctx->boundary_header.len + range[i].content_range.len
490 + (size_t) (range[i].end - range[i].start);
493 r->headers_out.content_length_n = len;
495 if (r->headers_out.content_length) {
496 r->headers_out.content_length->hash = 0;
497 r->headers_out.content_length = NULL;
500 return ngx_http_next_header_filter(r);
505 ngx_http_range_not_satisfiable(ngx_http_request_t *r)
507 ngx_table_elt_t *content_range;
509 r->headers_out.status = NGX_HTTP_RANGE_NOT_SATISFIABLE;
511 content_range = ngx_list_push(&r->headers_out.headers);
512 if (content_range == NULL) {
516 r->headers_out.content_range = content_range;
518 content_range->hash = 1;
519 content_range->key.len = sizeof("Content-Range") - 1;
520 content_range->key.data = (u_char *) "Content-Range";
522 content_range->value.data = ngx_pnalloc(r->pool,
523 sizeof("bytes */") - 1 + NGX_OFF_T_LEN);
524 if (content_range->value.data == NULL) {
528 content_range->value.len = ngx_sprintf(content_range->value.data,
530 r->headers_out.content_length_n)
531 - content_range->value.data;
533 ngx_http_clear_content_length(r);
535 return NGX_HTTP_RANGE_NOT_SATISFIABLE;
540 ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
542 ngx_http_range_filter_ctx_t *ctx;
545 return ngx_http_next_body_filter(r, in);
548 ctx = ngx_http_get_module_ctx(r, ngx_http_range_body_filter_module);
551 return ngx_http_next_body_filter(r, in);
554 if (ctx->ranges.nelts == 1) {
555 return ngx_http_range_singlepart_body(r, ctx, in);
559 * multipart ranges are supported only if whole body is in a single buffer
562 if (ngx_buf_special(in->buf)) {
563 return ngx_http_next_body_filter(r, in);
566 if (ngx_http_range_test_overlapped(r, ctx, in) != NGX_OK) {
570 return ngx_http_range_multipart_body(r, ctx, in);
575 ngx_http_range_test_overlapped(ngx_http_request_t *r,
576 ngx_http_range_filter_ctx_t *ctx, ngx_chain_t *in)
581 ngx_http_range_t *range;
589 if (!buf->last_buf) {
592 start = buf->file_pos + ctx->offset;
593 last = buf->file_last + ctx->offset;
596 start = buf->pos - buf->start + ctx->offset;
597 last = buf->last - buf->start + ctx->offset;
600 range = ctx->ranges.elts;
601 for (i = 0; i < ctx->ranges.nelts; i++) {
602 if (start > range[i].start || last < range[i].end) {
608 ctx->offset = ngx_buf_size(buf);
614 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
615 "range in overlapped buffers");
622 ngx_http_range_singlepart_body(ngx_http_request_t *r,
623 ngx_http_range_filter_ctx_t *ctx, ngx_chain_t *in)
627 ngx_chain_t *out, *cl, **ll;
628 ngx_http_range_t *range;
632 range = ctx->ranges.elts;
634 for (cl = in; cl; cl = cl->next) {
639 last = ctx->offset + ngx_buf_size(buf);
643 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
644 "http range body buf: %O-%O", start, last);
646 if (ngx_buf_special(buf)) {
652 if (range->end <= start || range->start >= last) {
654 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
655 "http range body skip");
658 buf->file_pos = buf->file_last;
661 buf->pos = buf->last;
667 if (range->start > start) {
670 buf->file_pos += range->start - start;
673 if (ngx_buf_in_memory(buf)) {
674 buf->pos += (size_t) (range->start - start);
678 if (range->end <= last) {
681 buf->file_last -= last - range->end;
684 if (ngx_buf_in_memory(buf)) {
685 buf->last -= (size_t) (last - range->end);
703 return ngx_http_next_body_filter(r, out);
708 ngx_http_range_multipart_body(ngx_http_request_t *r,
709 ngx_http_range_filter_ctx_t *ctx, ngx_chain_t *in)
713 ngx_chain_t *out, *hcl, *rcl, *dcl, **ll;
714 ngx_http_range_t *range;
718 range = ctx->ranges.elts;
720 for (i = 0; i < ctx->ranges.nelts; i++) {
723 * The boundary header of the range:
725 * "--0123456789" CRLF
726 * "Content-Type: image/jpeg" CRLF
727 * "Content-Range: bytes "
730 b = ngx_calloc_buf(r->pool);
736 b->pos = ctx->boundary_header.data;
737 b->last = ctx->boundary_header.data + ctx->boundary_header.len;
739 hcl = ngx_alloc_chain_link(r->pool);
747 /* "SSSS-EEEE/TTTT" CRLF CRLF */
749 b = ngx_calloc_buf(r->pool);
755 b->pos = range[i].content_range.data;
756 b->last = range[i].content_range.data + range[i].content_range.len;
758 rcl = ngx_alloc_chain_link(r->pool);
768 b = ngx_calloc_buf(r->pool);
773 b->in_file = buf->in_file;
774 b->temporary = buf->temporary;
775 b->memory = buf->memory;
780 b->file_pos = range[i].start;
781 b->file_last = range[i].end;
784 if (ngx_buf_in_memory(buf)) {
785 b->pos = buf->start + (size_t) range[i].start;
786 b->last = buf->start + (size_t) range[i].end;
789 dcl = ngx_alloc_chain_link(r->pool);
802 /* the last boundary CRLF "--0123456789--" CRLF */
804 b = ngx_calloc_buf(r->pool);
812 b->pos = ngx_pnalloc(r->pool, sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
813 + sizeof("--" CRLF) - 1);
814 if (b->pos == NULL) {
818 b->last = ngx_cpymem(b->pos, ctx->boundary_header.data,
819 sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN);
820 *b->last++ = '-'; *b->last++ = '-';
821 *b->last++ = CR; *b->last++ = LF;
823 hcl = ngx_alloc_chain_link(r->pool);
833 return ngx_http_next_body_filter(r, out);
838 ngx_http_range_header_filter_init(ngx_conf_t *cf)
840 ngx_http_next_header_filter = ngx_http_top_header_filter;
841 ngx_http_top_header_filter = ngx_http_range_header_filter;
848 ngx_http_range_body_filter_init(ngx_conf_t *cf)
850 ngx_http_next_body_filter = ngx_http_top_body_filter;
851 ngx_http_top_body_filter = ngx_http_range_body_filter;