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