comparison src/http/modules/ngx_http_gzip_filter.c @ 343:6bdf858bff8c

nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
author Igor Sysoev <igor@sysoev.ru>
date Fri, 28 May 2004 15:49:23 +0000
parents d4241d7787fe
children e366ba5db8f8
comparison
equal deleted inserted replaced
342:0ee0642af5f1 343:6bdf858bff8c
37 ngx_chain_t *in; 37 ngx_chain_t *in;
38 ngx_chain_t *free; 38 ngx_chain_t *free;
39 ngx_chain_t *busy; 39 ngx_chain_t *busy;
40 ngx_chain_t *out; 40 ngx_chain_t *out;
41 ngx_chain_t **last_out; 41 ngx_chain_t **last_out;
42 ngx_hunk_t *in_hunk; 42 ngx_buf_t *in_buf;
43 ngx_hunk_t *out_hunk; 43 ngx_buf_t *out_buf;
44 ngx_int_t hunks; 44 ngx_int_t bufs;
45 45
46 off_t length; 46 off_t length;
47 47
48 void *preallocated; 48 void *preallocated;
49 char *free_mem; 49 char *free_mem;
405 405
406 static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in) 406 static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
407 { 407 {
408 int rc, wbits, memlevel, last; 408 int rc, wbits, memlevel, last;
409 struct gztrailer *trailer; 409 struct gztrailer *trailer;
410 ngx_hunk_t *h; 410 ngx_buf_t *b;
411 ngx_chain_t *cl; 411 ngx_chain_t *cl;
412 ngx_http_gzip_ctx_t *ctx; 412 ngx_http_gzip_ctx_t *ctx;
413 ngx_http_gzip_conf_t *conf; 413 ngx_http_gzip_conf_t *conf;
414 414
415 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module); 415 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
463 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 463 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
464 "deflateInit2() failed: %d", rc); 464 "deflateInit2() failed: %d", rc);
465 return ngx_http_gzip_error(ctx); 465 return ngx_http_gzip_error(ctx);
466 } 466 }
467 467
468 ngx_test_null(h, ngx_calloc_hunk(r->pool), ngx_http_gzip_error(ctx)); 468 ngx_test_null(b, ngx_calloc_buf(r->pool), ngx_http_gzip_error(ctx));
469 469
470 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_MEMORY; 470 b->memory = 1;
471 h->pos = gzheader; 471 b->pos = gzheader;
472 h->last = h->pos + 10; 472 b->last = b->pos + 10;
473 473
474 ngx_alloc_link_and_set_hunk(cl, h, r->pool, ngx_http_gzip_error(ctx)); 474 ngx_alloc_link_and_set_buf(cl, b, r->pool, ngx_http_gzip_error(ctx));
475 ctx->out = cl; 475 ctx->out = cl;
476 ctx->last_out = &cl->next; 476 ctx->last_out = &cl->next;
477 477
478 ctx->crc32 = crc32(0L, Z_NULL, 0); 478 ctx->crc32 = crc32(0L, Z_NULL, 0);
479 ctx->flush = Z_NO_FLUSH; 479 ctx->flush = Z_NO_FLUSH;
502 502
503 if (ctx->in == NULL) { 503 if (ctx->in == NULL) {
504 break; 504 break;
505 } 505 }
506 506
507 ctx->in_hunk = ctx->in->hunk; 507 ctx->in_buf = ctx->in->buf;
508 ctx->in = ctx->in->next; 508 ctx->in = ctx->in->next;
509 509
510 ctx->zstream.next_in = ctx->in_hunk->pos; 510 ctx->zstream.next_in = ctx->in_buf->pos;
511 ctx->zstream.avail_in = ctx->in_hunk->last - ctx->in_hunk->pos; 511 ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
512 512
513 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 513 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
514 "gzip in_hunk:" PTR_FMT " ni:" PTR_FMT " ai:%d", 514 "gzip in_buf:" PTR_FMT " ni:" PTR_FMT " ai:%d",
515 ctx->in_hunk, 515 ctx->in_buf,
516 ctx->zstream.next_in, ctx->zstream.avail_in); 516 ctx->zstream.next_in, ctx->zstream.avail_in);
517 517
518 /* STUB */ 518 /* STUB */
519 if (ctx->in_hunk->last < ctx->in_hunk->pos) { 519 if (ctx->in_buf->last < ctx->in_buf->pos) {
520 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 520 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
521 "zstream.avail_in is huge"); 521 "zstream.avail_in is huge");
522 ctx->done = 1; 522 ctx->done = 1;
523 return NGX_ERROR; 523 return NGX_ERROR;
524 } 524 }
525 525
526 if (ctx->in_hunk->type & NGX_HUNK_LAST) { 526 if (ctx->in_buf->last_buf) {
527 ctx->flush = Z_FINISH; 527 ctx->flush = Z_FINISH;
528 528
529 } else if (ctx->in_hunk->type & NGX_HUNK_FLUSH) { 529 } else if (ctx->in_buf->flush) {
530 ctx->flush = Z_SYNC_FLUSH; 530 ctx->flush = Z_SYNC_FLUSH;
531 } 531 }
532 532
533 if (ctx->zstream.avail_in == 0) { 533 if (ctx->zstream.avail_in == 0) {
534 if (ctx->flush == Z_NO_FLUSH) { 534 if (ctx->flush == Z_NO_FLUSH) {
543 543
544 /* is there a space for the gzipped data ? */ 544 /* is there a space for the gzipped data ? */
545 545
546 if (ctx->zstream.avail_out == 0) { 546 if (ctx->zstream.avail_out == 0) {
547 if (ctx->free) { 547 if (ctx->free) {
548 ctx->out_hunk = ctx->free->hunk; 548 ctx->out_buf = ctx->free->buf;
549 ctx->free = ctx->free->next; 549 ctx->free = ctx->free->next;
550 550
551 } else if (ctx->hunks < conf->bufs.num) { 551 } else if (ctx->bufs < conf->bufs.num) {
552 ngx_test_null(ctx->out_hunk, 552 ngx_test_null(ctx->out_buf,
553 ngx_create_temp_hunk(r->pool, conf->bufs.size), 553 ngx_create_temp_buf(r->pool, conf->bufs.size),
554 ngx_http_gzip_error(ctx)); 554 ngx_http_gzip_error(ctx));
555 ctx->out_hunk->tag = (ngx_hunk_tag_t) 555 ctx->out_buf->tag = (ngx_buf_tag_t)
556 &ngx_http_gzip_filter_module; 556 &ngx_http_gzip_filter_module;
557 ctx->out_hunk->type |= NGX_HUNK_RECYCLED; 557 ctx->out_buf->recycled = 1;
558 ctx->hunks++; 558 ctx->bufs++;
559 559
560 } else { 560 } else {
561 ctx->blocked = 1; 561 ctx->blocked = 1;
562 break; 562 break;
563 } 563 }
564 564
565 ctx->blocked = 0; 565 ctx->blocked = 0;
566 ctx->zstream.next_out = ctx->out_hunk->pos; 566 ctx->zstream.next_out = ctx->out_buf->pos;
567 ctx->zstream.avail_out = conf->bufs.size; 567 ctx->zstream.avail_out = conf->bufs.size;
568 } 568 }
569 569
570 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 570 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
571 "deflate in: ni:%X no:%X ai:%d ao:%d fl:%d redo:%d", 571 "deflate in: ni:%X no:%X ai:%d ao:%d fl:%d redo:%d",
585 ctx->zstream.next_in, ctx->zstream.next_out, 585 ctx->zstream.next_in, ctx->zstream.next_out,
586 ctx->zstream.avail_in, ctx->zstream.avail_out, 586 ctx->zstream.avail_in, ctx->zstream.avail_out,
587 rc); 587 rc);
588 588
589 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 589 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
590 "gzip in_hunk:" PTR_FMT " pos:" PTR_FMT, 590 "gzip in_buf:" PTR_FMT " pos:" PTR_FMT,
591 ctx->in_hunk, ctx->in_hunk->pos); 591 ctx->in_buf, ctx->in_buf->pos);
592 592
593 593
594 if (ctx->zstream.next_in) { 594 if (ctx->zstream.next_in) {
595 ctx->in_hunk->pos = ctx->zstream.next_in; 595 ctx->in_buf->pos = ctx->zstream.next_in;
596 596
597 if (ctx->zstream.avail_in == 0) { 597 if (ctx->zstream.avail_in == 0) {
598 ctx->zstream.next_in = NULL; 598 ctx->zstream.next_in = NULL;
599 } 599 }
600 } 600 }
601 601
602 ctx->out_hunk->last = ctx->zstream.next_out; 602 ctx->out_buf->last = ctx->zstream.next_out;
603 603
604 if (ctx->zstream.avail_out == 0) { 604 if (ctx->zstream.avail_out == 0) {
605 ngx_alloc_link_and_set_hunk(cl, ctx->out_hunk, r->pool, 605 ngx_alloc_link_and_set_buf(cl, ctx->out_buf, r->pool,
606 ngx_http_gzip_error(ctx)); 606 ngx_http_gzip_error(ctx));
607 *ctx->last_out = cl; 607 *ctx->last_out = cl;
608 ctx->last_out = &cl->next; 608 ctx->last_out = &cl->next;
609 ctx->redo = 1; 609 ctx->redo = 1;
610 610
611 continue; 611 continue;
612 } 612 }
613 613
614 ctx->redo = 0; 614 ctx->redo = 0;
615 615
616 if (ctx->flush == Z_SYNC_FLUSH) { 616 if (ctx->flush == Z_SYNC_FLUSH) {
617 ctx->out_hunk->type |= NGX_HUNK_FLUSH; 617 ctx->out_buf->flush = 0;
618 ctx->flush = Z_NO_FLUSH; 618 ctx->flush = Z_NO_FLUSH;
619 619
620 ngx_alloc_link_and_set_hunk(cl, ctx->out_hunk, r->pool, 620 ngx_alloc_link_and_set_buf(cl, ctx->out_buf, r->pool,
621 ngx_http_gzip_error(ctx)); 621 ngx_http_gzip_error(ctx));
622 *ctx->last_out = cl; 622 *ctx->last_out = cl;
623 ctx->last_out = &cl->next; 623 ctx->last_out = &cl->next;
624 ctx->pass = 1; 624 ctx->pass = 1;
625 625
626 break; 626 break;
638 return ngx_http_gzip_error(ctx); 638 return ngx_http_gzip_error(ctx);
639 } 639 }
640 640
641 ngx_pfree(r->pool, ctx->preallocated); 641 ngx_pfree(r->pool, ctx->preallocated);
642 642
643 ngx_alloc_link_and_set_hunk(cl, ctx->out_hunk, r->pool, 643 ngx_alloc_link_and_set_buf(cl, ctx->out_buf, r->pool,
644 ngx_http_gzip_error(ctx)); 644 ngx_http_gzip_error(ctx));
645 *ctx->last_out = cl; 645 *ctx->last_out = cl;
646 ctx->last_out = &cl->next; 646 ctx->last_out = &cl->next;
647 647
648 if (ctx->zstream.avail_out >= 8) { 648 if (ctx->zstream.avail_out >= 8) {
649 trailer = (struct gztrailer *) ctx->out_hunk->last; 649 trailer = (struct gztrailer *) ctx->out_buf->last;
650 ctx->out_hunk->type |= NGX_HUNK_LAST; 650 ctx->out_buf->last += 8;
651 ctx->out_hunk->last += 8; 651 ctx->out_buf->last_buf = 1;
652 652
653 } else { 653 } else {
654 ngx_test_null(h, ngx_create_temp_hunk(r->pool, 8), 654 ngx_test_null(b, ngx_create_temp_buf(r->pool, 8),
655 ngx_http_gzip_error(ctx)); 655 ngx_http_gzip_error(ctx));
656 656
657 h->type |= NGX_HUNK_LAST; 657 b->last_buf = 1;
658 658
659 ngx_alloc_link_and_set_hunk(cl, h, r->pool, 659 ngx_alloc_link_and_set_buf(cl, b, r->pool,
660 ngx_http_gzip_error(ctx)); 660 ngx_http_gzip_error(ctx));
661 *ctx->last_out = cl; 661 *ctx->last_out = cl;
662 ctx->last_out = &cl->next; 662 ctx->last_out = &cl->next;
663 trailer = (struct gztrailer *) h->pos; 663 trailer = (struct gztrailer *) b->pos;
664 h->last += 8; 664 b->last += 8;
665 } 665 }
666 666
667 #if (HAVE_LITTLE_ENDIAN) 667 #if (HAVE_LITTLE_ENDIAN)
668 trailer->crc32 = ctx->crc32; 668 trailer->crc32 = ctx->crc32;
669 trailer->zlen = ctx->zin; 669 trailer->zlen = ctx->zin;
679 679
680 break; 680 break;
681 } 681 }
682 682
683 if (conf->no_buffer && ctx->in == NULL) { 683 if (conf->no_buffer && ctx->in == NULL) {
684 ngx_alloc_link_and_set_hunk(cl, ctx->out_hunk, r->pool, 684 ngx_alloc_link_and_set_buf(cl, ctx->out_buf, r->pool,
685 ngx_http_gzip_error(ctx)); 685 ngx_http_gzip_error(ctx));
686 *ctx->last_out = cl; 686 *ctx->last_out = cl;
687 ctx->last_out = &cl->next; 687 ctx->last_out = &cl->next;
688 ctx->pass = 1; 688 ctx->pass = 1;
689 689
690 break; 690 break;
717 if (last == NGX_ERROR) { 717 if (last == NGX_ERROR) {
718 return ngx_http_gzip_error(ctx); 718 return ngx_http_gzip_error(ctx);
719 } 719 }
720 720
721 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out, 721 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out,
722 (ngx_hunk_tag_t) &ngx_http_gzip_filter_module); 722 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
723 ctx->last_out = &ctx->out; 723 ctx->last_out = &ctx->out;
724 724
725 if (ctx->done) { 725 if (ctx->done) {
726 return last; 726 return last;
727 } 727 }