Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.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 #include <nginx.h>
0012 
0013 
0014 static ngx_int_t ngx_http_header_filter_init(ngx_conf_t *cf);
0015 static ngx_int_t ngx_http_header_filter(ngx_http_request_t *r);
0016 
0017 
0018 static ngx_http_module_t  ngx_http_header_filter_module_ctx = {
0019     NULL,                                  /* preconfiguration */
0020     ngx_http_header_filter_init,           /* postconfiguration */
0021 
0022     NULL,                                  /* create main configuration */
0023     NULL,                                  /* init main configuration */
0024 
0025     NULL,                                  /* create server configuration */
0026     NULL,                                  /* merge server configuration */
0027 
0028     NULL,                                  /* create location configuration */
0029     NULL,                                  /* merge location configuration */
0030 };
0031 
0032 
0033 ngx_module_t  ngx_http_header_filter_module = {
0034     NGX_MODULE_V1,
0035     &ngx_http_header_filter_module_ctx,    /* module context */
0036     NULL,                                  /* module directives */
0037     NGX_HTTP_MODULE,                       /* module type */
0038     NULL,                                  /* init master */
0039     NULL,                                  /* init module */
0040     NULL,                                  /* init process */
0041     NULL,                                  /* init thread */
0042     NULL,                                  /* exit thread */
0043     NULL,                                  /* exit process */
0044     NULL,                                  /* exit master */
0045     NGX_MODULE_V1_PADDING
0046 };
0047 
0048 
0049 static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
0050 static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
0051 static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
0052 
0053 
0054 static ngx_str_t ngx_http_status_lines[] = {
0055 
0056     ngx_string("200 OK"),
0057     ngx_string("201 Created"),
0058     ngx_string("202 Accepted"),
0059     ngx_null_string,  /* "203 Non-Authoritative Information" */
0060     ngx_string("204 No Content"),
0061     ngx_null_string,  /* "205 Reset Content" */
0062     ngx_string("206 Partial Content"),
0063 
0064     /* ngx_null_string, */  /* "207 Multi-Status" */
0065 
0066 #define NGX_HTTP_LAST_2XX  207
0067 #define NGX_HTTP_OFF_3XX   (NGX_HTTP_LAST_2XX - 200)
0068 
0069     /* ngx_null_string, */  /* "300 Multiple Choices" */
0070 
0071     ngx_string("301 Moved Permanently"),
0072     ngx_string("302 Moved Temporarily"),
0073     ngx_string("303 See Other"),
0074     ngx_string("304 Not Modified"),
0075     ngx_null_string,  /* "305 Use Proxy" */
0076     ngx_null_string,  /* "306 unused" */
0077     ngx_string("307 Temporary Redirect"),
0078 
0079 #define NGX_HTTP_LAST_3XX  308
0080 #define NGX_HTTP_OFF_4XX   (NGX_HTTP_LAST_3XX - 301 + NGX_HTTP_OFF_3XX)
0081 
0082     ngx_string("400 Bad Request"),
0083     ngx_string("401 Unauthorized"),
0084     ngx_string("402 Payment Required"),
0085     ngx_string("403 Forbidden"),
0086     ngx_string("404 Not Found"),
0087     ngx_string("405 Not Allowed"),
0088     ngx_string("406 Not Acceptable"),
0089     ngx_null_string,  /* "407 Proxy Authentication Required" */
0090     ngx_string("408 Request Time-out"),
0091     ngx_string("409 Conflict"),
0092     ngx_string("410 Gone"),
0093     ngx_string("411 Length Required"),
0094     ngx_string("412 Precondition Failed"),
0095     ngx_string("413 Request Entity Too Large"),
0096     ngx_string("414 Request-URI Too Large"),
0097     ngx_string("415 Unsupported Media Type"),
0098     ngx_string("416 Requested Range Not Satisfiable"),
0099     ngx_null_string,  /* "417 Expectation Failed" */
0100     ngx_null_string,  /* "418 unused" */
0101     ngx_null_string,  /* "419 unused" */
0102     ngx_null_string,  /* "420 unused" */
0103     ngx_string("421 Misdirected Request"),
0104     ngx_null_string,  /* "422 Unprocessable Entity" */
0105     ngx_null_string,  /* "423 Locked" */
0106     ngx_null_string,  /* "424 Failed Dependency" */
0107     ngx_null_string,  /* "425 unused" */
0108     ngx_null_string,  /* "426 Upgrade Required" */
0109     ngx_null_string,  /* "427 unused" */
0110     ngx_null_string,  /* "428 Precondition Required" */
0111     ngx_string("429 Too Many Requests"),
0112 
0113 #define NGX_HTTP_LAST_4XX  430
0114 #define NGX_HTTP_OFF_5XX   (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)
0115 
0116     ngx_string("500 Internal Server Error"),
0117     ngx_string("501 Not Implemented"),
0118     ngx_string("502 Bad Gateway"),
0119     ngx_string("503 Service Temporarily Unavailable"),
0120     ngx_string("504 Gateway Time-out"),
0121     ngx_null_string,        /* "505 HTTP Version Not Supported" */
0122     ngx_null_string,        /* "506 Variant Also Negotiates" */
0123     ngx_string("507 Insufficient Storage"),
0124 
0125     /* ngx_null_string, */  /* "508 unused" */
0126     /* ngx_null_string, */  /* "509 unused" */
0127     /* ngx_null_string, */  /* "510 Not Extended" */
0128 
0129 #define NGX_HTTP_LAST_5XX  508
0130 
0131 };
0132 
0133 
0134 ngx_http_header_out_t  ngx_http_headers_out[] = {
0135     { ngx_string("Server"), offsetof(ngx_http_headers_out_t, server) },
0136     { ngx_string("Date"), offsetof(ngx_http_headers_out_t, date) },
0137     { ngx_string("Content-Length"),
0138                  offsetof(ngx_http_headers_out_t, content_length) },
0139     { ngx_string("Content-Encoding"),
0140                  offsetof(ngx_http_headers_out_t, content_encoding) },
0141     { ngx_string("Location"), offsetof(ngx_http_headers_out_t, location) },
0142     { ngx_string("Last-Modified"),
0143                  offsetof(ngx_http_headers_out_t, last_modified) },
0144     { ngx_string("Accept-Ranges"),
0145                  offsetof(ngx_http_headers_out_t, accept_ranges) },
0146     { ngx_string("Expires"), offsetof(ngx_http_headers_out_t, expires) },
0147     { ngx_string("Cache-Control"),
0148                  offsetof(ngx_http_headers_out_t, cache_control) },
0149     { ngx_string("ETag"), offsetof(ngx_http_headers_out_t, etag) },
0150 
0151     { ngx_null_string, 0 }
0152 };
0153 
0154 
0155 static ngx_int_t
0156 ngx_http_header_filter(ngx_http_request_t *r)
0157 {
0158     u_char                    *p;
0159     size_t                     len;
0160     ngx_str_t                  host, *status_line;
0161     ngx_buf_t                 *b;
0162     ngx_uint_t                 status, i, port;
0163     ngx_chain_t                out;
0164     ngx_list_part_t           *part;
0165     ngx_table_elt_t           *header;
0166     ngx_connection_t          *c;
0167     ngx_http_core_loc_conf_t  *clcf;
0168     ngx_http_core_srv_conf_t  *cscf;
0169     u_char                     addr[NGX_SOCKADDR_STRLEN];
0170 
0171     if (r->header_sent) {
0172         return NGX_OK;
0173     }
0174 
0175     r->header_sent = 1;
0176 
0177     if (r != r->main) {
0178         return NGX_OK;
0179     }
0180 
0181     if (r->http_version < NGX_HTTP_VERSION_10) {
0182         return NGX_OK;
0183     }
0184 
0185     if (r->method == NGX_HTTP_HEAD) {
0186         r->header_only = 1;
0187     }
0188 
0189     if (r->headers_out.last_modified_time != -1) {
0190         if (r->headers_out.status != NGX_HTTP_OK
0191             && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
0192             && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)
0193         {
0194             r->headers_out.last_modified_time = -1;
0195             r->headers_out.last_modified = NULL;
0196         }
0197     }
0198 
0199     len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
0200           /* the end of the header */
0201           + sizeof(CRLF) - 1;
0202 
0203     /* status line */
0204 
0205     if (r->headers_out.status_line.len) {
0206         len += r->headers_out.status_line.len;
0207         status_line = &r->headers_out.status_line;
0208 #if (NGX_SUPPRESS_WARN)
0209         status = 0;
0210 #endif
0211 
0212     } else {
0213 
0214         status = r->headers_out.status;
0215 
0216         if (status >= NGX_HTTP_OK
0217             && status < NGX_HTTP_LAST_2XX)
0218         {
0219             /* 2XX */
0220 
0221             if (status == NGX_HTTP_NO_CONTENT) {
0222                 r->header_only = 1;
0223                 ngx_str_null(&r->headers_out.content_type);
0224                 r->headers_out.last_modified_time = -1;
0225                 r->headers_out.last_modified = NULL;
0226                 r->headers_out.content_length = NULL;
0227                 r->headers_out.content_length_n = -1;
0228             }
0229 
0230             status -= NGX_HTTP_OK;
0231             status_line = &ngx_http_status_lines[status];
0232             len += ngx_http_status_lines[status].len;
0233 
0234         } else if (status >= NGX_HTTP_MOVED_PERMANENTLY
0235                    && status < NGX_HTTP_LAST_3XX)
0236         {
0237             /* 3XX */
0238 
0239             if (status == NGX_HTTP_NOT_MODIFIED) {
0240                 r->header_only = 1;
0241             }
0242 
0243             status = status - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX;
0244             status_line = &ngx_http_status_lines[status];
0245             len += ngx_http_status_lines[status].len;
0246 
0247         } else if (status >= NGX_HTTP_BAD_REQUEST
0248                    && status < NGX_HTTP_LAST_4XX)
0249         {
0250             /* 4XX */
0251             status = status - NGX_HTTP_BAD_REQUEST
0252                             + NGX_HTTP_OFF_4XX;
0253 
0254             status_line = &ngx_http_status_lines[status];
0255             len += ngx_http_status_lines[status].len;
0256 
0257         } else if (status >= NGX_HTTP_INTERNAL_SERVER_ERROR
0258                    && status < NGX_HTTP_LAST_5XX)
0259         {
0260             /* 5XX */
0261             status = status - NGX_HTTP_INTERNAL_SERVER_ERROR
0262                             + NGX_HTTP_OFF_5XX;
0263 
0264             status_line = &ngx_http_status_lines[status];
0265             len += ngx_http_status_lines[status].len;
0266 
0267         } else {
0268             len += NGX_INT_T_LEN + 1 /* SP */;
0269             status_line = NULL;
0270         }
0271 
0272         if (status_line && status_line->len == 0) {
0273             status = r->headers_out.status;
0274             len += NGX_INT_T_LEN + 1 /* SP */;
0275             status_line = NULL;
0276         }
0277     }
0278 
0279     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
0280 
0281     if (r->headers_out.server == NULL) {
0282         if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
0283             len += sizeof(ngx_http_server_full_string) - 1;
0284 
0285         } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
0286             len += sizeof(ngx_http_server_build_string) - 1;
0287 
0288         } else {
0289             len += sizeof(ngx_http_server_string) - 1;
0290         }
0291     }
0292 
0293     if (r->headers_out.date == NULL) {
0294         len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
0295     }
0296 
0297     if (r->headers_out.content_type.len) {
0298         len += sizeof("Content-Type: ") - 1
0299                + r->headers_out.content_type.len + 2;
0300 
0301         if (r->headers_out.content_type_len == r->headers_out.content_type.len
0302             && r->headers_out.charset.len)
0303         {
0304             len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
0305         }
0306     }
0307 
0308     if (r->headers_out.content_length == NULL
0309         && r->headers_out.content_length_n >= 0)
0310     {
0311         len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
0312     }
0313 
0314     if (r->headers_out.last_modified == NULL
0315         && r->headers_out.last_modified_time != -1)
0316     {
0317         len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
0318     }
0319 
0320     c = r->connection;
0321 
0322     if (r->headers_out.location
0323         && r->headers_out.location->value.len
0324         && r->headers_out.location->value.data[0] == '/'
0325         && clcf->absolute_redirect)
0326     {
0327         r->headers_out.location->hash = 0;
0328 
0329         if (clcf->server_name_in_redirect) {
0330             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
0331             host = cscf->server_name;
0332 
0333         } else if (r->headers_in.server.len) {
0334             host = r->headers_in.server;
0335 
0336         } else {
0337             host.len = NGX_SOCKADDR_STRLEN;
0338             host.data = addr;
0339 
0340             if (ngx_connection_local_sockaddr(c, &host, 0) != NGX_OK) {
0341                 return NGX_ERROR;
0342             }
0343         }
0344 
0345         port = ngx_inet_get_port(c->local_sockaddr);
0346 
0347         len += sizeof("Location: https://") - 1
0348                + host.len
0349                + r->headers_out.location->value.len + 2;
0350 
0351         if (clcf->port_in_redirect) {
0352 
0353 #if (NGX_HTTP_SSL)
0354             if (c->ssl)
0355                 port = (port == 443) ? 0 : port;
0356             else
0357 #endif
0358                 port = (port == 80) ? 0 : port;
0359 
0360         } else {
0361             port = 0;
0362         }
0363 
0364         if (port) {
0365             len += sizeof(":65535") - 1;
0366         }
0367 
0368     } else {
0369         ngx_str_null(&host);
0370         port = 0;
0371     }
0372 
0373     if (r->chunked) {
0374         len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
0375     }
0376 
0377     if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) {
0378         len += sizeof("Connection: upgrade" CRLF) - 1;
0379 
0380     } else if (r->keepalive) {
0381         len += sizeof("Connection: keep-alive" CRLF) - 1;
0382 
0383         /*
0384          * MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
0385          * MSIE keeps the connection alive for about 60-65 seconds.
0386          * Opera keeps the connection alive very long.
0387          * Mozilla keeps the connection alive for N plus about 1-10 seconds.
0388          * Konqueror keeps the connection alive for about N seconds.
0389          */
0390 
0391         if (clcf->keepalive_header) {
0392             len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
0393         }
0394 
0395     } else {
0396         len += sizeof("Connection: close" CRLF) - 1;
0397     }
0398 
0399 #if (NGX_HTTP_GZIP)
0400     if (r->gzip_vary) {
0401         if (clcf->gzip_vary) {
0402             len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
0403 
0404         } else {
0405             r->gzip_vary = 0;
0406         }
0407     }
0408 #endif
0409 
0410     part = &r->headers_out.headers.part;
0411     header = part->elts;
0412 
0413     for (i = 0; /* void */; i++) {
0414 
0415         if (i >= part->nelts) {
0416             if (part->next == NULL) {
0417                 break;
0418             }
0419 
0420             part = part->next;
0421             header = part->elts;
0422             i = 0;
0423         }
0424 
0425         if (header[i].hash == 0) {
0426             continue;
0427         }
0428 
0429         len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
0430                + sizeof(CRLF) - 1;
0431     }
0432 
0433     b = ngx_create_temp_buf(r->pool, len);
0434     if (b == NULL) {
0435         return NGX_ERROR;
0436     }
0437 
0438     /* "HTTP/1.x " */
0439     b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
0440 
0441     /* status line */
0442     if (status_line) {
0443         b->last = ngx_copy(b->last, status_line->data, status_line->len);
0444 
0445     } else {
0446         b->last = ngx_sprintf(b->last, "%03ui ", status);
0447     }
0448     *b->last++ = CR; *b->last++ = LF;
0449 
0450     if (r->headers_out.server == NULL) {
0451         if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
0452             p = ngx_http_server_full_string;
0453             len = sizeof(ngx_http_server_full_string) - 1;
0454 
0455         } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
0456             p = ngx_http_server_build_string;
0457             len = sizeof(ngx_http_server_build_string) - 1;
0458 
0459         } else {
0460             p = ngx_http_server_string;
0461             len = sizeof(ngx_http_server_string) - 1;
0462         }
0463 
0464         b->last = ngx_cpymem(b->last, p, len);
0465     }
0466 
0467     if (r->headers_out.date == NULL) {
0468         b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);
0469         b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
0470                              ngx_cached_http_time.len);
0471 
0472         *b->last++ = CR; *b->last++ = LF;
0473     }
0474 
0475     if (r->headers_out.content_type.len) {
0476         b->last = ngx_cpymem(b->last, "Content-Type: ",
0477                              sizeof("Content-Type: ") - 1);
0478         p = b->last;
0479         b->last = ngx_copy(b->last, r->headers_out.content_type.data,
0480                            r->headers_out.content_type.len);
0481 
0482         if (r->headers_out.content_type_len == r->headers_out.content_type.len
0483             && r->headers_out.charset.len)
0484         {
0485             b->last = ngx_cpymem(b->last, "; charset=",
0486                                  sizeof("; charset=") - 1);
0487             b->last = ngx_copy(b->last, r->headers_out.charset.data,
0488                                r->headers_out.charset.len);
0489 
0490             /* update r->headers_out.content_type for possible logging */
0491 
0492             r->headers_out.content_type.len = b->last - p;
0493             r->headers_out.content_type.data = p;
0494         }
0495 
0496         *b->last++ = CR; *b->last++ = LF;
0497     }
0498 
0499     if (r->headers_out.content_length == NULL
0500         && r->headers_out.content_length_n >= 0)
0501     {
0502         b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
0503                               r->headers_out.content_length_n);
0504     }
0505 
0506     if (r->headers_out.last_modified == NULL
0507         && r->headers_out.last_modified_time != -1)
0508     {
0509         b->last = ngx_cpymem(b->last, "Last-Modified: ",
0510                              sizeof("Last-Modified: ") - 1);
0511         b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
0512 
0513         *b->last++ = CR; *b->last++ = LF;
0514     }
0515 
0516     if (host.data) {
0517 
0518         p = b->last + sizeof("Location: ") - 1;
0519 
0520         b->last = ngx_cpymem(b->last, "Location: http",
0521                              sizeof("Location: http") - 1);
0522 
0523 #if (NGX_HTTP_SSL)
0524         if (c->ssl) {
0525             *b->last++ ='s';
0526         }
0527 #endif
0528 
0529         *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
0530         b->last = ngx_copy(b->last, host.data, host.len);
0531 
0532         if (port) {
0533             b->last = ngx_sprintf(b->last, ":%ui", port);
0534         }
0535 
0536         b->last = ngx_copy(b->last, r->headers_out.location->value.data,
0537                            r->headers_out.location->value.len);
0538 
0539         /* update r->headers_out.location->value for possible logging */
0540 
0541         r->headers_out.location->value.len = b->last - p;
0542         r->headers_out.location->value.data = p;
0543         ngx_str_set(&r->headers_out.location->key, "Location");
0544 
0545         *b->last++ = CR; *b->last++ = LF;
0546     }
0547 
0548     if (r->chunked) {
0549         b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
0550                              sizeof("Transfer-Encoding: chunked" CRLF) - 1);
0551     }
0552 
0553     if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) {
0554         b->last = ngx_cpymem(b->last, "Connection: upgrade" CRLF,
0555                              sizeof("Connection: upgrade" CRLF) - 1);
0556 
0557     } else if (r->keepalive) {
0558         b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
0559                              sizeof("Connection: keep-alive" CRLF) - 1);
0560 
0561         if (clcf->keepalive_header) {
0562             b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
0563                                   clcf->keepalive_header);
0564         }
0565 
0566     } else {
0567         b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
0568                              sizeof("Connection: close" CRLF) - 1);
0569     }
0570 
0571 #if (NGX_HTTP_GZIP)
0572     if (r->gzip_vary) {
0573         b->last = ngx_cpymem(b->last, "Vary: Accept-Encoding" CRLF,
0574                              sizeof("Vary: Accept-Encoding" CRLF) - 1);
0575     }
0576 #endif
0577 
0578     part = &r->headers_out.headers.part;
0579     header = part->elts;
0580 
0581     for (i = 0; /* void */; i++) {
0582 
0583         if (i >= part->nelts) {
0584             if (part->next == NULL) {
0585                 break;
0586             }
0587 
0588             part = part->next;
0589             header = part->elts;
0590             i = 0;
0591         }
0592 
0593         if (header[i].hash == 0) {
0594             continue;
0595         }
0596 
0597         b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
0598         *b->last++ = ':'; *b->last++ = ' ';
0599 
0600         b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
0601         *b->last++ = CR; *b->last++ = LF;
0602     }
0603 
0604     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
0605                    "%*s", (size_t) (b->last - b->pos), b->pos);
0606 
0607     /* the end of HTTP header */
0608     *b->last++ = CR; *b->last++ = LF;
0609 
0610     r->header_size = b->last - b->pos;
0611 
0612     if (r->header_only) {
0613         b->last_buf = 1;
0614     }
0615 
0616     out.buf = b;
0617     out.next = NULL;
0618 
0619     return ngx_http_write_filter(r, &out);
0620 }
0621 
0622 
0623 static ngx_int_t
0624 ngx_http_header_filter_init(ngx_conf_t *cf)
0625 {
0626     ngx_http_top_header_filter = ngx_http_header_filter;
0627 
0628     return NGX_OK;
0629 }