comparison src/http/modules/perl/nginx.xs @ 1454:f497ed7682a7

open_file_cache in HTTP
author Igor Sysoev <igor@sysoev.ru>
date Sat, 01 Sep 2007 12:12:48 +0000
parents 37938e68910b
children 223e92651ca5
comparison
equal deleted inserted replaced
1453:f2feed5bffe1 1454:f497ed7682a7
603 603
604 void 604 void
605 sendfile(r, filename, offset = -1, bytes = 0) 605 sendfile(r, filename, offset = -1, bytes = 0)
606 CODE: 606 CODE:
607 607
608 ngx_http_request_t *r; 608 ngx_http_request_t *r;
609 char *filename; 609 char *filename;
610 int offset; 610 int offset;
611 size_t bytes; 611 size_t bytes;
612 ngx_fd_t fd; 612 ngx_int_t rc;
613 ngx_buf_t *b; 613 ngx_str_t path;
614 ngx_file_info_t fi; 614 ngx_buf_t *b;
615 ngx_pool_cleanup_t *cln; 615 ngx_open_file_info_t of;
616 ngx_pool_cleanup_file_t *clnf; 616 ngx_http_core_loc_conf_t *clcf;
617 617
618 ngx_http_perl_set_request(r); 618 ngx_http_perl_set_request(r);
619 619
620 filename = SvPV_nolen(ST(1)); 620 filename = SvPV_nolen(ST(1));
621 621
634 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); 634 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
635 if (b->file == NULL) { 635 if (b->file == NULL) {
636 XSRETURN_EMPTY; 636 XSRETURN_EMPTY;
637 } 637 }
638 638
639 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)); 639 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
640 if (cln == NULL) { 640
641 XSRETURN_EMPTY; 641 of.test_dir = 0;
642 } 642 of.retest = clcf->open_file_cache_retest;
643 643 of.errors = clcf->open_file_cache_errors;
644 fd = ngx_open_file((u_char *) filename, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); 644
645 645 path.len = ngx_strlen(filename);
646 if (fd == NGX_INVALID_FILE) { 646
647 path.data = ngx_pcalloc(r->pool, path.len + 1);
648 if (path.data == NULL) {
649 XSRETURN_EMPTY;
650 }
651
652 (void) ngx_cpystrn(path.data, filename, path.len + 1);
653
654 rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool);
655
656 if (rc == NGX_ERROR) {
657
658 if (of.err == 0) {
659 XSRETURN_EMPTY;
660 }
661
647 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, 662 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
648 ngx_open_file_n " \"%s\" failed", filename); 663 ngx_open_file_n " \"%s\" failed", filename);
649 XSRETURN_EMPTY; 664 XSRETURN_EMPTY;
650 } 665 }
651 666
652 if (offset == -1) { 667 if (offset == -1) {
653 offset = 0; 668 offset = 0;
654 } 669 }
655 670
656 if (bytes == 0) { 671 if (bytes == 0) {
657 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { 672 bytes = of.size - offset;
658 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, 673 }
659 ngx_fd_info_n " \"%s\" failed", filename);
660
661 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
662 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
663 ngx_close_file_n " \"%s\" failed", filename);
664 }
665
666 XSRETURN_EMPTY;
667 }
668
669 bytes = ngx_file_size(&fi) - offset;
670 }
671
672 cln->handler = ngx_pool_cleanup_file;
673 clnf = cln->data;
674
675 clnf->fd = fd;
676 clnf->name = (u_char *) "";
677 clnf->log = r->pool->log;
678 674
679 b->in_file = 1; 675 b->in_file = 1;
680 676
681 b->file_pos = offset; 677 b->file_pos = offset;
682 b->file_last = offset + bytes; 678 b->file_last = offset + bytes;
683 679
684 b->file->fd = fd; 680 b->file->fd = of.fd;
685 b->file->log = r->connection->log; 681 b->file->log = r->connection->log;
686 682
687 (void) ngx_http_perl_output(r, b); 683 (void) ngx_http_perl_output(r, b);
688 684
689 685