comparison ngx_http_bytes_filter_module.c @ 3:3e8648918913

Body filter buffers fixup, currently limited to one range only.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 03 Jul 2008 04:21:17 +0400
parents 13a2fbcc8bc4
children ab9f96327bb6
comparison
equal deleted inserted replaced
2:13a2fbcc8bc4 3:3e8648918913
278 278
279 279
280 static ngx_int_t 280 static ngx_int_t
281 ngx_http_bytes_body_filter(ngx_http_request_t *r, ngx_chain_t *in) 281 ngx_http_bytes_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
282 { 282 {
283 off_t size;
283 ngx_uint_t i; 284 ngx_uint_t i;
285 ngx_chain_t *cl, **ll;
286 ngx_buf_t *buf;
284 ngx_http_bytes_ctx_t *ctx; 287 ngx_http_bytes_ctx_t *ctx;
285 ngx_http_bytes_t *range; 288 ngx_http_bytes_t *range;
286 289
290 if (in == NULL) {
291 return ngx_http_next_body_filter(r, in);
292 }
293
287 ctx = ngx_http_get_module_ctx(r, ngx_http_bytes_filter_module); 294 ctx = ngx_http_get_module_ctx(r, ngx_http_bytes_filter_module);
288 295
289 if (ctx == NULL) { 296 if (ctx == NULL) {
297 return ngx_http_next_body_filter(r, in);
298 }
299
300 buf = in->buf;
301
302 if (ngx_buf_special(buf)) {
290 return ngx_http_next_body_filter(r, in); 303 return ngx_http_next_body_filter(r, in);
291 } 304 }
292 305
293 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 306 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
294 "bytes body filter: r %p, in %p", r, in); 307 "bytes body filter: r %p, in %p", r, in);
297 310
298 for (i = 0; i < ctx->ranges.nelts; i++) { 311 for (i = 0; i < ctx->ranges.nelts; i++) {
299 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 312 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
300 "bytes body filter: %O-%O", range->start, range->end); 313 "bytes body filter: %O-%O", range->start, range->end);
301 range++; 314 range++;
315 }
316
317 range = ctx->ranges.elts;
318
319 #if 0
320 /* ... temp, rewrite */
321
322 if (ctx->ranges.nelts == 1) {
323
324 if (buf->in_file) {
325 buf->file_pos = range->start;
326 buf->file_last = range->end;
327 }
328
329 if (ngx_buf_in_memory(buf)) {
330 buf->pos = buf->start + (size_t) range->start;
331 buf->last = buf->start + (size_t) range->end;
332 }
333
334 return ngx_http_next_body_filter(r, in);
335 }
336 #endif
337
338 /* ... */
339
340 for (ll = &in, cl = in; cl; ll = &cl->next, cl = cl->next) {
341
342 buf = cl->buf;
343 size = ngx_buf_size(buf);
344
345 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
346 "bytes body filter: b %d", size);
347
348 if (ngx_buf_special(buf)) {
349 /* pass out anyway */
350 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
351 "bytes body filter: special buffer");
352 continue;
353 }
354
355 if (range->start > ctx->offset + size) {
356 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
357 "bytes body filter: fully ignored buffer");
358 *ll = cl->next;
359 buf->pos = buf->last;
360 continue;
361 }
362
363 /* XXX multiple ranges */
364 /* XXX we should create new buffers/links for multiple ranges */
365
366 if (buf->in_file) {
367
368 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
369 "bytes body filter: in file, %O-%O",
370 buf->file_pos, buf->file_last);
371
372 if (range->start > ctx->offset) {
373 buf->file_pos += range->start - ctx->offset;
374 }
375 if (range->end - ctx->offset < size) {
376 buf->file_last -= size - (range->end - ctx->offset);
377 }
378
379 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
380 "bytes body filter: in file fixed, %O-%O",
381 buf->file_pos, buf->file_last);
382 }
383
384 if (ngx_buf_in_memory(buf)) {
385
386 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
387 "bytes body filter: in memory, %p-%p",
388 buf->pos, buf->last);
389
390 if (range->start > ctx->offset) {
391 buf->pos += (size_t) (range->start - ctx->offset);
392 }
393 if (range->end - ctx->offset < size) {
394 buf->last -= (size_t) (size - (range->end - ctx->offset));
395 }
396
397 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
398 "bytes body filter: in memory fixed, %p-%p",
399 buf->pos, buf->last);
400 }
401
402 ctx->offset += size;
302 } 403 }
303 404
304 return ngx_http_next_body_filter(r, in); 405 return ngx_http_next_body_filter(r, in);
305 } 406 }
306 407