Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 
0013 static ngx_int_t ngx_http_write_filter_init(ngx_conf_t *cf);
0014 
0015 
0016 static ngx_http_module_t  ngx_http_write_filter_module_ctx = {
0017     NULL,                                  /* preconfiguration */
0018     ngx_http_write_filter_init,            /* postconfiguration */
0019 
0020     NULL,                                  /* create main configuration */
0021     NULL,                                  /* init main configuration */
0022 
0023     NULL,                                  /* create server configuration */
0024     NULL,                                  /* merge server configuration */
0025 
0026     NULL,                                  /* create location configuration */
0027     NULL,                                  /* merge location configuration */
0028 };
0029 
0030 
0031 ngx_module_t  ngx_http_write_filter_module = {
0032     NGX_MODULE_V1,
0033     &ngx_http_write_filter_module_ctx,     /* module context */
0034     NULL,                                  /* module directives */
0035     NGX_HTTP_MODULE,                       /* module type */
0036     NULL,                                  /* init master */
0037     NULL,                                  /* init module */
0038     NULL,                                  /* init process */
0039     NULL,                                  /* init thread */
0040     NULL,                                  /* exit thread */
0041     NULL,                                  /* exit process */
0042     NULL,                                  /* exit master */
0043     NGX_MODULE_V1_PADDING
0044 };
0045 
0046 
0047 ngx_int_t
0048 ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
0049 {
0050     off_t                      size, sent, nsent, limit;
0051     ngx_uint_t                 last, flush, sync;
0052     ngx_msec_t                 delay;
0053     ngx_chain_t               *cl, *ln, **ll, *chain;
0054     ngx_connection_t          *c;
0055     ngx_http_core_loc_conf_t  *clcf;
0056 
0057     c = r->connection;
0058 
0059     if (c->error) {
0060         return NGX_ERROR;
0061     }
0062 
0063     size = 0;
0064     flush = 0;
0065     sync = 0;
0066     last = 0;
0067     ll = &r->out;
0068 
0069     /* find the size, the flush point and the last link of the saved chain */
0070 
0071     for (cl = r->out; cl; cl = cl->next) {
0072         ll = &cl->next;
0073 
0074         ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0,
0075                        "write old buf t:%d f:%d %p, pos %p, size: %z "
0076                        "file: %O, size: %O",
0077                        cl->buf->temporary, cl->buf->in_file,
0078                        cl->buf->start, cl->buf->pos,
0079                        cl->buf->last - cl->buf->pos,
0080                        cl->buf->file_pos,
0081                        cl->buf->file_last - cl->buf->file_pos);
0082 
0083         if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
0084             ngx_log_error(NGX_LOG_ALERT, c->log, 0,
0085                           "zero size buf in writer "
0086                           "t:%d r:%d f:%d %p %p-%p %p %O-%O",
0087                           cl->buf->temporary,
0088                           cl->buf->recycled,
0089                           cl->buf->in_file,
0090                           cl->buf->start,
0091                           cl->buf->pos,
0092                           cl->buf->last,
0093                           cl->buf->file,
0094                           cl->buf->file_pos,
0095                           cl->buf->file_last);
0096 
0097             ngx_debug_point();
0098             return NGX_ERROR;
0099         }
0100 
0101         if (ngx_buf_size(cl->buf) < 0) {
0102             ngx_log_error(NGX_LOG_ALERT, c->log, 0,
0103                           "negative size buf in writer "
0104                           "t:%d r:%d f:%d %p %p-%p %p %O-%O",
0105                           cl->buf->temporary,
0106                           cl->buf->recycled,
0107                           cl->buf->in_file,
0108                           cl->buf->start,
0109                           cl->buf->pos,
0110                           cl->buf->last,
0111                           cl->buf->file,
0112                           cl->buf->file_pos,
0113                           cl->buf->file_last);
0114 
0115             ngx_debug_point();
0116             return NGX_ERROR;
0117         }
0118 
0119         size += ngx_buf_size(cl->buf);
0120 
0121         if (cl->buf->flush || cl->buf->recycled) {
0122             flush = 1;
0123         }
0124 
0125         if (cl->buf->sync) {
0126             sync = 1;
0127         }
0128 
0129         if (cl->buf->last_buf) {
0130             last = 1;
0131         }
0132     }
0133 
0134     /* add the new chain to the existent one */
0135 
0136     for (ln = in; ln; ln = ln->next) {
0137         cl = ngx_alloc_chain_link(r->pool);
0138         if (cl == NULL) {
0139             return NGX_ERROR;
0140         }
0141 
0142         cl->buf = ln->buf;
0143         *ll = cl;
0144         ll = &cl->next;
0145 
0146         ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0,
0147                        "write new buf t:%d f:%d %p, pos %p, size: %z "
0148                        "file: %O, size: %O",
0149                        cl->buf->temporary, cl->buf->in_file,
0150                        cl->buf->start, cl->buf->pos,
0151                        cl->buf->last - cl->buf->pos,
0152                        cl->buf->file_pos,
0153                        cl->buf->file_last - cl->buf->file_pos);
0154 
0155         if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
0156             ngx_log_error(NGX_LOG_ALERT, c->log, 0,
0157                           "zero size buf in writer "
0158                           "t:%d r:%d f:%d %p %p-%p %p %O-%O",
0159                           cl->buf->temporary,
0160                           cl->buf->recycled,
0161                           cl->buf->in_file,
0162                           cl->buf->start,
0163                           cl->buf->pos,
0164                           cl->buf->last,
0165                           cl->buf->file,
0166                           cl->buf->file_pos,
0167                           cl->buf->file_last);
0168 
0169             ngx_debug_point();
0170             return NGX_ERROR;
0171         }
0172 
0173         if (ngx_buf_size(cl->buf) < 0) {
0174             ngx_log_error(NGX_LOG_ALERT, c->log, 0,
0175                           "negative size buf in writer "
0176                           "t:%d r:%d f:%d %p %p-%p %p %O-%O",
0177                           cl->buf->temporary,
0178                           cl->buf->recycled,
0179                           cl->buf->in_file,
0180                           cl->buf->start,
0181                           cl->buf->pos,
0182                           cl->buf->last,
0183                           cl->buf->file,
0184                           cl->buf->file_pos,
0185                           cl->buf->file_last);
0186 
0187             ngx_debug_point();
0188             return NGX_ERROR;
0189         }
0190 
0191         size += ngx_buf_size(cl->buf);
0192 
0193         if (cl->buf->flush || cl->buf->recycled) {
0194             flush = 1;
0195         }
0196 
0197         if (cl->buf->sync) {
0198             sync = 1;
0199         }
0200 
0201         if (cl->buf->last_buf) {
0202             last = 1;
0203         }
0204     }
0205 
0206     *ll = NULL;
0207 
0208     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
0209                    "http write filter: l:%ui f:%ui s:%O", last, flush, size);
0210 
0211     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
0212 
0213     /*
0214      * avoid the output if there are no last buf, no flush point,
0215      * there are the incoming bufs and the size of all bufs
0216      * is smaller than "postpone_output" directive
0217      */
0218 
0219     if (!last && !flush && in && size < (off_t) clcf->postpone_output) {
0220         return NGX_OK;
0221     }
0222 
0223     if (c->write->delayed) {
0224         c->buffered |= NGX_HTTP_WRITE_BUFFERED;
0225         return NGX_AGAIN;
0226     }
0227 
0228     if (size == 0
0229         && !(c->buffered & NGX_LOWLEVEL_BUFFERED)
0230         && !(last && c->need_last_buf))
0231     {
0232         if (last || flush || sync) {
0233             for (cl = r->out; cl; /* void */) {
0234                 ln = cl;
0235                 cl = cl->next;
0236                 ngx_free_chain(r->pool, ln);
0237             }
0238 
0239             r->out = NULL;
0240             c->buffered &= ~NGX_HTTP_WRITE_BUFFERED;
0241 
0242             return NGX_OK;
0243         }
0244 
0245         ngx_log_error(NGX_LOG_ALERT, c->log, 0,
0246                       "the http output chain is empty");
0247 
0248         ngx_debug_point();
0249 
0250         return NGX_ERROR;
0251     }
0252 
0253     if (r->limit_rate) {
0254         if (r->limit_rate_after == 0) {
0255             r->limit_rate_after = clcf->limit_rate_after;
0256         }
0257 
0258         limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1)
0259                 - (c->sent - r->limit_rate_after);
0260 
0261         if (limit <= 0) {
0262             c->write->delayed = 1;
0263             delay = (ngx_msec_t) (- limit * 1000 / r->limit_rate + 1);
0264             ngx_add_timer(c->write, delay);
0265 
0266             c->buffered |= NGX_HTTP_WRITE_BUFFERED;
0267 
0268             return NGX_AGAIN;
0269         }
0270 
0271         if (clcf->sendfile_max_chunk
0272             && (off_t) clcf->sendfile_max_chunk < limit)
0273         {
0274             limit = clcf->sendfile_max_chunk;
0275         }
0276 
0277     } else {
0278         limit = clcf->sendfile_max_chunk;
0279     }
0280 
0281     sent = c->sent;
0282 
0283     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
0284                    "http write filter limit %O", limit);
0285 
0286     chain = c->send_chain(c, r->out, limit);
0287 
0288     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
0289                    "http write filter %p", chain);
0290 
0291     if (chain == NGX_CHAIN_ERROR) {
0292         c->error = 1;
0293         return NGX_ERROR;
0294     }
0295 
0296     if (r->limit_rate) {
0297 
0298         nsent = c->sent;
0299 
0300         if (r->limit_rate_after) {
0301 
0302             sent -= r->limit_rate_after;
0303             if (sent < 0) {
0304                 sent = 0;
0305             }
0306 
0307             nsent -= r->limit_rate_after;
0308             if (nsent < 0) {
0309                 nsent = 0;
0310             }
0311         }
0312 
0313         delay = (ngx_msec_t) ((nsent - sent) * 1000 / r->limit_rate);
0314 
0315         if (delay > 0) {
0316             limit = 0;
0317             c->write->delayed = 1;
0318             ngx_add_timer(c->write, delay);
0319         }
0320     }
0321 
0322     if (limit
0323         && c->write->ready
0324         && c->sent - sent >= limit - (off_t) (2 * ngx_pagesize))
0325     {
0326         c->write->delayed = 1;
0327         ngx_add_timer(c->write, 1);
0328     }
0329 
0330     for (cl = r->out; cl && cl != chain; /* void */) {
0331         ln = cl;
0332         cl = cl->next;
0333         ngx_free_chain(r->pool, ln);
0334     }
0335 
0336     r->out = chain;
0337 
0338     if (chain) {
0339         c->buffered |= NGX_HTTP_WRITE_BUFFERED;
0340         return NGX_AGAIN;
0341     }
0342 
0343     c->buffered &= ~NGX_HTTP_WRITE_BUFFERED;
0344 
0345     if ((c->buffered & NGX_LOWLEVEL_BUFFERED) && r->postponed == NULL) {
0346         return NGX_AGAIN;
0347     }
0348 
0349     return NGX_OK;
0350 }
0351 
0352 
0353 static ngx_int_t
0354 ngx_http_write_filter_init(ngx_conf_t *cf)
0355 {
0356     ngx_http_top_body_filter = ngx_http_write_filter;
0357 
0358     return NGX_OK;
0359 }