~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/http/ngx_http_header_filter_module.c

Version: ~ [ nginx-0.8.20 ] ~ [ nginx-0.7.62 ] ~ [ nginx-0.6.39 ] ~

  1 
  2 /*
  3  * Copyright (C) Igor Sysoev
  4  */
  5 
  6 
  7 #include <ngx_config.h>
  8 #include <ngx_core.h>
  9 #include <ngx_http.h>
 10 #include <nginx.h>
 11 
 12 
 13 static ngx_int_t ngx_http_header_filter_init(ngx_conf_t *cf);
 14 static ngx_int_t ngx_http_header_filter(ngx_http_request_t *r);
 15 
 16 
 17 static ngx_http_module_t  ngx_http_header_filter_module_ctx = {
 18     NULL,                                  /* preconfiguration */
 19     ngx_http_header_filter_init,           /* postconfiguration */
 20 
 21     NULL,                                  /* create main configuration */
 22     NULL,                                  /* init main configuration */
 23 
 24     NULL,                                  /* create server configuration */
 25     NULL,                                  /* merge server configuration */
 26 
 27     NULL,                                  /* create location configuration */
 28     NULL,                                  /* merge location configuration */
 29 };
 30 
 31 
 32 ngx_module_t  ngx_http_header_filter_module = {
 33     NGX_MODULE_V1,
 34     &ngx_http_header_filter_module_ctx,    /* module context */
 35     NULL,                                  /* module directives */
 36     NGX_HTTP_MODULE,                       /* module type */
 37     NULL,                                  /* init master */
 38     NULL,                                  /* init module */
 39     NULL,                                  /* init process */
 40     NULL,                                  /* init thread */
 41     NULL,                                  /* exit thread */
 42     NULL,                                  /* exit process */
 43     NULL,                                  /* exit master */
 44     NGX_MODULE_V1_PADDING
 45 };
 46 
 47 
 48 static char ngx_http_server_string[] = "Server: nginx" CRLF;
 49 static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
 50 
 51 
 52 static ngx_str_t ngx_http_status_lines[] = {
 53 
 54     ngx_string("200 OK"),
 55     ngx_string("201 Created"),
 56     ngx_null_string,  /* "202 Accepted" */
 57     ngx_null_string,  /* "203 Non-Authoritative Information" */
 58     ngx_string("204 No Content"),
 59     ngx_null_string,  /* "205 Reset Content" */
 60     ngx_string("206 Partial Content"),
 61 
 62     /* ngx_null_string, */  /* "207 Multi-Status" */
 63 
 64 #define NGX_HTTP_LAST_LEVEL_200  207
 65 #define NGX_HTTP_LEVEL_200       (NGX_HTTP_LAST_LEVEL_200 - 200)
 66 
 67     /* ngx_null_string, */  /* "300 Multiple Choices" */
 68 
 69     ngx_string("301 Moved Permanently"),
 70     ngx_string("302 Moved Temporarily"),
 71     ngx_null_string,  /* "303 See Other" */
 72     ngx_string("304 Not Modified"),
 73 
 74     /* ngx_null_string, */  /* "305 Use Proxy" */
 75     /* ngx_null_string, */  /* "306 unused" */
 76     /* ngx_null_string, */  /* "307 Temporary Redirect" */
 77 
 78 #define NGX_HTTP_LAST_LEVEL_300  305
 79 #define NGX_HTTP_LEVEL_300       (NGX_HTTP_LAST_LEVEL_300 - 301)
 80 
 81     ngx_string("400 Bad Request"),
 82     ngx_string("401 Unauthorized"),
 83     ngx_string("402 Payment Required"),
 84     ngx_string("403 Forbidden"),
 85     ngx_string("404 Not Found"),
 86     ngx_string("405 Not Allowed"),
 87     ngx_string("406 Not Acceptable"),
 88     ngx_null_string,  /* "407 Proxy Authentication Required" */
 89     ngx_string("408 Request Time-out"),
 90     ngx_string("409 Conflict"),
 91     ngx_string("410 Gone"),
 92     ngx_string("411 Length Required"),
 93     ngx_string("412 Precondition Failed"),
 94     ngx_string("413 Request Entity Too Large"),
 95     ngx_null_string,  /* "414 Request-URI Too Large", but we never send it
 96                        * because we treat such requests as the HTTP/0.9
 97                        * requests and send only a body without a header
 98                        */
 99     ngx_string("415 Unsupported Media Type"),
100     ngx_string("416 Requested Range Not Satisfiable"),
101 
102     /* ngx_null_string, */  /* "417 Expectation Failed" */
103     /* ngx_null_string, */  /* "418 unused" */
104     /* ngx_null_string, */  /* "419 unused" */
105     /* ngx_null_string, */  /* "420 unused" */
106     /* ngx_null_string, */  /* "421 unused" */
107     /* ngx_null_string, */  /* "422 Unprocessable Entity" */
108     /* ngx_null_string, */  /* "423 Locked" */
109     /* ngx_null_string, */  /* "424 Failed Dependency" */
110 
111 #define NGX_HTTP_LAST_LEVEL_400  417
112 #define NGX_HTTP_LEVEL_400       (NGX_HTTP_LAST_LEVEL_400 - 400)
113 
114     ngx_string("500 Internal Server Error"),
115     ngx_string("501 Method Not Implemented"),
116     ngx_string("502 Bad Gateway"),
117     ngx_string("503 Service Temporarily Unavailable"),
118     ngx_string("504 Gateway Time-out"),
119 
120     ngx_null_string,        /* "505 HTTP Version Not Supported" */
121     ngx_null_string,        /* "506 Variant Also Negotiates" */
122     ngx_string("507 Insufficient Storage"),
123     /* ngx_null_string, */  /* "508 unused" */
124     /* ngx_null_string, */  /* "509 unused" */
125     /* ngx_null_string, */  /* "510 Not Extended" */
126 
127 #define NGX_HTTP_LAST_LEVEL_500  508
128 
129 };
130 
131 
132 ngx_http_header_out_t  ngx_http_headers_out[] = {
133     { ngx_string("Server"), offsetof(ngx_http_headers_out_t, server) },
134     { ngx_string("Date"), offsetof(ngx_http_headers_out_t, date) },
135 #if 0
136     { ngx_string("Content-Type"),
137                  offsetof(ngx_http_headers_out_t, content_type) },
138 #endif
139     { ngx_string("Content-Length"),
140                  offsetof(ngx_http_headers_out_t, content_length) },
141     { ngx_string("Content-Encoding"),
142                  offsetof(ngx_http_headers_out_t, content_encoding) },
143     { ngx_string("Location"), offsetof(ngx_http_headers_out_t, location) },
144     { ngx_string("Last-Modified"),
145                  offsetof(ngx_http_headers_out_t, last_modified) },
146     { ngx_string("Accept-Ranges"),
147                  offsetof(ngx_http_headers_out_t, accept_ranges) },
148     { ngx_string("Expires"), offsetof(ngx_http_headers_out_t, expires) },
149     { ngx_string("Cache-Control"),
150                  offsetof(ngx_http_headers_out_t, cache_control) },
151     { ngx_string("ETag"), offsetof(ngx_http_headers_out_t, etag) },
152 
153     { ngx_null_string, 0 }
154 };
155 
156 
157 static ngx_int_t
158 ngx_http_header_filter(ngx_http_request_t *r)
159 {
160     u_char                    *p;
161     size_t                     len;
162     ngx_str_t                  host, *status_line;
163     ngx_buf_t                 *b;
164     ngx_uint_t                 status, i, port;
165     ngx_chain_t                out;
166     ngx_list_part_t           *part;
167     ngx_table_elt_t           *header;
168     ngx_connection_t          *c;
169     ngx_http_core_loc_conf_t  *clcf;
170     ngx_http_core_srv_conf_t  *cscf;
171     struct sockaddr_in        *sin;
172 #if (NGX_HAVE_INET6)
173     struct sockaddr_in6       *sin6;
174 #endif
175     u_char                     addr[NGX_SOCKADDR_STRLEN];
176 
177     r->header_sent = 1;
178 
179     if (r != r->main) {
180         return NGX_OK;
181     }
182 
183     if (r->http_version < NGX_HTTP_VERSION_10) {
184         return NGX_OK;
185     }
186 
187     if (r->method == NGX_HTTP_HEAD) {
188         r->header_only = 1;
189     }
190 
191     if (r->headers_out.last_modified_time != -1) {
192         if (r->headers_out.status != NGX_HTTP_OK
193             && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
194             && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)
195         {
196             r->headers_out.last_modified_time = -1;
197             r->headers_out.last_modified = NULL;
198         }
199     }
200 
201     len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
202           /* the end of the header */
203           + sizeof(CRLF) - 1;
204 
205     /* status line */
206 
207     if (r->headers_out.status_line.len) {
208         len += r->headers_out.status_line.len;
209         status_line = &r->headers_out.status_line;
210 #if (NGX_SUPPRESS_WARN)
211         status = 0;
212 #endif
213 
214     } else {
215 
216         status = r->headers_out.status;
217 
218         if (status >= NGX_HTTP_OK
219             && status < NGX_HTTP_LAST_LEVEL_200)
220         {
221             /* 2XX */
222 
223             if (status == NGX_HTTP_NO_CONTENT) {
224                 r->header_only = 1;
225                 r->headers_out.content_type.len = 0;
226                 r->headers_out.content_type.data = NULL;
227                 r->headers_out.last_modified_time = -1;
228                 r->headers_out.last_modified = NULL;
229                 r->headers_out.content_length = NULL;
230                 r->headers_out.content_length_n = -1;
231             }
232 
233             status -= NGX_HTTP_OK;
234             status_line = &ngx_http_status_lines[status];
235             len += ngx_http_status_lines[status].len;
236 
237         } else if (status >= NGX_HTTP_MOVED_PERMANENTLY
238                    && status < NGX_HTTP_LAST_LEVEL_300)
239         {
240             /* 3XX */
241 
242             if (status == NGX_HTTP_NOT_MODIFIED) {
243                 r->header_only = 1;
244             }
245 
246             status = status - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_LEVEL_200;
247             status_line = &ngx_http_status_lines[status];
248             len += ngx_http_status_lines[status].len;
249 
250         } else if (status >= NGX_HTTP_BAD_REQUEST
251                    && status < NGX_HTTP_LAST_LEVEL_400)
252         {
253             /* 4XX */
254             status = status - NGX_HTTP_BAD_REQUEST
255                             + NGX_HTTP_LEVEL_200
256                             + NGX_HTTP_LEVEL_300;
257 
258             status_line = &ngx_http_status_lines[status];
259             len += ngx_http_status_lines[status].len;
260 
261         } else if (status >= NGX_HTTP_INTERNAL_SERVER_ERROR
262                    && status < NGX_HTTP_LAST_LEVEL_500)
263         {
264             /* 5XX */
265             status = status - NGX_HTTP_INTERNAL_SERVER_ERROR
266                             + NGX_HTTP_LEVEL_200
267                             + NGX_HTTP_LEVEL_300
268                             + NGX_HTTP_LEVEL_400;
269 
270             status_line = &ngx_http_status_lines[status];
271             len += ngx_http_status_lines[status].len;
272 
273         } else {
274             len += NGX_INT_T_LEN;
275             status_line = NULL;
276         }
277     }
278 
279     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
280 
281     if (r->headers_out.server == NULL) {
282         len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1:
283                                      sizeof(ngx_http_server_string) - 1;
284     }
285 
286     if (r->headers_out.date == NULL) {
287         len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
288     }
289 
290     if (r->headers_out.content_type.len) {
291         len += sizeof("Content-Type: ") - 1
292                + r->headers_out.content_type.len + 2;
293 
294         if (r->headers_out.content_type_len == r->headers_out.content_type.len
295             && r->headers_out.charset.len)
296         {
297             len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
298         }
299     }
300 
301     if (r->headers_out.content_length == NULL
302         && r->headers_out.content_length_n >= 0)
303     {
304         len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
305     }
306 
307     if (r->headers_out.last_modified == NULL
308         && r->headers_out.last_modified_time != -1)
309     {
310         len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
311     }
312 
313     c = r->connection;
314 
315     if (r->headers_out.location
316         && r->headers_out.location->value.len
317         && r->headers_out.location->value.data[0] == '/')
318     {
319         r->headers_out.location->hash = 0;
320 
321         if (clcf->server_name_in_redirect) {
322             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
323             host = cscf->server_name;
324 
325         } else if (r->headers_in.server.len) {
326             host = r->headers_in.server;
327 
328         } else {
329             host.len = NGX_SOCKADDR_STRLEN;
330             host.data = addr;
331 
332             if (ngx_connection_local_sockaddr(c, &host, 0) != NGX_OK) {
333                 return NGX_ERROR;
334             }
335         }
336 
337         switch (c->local_sockaddr->sa_family) {
338 
339 #if (NGX_HAVE_INET6)
340         case AF_INET6:
341             sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
342             port = ntohs(sin6->sin6_port);
343             break;
344 #endif
345         default: /* AF_INET */
346             sin = (struct sockaddr_in *) c->local_sockaddr;
347             port = ntohs(sin->sin_port);
348             break;
349         }
350 
351         len += sizeof("Location: https://") - 1
352                + host.len
353                + r->headers_out.location->value.len + 2;
354 
355         if (clcf->port_in_redirect) {
356 
357 #if (NGX_HTTP_SSL)
358             if (c->ssl)
359                 port = (port == 443) ? 0 : port;
360             else
361 #endif
362                 port = (port == 80) ? 0 : port;
363 
364         } else {
365             port = 0;
366         }
367 
368         if (port) {
369             len += sizeof(":65535") - 1;
370         }
371 
372     } else {
373         host.len = 0;
374         host.data = NULL;
375         port = 0;
376     }
377 
378     if (r->chunked) {
379         len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
380     }
381 
382     if (r->keepalive) {
383         len += sizeof("Connection: keep-alive" CRLF) - 1;
384 
385         /*
386          * MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
387          * MSIE keeps the connection alive for about 60-65 seconds.
388          * Opera keeps the connection alive very long.
389          * Mozilla keeps the connection alive for N plus about 1-10 seconds.
390          * Konqueror keeps the connection alive for about N seconds.
391          */
392 
393         if (clcf->keepalive_header) {
394             len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
395         }
396 
397     } else {
398         len += sizeof("Connection: closed" CRLF) - 1;
399     }
400 
401 #if (NGX_HTTP_GZIP)
402     if (r->gzip && clcf->gzip_vary) {
403         len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
404     }
405 #endif
406 
407     part = &r->headers_out.headers.part;
408     header = part->elts;
409 
410     for (i = 0; /* void */; i++) {
411 
412         if (i >= part->nelts) {
413             if (part->next == NULL) {
414                 break;
415             }
416 
417             part = part->next;
418             header = part->elts;
419             i = 0;
420         }
421 
422         if (header[i].hash == 0) {
423             continue;
424         }
425 
426         len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
427                + sizeof(CRLF) - 1;
428     }
429 
430     b = ngx_create_temp_buf(r->pool, len);
431     if (b == NULL) {
432         return NGX_ERROR;
433     }
434 
435     /* "HTTP/1.x " */
436     b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
437 
438     /* status line */
439     if (status_line) {
440         b->last = ngx_copy(b->last, status_line->data, status_line->len);
441 
442     } else {
443         b->last = ngx_sprintf(b->last, "%ui", status);
444     }
445     *b->last++ = CR; *b->last++ = LF;
446 
447     if (r->headers_out.server == NULL) {
448         if (clcf->server_tokens) {
449             p = (u_char *) ngx_http_server_full_string;
450             len = sizeof(ngx_http_server_full_string) - 1;
451 
452         } else {
453             p = (u_char *) ngx_http_server_string;
454             len = sizeof(ngx_http_server_string) - 1;
455         }
456 
457         b->last = ngx_cpymem(b->last, p, len);
458     }
459 
460     if (r->headers_out.date == NULL) {
461         b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);
462         b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
463                              ngx_cached_http_time.len);
464 
465         *b->last++ = CR; *b->last++ = LF;
466     }
467 
468     if (r->headers_out.content_type.len) {
469         b->last = ngx_cpymem(b->last, "Content-Type: ",
470                              sizeof("Content-Type: ") - 1);
471         p = b->last;
472         b->last = ngx_copy(b->last, r->headers_out.content_type.data,
473                            r->headers_out.content_type.len);
474 
475         if (r->headers_out.content_type_len == r->headers_out.content_type.len
476             && r->headers_out.charset.len)
477         {
478             b->last = ngx_cpymem(b->last, "; charset=",
479                                  sizeof("; charset=") - 1);
480             b->last = ngx_copy(b->last, r->headers_out.charset.data,
481                                r->headers_out.charset.len);
482 
483             /* update r->headers_out.content_type for possible logging */
484 
485             r->headers_out.content_type.len = b->last - p;
486             r->headers_out.content_type.data = p;
487         }
488 
489         *b->last++ = CR; *b->last++ = LF;
490     }
491 
492     if (r->headers_out.content_length == NULL
493         && r->headers_out.content_length_n >= 0)
494     {
495         b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
496                               r->headers_out.content_length_n);
497     }
498 
499     if (r->headers_out.last_modified == NULL
500         && r->headers_out.last_modified_time != -1)
501     {
502         b->last = ngx_cpymem(b->last, "Last-Modified: ",
503                              sizeof("Last-Modified: ") - 1);
504         b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
505 
506         *b->last++ = CR; *b->last++ = LF;
507     }
508 
509     if (host.data) {
510 
511         p = b->last + sizeof("Location: ") - 1;
512 
513         b->last = ngx_cpymem(b->last, "Location: http",
514                              sizeof("Location: http") - 1);
515 
516 #if (NGX_HTTP_SSL)
517         if (c->ssl) {
518             *b->last++ ='s';
519         }
520 #endif
521 
522         *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/';
523         b->last = ngx_copy(b->last, host.data, host.len);
524 
525         if (port) {
526             b->last = ngx_sprintf(b->last, ":%ui", port);
527         }
528 
529         b->last = ngx_copy(b->last, r->headers_out.location->value.data,
530                            r->headers_out.location->value.len);
531 
532         /* update r->headers_out.location->value for possible logging */
533 
534         r->headers_out.location->value.len = b->last - p;
535         r->headers_out.location->value.data = p;
536         r->headers_out.location->key.len = sizeof("Location: ") - 1;
537         r->headers_out.location->key.data = (u_char *) "Location: ";
538 
539         *b->last++ = CR; *b->last++ = LF;
540     }
541 
542     if (r->chunked) {
543         b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
544                              sizeof("Transfer-Encoding: chunked" CRLF) - 1);
545     }
546 
547     if (r->keepalive) {
548         b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
549                              sizeof("Connection: keep-alive" CRLF) - 1);
550 
551         if (clcf->keepalive_header) {
552             b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
553                                   clcf->keepalive_header);
554         }
555 
556     } else {
557         b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
558                              sizeof("Connection: close" CRLF) - 1);
559     }
560 
561 #if (NGX_HTTP_GZIP)
562     if (r->gzip && clcf->gzip_vary) {
563         b->last = ngx_cpymem(b->last, "Vary: Accept-Encoding" CRLF,
564                              sizeof("Vary: Accept-Encoding" CRLF) - 1);
565     }
566 #endif
567 
568     part = &r->headers_out.headers.part;
569     header = part->elts;
570 
571     for (i = 0; /* void */; i++) {
572 
573         if (i >= part->nelts) {
574             if (part->next == NULL) {
575                 break;
576             }
577 
578             part = part->next;
579             header = part->elts;
580             i = 0;
581         }
582 
583         if (header[i].hash == 0) {
584             continue;
585         }
586 
587         b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
588         *b->last++ = ':'; *b->last++ = ' ';
589 
590         b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
591         *b->last++ = CR; *b->last++ = LF;
592     }
593 
594     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
595                    "%*s", (size_t) (b->last - b->pos), b->pos);
596 
597     /* the end of HTTP header */
598     *b->last++ = CR; *b->last++ = LF;
599 
600     r->header_size = b->last - b->pos;
601 
602     if (r->header_only) {
603         b->last_buf = 1;
604     }
605 
606     out.buf = b;
607     out.next = NULL;
608 
609     return ngx_http_write_filter(r, &out);
610 }
611 
612 
613 static ngx_int_t
614 ngx_http_header_filter_init(ngx_conf_t *cf)
615 {
616     ngx_http_top_header_filter = ngx_http_header_filter;
617 
618     return NGX_OK;
619 }
620 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.