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