xref: /lighttpd1.4/src/http-header-glue.c (revision f2e4e83a)
1 #include "first.h"
2 
3 #include "base.h"
4 #include "array.h"
5 #include "buffer.h"
6 #include "chunk.h"
7 #include "fdevent.h"
8 #include "log.h"
9 #include "etag.h"
10 #include "http_chunk.h"
11 #include "http_header.h"
12 #include "response.h"
13 #include "sock_addr.h"
14 #include "stat_cache.h"
15 
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 
21 #include <time.h>
22 
23 #include "sys-socket.h"
24 #include <unistd.h>
25 
26 /**
27  * max size of the HTTP response header from backends
28  * (differs from server.max-request-field-size for max request field size)
29  */
30 #define MAX_HTTP_RESPONSE_FIELD_SIZE 65535
31 
32 
33 __attribute_cold__
34 static int http_response_buffer_append_authority(request_st * const r, buffer * const o) {
35 	if (!buffer_string_is_empty(&r->uri.authority)) {
36 		buffer_append_string_buffer(o, &r->uri.authority);
37 	} else {
38 		/* get the name of the currently connected socket */
39 		sock_addr our_addr;
40 		socklen_t our_addr_len;
41 
42 		our_addr.plain.sa_family = 0;
43 		our_addr_len = sizeof(our_addr);
44 
45 		if (-1 == getsockname(r->con->fd, (struct sockaddr *)&our_addr, &our_addr_len)
46 		    || our_addr_len > (socklen_t)sizeof(our_addr)) {
47 			r->http_status = 500;
48 			log_perror(r->conf.errh, __FILE__, __LINE__, "can't get sockname");
49 			return -1;
50 		}
51 
52 		if (our_addr.plain.sa_family == AF_INET
53 		    && our_addr.ipv4.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
54 			static char lhost[32];
55 			static size_t lhost_len = 0;
56 			if (0 != lhost_len) {
57 				buffer_append_string_len(o, lhost, lhost_len);
58 			}
59 			else {
60 				size_t olen = buffer_string_length(o);
61 				if (0 == sock_addr_nameinfo_append_buffer(o, &our_addr, r->conf.errh)) {
62 					lhost_len = buffer_string_length(o) - olen;
63 					if (lhost_len < sizeof(lhost)) {
64 						memcpy(lhost, o->ptr+olen, lhost_len+1); /*(+1 for '\0')*/
65 					}
66 					else {
67 						lhost_len = 0;
68 					}
69 				}
70 				else {
71 					lhost_len = sizeof("localhost")-1;
72 					memcpy(lhost, "localhost", lhost_len+1); /*(+1 for '\0')*/
73 					buffer_append_string_len(o, lhost, lhost_len);
74 				}
75 			}
76 		} else if (!buffer_string_is_empty(r->server_name)) {
77 			buffer_append_string_buffer(o, r->server_name);
78 		} else
79 		/* Lookup name: secondly try to get hostname for bind address */
80 		if (0 != sock_addr_nameinfo_append_buffer(o, &our_addr, r->conf.errh)) {
81 			r->http_status = 500;
82 			return -1;
83 		}
84 
85 		{
86 			unsigned short listen_port = sock_addr_get_port(&our_addr);
87 			unsigned short default_port = 80;
88 			if (buffer_is_equal_string(&r->uri.scheme, CONST_STR_LEN("https"))) {
89 				default_port = 443;
90 			}
91 			if (0 == listen_port) listen_port = r->con->srv->srvconf.port;
92 			if (default_port != listen_port) {
93 				buffer_append_string_len(o, CONST_STR_LEN(":"));
94 				buffer_append_int(o, listen_port);
95 			}
96 		}
97 	}
98 	return 0;
99 }
100 
101 int http_response_redirect_to_directory(request_st * const r, int status) {
102 	buffer *o = r->tmp_buf;
103 	buffer_clear(o);
104 	/* XXX: store flag in global at startup? */
105 	if (r->con->srv->srvconf.absolute_dir_redirect) {
106 		buffer_copy_buffer(o, &r->uri.scheme);
107 		buffer_append_string_len(o, CONST_STR_LEN("://"));
108 		if (0 != http_response_buffer_append_authority(r, o)) {
109 			return -1;
110 		}
111 	}
112 	buffer_append_string_encoded(o, CONST_BUF_LEN(&r->uri.path), ENCODING_REL_URI);
113 	buffer_append_string_len(o, CONST_STR_LEN("/"));
114 	if (!buffer_string_is_empty(&r->uri.query)) {
115 		buffer_append_string_len(o, CONST_STR_LEN("?"));
116 		buffer_append_string_buffer(o, &r->uri.query);
117 	}
118 
119 	if (status >= 300) {
120 		http_header_response_set(r, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"), CONST_BUF_LEN(o));
121 		r->http_status = status;
122 		r->resp_body_finished = 1;
123 	}
124 	else {
125 		http_header_response_set(r, HTTP_HEADER_CONTENT_LOCATION, CONST_STR_LEN("Content-Location"), CONST_BUF_LEN(o));
126 	}
127 
128 	return 0;
129 }
130 
131 #define MTIME_CACHE_MAX 16
132 struct mtime_cache_type {
133     time_t mtime;  /* key */
134     buffer str;    /* buffer for string representation */
135 };
136 static struct mtime_cache_type mtime_cache[MTIME_CACHE_MAX];
137 static char mtime_cache_str[MTIME_CACHE_MAX][30];
138 /* 30-chars for "%a, %d %b %Y %H:%M:%S GMT" */
139 
140 void strftime_cache_reset(void) {
141     for (int i = 0; i < MTIME_CACHE_MAX; ++i) {
142         mtime_cache[i].mtime = (time_t)-1;
143         mtime_cache[i].str.ptr = mtime_cache_str[i];
144         mtime_cache[i].str.used = sizeof(mtime_cache_str[0]);
145         mtime_cache[i].str.size = sizeof(mtime_cache_str[0]);
146     }
147 }
148 
149 const buffer * strftime_cache_get(const time_t last_mod) {
150     static int mtime_cache_idx;
151 
152     for (int j = 0; j < MTIME_CACHE_MAX; ++j) {
153         if (mtime_cache[j].mtime == last_mod)
154             return &mtime_cache[j].str; /* found cache-entry */
155     }
156 
157     if (++mtime_cache_idx == MTIME_CACHE_MAX) mtime_cache_idx = 0;
158 
159     const int i = mtime_cache_idx;
160     mtime_cache[i].mtime = last_mod;
161     strftime(mtime_cache[i].str.ptr, sizeof(mtime_cache_str[0]),
162              "%a, %d %b %Y %H:%M:%S GMT", gmtime(&mtime_cache[i].mtime));
163 
164     return &mtime_cache[i].str;
165 }
166 
167 
168 int http_response_handle_cachable(request_st * const r, const buffer * const mtime) {
169 	const buffer *vb;
170 
171 	/*
172 	 * 14.26 If-None-Match
173 	 *    [...]
174 	 *    If none of the entity tags match, then the server MAY perform the
175 	 *    requested method as if the If-None-Match header field did not exist,
176 	 *    but MUST also ignore any If-Modified-Since header field(s) in the
177 	 *    request. That is, if no entity tags match, then the server MUST NOT
178 	 *    return a 304 (Not Modified) response.
179 	 */
180 
181 	if ((vb = http_header_request_get(r, HTTP_HEADER_IF_NONE_MATCH, CONST_STR_LEN("If-None-Match")))) {
182 		/*(weak etag comparison must not be used for ranged requests)*/
183 		int range_request =
184 		  (r->conf.range_requests
185 		   && (200 == r->http_status || 0 == r->http_status)
186 		   && NULL != http_header_request_get(r, HTTP_HEADER_RANGE, CONST_STR_LEN("Range")));
187 		if (etag_is_equal(&r->physical.etag, vb->ptr, !range_request)) {
188 			if (http_method_get_or_head(r->http_method)) {
189 				r->http_status = 304;
190 				return HANDLER_FINISHED;
191 			} else {
192 				r->http_status = 412;
193 				r->handler_module = NULL;
194 				return HANDLER_FINISHED;
195 			}
196 		}
197 	} else if (http_method_get_or_head(r->http_method)
198 		   && (vb = http_header_request_get(r, HTTP_HEADER_IF_MODIFIED_SINCE, CONST_STR_LEN("If-Modified-Since")))) {
199 		/* last-modified handling */
200 		size_t used_len;
201 		char *semicolon;
202 
203 		if (NULL == (semicolon = strchr(vb->ptr, ';'))) {
204 			used_len = buffer_string_length(vb);
205 		} else {
206 			used_len = semicolon - vb->ptr;
207 		}
208 
209 		if (buffer_is_equal_string(mtime, vb->ptr, used_len)) {
210 			if ('\0' == mtime->ptr[used_len]) r->http_status = 304;
211 			return HANDLER_FINISHED;
212 		} else {
213 			char buf[sizeof("Sat, 23 Jul 2005 21:20:01 GMT")];
214 			time_t t_header, t_file;
215 			struct tm tm;
216 
217 			/* convert to timestamp */
218 			if (used_len >= sizeof(buf)) return HANDLER_GO_ON;
219 
220 			memcpy(buf, vb->ptr, used_len);
221 			buf[used_len] = '\0';
222 
223 			if (NULL == strptime(buf, "%a, %d %b %Y %H:%M:%S GMT", &tm)) {
224 				/**
225 				 * parsing failed, let's get out of here
226 				 */
227 				return HANDLER_GO_ON;
228 			}
229 			tm.tm_isdst = 0;
230 			t_header = mktime(&tm);
231 
232 			strptime(mtime->ptr, "%a, %d %b %Y %H:%M:%S GMT", &tm);
233 			tm.tm_isdst = 0;
234 			t_file = mktime(&tm);
235 
236 			if (t_file > t_header) return HANDLER_GO_ON;
237 
238 			r->http_status = 304;
239 			return HANDLER_FINISHED;
240 		}
241 	}
242 
243 	return HANDLER_GO_ON;
244 }
245 
246 
247 void http_response_body_clear (request_st * const r, int preserve_length) {
248     r->resp_send_chunked = 0;
249     if (r->resp_htags & HTTP_HEADER_TRANSFER_ENCODING) {
250         http_header_response_unset(r, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"));
251     }
252     if (!preserve_length) { /* preserve for HEAD responses and no-content responses (204, 205, 304) */
253         r->content_length = -1;
254         if (r->resp_htags & HTTP_HEADER_CONTENT_LENGTH) {
255             http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
256         }
257         /*(if not preserving Content-Length, do not preserve trailers, if any)*/
258         r->resp_decode_chunked = 0;
259         if (r->gw_dechunk) {
260             free(r->gw_dechunk->b.ptr);
261             free(r->gw_dechunk);
262             r->gw_dechunk = NULL;
263         }
264     }
265     chunkqueue_reset(r->write_queue);
266 }
267 
268 
269 void http_response_reset (request_st * const r) {
270     r->http_status = 0;
271     r->con->is_writable = 1;
272     r->resp_body_finished = 0;
273     r->resp_body_started = 0;
274     r->handler_module = NULL;
275     if (r->physical.path.ptr) { /*(skip for mod_fastcgi authorizer)*/
276         buffer_clear(&r->physical.doc_root);
277         buffer_clear(&r->physical.basedir);
278         buffer_clear(&r->physical.etag);
279         buffer_reset(&r->physical.path);
280         buffer_reset(&r->physical.rel_path);
281     }
282     r->resp_htags = 0;
283     r->resp_header_len = 0;
284     r->resp_header_repeated = 0;
285     array_reset_data_strings(&r->resp_headers);
286     http_response_body_clear(r, 0);
287 }
288 
289 
290 handler_t http_response_reqbody_read_error (request_st * const r, int http_status) {
291     r->keep_alive = 0;
292 
293     /*(do not change status if response headers already set and possibly sent)*/
294     if (0 != r->resp_header_len) return HANDLER_ERROR;
295 
296     http_response_body_clear(r, 0);
297     r->http_status = http_status;
298     r->handler_module = NULL;
299     return HANDLER_FINISHED;
300 }
301 
302 
303 static int http_response_parse_range(request_st * const r, buffer * const path, stat_cache_entry * const sce, const char * const range) {
304 	int multipart = 0;
305 	int error;
306 	off_t start, end;
307 	const char *s, *minus;
308 	static const char boundary[] = "fkj49sn38dcn3";
309 	const buffer *content_type = http_header_response_get(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"));
310 
311 	start = 0;
312 	end = sce->st.st_size - 1;
313 
314 	r->content_length = 0;
315 
316 	for (s = range, error = 0;
317 	     !error && *s && NULL != (minus = strchr(s, '-')); ) {
318 		char *err;
319 		off_t la = 0, le;
320 		*((const char **)&err) = s; /*(quiet clang --analyze)*/
321 
322 		if (s != minus) {
323 			la = strtoll(s, &err, 10);
324 			if (err != minus) {
325 				/* should not have multiple range-unit in Range, but
326 				 * handle just in case multiple Range headers merged */
327 				while (*s == ' ' || *s == '\t') ++s;
328 				if (0 != strncmp(s, "bytes=", 6)) return -1;
329 				s += 6;
330 				if (s != minus) {
331 					la = strtoll(s, &err, 10);
332 					if (err != minus) return -1;
333 				}
334 			}
335 		}
336 
337 		if (s == minus) {
338 			/* -<stop> */
339 
340 			le = strtoll(s, &err, 10);
341 
342 			if (le == 0) {
343 				/* RFC 2616 - 14.35.1 */
344 
345 				r->http_status = 416;
346 				error = 1;
347 			} else if (*err == '\0') {
348 				/* end */
349 				s = err;
350 
351 				end = sce->st.st_size - 1;
352 				start = sce->st.st_size + le;
353 			} else if (*err == ',') {
354 				multipart = 1;
355 				s = err + 1;
356 
357 				end = sce->st.st_size - 1;
358 				start = sce->st.st_size + le;
359 			} else {
360 				error = 1;
361 			}
362 
363 		} else if (*(minus+1) == '\0' || *(minus+1) == ',') {
364 			/* <start>- */
365 
366 				/* ok */
367 
368 				if (*(err + 1) == '\0') {
369 					s = err + 1;
370 
371 					end = sce->st.st_size - 1;
372 					start = la;
373 
374 				} else if (*(err + 1) == ',') {
375 					multipart = 1;
376 					s = err + 2;
377 
378 					end = sce->st.st_size - 1;
379 					start = la;
380 				} else {
381 					error = 1;
382 				}
383 		} else {
384 			/* <start>-<stop> */
385 
386 				le = strtoll(minus+1, &err, 10);
387 
388 				/* RFC 2616 - 14.35.1 */
389 				if (la > le) {
390 					error = 1;
391 				}
392 
393 				if (*err == '\0') {
394 					/* ok, end*/
395 					s = err;
396 
397 					end = le;
398 					start = la;
399 				} else if (*err == ',') {
400 					multipart = 1;
401 					s = err + 1;
402 
403 					end = le;
404 					start = la;
405 				} else {
406 					/* error */
407 
408 					error = 1;
409 				}
410 		}
411 
412 		if (!error) {
413 			if (start < 0) start = 0;
414 
415 			/* RFC 2616 - 14.35.1 */
416 			if (end > sce->st.st_size - 1) end = sce->st.st_size - 1;
417 
418 			if (start > sce->st.st_size - 1) {
419 				error = 1;
420 
421 				r->http_status = 416;
422 			}
423 		}
424 
425 		if (!error) {
426 			if (multipart) {
427 				/* write boundary-header */
428 				buffer *b = r->tmp_buf;
429 				buffer_copy_string_len(b, CONST_STR_LEN("\r\n--"));
430 				buffer_append_string_len(b, boundary, sizeof(boundary)-1);
431 
432 				/* write Content-Range */
433 				buffer_append_string_len(b, CONST_STR_LEN("\r\nContent-Range: bytes "));
434 				buffer_append_int(b, start);
435 				buffer_append_string_len(b, CONST_STR_LEN("-"));
436 				buffer_append_int(b, end);
437 				buffer_append_string_len(b, CONST_STR_LEN("/"));
438 				buffer_append_int(b, sce->st.st_size);
439 
440 				if (content_type) {
441 					buffer_append_string_len(b, CONST_STR_LEN("\r\nContent-Type: "));
442 					buffer_append_string_buffer(b, content_type);
443 				}
444 
445 				/* write END-OF-HEADER */
446 				buffer_append_string_len(b, CONST_STR_LEN("\r\n\r\n"));
447 
448 				r->content_length += buffer_string_length(b);
449 				chunkqueue_append_mem(r->write_queue, CONST_BUF_LEN(b));
450 			}
451 
452 			chunkqueue_append_file(r->write_queue, path, start, end - start + 1);
453 			r->content_length += end - start + 1;
454 		}
455 	}
456 
457 	/* something went wrong */
458 	if (error) return -1;
459 
460 	buffer * const tb = r->tmp_buf;
461 
462 	if (multipart) {
463 		/* add boundary end */
464 		buffer_copy_string_len(tb, "\r\n--", 4);
465 		buffer_append_string_len(tb, boundary, sizeof(boundary)-1);
466 		buffer_append_string_len(tb, "--\r\n", 4);
467 
468 		r->content_length += buffer_string_length(tb);
469 		chunkqueue_append_mem(r->write_queue, CONST_BUF_LEN(tb));
470 
471 		/* set header-fields */
472 
473 		buffer_copy_string_len(tb, CONST_STR_LEN("multipart/byteranges; boundary="));
474 		buffer_append_string_len(tb, boundary, sizeof(boundary)-1);
475 
476 		/* overwrite content-type */
477 		http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(tb));
478 	} else {
479 		/* add Content-Range-header */
480 
481 		buffer_copy_string_len(tb, CONST_STR_LEN("bytes "));
482 		buffer_append_int(tb, start);
483 		buffer_append_string_len(tb, CONST_STR_LEN("-"));
484 		buffer_append_int(tb, end);
485 		buffer_append_string_len(tb, CONST_STR_LEN("/"));
486 		buffer_append_int(tb, sce->st.st_size);
487 
488 		http_header_response_set(r, HTTP_HEADER_OTHER, CONST_STR_LEN("Content-Range"), CONST_BUF_LEN(tb));
489 	}
490 
491 	/* ok, the file is set-up */
492 	return 0;
493 }
494 
495 
496 void http_response_send_file (request_st * const r, buffer * const path) {
497 	stat_cache_entry * const sce = stat_cache_get_entry(path);
498 	const buffer *mtime = NULL;
499 	const buffer *vb;
500 	int allow_caching = (0 == r->http_status || 200 == r->http_status);
501 
502 	if (NULL == sce) {
503 		r->http_status = (errno == ENOENT) ? 404 : 403;
504 		log_error(r->conf.errh, __FILE__, __LINE__,
505 		  "not a regular file: %s -> %s", r->uri.path.ptr, path->ptr);
506 		return;
507 	}
508 
509 	if (!r->conf.follow_symlink
510 	    && 0 != stat_cache_path_contains_symlink(path, r->conf.errh)) {
511 		r->http_status = 403;
512 		if (r->conf.log_request_handling) {
513 			log_error(r->conf.errh, __FILE__, __LINE__,
514 			  "-- access denied due symlink restriction");
515 			log_error(r->conf.errh, __FILE__, __LINE__,
516 			  "Path         : %s", path->ptr);
517 		}
518 		return;
519 	}
520 
521 	/* we only handle regular files */
522 	if (!S_ISREG(sce->st.st_mode)) {
523 		r->http_status = 403;
524 		if (r->conf.log_file_not_found) {
525 			log_error(r->conf.errh, __FILE__, __LINE__,
526 			  "not a regular file: %s -> %s",
527 			  r->uri.path.ptr, path->ptr);
528 		}
529 		return;
530 	}
531 
532 	/*(Note: O_NOFOLLOW affects only the final path segment,
533 	 * the target file, not any intermediate symlinks along path)*/
534 	const int fd = (0 != sce->st.st_size)
535 	  ? fdevent_open_cloexec(path->ptr, r->conf.follow_symlink, O_RDONLY, 0)
536 	  : -1;
537 	if (fd < 0 && 0 != sce->st.st_size) {
538 		r->http_status = (errno == ENOENT) ? 404 : 403;
539 		if (r->conf.log_request_handling) {
540 			log_perror(r->conf.errh, __FILE__, __LINE__,
541 			  "file open failed: %s", path->ptr);
542 		}
543 		return;
544 	}
545 
546 	/* set response content-type, if not set already */
547 
548 	if (NULL == http_header_response_get(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"))) {
549 		const buffer *content_type = stat_cache_content_type_get(sce, r);
550 		if (buffer_string_is_empty(content_type)) {
551 			/* we are setting application/octet-stream, but also announce that
552 			 * this header field might change in the seconds few requests
553 			 *
554 			 * This should fix the aggressive caching of FF and the script download
555 			 * seen by the first installations
556 			 */
557 			http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("application/octet-stream"));
558 
559 			allow_caching = 0;
560 		} else {
561 			http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(content_type));
562 		}
563 	}
564 
565 	if (r->conf.range_requests) {
566 		http_header_response_append(r, HTTP_HEADER_OTHER, CONST_STR_LEN("Accept-Ranges"), CONST_STR_LEN("bytes"));
567 	}
568 
569 	if (allow_caching) {
570 		const buffer *etag = (0 != r->conf.etag_flags)
571 		  ? stat_cache_etag_get(sce, r->conf.etag_flags)
572 		  : NULL;
573 		if (!buffer_string_is_empty(etag)) {
574 			if (NULL == http_header_response_get(r, HTTP_HEADER_ETAG, CONST_STR_LEN("ETag"))) {
575 				/* generate e-tag */
576 				etag_mutate(&r->physical.etag, etag);
577 
578 				http_header_response_set(r, HTTP_HEADER_ETAG, CONST_STR_LEN("ETag"), CONST_BUF_LEN(&r->physical.etag));
579 			}
580 		}
581 
582 		/* prepare header */
583 		if (NULL == (mtime = http_header_response_get(r, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified")))) {
584 			mtime = strftime_cache_get(sce->st.st_mtime);
585 			http_header_response_set(r, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
586 		}
587 
588 		if (HANDLER_FINISHED == http_response_handle_cachable(r, mtime)) {
589 			if (fd >= 0) close(fd);
590 			return;
591 		}
592 	}
593 
594 	if (fd < 0) { /* 0-length file */
595 		r->http_status = 200;
596 		r->resp_body_finished = 1;
597 		return;
598 	}
599 
600 	if (r->conf.range_requests
601 	    && (200 == r->http_status || 0 == r->http_status)
602 	    && NULL != (vb = http_header_request_get(r, HTTP_HEADER_RANGE, CONST_STR_LEN("Range")))
603 	    && NULL == http_header_response_get(r, HTTP_HEADER_CONTENT_ENCODING, CONST_STR_LEN("Content-Encoding"))) {
604 		const buffer *range = vb;
605 		int do_range_request = 1;
606 		/* check if we have a conditional GET */
607 
608 		if (NULL != (vb = http_header_request_get(r, HTTP_HEADER_OTHER, CONST_STR_LEN("If-Range")))) {
609 			/* if the value is the same as our ETag, we do a Range-request,
610 			 * otherwise a full 200 */
611 
612 			if (vb->ptr[0] == '"') {
613 				/**
614 				 * client wants a ETag
615 				 */
616 				if (!buffer_is_equal(vb, &r->physical.etag)) {
617 					do_range_request = 0;
618 				}
619 			} else if (!mtime) {
620 				/**
621 				 * we don't have a Last-Modified and can match the If-Range:
622 				 *
623 				 * sending all
624 				 */
625 				do_range_request = 0;
626 			} else if (!buffer_is_equal(vb, mtime)) {
627 				do_range_request = 0;
628 			}
629 		}
630 
631 		if (do_range_request
632 		    && !buffer_string_is_empty(range)
633 		    && 0 == strncmp(range->ptr, "bytes=", 6)) {
634 			/* support only "bytes" byte-unit */
635 			/* content prepared, I'm done */
636 			r->resp_body_finished = 1;
637 
638 			if (0 == http_response_parse_range(r, path, sce, range->ptr+6)) {
639 				r->http_status = 206;
640 			}
641 			close(fd);
642 			return;
643 		}
644 	}
645 
646 	/* if we are still here, prepare body */
647 
648 	/* we add it here for all requests
649 	 * the HEAD request will drop it afterwards again
650 	 */
651 
652 	if (0 == http_chunk_append_file_fd(r, path, fd, sce->st.st_size)) {
653 		r->http_status = 200;
654 		r->resp_body_finished = 1;
655 	}
656 	else {
657 		r->http_status = 500;
658 	}
659 }
660 
661 
662 static void http_response_xsendfile (request_st * const r, buffer * const path, const array * const xdocroot) {
663 	const int status = r->http_status;
664 	int valid = 1;
665 
666 	/* reset Content-Length, if set by backend
667 	 * Content-Length might later be set to size of X-Sendfile static file,
668 	 * determined by open(), fstat() to reduces race conditions if the file
669 	 * is modified between stat() (stat_cache_get_entry()) and open(). */
670 	if (r->resp_htags & HTTP_HEADER_CONTENT_LENGTH) {
671 		http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
672 		r->content_length = -1;
673 	}
674 
675 	buffer_urldecode_path(path);
676 	if (!buffer_is_valid_UTF8(path)) {
677 		log_error(r->conf.errh, __FILE__, __LINE__,
678 		  "X-Sendfile invalid UTF-8 after url-decode: %s", path->ptr);
679 		if (r->http_status < 400) {
680 			r->http_status = 502;
681 			r->handler_module = NULL;
682 		}
683 		return;
684 	}
685 	buffer_path_simplify(path, path);
686 	if (r->conf.force_lowercase_filenames) {
687 		buffer_to_lower(path);
688 	}
689 
690 	/* check that path is under xdocroot(s)
691 	 * - xdocroot should have trailing slash appended at config time
692 	 * - r->conf.force_lowercase_filenames is not a server-wide setting,
693 	 *   and so can not be definitively applied to xdocroot at config time*/
694 	if (xdocroot) {
695 		size_t i, xlen = buffer_string_length(path);
696 		for (i = 0; i < xdocroot->used; ++i) {
697 			data_string *ds = (data_string *)xdocroot->data[i];
698 			size_t dlen = buffer_string_length(&ds->value);
699 			if (dlen <= xlen
700 			    && (!r->conf.force_lowercase_filenames
701 				? 0 == memcmp(path->ptr, ds->value.ptr, dlen)
702 				: buffer_eq_icase_ssn(path->ptr, ds->value.ptr, dlen))) {
703 				break;
704 			}
705 		}
706 		if (i == xdocroot->used && 0 != i) {
707 			log_error(r->conf.errh, __FILE__, __LINE__,
708 			  "X-Sendfile (%s) not under configured x-sendfile-docroot(s)", path->ptr);
709 			r->http_status = 403;
710 			valid = 0;
711 		}
712 	}
713 
714 	if (valid) http_response_send_file(r, path);
715 
716 	if (r->http_status >= 400 && status < 300) {
717 		r->handler_module = NULL;
718 	} else if (0 != status && 200 != status) {
719 		r->http_status = status;
720 	}
721 }
722 
723 
724 static void http_response_xsendfile2(request_st * const r, const buffer * const value, const array * const xdocroot) {
725     const char *pos = value->ptr;
726     buffer * const b = r->tmp_buf;
727     const int status = r->http_status;
728 
729     /* reset Content-Length, if set by backend */
730     if (r->resp_htags & HTTP_HEADER_CONTENT_LENGTH) {
731         http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
732         r->content_length = -1;
733     }
734 
735     while (*pos) {
736         const char *filename, *range;
737         stat_cache_entry *sce;
738         off_t begin_range, end_range, range_len;
739 
740         while (' ' == *pos) pos++;
741         if (!*pos) break;
742 
743         filename = pos;
744         if (NULL == (range = strchr(pos, ' '))) {
745             /* missing range */
746             log_error(r->conf.errh, __FILE__, __LINE__,
747               "Couldn't find range after filename: %s", filename);
748             r->http_status = 502;
749             break;
750         }
751         buffer_copy_string_len(b, filename, range - filename);
752 
753         /* find end of range */
754         for (pos = ++range; *pos && *pos != ' ' && *pos != ','; pos++) ;
755 
756         buffer_urldecode_path(b);
757         if (!buffer_is_valid_UTF8(b)) {
758             log_error(r->conf.errh, __FILE__, __LINE__,
759               "X-Sendfile2 invalid UTF-8 after url-decode: %s", b->ptr);
760             r->http_status = 502;
761             break;
762         }
763         buffer_path_simplify(b, b);
764         if (r->conf.force_lowercase_filenames) {
765             buffer_to_lower(b);
766         }
767         if (xdocroot) {
768             size_t i, xlen = buffer_string_length(b);
769             for (i = 0; i < xdocroot->used; ++i) {
770                 data_string *ds = (data_string *)xdocroot->data[i];
771                 size_t dlen = buffer_string_length(&ds->value);
772                 if (dlen <= xlen
773                     && (!r->conf.force_lowercase_filenames
774                     ? 0 == memcmp(b->ptr, ds->value.ptr, dlen)
775                     : buffer_eq_icase_ssn(b->ptr, ds->value.ptr, dlen))) {
776                     break;
777                 }
778             }
779             if (i == xdocroot->used && 0 != i) {
780                 log_error(r->conf.errh, __FILE__, __LINE__,
781                   "X-Sendfile2 (%s) not under configured x-sendfile-docroot(s)",
782                   b->ptr);
783                 r->http_status = 403;
784                 break;
785             }
786         }
787 
788         sce = stat_cache_get_entry(b);
789         if (NULL == sce) {
790             log_error(r->conf.errh, __FILE__, __LINE__,
791               "send-file error: couldn't get stat_cache entry for "
792               "X-Sendfile2: %s", b->ptr);
793             r->http_status = 404;
794             break;
795         } else if (!S_ISREG(sce->st.st_mode)) {
796             log_error(r->conf.errh, __FILE__, __LINE__,
797               "send-file error: wrong filetype for X-Sendfile2: %s", b->ptr);
798             r->http_status = 502;
799             break;
800         }
801         /* found the file */
802 
803         /* parse range */
804         end_range = sce->st.st_size - 1;
805         {
806             char *rpos = NULL;
807             errno = 0;
808             begin_range = strtoll(range, &rpos, 10);
809             if (errno != 0 || begin_range < 0 || rpos == range)
810                 goto range_failed;
811             if ('-' != *rpos++) goto range_failed;
812             if (rpos != pos) {
813                 range = rpos;
814                 end_range = strtoll(range, &rpos, 10);
815                 if (errno != 0 || end_range < 0 || rpos == range)
816                     goto range_failed;
817             }
818             if (rpos != pos) goto range_failed;
819 
820             goto range_success;
821 
822 range_failed:
823             log_error(r->conf.errh, __FILE__, __LINE__,
824               "Couldn't decode range after filename: %s", filename);
825             r->http_status = 502;
826             break;
827 
828 range_success: ;
829         }
830 
831         /* no parameters accepted */
832 
833         while (*pos == ' ') pos++;
834         if (*pos != '\0' && *pos != ',') {
835             r->http_status = 502;
836             break;
837         }
838 
839         range_len = end_range - begin_range + 1;
840         if (range_len < 0) {
841             r->http_status = 502;
842             break;
843         }
844         if (range_len != 0) {
845             if (0 !=
846                 http_chunk_append_file_range(r, b, begin_range, range_len)) {
847                 r->http_status = 502;
848                 break;
849             }
850         }
851 
852         if (*pos == ',') pos++;
853     }
854 
855     if (r->http_status >= 400 && status < 300) {
856 	r->handler_module = NULL;
857     } else if (0 != status && 200 != status) {
858         r->http_status = status;
859     }
860 }
861 
862 
863 void http_response_backend_error (request_st * const r) {
864 	if (r->resp_body_started) {
865 		/*(response might have been already started, kill the connection)*/
866 		/*(mode == DIRECT to avoid later call to http_response_backend_done())*/
867 		r->handler_module = NULL;  /*(avoid sending final chunked block)*/
868 		r->keep_alive = 0;
869 		r->resp_body_finished = 1;
870 	} /*(else error status set later by http_response_backend_done())*/
871 }
872 
873 void http_response_backend_done (request_st * const r) {
874 	/* (not CON_STATE_ERROR and not CON_STATE_RESPONSE_END,
875 	 *  i.e. not called from handle_connection_close or handle_request_reset
876 	 *  hooks, except maybe from errdoc handler, which later resets state)*/
877 	switch (r->state) {
878 	case CON_STATE_HANDLE_REQUEST:
879 	case CON_STATE_READ_POST:
880 		if (!r->resp_body_started) {
881 			/* Send an error if we haven't sent any data yet */
882 			r->http_status = 500;
883 			r->handler_module = NULL;
884 			break;
885 		} /* else fall through */
886 	case CON_STATE_WRITE:
887 		if (!r->resp_body_finished) {
888 			if (r->http_version == HTTP_VERSION_1_1)
889 				http_chunk_close(r);
890 			r->resp_body_finished = 1;
891 		}
892 	default:
893 		break;
894 	}
895 }
896 
897 
898 void http_response_upgrade_read_body_unknown(request_st * const r) {
899     /* act as transparent proxy */
900     if (!(r->conf.stream_request_body & FDEVENT_STREAM_REQUEST))
901         r->conf.stream_request_body |=
902           (FDEVENT_STREAM_REQUEST_BUFMIN | FDEVENT_STREAM_REQUEST);
903     if (!(r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE))
904         r->conf.stream_response_body |=
905           (FDEVENT_STREAM_RESPONSE_BUFMIN | FDEVENT_STREAM_RESPONSE);
906     r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
907     r->reqbody_length = -2;
908     r->keep_alive = 0;
909 }
910 
911 
912 static handler_t http_response_process_local_redir(request_st * const r, size_t blen) {
913     /* [RFC3875] The Common Gateway Interface (CGI) Version 1.1
914      * [RFC3875] 6.2.2 Local Redirect Response
915      *
916      *    The CGI script can return a URI path and query-string
917      *    ('local-pathquery') for a local resource in a Location header field.
918      *    This indicates to the server that it should reprocess the request
919      *    using the path specified.
920      *
921      *      local-redir-response = local-Location NL
922      *
923      *    The script MUST NOT return any other header fields or a message-body,
924      *    and the server MUST generate the response that it would have produced
925      *    in response to a request containing the URL
926      *
927      *      scheme "://" server-name ":" server-port local-pathquery
928      *
929      * (Might not have begun to receive body yet, but do skip local-redir
930      *  if we already have started receiving a response body (blen > 0))
931      * (Also, while not required by the RFC, do not send local-redir back
932      *  to same URL, since CGI should have handled it internally if it
933      *  really wanted to do that internally)
934      */
935 
936     /* r->http_status >= 300 && r->http_status < 400) */
937     size_t ulen = buffer_string_length(&r->uri.path);
938     const buffer *vb = http_header_response_get(r, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"));
939     if (NULL != vb
940         && vb->ptr[0] == '/'
941         && (0 != strncmp(vb->ptr, r->uri.path.ptr, ulen)
942             || (   vb->ptr[ulen] != '\0'
943                 && vb->ptr[ulen] != '/'
944                 && vb->ptr[ulen] != '?'))
945         && 0 == blen
946         && !(r->resp_htags & HTTP_HEADER_STATUS) /*no "Status" or NPH response*/
947         && 1 == r->resp_headers.used) {
948         if (++r->loops_per_request > 5) {
949             log_error(r->conf.errh, __FILE__, __LINE__,
950               "too many internal loops while processing request: %s",
951               r->target_orig.ptr);
952             r->http_status = 500; /* Internal Server Error */
953             r->handler_module = NULL;
954             return HANDLER_FINISHED;
955         }
956 
957         buffer_copy_buffer(&r->target, vb);
958 
959         if (r->reqbody_length) {
960             if (r->reqbody_length
961                 != r->reqbody_queue->bytes_in) {
962                 r->keep_alive = 0;
963             }
964             r->reqbody_length = 0;
965             chunkqueue_reset(r->reqbody_queue);
966         }
967 
968         if (r->http_status != 307 && r->http_status != 308) {
969             /* Note: request body (if any) sent to initial dynamic handler
970              * and is not available to the internal redirect */
971             r->http_method = HTTP_METHOD_GET;
972         }
973 
974         /*(caller must reset request as follows)*/
975         /*http_response_reset(r);*/ /*(sets r->http_status = 0)*/
976         /*plugins_call_handle_request_reset(r);*/
977 
978         return HANDLER_COMEBACK;
979     }
980 
981     return HANDLER_GO_ON;
982 }
983 
984 
985 static int http_response_process_headers(request_st * const r, http_response_opts * const opts, buffer * const hdrs) {
986     char *ns;
987     const char *s;
988     int line = 0;
989     int status_is_set = 0;
990 
991     for (s = hdrs->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1, ++line) {
992         const char *key, *value;
993         int key_len;
994         enum http_header_e id;
995 
996         /* strip the \n */
997         ns[0] = '\0';
998         if (ns > s && ns[-1] == '\r') ns[-1] = '\0';
999 
1000         if (0 == line && 0 == strncmp(s, "HTTP/1.", 7)) {
1001             /* non-parsed headers ... we parse them anyway */
1002             if ((s[7] == '1' || s[7] == '0') && s[8] == ' ') {
1003                 /* after the space should be a status code for us */
1004                 int status = http_header_str_to_code(s+9);
1005                 if (status >= 100 && status < 1000) {
1006                     status_is_set = 1;
1007                     r->resp_htags |= HTTP_HEADER_STATUS;
1008                     r->http_status = status;
1009                 } /* else we expected 3 digits and didn't get them */
1010             }
1011 
1012             if (0 == r->http_status) {
1013                 log_error(r->conf.errh, __FILE__, __LINE__,
1014                   "invalid HTTP status line: %s", s);
1015                 r->http_status = 502; /* Bad Gateway */
1016                 r->handler_module = NULL;
1017                 return -1;
1018             }
1019 
1020             continue;
1021         }
1022 
1023         /* parse the headers */
1024         key = s;
1025         if (NULL == (value = strchr(s, ':'))) {
1026             /* we expect: "<key>: <value>\r\n" */
1027             continue;
1028         }
1029 
1030         key_len = value - key;
1031         if (0 == key_len) continue; /*(already ignored when writing response)*/
1032         do { ++value; } while (*value == ' ' || *value == '\t'); /* skip LWS */
1033         id = http_header_hkey_get(key, key_len);
1034 
1035         if (opts->authorizer) {
1036             if (0 == r->http_status || 200 == r->http_status) {
1037                 if (id == HTTP_HEADER_STATUS) {
1038                     int status = http_header_str_to_code(value);
1039                     if (status >= 100 && status < 1000) {
1040                         r->http_status = status;
1041                     } else {
1042                         r->http_status = 502; /* Bad Gateway */
1043                         break;
1044                     }
1045                 }
1046                 else if (id == HTTP_HEADER_OTHER && key_len > 9
1047                          && (key[0] & 0xdf) == 'V'
1048                          && buffer_eq_icase_ssn(key,
1049                                                 CONST_STR_LEN("Variable-"))) {
1050                     http_header_env_append(r, key + 9, key_len - 9, value, strlen(value));
1051                 }
1052                 continue;
1053             }
1054         }
1055 
1056         switch (id) {
1057           case HTTP_HEADER_STATUS:
1058             {
1059                 if (opts->backend == BACKEND_PROXY) break; /*(pass w/o parse)*/
1060                 int status = http_header_str_to_code(value);
1061                 if (status >= 100 && status < 1000) {
1062                     r->http_status = status;
1063                     status_is_set = 1;
1064                 } else {
1065                     r->http_status = 502;
1066                     r->handler_module = NULL;
1067                 }
1068                 continue; /* do not send Status to client */
1069             }
1070             break;
1071           case HTTP_HEADER_UPGRADE:
1072             /*(technically, should also verify Connection: upgrade)*/
1073             /*(flag only for mod_proxy and mod_cgi (for now))*/
1074             if (opts->backend != BACKEND_PROXY
1075                 && opts->backend != BACKEND_CGI) {
1076                 id = HTTP_HEADER_OTHER;
1077             }
1078             if (r->http_version >= HTTP_VERSION_2) continue;
1079             break;
1080           case HTTP_HEADER_CONNECTION:
1081             if (opts->backend == BACKEND_PROXY) continue;
1082             /*(should parse for tokens and do case-insensitive match for "close"
1083              * but this is an imperfect though simplistic attempt to honor
1084              * backend request to close)*/
1085             if (NULL != strstr(value, "lose")) r->keep_alive = 0;
1086             if (r->http_version >= HTTP_VERSION_2) continue;
1087             break;
1088           case HTTP_HEADER_CONTENT_LENGTH:
1089             r->content_length = strtoul(value, NULL, 10);
1090             if (*value == '+') ++value;
1091             break;
1092           case HTTP_HEADER_TRANSFER_ENCODING:
1093             /*(assumes "Transfer-Encoding: chunked"; does not verify)*/
1094             r->resp_decode_chunked = 1;
1095             r->gw_dechunk = calloc(1, sizeof(response_dechunk));
1096             /* XXX: future: might consider using chunk_buffer_acquire()
1097              *      and chunk_buffer_release() for r->gw_dechunk->b */
1098             force_assert(r->gw_dechunk);
1099             continue;
1100           case HTTP_HEADER_HTTP2_SETTINGS:
1101             /* RFC7540 3.2.1
1102              *   A server MUST NOT send this header field. */
1103             /* (not bothering to remove HTTP2-Settings from Connection) */
1104             continue;
1105           default:
1106             break;
1107         }
1108 
1109         http_header_response_insert(r, id, key, key_len, value, strlen(value));
1110     }
1111 
1112     /* CGI/1.1 rev 03 - 7.2.1.2 */
1113     /* (proxy requires Status-Line, so never true for proxy)*/
1114     if (!status_is_set && (r->resp_htags & HTTP_HEADER_LOCATION)) {
1115         r->http_status = 302;
1116     }
1117 
1118     return 0;
1119 }
1120 
1121 
1122 __attribute_hot__
1123 __attribute_pure__
1124 static const char *
1125 http_response_end_of_header (const char * const restrict ptr)
1126 {
1127     /* find \n(\r)?\n sequence */
1128     for (const char *n=ptr-1, *nn=NULL; NULL != (n = strchr(n+1, '\n')); nn=n) {
1129         if (n - nn == 2 ? n[-1] == '\r' : n - nn == 1) return n+1;
1130     }
1131     return NULL;
1132 }
1133 
1134 
1135 __attribute_cold__
1136 static uint32_t
1137 http_response_discard_1xx (buffer * const restrict b, uint32_t len)
1138 {
1139     char * const ptr = b->ptr;
1140     const char * const n = http_response_end_of_header(ptr+12);
1141     if (NULL == n) return 0; /* * unfinished headers */
1142 
1143     /* discard 100 Continue headers */
1144     len -= (uint32_t)(n - ptr);
1145     memmove(ptr, n, len);
1146     buffer_string_set_length(b, len);
1147     return len;
1148 }
1149 
1150 
1151 __attribute_cold__
1152 __attribute_noinline__
1153 static uint32_t
1154 http_response_check_1xx (buffer * const restrict b, uint32_t len)
1155 {
1156     const char * const s = b->ptr;
1157     while (len > 7 && 0 == memcmp(s, "HTTP/1.", 7)) {
1158         /* discard HTTP/1.1 1xx responses other than 101 Switching Protocols */
1159         if (len > 12
1160             && s[8] == ' ' && s[9] == '1' && s[10] == '0' && s[11] != '1') {
1161             len = http_response_discard_1xx(b, len);
1162             continue;
1163         }
1164         break;
1165     }
1166     return len;
1167 }
1168 
1169 
1170 handler_t http_response_parse_headers(request_st * const r, http_response_opts * const opts, buffer * const b) {
1171     /**
1172      * possible formats of response headers:
1173      *
1174      * proxy or NPH (non-parsed headers):
1175      *
1176      *   HTTP/1.0 200 Ok\n
1177      *   Header: Value\n
1178      *   \n
1179      *
1180      * CGI:
1181      *
1182      *   Header: Value\n
1183      *   Status: 200\n
1184      *   \n
1185      *
1186      * and different mixes of \n and \r\n combinations
1187      *
1188      * Some users also forget about CGI and just send a response
1189      * and hope we handle it. No headers, no header-content separator
1190      */
1191 
1192     uint32_t header_len = buffer_string_length(b);
1193     if (header_len > 12 && b->ptr[9] == '1') {
1194         header_len = http_response_check_1xx(b, header_len);
1195         if (0 == header_len)
1196             return HANDLER_GO_ON; /* unfinished header */
1197     }
1198     /*("HTTP/1.1 200 " is at least 13 chars + \r\n)*/
1199     const int is_nph = (header_len > 12 && 0 == memcmp(b->ptr, "HTTP/1.", 7));
1200 
1201     int is_header_end = 0;
1202     uint32_t i = 0, blen;
1203     const char *bstart;
1204 
1205     if (b->ptr[0] == '\n' || (b->ptr[0] == '\r' && b->ptr[1] == '\n')) {
1206         /* no HTTP headers */
1207         i = (b->ptr[0] == '\n') ? 0 : 1;
1208         is_header_end = 1;
1209     } else if (is_nph || b->ptr[(i = strcspn(b->ptr, ":\n"))] == ':') {
1210         /* HTTP headers */
1211         const char *n = http_response_end_of_header(b->ptr+i+1);
1212         if (n) {
1213             i = (uint32_t)(n - b->ptr - 1);
1214             is_header_end = 1;
1215         }
1216     } else if (i == header_len) { /* (no newline yet; partial header line?) */
1217     } else if (opts->backend == BACKEND_CGI) {
1218         /* no HTTP headers, but a body (special-case for CGI compat) */
1219         /* no colon found; does not appear to be HTTP headers */
1220         if (0 != http_chunk_append_buffer(r, b)) {
1221             return HANDLER_ERROR;
1222         }
1223         r->http_status = 200; /* OK */
1224         r->resp_body_started = 1;
1225         return HANDLER_GO_ON;
1226     } else {
1227         /* invalid response headers */
1228         r->http_status = 502; /* Bad Gateway */
1229         r->handler_module = NULL;
1230         return HANDLER_FINISHED;
1231     }
1232 
1233     if (!is_header_end) {
1234         if (header_len > MAX_HTTP_RESPONSE_FIELD_SIZE) {
1235             log_error(r->conf.errh, __FILE__, __LINE__,
1236               "response headers too large for %s", r->uri.path.ptr);
1237             r->http_status = 502; /* Bad Gateway */
1238             r->handler_module = NULL;
1239             return HANDLER_FINISHED;
1240         }
1241         return HANDLER_GO_ON;
1242     }
1243 
1244     /* the body starts after the EOL */
1245     bstart = b->ptr + (i + 1);
1246     blen = header_len - (i + 1);
1247 
1248     /* strip the last \r?\n */
1249     if (i > 0 && (b->ptr[i - 1] == '\r')) {
1250         i--;
1251     }
1252 
1253     buffer_string_set_length(b, i);
1254 
1255     if (opts->backend == BACKEND_PROXY && !is_nph) {
1256         /* invalid response Status-Line from HTTP proxy */
1257         r->http_status = 502; /* Bad Gateway */
1258         r->handler_module = NULL;
1259         return HANDLER_FINISHED;
1260     }
1261 
1262     if (0 != http_response_process_headers(r, opts, b)) {
1263         return HANDLER_ERROR;
1264     }
1265 
1266     r->resp_body_started = 1;
1267 
1268     if (opts->authorizer
1269         && (r->http_status == 0 || r->http_status == 200)) {
1270         return HANDLER_GO_ON;
1271     }
1272 
1273     if (NULL == r->handler_module) {
1274         return HANDLER_FINISHED;
1275     }
1276 
1277     if (opts->local_redir && r->http_status >= 300 && r->http_status < 400){
1278         /*(r->resp_htags & HTTP_HEADER_LOCATION)*/
1279         handler_t rc = http_response_process_local_redir(r, blen);
1280         if (NULL == r->handler_module)
1281             r->resp_body_started = 0;
1282         if (rc != HANDLER_GO_ON) return rc;
1283     }
1284 
1285     if (opts->xsendfile_allow) {
1286         buffer *vb;
1287         /* X-Sendfile2 is deprecated; historical for fastcgi */
1288         if (opts->backend == BACKEND_FASTCGI
1289             && NULL != (vb = http_header_response_get(r, HTTP_HEADER_OTHER, CONST_STR_LEN("X-Sendfile2")))) {
1290             http_response_xsendfile2(r, vb, opts->xsendfile_docroot);
1291             /* http_header_response_unset() shortcut for HTTP_HEADER_OTHER */
1292             buffer_clear(vb); /*(do not send to client)*/
1293             if (NULL == r->handler_module)
1294                 r->resp_body_started = 0;
1295             return HANDLER_FINISHED;
1296         } else if (NULL != (vb = http_header_response_get(r, HTTP_HEADER_OTHER, CONST_STR_LEN("X-Sendfile")))
1297                    || (opts->backend == BACKEND_FASTCGI /* X-LIGHTTPD-send-file is deprecated; historical for fastcgi */
1298                        && NULL != (vb = http_header_response_get(r, HTTP_HEADER_OTHER, CONST_STR_LEN("X-LIGHTTPD-send-file"))))) {
1299             http_response_xsendfile(r, vb, opts->xsendfile_docroot);
1300             /* http_header_response_unset() shortcut for HTTP_HEADER_OTHER */
1301             buffer_clear(vb); /*(do not send to client)*/
1302             if (NULL == r->handler_module)
1303                 r->resp_body_started = 0;
1304             return HANDLER_FINISHED;
1305         }
1306     }
1307 
1308     if (blen > 0) {
1309         if (0 != http_chunk_decode_append_mem(r, bstart, blen))
1310             return HANDLER_ERROR;
1311     }
1312 
1313     /* (callback for response headers complete) */
1314     return (opts->headers) ? opts->headers(r, opts) : HANDLER_GO_ON;
1315 }
1316 
1317 
1318 handler_t http_response_read(request_st * const r, http_response_opts * const opts, buffer * const b, fdnode * const fdn) {
1319     const int fd = fdn->fd;
1320     while (1) {
1321         ssize_t n;
1322         size_t avail = buffer_string_space(b);
1323         unsigned int toread = 0;
1324 
1325         if (0 == fdevent_ioctl_fionread(fd, opts->fdfmt, (int *)&toread)) {
1326             if (avail < toread) {
1327                 size_t blen = buffer_string_length(b);
1328                 if (toread + blen < 4096)
1329                     toread = 4095 - blen;
1330                 else if (toread > MAX_READ_LIMIT)
1331                     toread = MAX_READ_LIMIT;
1332             }
1333             else if (0 == toread) {
1334               #if 0
1335                 return (fdevent_fdnode_interest(fdn) & FDEVENT_IN)
1336                   ? HANDLER_FINISHED  /* read finished */
1337                   : HANDLER_GO_ON;    /* optimistic read; data not ready */
1338               #else
1339                 if (!(fdevent_fdnode_interest(fdn) & FDEVENT_IN)) {
1340                     if (!(r->conf.stream_response_body
1341                           & FDEVENT_STREAM_RESPONSE_POLLRDHUP))
1342                         return HANDLER_GO_ON;/*optimistic read; data not ready*/
1343                 }
1344                 if (0 == avail) /* let read() below indicate if EOF or EAGAIN */
1345                     toread = 1024;
1346               #endif
1347             }
1348         }
1349         else if (avail < 1024) {
1350             toread = 4095 - avail;
1351         }
1352 
1353         if (r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
1354             off_t cqlen = chunkqueue_length(r->write_queue);
1355             if (cqlen + (off_t)toread > 65536 - 4096) {
1356                 if (!r->con->is_writable) {
1357                     /*(defer removal of FDEVENT_IN interest since
1358                      * connection_state_machine() might be able to send data
1359                      * immediately, unless !con->is_writable, where
1360                      * connection_state_machine() might not loop back to call
1361                      * mod_proxy_handle_subrequest())*/
1362                     fdevent_fdnode_event_clr(r->con->srv->ev, fdn, FDEVENT_IN);
1363                 }
1364                 if (cqlen >= 65536-1) return HANDLER_GO_ON;
1365                 toread = 65536 - 1 - (unsigned int)cqlen;
1366                 /* Note: heuristic is fuzzy in that it limits how much to read
1367                  * from backend based on how much is pending to write to client.
1368                  * Modules where data from backend is framed (e.g. FastCGI) may
1369                  * want to limit how much is buffered from backend while waiting
1370                  * for a complete data frame or data packet from backend. */
1371             }
1372         }
1373 
1374         if (avail < toread) {
1375             /*(add avail+toread to reduce allocations when ioctl EOPNOTSUPP)*/
1376             avail = avail ? avail - 1 + toread : toread;
1377             buffer_string_prepare_append(b, avail);
1378         }
1379 
1380         n = read(fd, b->ptr+buffer_string_length(b), avail);
1381 
1382         if (n < 0) {
1383             switch (errno) {
1384               case EAGAIN:
1385              #ifdef EWOULDBLOCK
1386              #if EWOULDBLOCK != EAGAIN
1387               case EWOULDBLOCK:
1388              #endif
1389              #endif
1390               case EINTR:
1391                 return HANDLER_GO_ON;
1392               default:
1393                 log_perror(r->conf.errh, __FILE__, __LINE__,
1394                   "read() %d %d", r->con->fd, fd);
1395                 return HANDLER_ERROR;
1396             }
1397         }
1398 
1399         buffer_commit(b, (size_t)n);
1400       #ifdef __COVERITY__
1401         /* Coverity Scan overlooks the effect of buffer_commit() */
1402         b->ptr[buffer_string_length(b)+n] = '\0';
1403       #endif
1404 
1405         if (NULL != opts->parse) {
1406             handler_t rc = opts->parse(r, opts, b, (size_t)n);
1407             if (rc != HANDLER_GO_ON) return rc;
1408         } else if (0 == n) {
1409             /* note: no further data is sent to backend after read EOF on socket
1410              * (not checking for half-closed TCP socket)
1411              * (backend should read all data desired prior to closing socket,
1412              *  though might send app-level close data frame, if applicable) */
1413             return HANDLER_FINISHED; /* read finished */
1414         } else if (0 == r->resp_body_started) {
1415             /* split header from body */
1416             handler_t rc = http_response_parse_headers(r, opts, b);
1417             if (rc != HANDLER_GO_ON) return rc;
1418             /* accumulate response in b until headers completed (or error) */
1419             if (r->resp_body_started) buffer_clear(b);
1420         } else {
1421             if (0 != http_chunk_decode_append_buffer(r, b)) {
1422                 /* error writing to tempfile;
1423                  * truncate response or send 500 if nothing sent yet */
1424                 return HANDLER_ERROR;
1425             }
1426             buffer_clear(b);
1427         }
1428 
1429         if (r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
1430             if (chunkqueue_length(r->write_queue) > 65536 - 4096) {
1431                 /*(defer removal of FDEVENT_IN interest since
1432                  * connection_state_machine() might be able to send
1433                  * data immediately, unless !con->is_writable, where
1434                  * connection_state_machine() might not loop back to
1435                  * call the subrequest handler)*/
1436                 if (!r->con->is_writable)
1437                     fdevent_fdnode_event_clr(r->con->srv->ev, fdn, FDEVENT_IN);
1438                 break;
1439             }
1440         }
1441 
1442         if ((size_t)n < avail)
1443             break; /* emptied kernel read buffer or partial read */
1444     }
1445 
1446     return HANDLER_GO_ON;
1447 }
1448 
1449 
1450 int http_cgi_headers (request_st * const r, http_cgi_opts * const opts, http_cgi_header_append_cb cb, void *vdata) {
1451 
1452     /* CGI-SPEC 6.1.2, FastCGI spec 6.3 and SCGI spec */
1453 
1454     int rc = 0;
1455     const connection * const con = r->con;
1456     server_socket * const srv_sock = con->srv_socket;
1457     buffer * const tb = r->tmp_buf;
1458     const char *s;
1459     size_t n;
1460     char buf[LI_ITOSTRING_LENGTH];
1461     sock_addr *addr;
1462     sock_addr addrbuf;
1463     char b2[INET6_ADDRSTRLEN + 1];
1464 
1465     /* (CONTENT_LENGTH must be first for SCGI) */
1466     if (!opts->authorizer) {
1467         rc |= cb(vdata, CONST_STR_LEN("CONTENT_LENGTH"),
1468                  buf, li_itostrn(buf,sizeof(buf),r->reqbody_length));
1469     }
1470 
1471     if (!buffer_string_is_empty(&r->uri.query)) {
1472         rc |= cb(vdata, CONST_STR_LEN("QUERY_STRING"),
1473                         CONST_BUF_LEN(&r->uri.query));
1474     } else {
1475         rc |= cb(vdata, CONST_STR_LEN("QUERY_STRING"),
1476                         CONST_STR_LEN(""));
1477     }
1478     if (!buffer_string_is_empty(opts->strip_request_uri)) {
1479         /**
1480          * /app1/index/list
1481          *
1482          * stripping /app1 or /app1/ should lead to
1483          *
1484          * /index/list
1485          *
1486          */
1487         size_t len = buffer_string_length(opts->strip_request_uri);
1488         if ('/' == opts->strip_request_uri->ptr[len-1]) {
1489             --len;
1490         }
1491 
1492         if (buffer_string_length(&r->target_orig) >= len
1493             && 0 == memcmp(r->target_orig.ptr,
1494                            opts->strip_request_uri->ptr, len)
1495             && r->target_orig.ptr[len] == '/') {
1496             rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"),
1497                             r->target_orig.ptr+len,
1498                             buffer_string_length(&r->target_orig)-len);
1499         } else {
1500             rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"),
1501                             CONST_BUF_LEN(&r->target_orig));
1502         }
1503     } else {
1504         rc |= cb(vdata, CONST_STR_LEN("REQUEST_URI"),
1505                         CONST_BUF_LEN(&r->target_orig));
1506     }
1507     if (!buffer_is_equal(&r->target, &r->target_orig)) {
1508         rc |= cb(vdata, CONST_STR_LEN("REDIRECT_URI"),
1509                         CONST_BUF_LEN(&r->target));
1510     }
1511     /* set REDIRECT_STATUS for php compiled with --force-redirect
1512      * (if REDIRECT_STATUS has not already been set by error handler) */
1513     if (0 == r->error_handler_saved_status) {
1514         rc |= cb(vdata, CONST_STR_LEN("REDIRECT_STATUS"),
1515                         CONST_STR_LEN("200"));
1516     }
1517 
1518     /*
1519      * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to
1520      * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html
1521      * (6.1.14, 6.1.6, 6.1.7)
1522      */
1523     if (!opts->authorizer) {
1524         rc |= cb(vdata, CONST_STR_LEN("SCRIPT_NAME"),
1525                         CONST_BUF_LEN(&r->uri.path));
1526         if (!buffer_string_is_empty(&r->pathinfo)) {
1527             rc |= cb(vdata, CONST_STR_LEN("PATH_INFO"),
1528                             CONST_BUF_LEN(&r->pathinfo));
1529             /* PATH_TRANSLATED is only defined if PATH_INFO is set */
1530             if (!buffer_string_is_empty(opts->docroot)) {
1531                 buffer_copy_buffer(tb, opts->docroot);
1532             } else {
1533                 buffer_copy_buffer(tb, &r->physical.basedir);
1534             }
1535             buffer_append_string_buffer(tb, &r->pathinfo);
1536             rc |= cb(vdata, CONST_STR_LEN("PATH_TRANSLATED"),
1537                             CONST_BUF_LEN(tb));
1538         }
1539     }
1540 
1541    /*
1542     * SCRIPT_FILENAME and DOCUMENT_ROOT for php
1543     * The PHP manual http://www.php.net/manual/en/reserved.variables.php
1544     * treatment of PATH_TRANSLATED is different from the one of CGI specs.
1545     * (see php.ini cgi.fix_pathinfo = 1 config parameter)
1546     */
1547 
1548     if (!buffer_string_is_empty(opts->docroot)) {
1549         /* alternate docroot, e.g. for remote FastCGI or SCGI server */
1550         buffer_copy_buffer(tb, opts->docroot);
1551         buffer_append_string_buffer(tb, &r->uri.path);
1552         rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"),
1553                         CONST_BUF_LEN(tb));
1554         rc |= cb(vdata, CONST_STR_LEN("DOCUMENT_ROOT"),
1555                         CONST_BUF_LEN(opts->docroot));
1556     } else {
1557         if (opts->break_scriptfilename_for_php) {
1558             /* php.ini config cgi.fix_pathinfo = 1 need a broken SCRIPT_FILENAME
1559              * to find out what PATH_INFO is itself
1560              *
1561              * see src/sapi/cgi_main.c, init_request_info()
1562              */
1563             buffer_copy_buffer(tb, &r->physical.path);
1564             buffer_append_string_buffer(tb, &r->pathinfo);
1565             rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"),
1566                             CONST_BUF_LEN(tb));
1567         } else {
1568             rc |= cb(vdata, CONST_STR_LEN("SCRIPT_FILENAME"),
1569                             CONST_BUF_LEN(&r->physical.path));
1570         }
1571         rc |= cb(vdata, CONST_STR_LEN("DOCUMENT_ROOT"),
1572                         CONST_BUF_LEN(&r->physical.basedir));
1573     }
1574 
1575     s = get_http_method_name(r->http_method);
1576     force_assert(s);
1577     rc |= cb(vdata, CONST_STR_LEN("REQUEST_METHOD"), s, strlen(s));
1578 
1579     s = get_http_version_name(r->http_version);
1580     force_assert(s);
1581     rc |= cb(vdata, CONST_STR_LEN("SERVER_PROTOCOL"), s, strlen(s));
1582 
1583     rc |= cb(vdata, CONST_STR_LEN("SERVER_SOFTWARE"),
1584                     CONST_BUF_LEN(r->conf.server_tag));
1585 
1586     rc |= cb(vdata, CONST_STR_LEN("GATEWAY_INTERFACE"),
1587                     CONST_STR_LEN("CGI/1.1"));
1588 
1589     rc |= cb(vdata, CONST_STR_LEN("REQUEST_SCHEME"),
1590                     CONST_BUF_LEN(&r->uri.scheme));
1591 
1592     if (buffer_is_equal_string(&r->uri.scheme, CONST_STR_LEN("https"))) {
1593         rc |= cb(vdata, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on"));
1594     }
1595 
1596     addr = &srv_sock->addr;
1597     rc |= cb(vdata, CONST_STR_LEN("SERVER_PORT"),
1598              buf, li_utostrn(buf,sizeof(buf),sock_addr_get_port(addr)));
1599 
1600     switch (addr->plain.sa_family) {
1601     case AF_INET:
1602     case AF_INET6:
1603         if (sock_addr_is_addr_wildcard(addr)) {
1604             socklen_t addrlen = sizeof(addrbuf);
1605             if (0 == getsockname(con->fd,(struct sockaddr *)&addrbuf,&addrlen)){
1606                 addr = &addrbuf;
1607             } else {
1608                 s = "";
1609                 break;
1610             }
1611         }
1612         s = sock_addr_inet_ntop(addr, b2, sizeof(b2)-1);
1613         if (NULL == s) s = "";
1614         break;
1615     default:
1616         s = "";
1617         break;
1618     }
1619     force_assert(s);
1620     rc |= cb(vdata, CONST_STR_LEN("SERVER_ADDR"), s, strlen(s));
1621 
1622     if (!buffer_string_is_empty(r->server_name)) {
1623         size_t len = buffer_string_length(r->server_name);
1624 
1625         if (r->server_name->ptr[0] == '[') {
1626             const char *colon = strstr(r->server_name->ptr, "]:");
1627             if (colon) len = (colon + 1) - r->server_name->ptr;
1628         } else {
1629             const char *colon = strchr(r->server_name->ptr, ':');
1630             if (colon) len = colon - r->server_name->ptr;
1631         }
1632 
1633         rc |= cb(vdata, CONST_STR_LEN("SERVER_NAME"),
1634                         r->server_name->ptr, len);
1635     } else {
1636         /* set to be same as SERVER_ADDR (above) */
1637         rc |= cb(vdata, CONST_STR_LEN("SERVER_NAME"), s, strlen(s));
1638     }
1639 
1640     rc |= cb(vdata, CONST_STR_LEN("REMOTE_ADDR"),
1641                     CONST_BUF_LEN(con->dst_addr_buf));
1642 
1643     rc |= cb(vdata, CONST_STR_LEN("REMOTE_PORT"), buf,
1644              li_utostrn(buf,sizeof(buf),sock_addr_get_port(&con->dst_addr)));
1645 
1646     for (n = 0; n < r->rqst_headers.used; n++) {
1647         data_string *ds = (data_string *)r->rqst_headers.data[n];
1648         if (!buffer_string_is_empty(&ds->value) && !buffer_is_empty(&ds->key)) {
1649             /* Security: Do not emit HTTP_PROXY in environment.
1650              * Some executables use HTTP_PROXY to configure
1651              * outgoing proxy.  See also https://httpoxy.org/ */
1652             if (buffer_is_equal_caseless_string(&ds->key,
1653                                                 CONST_STR_LEN("Proxy"))) {
1654                 continue;
1655             }
1656             buffer_copy_string_encoded_cgi_varnames(tb,
1657                                                     CONST_BUF_LEN(&ds->key), 1);
1658             rc |= cb(vdata, CONST_BUF_LEN(tb),
1659                             CONST_BUF_LEN(&ds->value));
1660         }
1661     }
1662 
1663     con->srv->request_env(r);
1664 
1665     for (n = 0; n < r->env.used; n++) {
1666         data_string *ds = (data_string *)r->env.data[n];
1667         if (!buffer_is_empty(&ds->value) && !buffer_is_empty(&ds->key)) {
1668             buffer_copy_string_encoded_cgi_varnames(tb,
1669                                                     CONST_BUF_LEN(&ds->key), 0);
1670             rc |= cb(vdata, CONST_BUF_LEN(tb),
1671                             CONST_BUF_LEN(&ds->value));
1672         }
1673     }
1674 
1675     return rc;
1676 }
1677