annotate src/http/modules/ngx_http_range_filter_module.c @ 58:b55cbf18157e NGINX_0_1_29

nginx 0.1.29 *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; bug appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
author Igor Sysoev <http://sysoev.ru>
date Thu, 12 May 2005 00:00:00 +0400
parents 72eb30262aac
children 71c46860eb55
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
1
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
2 /*
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
4 */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
5
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
6
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
7 #include <ngx_config.h>
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
8 #include <ngx_core.h>
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
9 #include <ngx_http.h>
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
10
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
11
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
12 /*
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
13 * the single part format:
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
14 *
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
15 * "HTTP/1.0 206 Partial Content" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
16 * ... header ...
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
17 * "Content-Type: image/jpeg" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
18 * "Content-Length: SIZE" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
19 * "Content-Range: bytes START-END/SIZE" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
20 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
21 * ... data ...
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
22 *
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
23 *
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
24 * the mutlipart format:
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
25 *
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
26 * "HTTP/1.0 206 Partial Content" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
27 * ... header ...
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
28 * "Content-Type: multipart/byteranges; boundary=0123456789" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
29 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
30 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
31 * "--0123456789" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
32 * "Content-Type: image/jpeg" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
33 * "Content-Range: bytes START0-END0/SIZE" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
34 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
35 * ... data ...
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
36 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
37 * "--0123456789" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
38 * "Content-Type: image/jpeg" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
39 * "Content-Range: bytes START1-END1/SIZE" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
40 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
41 * ... data ...
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
42 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
43 * "--0123456789--" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
44 */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
45
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
46
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
47 typedef struct {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
48 ngx_str_t boundary_header;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
49 } ngx_http_range_filter_ctx_t;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
51
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
52 static ngx_int_t ngx_http_range_header_filter_init(ngx_cycle_t *cycle);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
53 static ngx_int_t ngx_http_range_body_filter_init(ngx_cycle_t *cycle);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
54
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
55
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
56 static ngx_http_module_t ngx_http_range_header_filter_module_ctx = {
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
57 NULL, /* preconfiguration */
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
58 NULL, /* postconfiguration */
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
59
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
60 NULL, /* create main configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
61 NULL, /* init main configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
62
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
63 NULL, /* create server configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
64 NULL, /* merge server configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
65
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
66 NULL, /* create location configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
67 NULL, /* merge location configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
68 };
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
69
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
70
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
71 ngx_module_t ngx_http_range_header_filter_module = {
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
72 NGX_MODULE_V1,
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
73 &ngx_http_range_header_filter_module_ctx, /* module context */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
74 NULL, /* module directives */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
75 NGX_HTTP_MODULE, /* module type */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
76 ngx_http_range_header_filter_init, /* init module */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
77 NULL /* init process */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
78 };
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
79
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
80
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
81 static ngx_http_module_t ngx_http_range_body_filter_module_ctx = {
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
82 NULL, /* preconfiguration */
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
83 NULL, /* postconfiguration */
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
84
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
85 NULL, /* create main configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
86 NULL, /* init main configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
87
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
88 NULL, /* create server configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
89 NULL, /* merge server configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
90
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
91 NULL, /* create location configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
92 NULL, /* merge location configuration */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
93 };
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
94
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
95
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
96 ngx_module_t ngx_http_range_body_filter_module = {
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
97 NGX_MODULE_V1,
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
98 &ngx_http_range_body_filter_module_ctx, /* module context */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
99 NULL, /* module directives */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
100 NGX_HTTP_MODULE, /* module type */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
101 ngx_http_range_body_filter_init, /* init module */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
102 NULL /* init process */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
103 };
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
104
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
105
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
106 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
107 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
108
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
109
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
110 static ngx_int_t
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
111 ngx_http_range_header_filter(ngx_http_request_t *r)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
112 {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
113 u_char *p;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
114 size_t len;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
115 off_t start, end;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
116 ngx_int_t rc;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
117 ngx_uint_t suffix, i;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
118 ngx_atomic_uint_t boundary;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
119 ngx_table_elt_t *content_range;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
120 ngx_http_range_t *range;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
121 ngx_http_range_filter_ctx_t *ctx;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
122
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
123 if (r->http_version < NGX_HTTP_VERSION_10
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
124 || r->headers_out.status != NGX_HTTP_OK
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
125 || r->main
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
126 || r->headers_out.content_length_n == -1
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
127 || !r->filter_allow_ranges)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
128 {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
129 return ngx_http_next_header_filter(r);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
130 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
131
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
132 if (r->headers_in.range == NULL
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
133 || r->headers_in.range->value.len < 7
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
134 || ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
135 {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
136
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
137 r->headers_out.accept_ranges = ngx_list_push(&r->headers_out.headers);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
138 if (r->headers_out.accept_ranges == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
139 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
140 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
141
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
142 r->headers_out.accept_ranges->hash = 1;
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
143 r->headers_out.accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
144 r->headers_out.accept_ranges->key.data = (u_char *) "Accept-Ranges";
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
145 r->headers_out.accept_ranges->value.len = sizeof("bytes") - 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
146 r->headers_out.accept_ranges->value.data = (u_char *) "bytes";
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
147
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
148 return ngx_http_next_header_filter(r);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
149 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
150
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
151 if (ngx_array_init(&r->headers_out.ranges, r->pool, 2,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
152 sizeof(ngx_http_range_t)) == NGX_ERROR)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
153 {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
154 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
155 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
156
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
157 rc = 0;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
158 range = NULL;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
159 p = r->headers_in.range->value.data + 6;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
160
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
161 for ( ;; ) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
162 start = 0;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
163 end = 0;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
164 suffix = 0;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
165
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
166 while (*p == ' ') { p++; }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
167
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
168 if (*p != '-') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
169 if (*p < '0' || *p > '9') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
170 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
171 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
172 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
173
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
174 while (*p >= '0' && *p <= '9') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
175 start = start * 10 + *p++ - '0';
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
176 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
177
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
178 while (*p == ' ') { p++; }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
179
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
180 if (*p++ != '-') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
181 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
182 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
183 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
184
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
185 if (start >= r->headers_out.content_length_n) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
186 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
187 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
188 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
189
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
190 while (*p == ' ') { p++; }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
191
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
192 if (*p == ',' || *p == '\0') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
193 range = ngx_array_push(&r->headers_out.ranges);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
194 if (range == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
195 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
196 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
197
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
198 range->start = start;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
199 range->end = r->headers_out.content_length_n;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
200
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
201 if (*p++ != ',') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
202 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
203 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
204
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
205 continue;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
206 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
207
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
208 } else {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
209 suffix = 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
210 p++;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
211 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
212
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
213 if (*p < '0' || *p > '9') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
214 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
215 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
216 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
217
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
218 while (*p >= '0' && *p <= '9') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
219 end = end * 10 + *p++ - '0';
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
220 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
221
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
222 while (*p == ' ') { p++; }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
223
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
224 if (*p != ',' && *p != '\0') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
225 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
226 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
227 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
228
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
229 if (suffix) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
230 start = r->headers_out.content_length_n - end;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
231 end = r->headers_out.content_length_n - 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
232 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
233
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
234 if (start > end) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
235 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
236 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
237 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
238
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
239 range = ngx_array_push(&r->headers_out.ranges);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
240 if (range == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
241 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
242 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
243
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
244 range->start = start;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
245
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
246 if (end >= r->headers_out.content_length_n) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
247 /*
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
248 * Download Accelerator sends the last byte position
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
249 * that equals to the file length
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
250 */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
251 range->end = r->headers_out.content_length_n;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
252
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
253 } else {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
254 range->end = end + 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
255 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
256
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
257 if (*p++ != ',') {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
258 break;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
259 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
260 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
261
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
262 if (rc) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
263
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
264 /* rc == NGX_HTTP_RANGE_NOT_SATISFIABLE */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
265
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
266 r->headers_out.status = rc;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
267 r->headers_out.ranges.nelts = 0;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
268
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
269 content_range = ngx_list_push(&r->headers_out.headers);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
270 if (content_range == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
271 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
272 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
273
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
274 r->headers_out.content_range = content_range;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
275
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
276 content_range->hash = 1;
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
277 content_range->key.len = sizeof("Content-Range") - 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
278 content_range->key.data = (u_char *) "Content-Range";
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
279
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
280 content_range->value.data = ngx_palloc(r->pool,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
281 sizeof("bytes */") - 1 + NGX_OFF_T_LEN);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
282 if (content_range->value.data == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
283 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
284 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
285
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
286 content_range->value.len = ngx_sprintf(content_range->value.data,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
287 "bytes */%O",
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
288 r->headers_out.content_length_n)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
289 - content_range->value.data;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
290
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
291 r->headers_out.content_length_n = -1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
292 if (r->headers_out.content_length) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
293 r->headers_out.content_length->key.len = 0;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
294 r->headers_out.content_length = NULL;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
295 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
296
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
297 return rc;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
298 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
299
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
300 r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
301
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
302 if (r->headers_out.ranges.nelts == 1) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
303
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
304 content_range = ngx_list_push(&r->headers_out.headers);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
305 if (content_range == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
306 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
307 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
308
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
309 r->headers_out.content_range = content_range;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
310
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
311 content_range->hash = 1;
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
312 content_range->key.len = sizeof("Content-Range") - 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
313 content_range->key.data = (u_char *) "Content-Range";
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
314
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
315 content_range->value.data =
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
316 ngx_palloc(r->pool, sizeof("bytes -/") - 1 + 3 * NGX_OFF_T_LEN);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
317 if (content_range->value.data == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
318 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
319 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
320
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
321 /* "Content-Range: bytes SSSS-EEEE/TTTT" header */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
322
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
323 content_range->value.len = ngx_sprintf(content_range->value.data,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
324 "bytes %O-%O/%O",
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
325 range->start, range->end - 1,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
326 r->headers_out.content_length_n)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
327 - content_range->value.data;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
328
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
329 r->headers_out.content_length_n = range->end - range->start;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
330
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
331 return ngx_http_next_header_filter(r);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
332 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
333
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
334
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
335 /* TODO: what if no content_type ?? */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
336
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
337 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_range_filter_ctx_t));
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
338 if (ctx == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
339 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
340 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
341
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
342 ngx_http_set_ctx(r, ctx, ngx_http_range_body_filter_module);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
343
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
344
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
345 len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
346 + sizeof(CRLF "Content-Type: ") - 1
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
347 + r->headers_out.content_type.len
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
348 + sizeof(CRLF "Content-Range: bytes ") - 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
349
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
350 if (r->headers_out.charset.len) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
351 len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
352 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
353
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
354 ctx->boundary_header.data = ngx_palloc(r->pool, len);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
355 if (ctx->boundary_header.data == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
356 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
357 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
358
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
359 boundary = ngx_next_temp_number(0);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
360
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
361 /*
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
362 * The boundary header of the range:
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
363 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
364 * "--0123456789" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
365 * "Content-Type: image/jpeg" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
366 * "Content-Range: bytes "
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
367 */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
368
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
369 if (r->headers_out.charset.len) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
370 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
371 CRLF "--%0muA" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
372 "Content-Type: %V; charset=%V" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
373 "Content-Range: bytes ",
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
374 boundary,
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
375 &r->headers_out.content_type,
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
376 &r->headers_out.charset)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
377 - ctx->boundary_header.data;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
378
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
379 r->headers_out.charset.len = 0;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
380
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
381 } else {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
382 ctx->boundary_header.len = ngx_sprintf(ctx->boundary_header.data,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
383 CRLF "--%0muA" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
384 "Content-Type: %V" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
385 "Content-Range: bytes ",
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
386 boundary,
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
387 &r->headers_out.content_type)
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
388 - ctx->boundary_header.data;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
389 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
390
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
391 r->headers_out.content_type.data =
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
392 ngx_palloc(r->pool,
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
393 sizeof("Content-Type: multipart/byteranges; boundary=") - 1
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
394 + NGX_ATOMIC_T_LEN);
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
395
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
396 if (r->headers_out.content_type.data == NULL) {
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
397 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
398 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
399
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
400 /* "Content-Type: multipart/byteranges; boundary=0123456789" */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
401
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
402 r->headers_out.content_type.len =
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
403 ngx_sprintf(r->headers_out.content_type.data,
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
404 "multipart/byteranges; boundary=%0muA",
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
405 boundary)
58
b55cbf18157e nginx 0.1.29
Igor Sysoev <http://sysoev.ru>
parents: 50
diff changeset
406 - r->headers_out.content_type.data;
50
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
407
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
408
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
409 /* the size of the last boundary CRLF "--0123456789--" CRLF */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
410
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
411 len = sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN + sizeof("--" CRLF) - 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
412
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
413 range = r->headers_out.ranges.elts;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
414 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
415
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
416 /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
417
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
418 range[i].content_range.data =
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
419 ngx_palloc(r->pool, 3 * NGX_OFF_T_LEN + 2 + 4);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
420
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
421 if (range[i].content_range.data == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
422 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
423 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
424
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
425 range[i].content_range.len = ngx_sprintf(range[i].content_range.data,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
426 "%O-%O/%O" CRLF CRLF,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
427 range[i].start, range[i].end - 1,
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
428 r->headers_out.content_length_n)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
429 - range[i].content_range.data;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
430
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
431 len += ctx->boundary_header.len + range[i].content_range.len
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
432 + (size_t) (range[i].end - range[i].start);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
433 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
434
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
435 r->headers_out.content_length_n = len;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
436 r->headers_out.content_length = NULL;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
437
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
438 return ngx_http_next_header_filter(r);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
439 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
440
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
441
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
442 static ngx_int_t
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
443 ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
444 {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
445 ngx_uint_t i;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
446 ngx_buf_t *b;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
447 ngx_chain_t *out, *hcl, *rcl, *dcl, **ll;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
448 ngx_http_range_t *range;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
449 ngx_http_range_filter_ctx_t *ctx;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
450
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
451 if (r->headers_out.ranges.nelts == 0) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
452 return ngx_http_next_body_filter(r, in);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
453 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
454
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
455 /*
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
456 * the optimized version for the static files only
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
457 * that are passed in the single file buf
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
458 */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
459
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
460 if (in && in->buf->in_file && in->buf->last_buf) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
461 range = r->headers_out.ranges.elts;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
462
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
463 if (r->headers_out.ranges.nelts == 1) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
464 in->buf->file_pos = range->start;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
465 in->buf->file_last = range->end;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
466
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
467 return ngx_http_next_body_filter(r, in);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
468 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
469
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
470 ctx = ngx_http_get_module_ctx(r, ngx_http_range_body_filter_module);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
471 ll = &out;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
472
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
473 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
474
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
475 /*
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
476 * The boundary header of the range:
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
477 * CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
478 * "--0123456789" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
479 * "Content-Type: image/jpeg" CRLF
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
480 * "Content-Range: bytes "
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
481 */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
482
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
483 b = ngx_calloc_buf(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
484 if (b == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
485 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
486 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
487
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
488 b->memory = 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
489 b->pos = ctx->boundary_header.data;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
490 b->last = ctx->boundary_header.data + ctx->boundary_header.len;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
491
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
492 hcl = ngx_alloc_chain_link(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
493 if (hcl == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
494 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
495 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
496
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
497 hcl->buf = b;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
498
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
499
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
500 /* "SSSS-EEEE/TTTT" CRLF CRLF */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
501
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
502 b = ngx_calloc_buf(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
503 if (b == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
504 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
505 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
506
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
507 b->temporary = 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
508 b->pos = range[i].content_range.data;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
509 b->last = range[i].content_range.data + range[i].content_range.len;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
510
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
511 rcl = ngx_alloc_chain_link(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
512 if (rcl == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
513 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
514 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
515
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
516 rcl->buf = b;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
517
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
518
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
519 /* the range data */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
520
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
521 b = ngx_calloc_buf(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
522 if (b == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
523 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
524 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
525
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
526 b->in_file = 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
527 b->file_pos = range[i].start;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
528 b->file_last = range[i].end;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
529 b->file = in->buf->file;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
530
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
531 dcl = ngx_alloc_chain_link(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
532 if (dcl == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
533 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
534 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
535
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
536 dcl->buf = b;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
537
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
538 *ll = hcl;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
539 hcl->next = rcl;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
540 rcl->next = dcl;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
541 ll = &dcl->next;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
542 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
543
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
544 /* the last boundary CRLF "--0123456789--" CRLF */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
545
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
546 b = ngx_calloc_buf(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
547 if (b == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
548 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
549 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
550
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
551 b->temporary = 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
552 b->last_buf = 1;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
553
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
554 b->pos = ngx_palloc(r->pool, sizeof(CRLF "--") - 1 + NGX_ATOMIC_T_LEN
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
555 + sizeof("--" CRLF) - 1);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
556 if (b->pos == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
557 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
558 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
559
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
560 b->last = ngx_cpymem(b->pos, ctx->boundary_header.data, 4 + 10);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
561 *b->last++ = '-'; *b->last++ = '-';
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
562 *b->last++ = CR; *b->last++ = LF;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
563
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
564 hcl = ngx_alloc_chain_link(r->pool);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
565 if (hcl == NULL) {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
566 return NGX_ERROR;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
567 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
568
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
569 hcl->buf = b;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
570 hcl->next = NULL;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
571
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
572 *ll = hcl;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
573
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
574 return ngx_http_next_body_filter(r, out);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
575 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
576
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
577 /* TODO: alert */
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
578
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
579 return ngx_http_next_body_filter(r, in);
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
580 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
581
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
582
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
583 static ngx_int_t
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
584 ngx_http_range_header_filter_init(ngx_cycle_t *cycle)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
585 {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
586 ngx_http_next_header_filter = ngx_http_top_header_filter;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
587 ngx_http_top_header_filter = ngx_http_range_header_filter;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
588
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
589 return NGX_OK;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
590 }
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
591
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
592
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
593 static ngx_int_t
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
594 ngx_http_range_body_filter_init(ngx_cycle_t *cycle)
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
595 {
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
596 ngx_http_next_body_filter = ngx_http_top_body_filter;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
597 ngx_http_top_body_filter = ngx_http_range_body_filter;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
598
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
599 return NGX_OK;
72eb30262aac nginx 0.1.25
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
600 }