upstream 0.7.33
[nginx.git] / nginx / src / http / ngx_http_special_response.c
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_send_error_page(ngx_http_request_t *r,
14     ngx_http_err_page_t *err_page);
15 static ngx_int_t ngx_http_send_special_response(ngx_http_request_t *r,
16     ngx_http_core_loc_conf_t *clcf, ngx_uint_t err);
17 static ngx_int_t ngx_http_send_refresh(ngx_http_request_t *r);
18
19
20 static u_char ngx_http_error_full_tail[] =
21 "<hr><center>" NGINX_VER "</center>" CRLF
22 "</body>" CRLF
23 "</html>" CRLF
24 ;
25
26
27 static u_char ngx_http_error_tail[] =
28 "<hr><center>nginx</center>" CRLF
29 "</body>" CRLF
30 "</html>" CRLF
31 ;
32
33
34 static u_char ngx_http_msie_stub[] =
35 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
36 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
37 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
38 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
39 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
40 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
41 ;
42
43
44 static u_char ngx_http_msie_refresh_head[] =
45 "<html><head><meta http-equiv=\"Refresh\" content=\"0; URL=";
46
47
48 static u_char ngx_http_msie_refresh_tail[] =
49 "\"></head><body></body></html>" CRLF;
50
51
52 static char ngx_http_error_301_page[] =
53 "<html>" CRLF
54 "<head><title>301 Moved Permanently</title></head>" CRLF
55 "<body bgcolor=\"white\">" CRLF
56 "<center><h1>301 Moved Permanently</h1></center>" CRLF
57 ;
58
59
60 static char ngx_http_error_302_page[] =
61 "<html>" CRLF
62 "<head><title>302 Found</title></head>" CRLF
63 "<body bgcolor=\"white\">" CRLF
64 "<center><h1>302 Found</h1></center>" CRLF
65 ;
66
67
68 static char ngx_http_error_400_page[] =
69 "<html>" CRLF
70 "<head><title>400 Bad Request</title></head>" CRLF
71 "<body bgcolor=\"white\">" CRLF
72 "<center><h1>400 Bad Request</h1></center>" CRLF
73 ;
74
75
76 static char ngx_http_error_401_page[] =
77 "<html>" CRLF
78 "<head><title>401 Authorization Required</title></head>" CRLF
79 "<body bgcolor=\"white\">" CRLF
80 "<center><h1>401 Authorization Required</h1></center>" CRLF
81 ;
82
83
84 static char ngx_http_error_402_page[] =
85 "<html>" CRLF
86 "<head><title>402 Payment Required</title></head>" CRLF
87 "<body bgcolor=\"white\">" CRLF
88 "<center><h1>402 Payment Required</h1></center>" CRLF
89 ;
90
91
92 static char ngx_http_error_403_page[] =
93 "<html>" CRLF
94 "<head><title>403 Forbidden</title></head>" CRLF
95 "<body bgcolor=\"white\">" CRLF
96 "<center><h1>403 Forbidden</h1></center>" CRLF
97 ;
98
99
100 static char ngx_http_error_404_page[] =
101 "<html>" CRLF
102 "<head><title>404 Not Found</title></head>" CRLF
103 "<body bgcolor=\"white\">" CRLF
104 "<center><h1>404 Not Found</h1></center>" CRLF
105 ;
106
107
108 static char ngx_http_error_405_page[] =
109 "<html>" CRLF
110 "<head><title>405 Not Allowed</title></head>" CRLF
111 "<body bgcolor=\"white\">" CRLF
112 "<center><h1>405 Not Allowed</h1></center>" CRLF
113 ;
114
115
116 static char ngx_http_error_406_page[] =
117 "<html>" CRLF
118 "<head><title>406 Not Acceptable</title></head>" CRLF
119 "<body bgcolor=\"white\">" CRLF
120 "<center><h1>406 Not Acceptable</h1></center>" CRLF
121 ;
122
123
124 static char ngx_http_error_408_page[] =
125 "<html>" CRLF
126 "<head><title>408 Request Time-out</title></head>" CRLF
127 "<body bgcolor=\"white\">" CRLF
128 "<center><h1>408 Request Time-out</h1></center>" CRLF
129 ;
130
131
132 static char ngx_http_error_409_page[] =
133 "<html>" CRLF
134 "<head><title>409 Conflict</title></head>" CRLF
135 "<body bgcolor=\"white\">" CRLF
136 "<center><h1>409 Conflict</h1></center>" CRLF
137 ;
138
139
140 static char ngx_http_error_410_page[] =
141 "<html>" CRLF
142 "<head><title>410 Gone</title></head>" CRLF
143 "<body bgcolor=\"white\">" CRLF
144 "<center><h1>410 Gone</h1></center>" CRLF
145 ;
146
147
148 static char ngx_http_error_411_page[] =
149 "<html>" CRLF
150 "<head><title>411 Length Required</title></head>" CRLF
151 "<body bgcolor=\"white\">" CRLF
152 "<center><h1>411 Length Required</h1></center>" CRLF
153 ;
154
155
156 static char ngx_http_error_412_page[] =
157 "<html>" CRLF
158 "<head><title>412 Precondition Failed</title></head>" CRLF
159 "<body bgcolor=\"white\">" CRLF
160 "<center><h1>412 Precondition Failed</h1></center>" CRLF
161 ;
162
163
164 static char ngx_http_error_413_page[] =
165 "<html>" CRLF
166 "<head><title>413 Request Entity Too Large</title></head>" CRLF
167 "<body bgcolor=\"white\">" CRLF
168 "<center><h1>413 Request Entity Too Large</h1></center>" CRLF
169 ;
170
171
172 static char ngx_http_error_414_page[] =
173 "<html>" CRLF
174 "<head><title>414 Request-URI Too Large</title></head>" CRLF
175 "<body bgcolor=\"white\">" CRLF
176 "<center><h1>414 Request-URI Too Large</h1></center>" CRLF
177 ;
178
179
180 static char ngx_http_error_415_page[] =
181 "<html>" CRLF
182 "<head><title>415 Unsupported Media Type</title></head>" CRLF
183 "<body bgcolor=\"white\">" CRLF
184 "<center><h1>415 Unsupported Media Type</h1></center>" CRLF
185 ;
186
187
188 static char ngx_http_error_416_page[] =
189 "<html>" CRLF
190 "<head><title>416 Requested Range Not Satisfiable</title></head>" CRLF
191 "<body bgcolor=\"white\">" CRLF
192 "<center><h1>416 Requested Range Not Satisfiable</h1></center>" CRLF
193 ;
194
195
196 static char ngx_http_error_495_page[] =
197 "<html>" CRLF
198 "<head><title>400 The SSL certificate error</title></head>"
199 CRLF
200 "<body bgcolor=\"white\">" CRLF
201 "<center><h1>400 Bad Request</h1></center>" CRLF
202 "<center>The SSL certificate error</center>" CRLF
203 ;
204
205
206 static char ngx_http_error_496_page[] =
207 "<html>" CRLF
208 "<head><title>400 No required SSL certificate was sent</title></head>"
209 CRLF
210 "<body bgcolor=\"white\">" CRLF
211 "<center><h1>400 Bad Request</h1></center>" CRLF
212 "<center>No required SSL certificate was sent</center>" CRLF
213 ;
214
215
216 static char ngx_http_error_497_page[] =
217 "<html>" CRLF
218 "<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>"
219 CRLF
220 "<body bgcolor=\"white\">" CRLF
221 "<center><h1>400 Bad Request</h1></center>" CRLF
222 "<center>The plain HTTP request was sent to HTTPS port</center>" CRLF
223 ;
224
225
226 static char ngx_http_error_500_page[] =
227 "<html>" CRLF
228 "<head><title>500 Internal Server Error</title></head>" CRLF
229 "<body bgcolor=\"white\">" CRLF
230 "<center><h1>500 Internal Server Error</h1></center>" CRLF
231 ;
232
233
234 static char ngx_http_error_501_page[] =
235 "<html>" CRLF
236 "<head><title>501 Method Not Implemented</title></head>" CRLF
237 "<body bgcolor=\"white\">" CRLF
238 "<center><h1>501 Method Not Implemented</h1></center>" CRLF
239 ;
240
241
242 static char ngx_http_error_502_page[] =
243 "<html>" CRLF
244 "<head><title>502 Bad Gateway</title></head>" CRLF
245 "<body bgcolor=\"white\">" CRLF
246 "<center><h1>502 Bad Gateway</h1></center>" CRLF
247 ;
248
249
250 static char ngx_http_error_503_page[] =
251 "<html>" CRLF
252 "<head><title>503 Service Temporarily Unavailable</title></head>" CRLF
253 "<body bgcolor=\"white\">" CRLF
254 "<center><h1>503 Service Temporarily Unavailable</h1></center>" CRLF
255 ;
256
257
258 static char ngx_http_error_504_page[] =
259 "<html>" CRLF
260 "<head><title>504 Gateway Time-out</title></head>" CRLF
261 "<body bgcolor=\"white\">" CRLF
262 "<center><h1>504 Gateway Time-out</h1></center>" CRLF
263 ;
264
265
266 static char ngx_http_error_507_page[] =
267 "<html>" CRLF
268 "<head><title>507 Insufficient Storage</title></head>" CRLF
269 "<body bgcolor=\"white\">" CRLF
270 "<center><h1>507 Insufficient Storage</h1></center>" CRLF
271 ;
272
273
274 static ngx_str_t ngx_http_error_pages[] = {
275
276     ngx_null_string,                     /* 201, 204 */
277
278 #define NGX_HTTP_LEVEL_200  1
279
280     /* ngx_null_string, */               /* 300 */
281     ngx_string(ngx_http_error_301_page),
282     ngx_string(ngx_http_error_302_page),
283     ngx_null_string,                     /* 303 */
284
285 #define NGX_HTTP_LEVEL_300  3
286
287     ngx_string(ngx_http_error_400_page),
288     ngx_string(ngx_http_error_401_page),
289     ngx_string(ngx_http_error_402_page),
290     ngx_string(ngx_http_error_403_page),
291     ngx_string(ngx_http_error_404_page),
292     ngx_string(ngx_http_error_405_page),
293     ngx_string(ngx_http_error_406_page),
294     ngx_null_string,                     /* 407 */
295     ngx_string(ngx_http_error_408_page),
296     ngx_string(ngx_http_error_409_page),
297     ngx_string(ngx_http_error_410_page),
298     ngx_string(ngx_http_error_411_page),
299     ngx_string(ngx_http_error_412_page),
300     ngx_string(ngx_http_error_413_page),
301     ngx_string(ngx_http_error_414_page),
302     ngx_string(ngx_http_error_415_page),
303     ngx_string(ngx_http_error_416_page),
304
305 #define NGX_HTTP_LEVEL_400  17
306
307     ngx_string(ngx_http_error_495_page), /* 495, https certificate error */
308     ngx_string(ngx_http_error_496_page), /* 496, https no certificate */
309     ngx_string(ngx_http_error_497_page), /* 497, http to https */
310     ngx_string(ngx_http_error_404_page), /* 498, canceled */
311     ngx_null_string,                     /* 499, client has closed connection */
312
313     ngx_string(ngx_http_error_500_page),
314     ngx_string(ngx_http_error_501_page),
315     ngx_string(ngx_http_error_502_page),
316     ngx_string(ngx_http_error_503_page),
317     ngx_string(ngx_http_error_504_page),
318     ngx_null_string,                     /* 505 */
319     ngx_null_string,                     /* 506 */
320     ngx_string(ngx_http_error_507_page)
321 };
322
323
324 static ngx_str_t  ngx_http_get_name = { 3, (u_char *) "GET " };
325
326
327 ngx_int_t
328 ngx_http_special_response_handler(ngx_http_request_t *r, ngx_int_t error)
329 {
330     ngx_uint_t                 i, err;
331     ngx_http_err_page_t       *err_page;
332     ngx_http_core_loc_conf_t  *clcf;
333
334     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
335                    "http special response: %d, \"%V?%V\"",
336                    error, &r->uri, &r->args);
337
338     r->err_status = error;
339
340     if (r->keepalive) {
341         switch (error) {
342             case NGX_HTTP_BAD_REQUEST:
343             case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE:
344             case NGX_HTTP_REQUEST_URI_TOO_LARGE:
345             case NGX_HTTP_TO_HTTPS:
346             case NGX_HTTPS_CERT_ERROR:
347             case NGX_HTTPS_NO_CERT:
348             case NGX_HTTP_INTERNAL_SERVER_ERROR:
349                 r->keepalive = 0;
350         }
351     }
352
353     if (r->lingering_close == 1) {
354         switch (error) {
355             case NGX_HTTP_BAD_REQUEST:
356             case NGX_HTTP_TO_HTTPS:
357             case NGX_HTTPS_CERT_ERROR:
358             case NGX_HTTPS_NO_CERT:
359                 r->lingering_close = 0;
360         }
361     }
362
363     r->headers_out.content_type.len = 0;
364
365     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
366
367     if (!r->error_page && clcf->error_pages && r->uri_changes != 0) {
368
369         if (clcf->recursive_error_pages == 0) {
370             r->error_page = 1;
371         }
372
373         err_page = clcf->error_pages->elts;
374
375         for (i = 0; i < clcf->error_pages->nelts; i++) {
376             if (err_page[i].status == error) {
377                 return ngx_http_send_error_page(r, &err_page[i]);
378             }
379         }
380     }
381
382     r->expect_tested = 1;
383
384     if (ngx_http_discard_request_body(r) != NGX_OK) {
385         error = NGX_HTTP_INTERNAL_SERVER_ERROR;
386     }
387
388     if (clcf->msie_refresh
389         && r->headers_in.msie
390         && (error == NGX_HTTP_MOVED_PERMANENTLY
391             || error == NGX_HTTP_MOVED_TEMPORARILY))
392     {
393         return ngx_http_send_refresh(r);
394     }
395
396     if (error == NGX_HTTP_CREATED) {
397         /* 201 */
398         err = 0;
399         r->header_only = 1;
400
401     } else if (error == NGX_HTTP_NO_CONTENT) {
402         /* 204 */
403         err = 0;
404
405     } else if (error < NGX_HTTP_BAD_REQUEST) {
406         /* 3XX */
407         err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_LEVEL_200;
408
409     } else if (error < NGX_HTTP_OWN_CODES) {
410         /* 4XX */
411         err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_LEVEL_200
412                                            + NGX_HTTP_LEVEL_300;
413
414     } else {
415         /* 49X, 5XX */
416         err = error - NGX_HTTP_OWN_CODES + NGX_HTTP_LEVEL_200
417                                          + NGX_HTTP_LEVEL_300
418                                          + NGX_HTTP_LEVEL_400;
419         switch (error) {
420             case NGX_HTTP_TO_HTTPS:
421             case NGX_HTTPS_CERT_ERROR:
422             case NGX_HTTPS_NO_CERT:
423                 r->err_status = NGX_HTTP_BAD_REQUEST;
424                 break;
425         }
426     }
427
428     return ngx_http_send_special_response(r, clcf, err);
429 }
430
431
432 static ngx_int_t
433 ngx_http_send_error_page(ngx_http_request_t *r, ngx_http_err_page_t *err_page)
434 {
435     u_char                     ch, *p, *last;
436     ngx_int_t                  overwrite;
437     ngx_str_t                 *uri, *args, u, a;
438     ngx_table_elt_t           *location;
439     ngx_http_core_loc_conf_t  *clcf;
440
441     overwrite = err_page->overwrite;
442
443     if (overwrite && overwrite != NGX_HTTP_OK) {
444         r->expect_tested = 1;
445     }
446
447     r->err_status = overwrite;
448
449     r->zero_in_uri = 0;
450
451     if (err_page->uri_lengths) {
452         if (ngx_http_script_run(r, &u, err_page->uri_lengths->elts, 0,
453                                 err_page->uri_values->elts)
454             == NULL)
455         {
456             return NGX_ERROR;
457         }
458
459         p = u.data;
460         uri = &u;
461         args = NULL;
462
463         if (*p == '/') {
464
465             last = p + uri->len;
466
467             while (p < last) {
468
469                 ch = *p++;
470
471                 if (ch == '?') {
472                     a.len = last - p;
473                     a.data = p;
474                     args = &a;
475
476                     u.len = p - 1 - u.data;
477
478                     while (p < last) {
479                         if (*p++ == '\0') {
480                             r->zero_in_uri = 1;
481                             break;
482                         }
483                     }
484
485                     break;
486                 }
487
488                 if (ch == '\0') {
489                     r->zero_in_uri = 1;
490                     continue;
491                 }
492             }
493         }
494
495     } else {
496         uri = &err_page->uri;
497         args = &err_page->args;
498     }
499
500     if (uri->data[0] == '/') {
501
502         if (r->method != NGX_HTTP_HEAD) {
503             r->method = NGX_HTTP_GET;
504             r->method_name = ngx_http_get_name;
505         }
506
507         return ngx_http_internal_redirect(r, uri, args);
508     }
509
510     if (uri->data[0] == '@') {
511         return ngx_http_named_location(r, uri);
512     }
513
514     location = ngx_list_push(&r->headers_out.headers);
515
516     if (location == NULL) {
517         return NGX_ERROR;
518     }
519
520     r->err_status = NGX_HTTP_MOVED_TEMPORARILY;
521
522     location->hash = 1;
523     location->key.len = sizeof("Location") - 1;
524     location->key.data = (u_char *) "Location";
525     location->value = *uri;
526
527     r->headers_out.location = location;
528
529     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
530
531     if (clcf->msie_refresh && r->headers_in.msie) {
532         return ngx_http_send_refresh(r);
533     }
534
535     return ngx_http_send_special_response(r, clcf, NGX_HTTP_MOVED_TEMPORARILY
536                                                    - NGX_HTTP_MOVED_PERMANENTLY
537                                                    + NGX_HTTP_LEVEL_200);
538 }
539
540
541 static ngx_int_t
542 ngx_http_send_special_response(ngx_http_request_t *r,
543     ngx_http_core_loc_conf_t *clcf, ngx_uint_t err)
544 {
545     u_char       *tail;
546     size_t        len;
547     ngx_int_t     rc;
548     ngx_buf_t    *b;
549     ngx_uint_t    msie_padding;
550     ngx_chain_t   out[3];
551
552     if (clcf->server_tokens) {
553         len = sizeof(ngx_http_error_full_tail) - 1;
554         tail = ngx_http_error_full_tail;
555
556     } else {
557         len = sizeof(ngx_http_error_tail) - 1;
558         tail = ngx_http_error_tail;
559     }
560
561     msie_padding = 0;
562
563     if (!r->zero_body) {
564         if (ngx_http_error_pages[err].len) {
565             r->headers_out.content_length_n = ngx_http_error_pages[err].len
566                                               + len;
567             if (clcf->msie_padding
568                 && r->headers_in.msie
569                 && r->http_version >= NGX_HTTP_VERSION_10
570                 && err >= NGX_HTTP_LEVEL_300)
571             {
572                 r->headers_out.content_length_n +=
573                                                 sizeof(ngx_http_msie_stub) - 1;
574                 msie_padding = 1;
575             }
576
577             r->headers_out.content_type_len = sizeof("text/html") - 1;
578             r->headers_out.content_type.len = sizeof("text/html") - 1;
579             r->headers_out.content_type.data = (u_char *) "text/html";
580
581         } else {
582             r->headers_out.content_length_n = -1;
583         }
584
585     } else {
586         r->headers_out.content_length_n = 0;
587         err = 0;
588     }
589
590     if (r->headers_out.content_length) {
591         r->headers_out.content_length->hash = 0;
592         r->headers_out.content_length = NULL;
593     }
594
595     ngx_http_clear_accept_ranges(r);
596     ngx_http_clear_last_modified(r);
597
598     rc = ngx_http_send_header(r);
599
600     if (rc == NGX_ERROR || r->header_only) {
601         return rc;
602     }
603
604     if (ngx_http_error_pages[err].len == 0) {
605         return NGX_OK;
606     }
607
608     b = ngx_calloc_buf(r->pool);
609     if (b == NULL) {
610         return NGX_ERROR;
611     }
612
613     b->memory = 1;
614     b->pos = ngx_http_error_pages[err].data;
615     b->last = ngx_http_error_pages[err].data + ngx_http_error_pages[err].len;
616
617     out[0].buf = b;
618     out[0].next = &out[1];
619
620     b = ngx_calloc_buf(r->pool);
621     if (b == NULL) {
622         return NGX_ERROR;
623     }
624
625     b->memory = 1;
626
627     b->pos = tail;
628     b->last = tail + len;
629
630     out[1].buf = b;
631     out[1].next = NULL;
632
633     if (msie_padding) {
634         b = ngx_calloc_buf(r->pool);
635         if (b == NULL) {
636             return NGX_ERROR;
637         }
638
639         b->memory = 1;
640         b->pos = ngx_http_msie_stub;
641         b->last = ngx_http_msie_stub + sizeof(ngx_http_msie_stub) - 1;
642
643         out[1].next = &out[2];
644         out[2].buf = b;
645         out[2].next = NULL;
646     }
647
648     if (r == r->main) {
649         b->last_buf = 1;
650     }
651
652     b->last_in_chain = 1;
653
654     return ngx_http_output_filter(r, &out[0]);
655 }
656
657
658 static ngx_int_t
659 ngx_http_send_refresh(ngx_http_request_t *r)
660 {
661     u_char       *p, *location;
662     size_t        len, size;
663     uintptr_t     escape;
664     ngx_int_t     rc;
665     ngx_buf_t    *b;
666     ngx_chain_t   out;
667
668     len = r->headers_out.location->value.len;
669     location = r->headers_out.location->value.data;
670
671     escape = 2 * ngx_escape_uri(NULL, location, len, NGX_ESCAPE_REFRESH);
672
673     size = sizeof(ngx_http_msie_refresh_head) - 1
674            + escape + len
675            + sizeof(ngx_http_msie_refresh_tail) - 1;
676
677     r->err_status = NGX_HTTP_OK;
678
679     r->headers_out.content_type_len = sizeof("text/html") - 1;
680     r->headers_out.content_type.len = sizeof("text/html") - 1;
681     r->headers_out.content_type.data = (u_char *) "text/html";
682
683     r->headers_out.location->hash = 0;
684     r->headers_out.location = NULL;
685
686     r->headers_out.content_length_n = size;
687
688     if (r->headers_out.content_length) {
689         r->headers_out.content_length->hash = 0;
690         r->headers_out.content_length = NULL;
691     }
692
693     ngx_http_clear_accept_ranges(r);
694     ngx_http_clear_last_modified(r);
695
696     rc = ngx_http_send_header(r);
697
698     if (rc == NGX_ERROR || r->header_only) {
699         return rc;
700     }
701
702     b = ngx_create_temp_buf(r->pool, size);
703     if (b == NULL) {
704         return NGX_ERROR;
705     }
706
707     p = ngx_cpymem(b->pos, ngx_http_msie_refresh_head,
708                    sizeof(ngx_http_msie_refresh_head) - 1);
709
710     if (escape == 0) {
711         p = ngx_cpymem(p, location, len);
712
713     } else {
714         p = (u_char *) ngx_escape_uri(p, location, len, NGX_ESCAPE_REFRESH);
715     }
716
717     b->last = ngx_cpymem(p, ngx_http_msie_refresh_tail,
718                          sizeof(ngx_http_msie_refresh_tail) - 1);
719
720     b->last_buf = 1;
721     b->last_in_chain = 1;
722
723     out.buf = b;
724     out.next = NULL;
725
726     return ngx_http_output_filter(r, &out);
727 }