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