Project

General

Profile

fastcgi-404-option.diff

patch: config option to send 404 if file is not present - bert, 2013-09-16 14:03

View differences:

src/mod_fastcgi.c (working copy)
241 241
	 */
242 242
	unsigned short allow_xsendfile;
243 243

  
244
	/*
245
	 * If X-Sendfile or X-Sendfile2 is used and the file doesn't exist,
246
	 * this module will send a "404 Not Found" error, just as lighttpd
247
	 * would. The default is to send a "502 Bad Gateway" error,
248
	 * indicating an error on the (possibly remote) FastCGI side.
249
	 */
250
	unsigned short allow_xsendfile_404;
251

  
244 252
	ssize_t load; /* replace by host->load */
245 253

  
246 254
	size_t max_id; /* corresponds most of the time to
......
1261 1269
						{ "strip-request-uri",  NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },      /* 13 */
1262 1270
						{ "kill-signal",        NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },       /* 14 */
1263 1271
						{ "fix-root-scriptname",   NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },  /* 15 */
1272
						{ "allow-x-send-file-404", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },  /* 16 */
1264 1273

  
1265 1274
						{ NULL,                NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
1266 1275
					};
......
1285 1294
					host->disable_time = 1;
1286 1295
					host->break_scriptfilename_for_php = 0;
1287 1296
					host->allow_xsendfile = 0; /* handle X-LIGHTTPD-send-file */
1297
					host->allow_xsendfile_404 = 0;
1288 1298
					host->kill_signal = SIGTERM;
1289 1299
					host->fix_root_path_name = 0;
1290 1300

  
......
1306 1316
					fcv[13].destination = host->strip_request_uri;
1307 1317
					fcv[14].destination = &(host->kill_signal);
1308 1318
					fcv[15].destination = &(host->fix_root_path_name);
1319
					fcv[16].destination = &(host->allow_xsendfile_404);
1309 1320

  
1310 1321
					if (0 != config_insert_values_internal(srv, da_host->value, fcv)) {
1311 1322
						return HANDLER_ERROR;
......
2268 2279
						if (p->conf.debug) {
2269 2280
							log_error_write(srv, __FILE__, __LINE__, "ss", "Couldn't find range after filename:", filename);
2270 2281
						}
2271
						return 1;
2282
						return 502;
2272 2283
					}
2273 2284
					buffer_copy_string_len(srv->tmp_buf, filename, range - filename);
2274 2285

  
......
2282 2293
								"send-file error: couldn't get stat_cache entry for X-Sendfile2:",
2283 2294
								srv->tmp_buf);
2284 2295
						}
2285
						return 1;
2296
						return host->allow_xsendfile_404 ? 404 : 502;
2286 2297
					} else if (!S_ISREG(sce->st.st_mode)) {
2287 2298
						if (p->conf.debug) {
2288 2299
							log_error_write(srv, __FILE__, __LINE__, "sb",
2289 2300
								"send-file error: wrong filetype for X-Sendfile2:",
2290 2301
								srv->tmp_buf);
2291 2302
						}
2292
						return 1;
2303
						return 502;
2293 2304
					}
2294 2305
					/* found the file */
2295 2306

  
......
2314 2325
						if (p->conf.debug) {
2315 2326
							log_error_write(srv, __FILE__, __LINE__, "ss", "Couldn't decode range after filename:", filename);
2316 2327
						}
2317
						return 1;
2328
						return 502;
2318 2329

  
2319 2330
range_success: ;
2320 2331
					}
......
2322 2333
					/* no parameters accepted */
2323 2334

  
2324 2335
					while (*pos == ' ') pos++;
2325
					if (*pos != '\0' && *pos != ',') return 1;
2336
					if (*pos != '\0' && *pos != ',') return 502;
2326 2337

  
2327 2338
					range_len = end_range - begin_range + 1;
2328
					if (range_len < 0) return 1;
2339
					if (range_len < 0) return 502;
2329 2340
					if (range_len != 0) {
2330 2341
						http_chunk_append_file(srv, con, srv->tmp_buf, begin_range, range_len);
2331 2342
					}
......
2484 2495

  
2485 2496
static int fcgi_demux_response(server *srv, handler_ctx *hctx) {
2486 2497
	int fin = 0;
2487
	int toread;
2498
	int toread, ret;
2488 2499
	ssize_t r;
2489 2500

  
2490 2501
	plugin_data *p    = hctx->plugin_data;
......
2596 2607
				}
2597 2608

  
2598 2609
				/* parse the response header */
2599
				if (fcgi_response_parse(srv, con, p, hctx->response_header)) {
2600
					con->http_status = 502;
2610
				if ((ret = fcgi_response_parse(srv, con, p, hctx->response_header))) {
2611
					con->http_status = ret;
2601 2612
					hctx->send_content_body = 0;
2602 2613
					con->file_started = 1;
2603 2614
					break;
......
2635 2646
						con->parsed_response |= HTTP_CONTENT_LENGTH;
2636 2647
						con->response.content_length = sce->st.st_size;
2637 2648
					} else {
2638
						log_error_write(srv, __FILE__, __LINE__, "sb",
2639
							"send-file error: couldn't get stat_cache entry for:",
2640
							ds->value);
2641
						con->http_status = 502;
2642
						hctx->send_content_body = 0;
2643
						con->file_started = 1;
2644
						break;
2649
						if (host->allow_xsendfile_404) {
2650
							con->http_status = 404;
2651
							hctx->send_content_body = 0;
2652
							con->file_started = 1;
2653
							break;
2654
						} else {
2655
							log_error_write(srv, __FILE__, __LINE__, "sb",
2656
								"send-file error: couldn't get stat_cache entry for:",
2657
								ds->value);
2658
							con->http_status = 502;
2659
							hctx->send_content_body = 0;
2660
							con->file_started = 1;
2661
							break;
2662
						}
2645 2663
					}
2646 2664
				}
2647 2665