comparison src/http/modules/ngx_http_range_filter.c @ 99:a059e1aa65d4

nginx-0.0.1-2003-06-02-19:24:30 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 02 Jun 2003 15:24:30 +0000
parents
children 7ebc8b7fb816
comparison
equal deleted inserted replaced
98:c9b243802a17 99:a059e1aa65d4
1
2 #include <ngx_config.h>
3 #include <ngx_core.h>
4 #include <ngx_http.h>
5
6
7 typedef struct {
8 ngx_str_t boundary_header;
9 } ngx_http_range_filter_ctx_t;
10
11
12 static int ngx_http_range_filter_init(ngx_pool_t *pool);
13
14
15 static ngx_http_module_t ngx_http_range_filter_module_ctx = {
16 NULL, /* create main configuration */
17 NULL, /* init main configuration */
18
19 NULL, /* create server configuration */
20 NULL, /* merge server configuration */
21
22 NULL, /* create location configuration */
23 NULL, /* merge location configuration */
24 };
25
26
27 ngx_module_t ngx_http_range_filter_module = {
28 NGX_MODULE,
29 &ngx_http_range_filter_module_ctx, /* module context */
30 NULL, /* module directives */
31 NGX_HTTP_MODULE, /* module type */
32 ngx_http_range_filter_init /* init module */
33 };
34
35
36 static int (*next_header_filter) (ngx_http_request_t *r);
37 static int (*next_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
38
39
40 static int ngx_http_range_header_filter(ngx_http_request_t *r)
41 {
42 int rc, boundary, len, i;
43 char *p;
44 off_t start, end;
45 ngx_http_range_t *range;
46 ngx_http_range_filter_ctx_t *ctx;
47
48 if (r->main
49 || r->http_version < NGX_HTTP_VERSION_10
50 || r->headers_out.status != NGX_HTTP_OK
51 || r->headers_out.content_length == -1
52 /* STUB: we currently support ranges for file hunks only */
53 || r->filter & NGX_HTTP_FILTER_NEED_IN_MEMORY
54 || r->headers_in.range == NULL
55 || r->headers_in.range->value.len < 7
56 || ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
57 {
58 return next_header_filter(r);
59 }
60
61 ngx_init_array(r->headers_out.ranges, r->pool, 5, sizeof(ngx_http_range_t),
62 NGX_ERROR);
63
64 rc = 0;
65 p = r->headers_in.range->value.data + 6;
66
67 for ( ;; ) {
68 start = end = 0;
69
70 while (*p == ' ') { p++; }
71
72 if (*p < '0' || *p > '9') {
73 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
74 break;
75 }
76
77 while (*p >= '0' && *p <= '9') {
78 start = start * 10 + *p++ - '0';
79 }
80
81 while (*p == ' ') { p++; }
82
83 if (*p++ != '-') {
84 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
85 break;
86 }
87
88 if (start >= r->headers_out.content_length) {
89 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
90 break;
91 }
92
93 while (*p == ' ') { p++; }
94
95 if (*p == ',' || *p == '\0') {
96 ngx_test_null(range, ngx_push_array(&r->headers_out.ranges),
97 NGX_ERROR);
98 range->start = start;
99 range->end = r->headers_out.content_length;
100
101 if (*p++ == ',') {
102 continue;
103 }
104
105 break;
106 }
107
108 if (*p < '0' || *p > '9') {
109 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
110 break;
111 }
112
113 while (*p >= '0' && *p <= '9') {
114 end = end * 10 + *p++ - '0';
115 }
116
117 while (*p == ' ') { p++; }
118
119 if (*p != ',' && *p != '\0') {
120 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
121 break;
122 }
123
124 if (end >= r->headers_out.content_length || start >= end) {
125 rc = NGX_HTTP_RANGE_NOT_SATISFIABLE;
126 break;
127 }
128
129 ngx_test_null(range, ngx_push_array(&r->headers_out.ranges), NGX_ERROR);
130 range->start = start;
131 range->end = end + 1;
132
133 if (*p++ == ',') {
134 continue;
135 }
136
137 break;
138 }
139
140 if (rc) {
141 r->headers_out.status = rc;
142 r->headers_out.ranges.nelts = 0;
143
144 ngx_test_null(r->headers_out.content_range,
145 ngx_push_table(r->headers_out.headers),
146 NGX_ERROR);
147
148 ngx_test_null(r->headers_out.content_range->value.data,
149 ngx_palloc(r->pool, 8 + 20 + 1),
150 NGX_ERROR);
151
152 r->headers_out.content_range->value.len =
153 ngx_snprintf(r->headers_out.content_range->value.data,
154 8 + 20 + 1, "bytes */" OFF_FMT,
155 r->headers_out.content_length);
156
157 r->headers_out.content_length = -1;
158
159 return rc;
160
161 } else {
162 r->headers_out.status = NGX_HTTP_PARTIAL_CONTENT;
163
164 if (r->headers_out.ranges.nelts == 1) {
165 ngx_test_null(r->headers_out.content_range,
166 ngx_push_table(r->headers_out.headers),
167 NGX_ERROR);
168
169 ngx_test_null(r->headers_out.content_range->value.data,
170 ngx_palloc(r->pool, 6 + 20 + 1 + 20 + 1 + 20 + 1),
171 NGX_ERROR);
172
173 r->headers_out.content_range->value.len =
174 ngx_snprintf(r->headers_out.content_range->value.data,
175 6 + 20 + 1 + 20 + 1 + 20 + 1,
176 "bytes " OFF_FMT "-" OFF_FMT "/" OFF_FMT,
177 range->start, range->end - 1,
178 r->headers_out.content_length);
179
180 r->headers_out.content_length = range->end - range->start;
181
182 } else {
183
184 #if 0
185 /* TODO: what if no content_type ?? */
186 ngx_test_null(r->headers_out.content_type,
187 ngx_push_table(r->headers_out.headers),
188 NGX_ERROR);
189 #endif
190
191 ngx_http_create_ctx(r, ctx, ngx_http_range_filter_module,
192 sizeof(ngx_http_range_filter_ctx_t), NGX_ERROR);
193
194 len = 4 + 10 + 2 + 14 + r->headers_out.content_type->value.len
195 + 2 + 21 + 1;
196
197 if (r->headers_out.charset.len) {
198 len += 10 + r->headers_out.charset.len;
199 }
200
201 ngx_test_null(ctx->boundary_header.data, ngx_palloc(r->pool, len),
202 NGX_ERROR);
203
204 boundary = ngx_next_temp_number(0);
205
206 if (r->headers_out.charset.len) {
207 ctx->boundary_header.len =
208 ngx_snprintf(ctx->boundary_header.data, len,
209 CRLF "--%010u" CRLF
210 "Content-Type: %s; charset=%s" CRLF
211 "Content-Range: bytes ",
212 boundary,
213 r->headers_out.content_type->value.data,
214 r->headers_out.charset.data);
215
216 r->headers_out.charset.len = 0;
217
218 } else {
219 ctx->boundary_header.len =
220 ngx_snprintf(ctx->boundary_header.data, len,
221 CRLF "--%010u" CRLF
222 "Content-Type: %s" CRLF
223 "Content-Range: bytes ",
224 boundary,
225 r->headers_out.content_type->value.data);
226 }
227
228 ngx_test_null(r->headers_out.content_type->value.data,
229 ngx_palloc(r->pool, 31 + 10 + 1),
230 NGX_ERROR);
231
232 r->headers_out.content_type->value.len =
233 ngx_snprintf(r->headers_out.content_type->value.data,
234 31 + 10 + 1,
235 "multipart/byteranges; boundary=%010u",
236 boundary);
237
238 /* the last "CRLF--BOUNDARY--CRLF" */
239 len = 4 + 10 + 4;
240
241 range = r->headers_out.ranges.elts;
242 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
243 ngx_test_null(range[i].content_range.data,
244 ngx_palloc(r->pool, 20 + 1 + 20 + 1 + 20 + 5),
245 NGX_ERROR);
246
247 range[i].content_range.len =
248 ngx_snprintf(range[i].content_range.data,
249 20 + 1 + 20 + 1 + 20 + 5,
250 OFF_FMT "-" OFF_FMT "/" OFF_FMT CRLF CRLF,
251 range[i].start, range[i].end - 1,
252 r->headers_out.content_length);
253
254 len += ctx->boundary_header.len + range[i].content_range.len
255 + range[i].end - range[i].start;
256 }
257
258 r->headers_out.content_length = len;
259 }
260 }
261
262 return next_header_filter(r);
263 }
264
265
266 static int ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
267 {
268 int i;
269 ngx_hunk_t *h;
270 ngx_chain_t *out, *hce, *rce, *dce, **le;
271 ngx_http_range_t *range;
272 ngx_http_range_filter_ctx_t *ctx;
273
274 if (r->headers_out.ranges.nelts == 0) {
275 return next_body_filter(r, in);
276 }
277
278 /* the optimized version for the static files only
279 that are passed in the single file hunk */
280
281 if (in
282 && in->hunk->type & NGX_HUNK_FILE
283 && in->hunk->type & NGX_HUNK_LAST)
284 {
285 if (r->headers_out.ranges.nelts == 1) {
286 range = r->headers_out.ranges.elts;
287 in->hunk->file_pos = range->start;
288 in->hunk->file_last = range->end;
289
290 return next_body_filter(r, in);
291 }
292
293 ctx = ngx_http_get_module_ctx(r, ngx_http_range_filter_module);
294 le = &out;
295
296 range = r->headers_out.ranges.elts;
297 for (i = 0; i < r->headers_out.ranges.nelts; i++) {
298
299 ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
300 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_MEMORY;
301 h->pos = ctx->boundary_header.data;
302 h->last = ctx->boundary_header.data + ctx->boundary_header.len;
303
304 ngx_test_null(hce, ngx_alloc_chain_entry(r->pool), NGX_ERROR);
305 hce->hunk = h;
306
307 ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
308 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
309 h->pos = range[i].content_range.data;
310 h->last = range[i].content_range.data + range[i].content_range.len;
311
312 ngx_test_null(rce, ngx_alloc_chain_entry(r->pool), NGX_ERROR);
313 rce->hunk = h;
314
315 ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
316 h->type = NGX_HUNK_FILE;
317 h->file_pos = range[i].start;
318 h->file_last = range[i].end;
319 h->file = in->hunk->file;
320
321 ngx_test_null(dce, ngx_alloc_chain_entry(r->pool), NGX_ERROR);
322 dce->hunk = h;
323 dce->next = NULL;
324
325 *le = hce;
326 hce->next = rce;
327 rce->next = dce;
328 le = &dce->next;
329 }
330
331 ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
332 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP|NGX_HUNK_LAST;
333 ngx_test_null(h->pos, ngx_palloc(r->pool, 4 + 10 + 4), NGX_ERROR);
334 h->last = ngx_cpymem(h->pos, ctx->boundary_header.data, 4 + 10);
335 *h->last++ = '-'; *h->last++ = '-';
336 *h->last++ = CR; *h->last++ = LF;
337
338 ngx_test_null(hce, ngx_alloc_chain_entry(r->pool), NGX_ERROR);
339 hce->hunk = h;
340 hce->next = NULL;
341 *le = hce;
342
343 return next_body_filter(r, out);
344 }
345
346 /* TODO: several incoming hunks of proxied responses
347 and memory hunks on platforms that have no sendfile() */
348
349 return next_body_filter(r, in);
350 }
351
352
353 static int ngx_http_range_filter_init(ngx_pool_t *pool)
354 {
355 next_header_filter = ngx_http_top_header_filter;
356 ngx_http_top_header_filter = ngx_http_range_header_filter;
357
358 next_body_filter = ngx_http_top_body_filter;
359 ngx_http_top_body_filter = ngx_http_range_body_filter;
360
361 return NGX_OK;
362 }