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