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

Linux Cross Reference
Nginx/http/ngx_http_header_filter_module.c

Version: ~ [ nginx-0.6.26 ] ~ [ nginx-0.5.35 ] ~ [ nginx-0.5.20 ] ~ [ nginx-0.5.19 ] ~

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

~ [ 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.