Exemplo n.º 1
0
ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
  int                           last;
  off_t                         size, flush, sent;
  ngx_chain_t                  *cl, *ln, **ll, *chain;
  ngx_connection_t             *c;
  ngx_http_core_loc_conf_t     *clcf;
  ngx_http_write_filter_ctx_t  *ctx;

  ctx = ngx_http_get_module_ctx(r->main ? r->main : r,
                  ngx_http_write_filter_module);

  if (ctx == NULL) {
    ngx_http_create_ctx(r, ctx, ngx_http_write_filter_module,
              sizeof(ngx_http_write_filter_ctx_t), NGX_ERROR);
  }

  size = 0;
  flush = 0;
  last = 0;
  ll = &ctx->out;

  /* find the size, the flush point and the last link of the saved chain */

  for (cl = ctx->out; cl; cl = cl->next) {
    ll = &cl->next;

    size += ngx_buf_size(cl->buf);

    if (cl->buf->flush || cl->buf->recycled) {
      flush = size;
    }

    if (cl->buf->last_buf) {
      last = 1;
    }
  }

  /* add the new chain to the existent one */

  for (ln = in; ln; ln = ln->next) {
    ngx_alloc_link_and_set_buf(cl, ln->buf, r->pool, NGX_ERROR);
    *ll = cl;
    ll = &cl->next;

    size += ngx_buf_size(cl->buf);

    if (cl->buf->flush || cl->buf->recycled) {
      flush = size;
    }

    if (cl->buf->last_buf) {
      last = 1;
    }
  }

  c = r->connection;

  ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
           "http write filter: l:%d f:" OFF_T_FMT " s:" OFF_T_FMT,
           last, flush, size);

  clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
                    ngx_http_core_module);

  /*
   * avoid the output if there is no last buf, no flush point,
   * there are the incoming bufs and the size of all bufs
   * is smaller than "postpone_output" directive
   */

  if (!last && flush == 0 && in && size < (off_t) clcf->postpone_output) {
    return NGX_OK;
  }

  if (c->write->delayed) {
    return NGX_AGAIN;
  }

  if (size == 0 && !c->buffered) {
    if (!last) {
      ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
              "the http output chain is empty");
    }
    return NGX_OK;
  }

  sent = c->sent;

  chain = c->send_chain(c, ctx->out,
              clcf->limit_rate ? clcf->limit_rate: OFF_T_MAX_VALUE);

  ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
           "http write filter %X", chain);

  if (clcf->limit_rate) {
    sent = c->sent - sent;
    c->write->delayed = 1;
    ngx_add_timer(r->connection->write,
            (ngx_msec_t) (sent * 1000 / clcf->limit_rate));
  }

  if (chain == NGX_CHAIN_ERROR) {
    return NGX_ERROR;
  }

  ctx->out = chain;

  if (chain || c->buffered) {
    return NGX_AGAIN;
  }

  return NGX_OK;
}
ngx_int_t ngx_http_special_response_handler(ngx_http_request_t *r, int error)
{
    ngx_int_t                  rc;
    ngx_uint_t                 err, i, msie_padding;
    ngx_buf_t                 *b;
    ngx_chain_t               *out, **ll, *cl;
    ngx_http_err_page_t       *err_page;
    ngx_http_core_loc_conf_t  *clcf;

    rc = ngx_http_discard_body(r);

    if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
        error = NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    r->headers_out.status = error;

    if (r->keepalive != 0) {
        switch (error) {
            case NGX_HTTP_BAD_REQUEST:
            case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE:
            case NGX_HTTP_REQUEST_URI_TOO_LARGE:
            case NGX_HTTP_TO_HTTPS:
            case NGX_HTTP_INTERNAL_SERVER_ERROR:
                r->keepalive = 0;
        }
    }

    if (r->lingering_close == 1) {
        switch (error) {
            case NGX_HTTP_BAD_REQUEST:
            case NGX_HTTP_TO_HTTPS:
                r->lingering_close = 0;
        }
    }

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

    if (r->err_ctx == NULL && clcf->error_pages) {
        err_page = clcf->error_pages->elts;
        for (i = 0; i < clcf->error_pages->nelts; i++) {
            if (err_page[i].status == error) {
                if (err_page[i].overwrite) {
                    r->err_status = err_page[i].overwrite;
                } else {
                    r->err_status = error;
                }
                r->err_ctx = r->ctx;
                return ngx_http_internal_redirect(r, &err_page[i].uri, NULL);
            }
        }
    }

    if (error < NGX_HTTP_BAD_REQUEST) {
        /* 3XX */
        err = error - NGX_HTTP_MOVED_PERMANENTLY;

    } else if (error < NGX_HTTP_NGX_CODES) {
        /* 4XX */
        err = error - NGX_HTTP_BAD_REQUEST + 3;

    } else {
        /* 49X, 5XX */
        err = error - NGX_HTTP_NGX_CODES + 3 + 17;

        switch (error) {
            case NGX_HTTP_TO_HTTPS:
                r->headers_out.status = NGX_HTTP_BAD_REQUEST;
                error = NGX_HTTP_BAD_REQUEST;
                break;

            case NGX_HTTP_INVALID_HOST:
                r->headers_out.status = NGX_HTTP_NOT_FOUND;
                error = NGX_HTTP_NOT_FOUND;
                break;
        }
    }

    msie_padding = 0;

    if (error_pages[err].len) {
        r->headers_out.content_length_n = error_pages[err].len
                                          + sizeof(error_tail) - 1;

        if (clcf->msie_padding
            && r->headers_in.msie
            && r->http_version >= NGX_HTTP_VERSION_10
            && error >= NGX_HTTP_BAD_REQUEST
            && error != NGX_HTTP_REQUEST_URI_TOO_LARGE)
        {
            r->headers_out.content_length_n += sizeof(msie_stub) - 1;
            msie_padding = 1;
        }

        r->headers_out.content_type = ngx_list_push(&r->headers_out.headers);
        if (r->headers_out.content_type == NULL) {
            return NGX_ERROR;
        }

        r->headers_out.content_type->key.len = sizeof("Content-Type") - 1;
        r->headers_out.content_type->key.data = (u_char *) "Content-Type";
        r->headers_out.content_type->value.len = sizeof("text/html") - 1;
        r->headers_out.content_type->value.data = (u_char *) "text/html";

    } else {
        r->headers_out.content_length_n = -1;
    }

    if (r->headers_out.content_length) {
        r->headers_out.content_length->key.len = 0;
        r->headers_out.content_length = NULL;
    }

    rc = ngx_http_send_header(r);

    if (rc == NGX_ERROR || r->header_only) {
        return rc;
    }

    if (error_pages[err].len == 0) {
        return NGX_OK;
    }

    out = NULL;
    ll = NULL;

    if (!(b = ngx_calloc_buf(r->pool))) {
        return NGX_ERROR;
    }
    b->memory = 1;
    b->pos = error_pages[err].data;
    b->last = error_pages[err].data + error_pages[err].len;

    ngx_alloc_link_and_set_buf(cl, b, r->pool, NGX_ERROR);
    ngx_chain_add_link(out, ll, cl);


    if (!(b = ngx_calloc_buf(r->pool))) {
        return NGX_ERROR;
    }
    b->memory = 1;
    b->pos = error_tail;
    b->last = error_tail + sizeof(error_tail) - 1;

    ngx_alloc_link_and_set_buf(cl, b, r->pool, NGX_ERROR);
    ngx_chain_add_link(out, ll, cl);

    if (msie_padding) {
        if (!(b = ngx_calloc_buf(r->pool))) {
            return NGX_ERROR;
        }
        b->memory = 1;
        b->pos = msie_stub;
        b->last = msie_stub + sizeof(msie_stub) - 1;

        ngx_alloc_link_and_set_buf(cl, b, r->pool, NGX_ERROR);
        ngx_chain_add_link(out, ll, cl);
    }

    b->last_buf = 1;

    return ngx_http_output_filter(r, out);
}