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