xref: /lighttpd1.4/src/http-header-glue.c (revision 66698469)
1 #include "first.h"
2 
3 #include "sys-time.h"
4 
5 #include "base.h"
6 #include "array.h"
7 #include "buffer.h"
8 #include "chunk.h"
9 #include "fdevent.h"
10 #include "log.h"
11 #include "http_chunk.h"
12 #include "http_cgi.h"
13 #include "http_date.h"
14 #include "http_etag.h"
15 #include "http_header.h"
16 #include "response.h"
17 #include "sock_addr.h"
18 #include "stat_cache.h"
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 
24 #include "sys-socket.h"
25 #include <unistd.h>
26 
27 /**
28  * max size of the HTTP response header from backends
29  * (differs from server.max-request-field-size for max request field size)
30  */
31 #define MAX_HTTP_RESPONSE_FIELD_SIZE 65535
32 
33 
34 __attribute_cold__
35 int http_response_buffer_append_authority(request_st * const r, buffer * const o) {
36 	if (!buffer_is_blank(&r->uri.authority)) {
37 		buffer_append_string_buffer(o, &r->uri.authority);
38 	} else {
39 		/* get the name of the currently connected socket */
40 		sock_addr our_addr;
41 		socklen_t our_addr_len;
42 
43 		our_addr.plain.sa_family = 0;
44 		our_addr_len = sizeof(our_addr);
45 
46 		if (-1 == getsockname(r->con->fd, (struct sockaddr *)&our_addr, &our_addr_len)
47 		    || our_addr_len > (socklen_t)sizeof(our_addr)) {
48 			r->http_status = 500;
49 			log_perror(r->conf.errh, __FILE__, __LINE__, "can't get sockname");
50 			return -1;
51 		}
52 
53 		if (our_addr.plain.sa_family == AF_INET
54 		    && our_addr.ipv4.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
55 			static char lhost[32];
56 			static size_t lhost_len = 0;
57 			if (0 != lhost_len) {
58 				buffer_append_string_len(o, lhost, lhost_len);
59 			}
60 			else {
61 				size_t olen = buffer_clen(o);
62 				if (0 == sock_addr_nameinfo_append_buffer(o, &our_addr, r->conf.errh)) {
63 					lhost_len = buffer_clen(o) - olen;
64 					if (lhost_len < sizeof(lhost)) {
65 						memcpy(lhost, o->ptr+olen, lhost_len+1); /*(+1 for '\0')*/
66 					}
67 					else {
68 						lhost_len = 0;
69 					}
70 				}
71 				else {
72 					lhost_len = sizeof("localhost")-1;
73 					memcpy(lhost, "localhost", lhost_len+1); /*(+1 for '\0')*/
74 					buffer_append_string_len(o, lhost, lhost_len);
75 				}
76 			}
77 		} else if (!buffer_is_blank(r->server_name)) {
78 			buffer_append_string_buffer(o, r->server_name);
79 		} else
80 		/* Lookup name: secondly try to get hostname for bind address */
81 		if (0 != sock_addr_nameinfo_append_buffer(o, &our_addr, r->conf.errh)) {
82 			r->http_status = 500;
83 			return -1;
84 		}
85 
86 		{
87 			unsigned short listen_port = sock_addr_get_port(&our_addr);
88 			unsigned short default_port = 80;
89 			if (buffer_is_equal_string(&r->uri.scheme, CONST_STR_LEN("https"))) {
90 				default_port = 443;
91 			}
92 			if (0 == listen_port) listen_port = r->con->srv->srvconf.port;
93 			if (default_port != listen_port) {
94 				buffer_append_char(o, ':');
95 				buffer_append_int(o, listen_port);
96 			}
97 		}
98 	}
99 	return 0;
100 }
101 
102 int http_response_redirect_to_directory(request_st * const r, int status) {
103 	buffer *o = r->tmp_buf;
104 	buffer_clear(o);
105 	/* XXX: store flag in global at startup? */
106 	if (r->con->srv->srvconf.absolute_dir_redirect) {
107 		buffer_append_str2(o, BUF_PTR_LEN(&r->uri.scheme),
108 		                      CONST_STR_LEN("://"));
109 		if (0 != http_response_buffer_append_authority(r, o)) {
110 			return -1;
111 		}
112 	}
113 	buffer *vb;
114 	if (status >= 300) {
115 		r->http_status = status;
116 		r->resp_body_finished = 1;
117 		vb = http_header_response_set_ptr(r, HTTP_HEADER_LOCATION,
118 		                                  CONST_STR_LEN("Location"));
119 	}
120 	else {
121 		vb = http_header_response_set_ptr(r, HTTP_HEADER_CONTENT_LOCATION,
122 		                                  CONST_STR_LEN("Content-Location"));
123 	}
124 	buffer_copy_buffer(vb, o);
125 	buffer_append_string_encoded(vb, BUF_PTR_LEN(&r->uri.path),
126 	                             ENCODING_REL_URI);
127 	buffer_append_char(vb, '/');
128 	if (!buffer_is_blank(&r->uri.query))
129 		buffer_append_str2(vb, CONST_STR_LEN("?"),
130 		                       BUF_PTR_LEN(&r->uri.query));
131 
132 	return 0;
133 }
134 
135 #define MTIME_CACHE_MAX 16
136 struct mtime_cache_type {
137     unix_time64_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][HTTP_DATE_SZ];
142 /* 30-chars for "%a, %d %b %Y %T GMT" */
143 
144 void strftime_cache_reset(void) {
145     for (int i = 0; i < MTIME_CACHE_MAX; ++i) {
146         mtime_cache[i].mtime = -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 static const buffer * strftime_cache_get(const unix_time64_t last_mod) {
154     /*(note: not bothering to convert *here* if last_mod < 0 (for cache key);
155      * last_mod < 0 handled in http_date_time_to_str() call to gmtime64_r())*/
156 
157     static int mtime_cache_idx;
158 
159     for (int j = 0; j < MTIME_CACHE_MAX; ++j) {
160         if (mtime_cache[j].mtime == last_mod)
161             return &mtime_cache[j].str; /* found cache-entry */
162     }
163 
164     if (++mtime_cache_idx == MTIME_CACHE_MAX) mtime_cache_idx = 0;
165 
166     const int i = mtime_cache_idx;
167     http_date_time_to_str(mtime_cache[i].str.ptr, sizeof(mtime_cache_str[0]),
168                           (mtime_cache[i].mtime = last_mod));
169 
170     return &mtime_cache[i].str;
171 }
172 
173 
174 const buffer * http_response_set_last_modified(request_st * const r, const unix_time64_t lmtime) {
175     buffer * const vb =
176       http_header_response_set_ptr(r, HTTP_HEADER_LAST_MODIFIED,
177                                    CONST_STR_LEN("Last-Modified"));
178     buffer_copy_buffer(vb, strftime_cache_get(lmtime));
179     return vb;
180 }
181 
182 
183 __attribute_pure__
184 static int http_response_maybe_cachable (const request_st * const r) {
185     return (r->rqst_htags
186             & (light_bshift(HTTP_HEADER_IF_NONE_MATCH)
187               |light_bshift(HTTP_HEADER_IF_MODIFIED_SINCE)));
188 }
189 
190 
191 int http_response_handle_cachable(request_st * const r, const buffer *lmod, const unix_time64_t lmtime) {
192 	if (!http_response_maybe_cachable(r))
193 		return HANDLER_GO_ON;
194 
195 	const buffer *vb, *etag;
196 
197 	/*
198 	 * 14.26 If-None-Match
199 	 *    [...]
200 	 *    If none of the entity tags match, then the server MAY perform the
201 	 *    requested method as if the If-None-Match header field did not exist,
202 	 *    but MUST also ignore any If-Modified-Since header field(s) in the
203 	 *    request. That is, if no entity tags match, then the server MUST NOT
204 	 *    return a 304 (Not Modified) response.
205 	 */
206 
207 	if ((vb = http_header_request_get(r, HTTP_HEADER_IF_NONE_MATCH,
208 	                                  CONST_STR_LEN("If-None-Match")))
209 	    && (etag = http_header_response_get(r, HTTP_HEADER_ETAG,
210 	                                        CONST_STR_LEN("ETag")))) {
211 		/*(weak etag comparison must not be used for ranged requests)*/
212 		int range_request = (0 != light_btst(r->rqst_htags, HTTP_HEADER_RANGE));
213 		if (http_etag_matches(etag, vb->ptr, !range_request)) {
214 			if (http_method_get_head_query(r->http_method)) {
215 				r->http_status = 304;
216 				return HANDLER_FINISHED;
217 			} else {
218 				r->http_status = 412;
219 				r->handler_module = NULL;
220 				return HANDLER_FINISHED;
221 			}
222 		}
223 	} else if (http_method_get_head_query(r->http_method)
224 		   && (vb = http_header_request_get(r, HTTP_HEADER_IF_MODIFIED_SINCE,
225 		                                    CONST_STR_LEN("If-Modified-Since")))
226 		   && (lmod
227 		       || (lmod = http_header_response_get(r, HTTP_HEADER_LAST_MODIFIED,
228 				                           CONST_STR_LEN("Last-Modified"))))) {
229 		/* last-modified handling */
230 		if (buffer_is_equal(lmod, vb)
231 		    || !http_date_if_modified_since(BUF_PTR_LEN(vb), lmtime)) {
232 			r->http_status = 304;
233 			return HANDLER_FINISHED;
234 		}
235 	}
236 
237 	return HANDLER_GO_ON;
238 }
239 
240 
241 void http_response_body_clear (request_st * const r, int preserve_length) {
242     r->resp_send_chunked = 0;
243     r->resp_body_scratchpad = -1;
244     if (light_btst(r->resp_htags, HTTP_HEADER_TRANSFER_ENCODING)) {
245         http_header_response_unset(r, HTTP_HEADER_TRANSFER_ENCODING,
246                                    CONST_STR_LEN("Transfer-Encoding"));
247     }
248     if (!preserve_length) { /* preserve for HEAD responses and no-content responses (204, 205, 304) */
249         if (light_btst(r->resp_htags, HTTP_HEADER_CONTENT_LENGTH)) {
250             http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH,
251                                        CONST_STR_LEN("Content-Length"));
252         }
253         /*(if not preserving Content-Length, do not preserve trailers, if any)*/
254         r->resp_decode_chunked = 0;
255         if (r->gw_dechunk) {
256             free(r->gw_dechunk->b.ptr);
257             free(r->gw_dechunk);
258             r->gw_dechunk = NULL;
259         }
260     }
261     chunkqueue_reset(&r->write_queue);
262 }
263 
264 
265 static void http_response_header_clear (request_st * const r) {
266     r->http_status = 0;
267     r->resp_htags = 0;
268     r->resp_header_len = 0;
269     r->resp_header_repeated = 0;
270     array_reset_data_strings(&r->resp_headers);
271 
272     /* Note: http_response_body_clear(r, 0) is not called here
273      * r->write_queue should be preserved for additional data after 1xx response
274      * However, if http_response_process_headers() was called and response had
275      * Transfer-Encoding: chunked set, then other items need to be reset */
276     r->resp_send_chunked = 0;
277     r->resp_decode_chunked = 0;
278     r->resp_body_scratchpad = -1;
279     if (r->gw_dechunk) {
280         free(r->gw_dechunk->b.ptr);
281         free(r->gw_dechunk);
282         r->gw_dechunk = NULL;
283     }
284 }
285 
286 
287 void http_response_reset (request_st * const r) {
288     r->http_status = 0;
289     r->con->is_writable = 1;
290     r->resp_body_finished = 0;
291     r->resp_body_started = 0;
292     r->handler_module = NULL;
293     if (r->physical.path.ptr) { /*(skip for mod_fastcgi authorizer)*/
294         buffer_clear(&r->physical.doc_root);
295         buffer_clear(&r->physical.basedir);
296         buffer_reset(&r->physical.path);
297         buffer_reset(&r->physical.rel_path);
298     }
299     r->resp_htags = 0;
300     r->resp_header_len = 0;
301     r->resp_header_repeated = 0;
302     array_reset_data_strings(&r->resp_headers);
303     http_response_body_clear(r, 0);
304 }
305 
306 
307 handler_t http_response_reqbody_read_error (request_st * const r, int http_status) {
308     r->keep_alive = 0;
309 
310     /*(do not change status if response headers already set and possibly sent)*/
311     if (0 != r->resp_header_len) return HANDLER_ERROR;
312 
313     http_response_body_clear(r, 0);
314     r->http_status = http_status;
315     r->handler_module = NULL;
316     return HANDLER_FINISHED;
317 }
318 
319 
320 void http_response_send_file (request_st * const r, const buffer * const path, stat_cache_entry *sce) {
321 	if (__builtin_expect( (NULL == sce), 0)
322 	    || (__builtin_expect( (sce->fd < 0), 0) && 0 != sce->st.st_size)) {
323 		sce = stat_cache_get_entry_open(path, r->conf.follow_symlink);
324 		if (NULL == sce) {
325 			r->http_status = (errno == ENOENT) ? 404 : 403;
326 			log_error(r->conf.errh, __FILE__, __LINE__,
327 			  "not a regular file: %s -> %s", r->uri.path.ptr, path->ptr);
328 			return;
329 		}
330 		if (sce->fd < 0 && __builtin_expect( (0 != sce->st.st_size), 0)) {
331 			r->http_status = (errno == ENOENT) ? 404 : 403;
332 			if (r->conf.log_request_handling) {
333 				log_perror(r->conf.errh, __FILE__, __LINE__,
334 				  "file open failed: %s", path->ptr);
335 			}
336 			return;
337 		}
338 	}
339 
340 	if (__builtin_expect( (!r->conf.follow_symlink), 0)
341 	    && 0 != stat_cache_path_contains_symlink(path, r->conf.errh)) {
342 		r->http_status = 403;
343 		if (r->conf.log_request_handling) {
344 			log_error(r->conf.errh, __FILE__, __LINE__,
345 			  "-- access denied due symlink restriction");
346 			log_error(r->conf.errh, __FILE__, __LINE__,
347 			  "Path         : %s", path->ptr);
348 		}
349 		return;
350 	}
351 
352 	/* we only handle regular files */
353 	if (__builtin_expect( (!S_ISREG(sce->st.st_mode)), 0)) {
354 		r->http_status = 403;
355 		if (r->conf.log_file_not_found) {
356 			log_error(r->conf.errh, __FILE__, __LINE__,
357 			  "not a regular file: %s -> %s",
358 			  r->uri.path.ptr, path->ptr);
359 		}
360 		return;
361 	}
362 
363 	/* set response content-type, if not set already */
364 	static const buffer octet_stream =
365 	  { CONST_STR_LEN("application/octet-stream")+1, 0 };
366 	const buffer *content_type = NULL;
367 	if (__builtin_expect( (!light_btst(r->resp_htags, HTTP_HEADER_CONTENT_TYPE)), 1)) {
368 		content_type = stat_cache_content_type_get(sce, r);
369 		if (__builtin_expect( (!content_type), 0)
370 		    || __builtin_expect( (buffer_is_blank(content_type)), 0))
371 			content_type = &octet_stream;
372 		http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE,
373 		                         CONST_STR_LEN("Content-Type"),
374 		                         BUF_PTR_LEN(content_type));
375 	}
376 
377 	/* avoid sending caching headers if implicit "application/octet-stream"
378 	 * This should workaround aggressive caching by FF and script download
379 	 * seen by the first installations (e.g. if lighttpd is misconfigured)*/
380 	int allow_caching = (content_type != &octet_stream)
381 	                 && (0 == r->http_status || 200 == r->http_status);
382 	if (__builtin_expect( (allow_caching), 1)) {
383 		if (__builtin_expect( (!light_btst(r->resp_htags, HTTP_HEADER_ETAG)), 1)
384 		    && 0 != r->conf.etag_flags) {
385 			const buffer *etag =
386 			  stat_cache_etag_get(sce, r->conf.etag_flags);
387 			if (etag && !buffer_is_blank(etag)) {
388 				http_header_response_set(r, HTTP_HEADER_ETAG,
389 				                         CONST_STR_LEN("ETag"),
390 				                         BUF_PTR_LEN(etag));
391 			}
392 		}
393 
394 		const buffer * const lmod =
395 		  __builtin_expect( (!light_btst(r->resp_htags, HTTP_HEADER_LAST_MODIFIED)), 1)
396 		  ? http_response_set_last_modified(r, sce->st.st_mtime)
397 		  : NULL;
398 
399 		if (http_response_maybe_cachable(r)
400 		    && HANDLER_FINISHED == http_response_handle_cachable(r, lmod, sce->st.st_mtime))
401 			return;
402 	}
403 
404 	/* if we are still here, prepare body */
405 
406 	/* we add it here for all requests
407 	 * the HEAD request will drop it afterwards again
408 	 */
409 
410 	if (0 == sce->st.st_size || 0 == http_chunk_append_file_ref(r, sce)) {
411 		r->http_status = 200;
412 		r->resp_body_finished = 1;
413 		/*(Transfer-Encoding should not have been set at this point)*/
414 		buffer_append_int(
415 		  http_header_response_set_ptr(r, HTTP_HEADER_CONTENT_LENGTH,
416 		                               CONST_STR_LEN("Content-Length")),
417 		  sce->st.st_size);
418 	}
419 	else {
420 		r->http_status = 500;
421 	}
422 }
423 
424 
425 static void http_response_xsendfile (request_st * const r, buffer * const path, const array * const xdocroot) {
426 	const int status = r->http_status;
427 	int valid = 1;
428 
429 	/* reset Content-Length, if set by backend
430 	 * Content-Length might later be set to size of X-Sendfile static file,
431 	 * determined by open(), fstat() to reduces race conditions if the file
432 	 * is modified between stat() (stat_cache_get_entry()) and open(). */
433 	if (light_btst(r->resp_htags, HTTP_HEADER_CONTENT_LENGTH)) {
434 		http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH,
435 		                           CONST_STR_LEN("Content-Length"));
436 	}
437 
438 	buffer_urldecode_path(path);
439 	if (!buffer_is_valid_UTF8(path)) {
440 		log_error(r->conf.errh, __FILE__, __LINE__,
441 		  "X-Sendfile invalid UTF-8 after url-decode: %s", path->ptr);
442 		if (r->http_status < 400) {
443 			r->http_status = 502;
444 			r->handler_module = NULL;
445 		}
446 		return;
447 	}
448 	buffer_path_simplify(path);
449 	if (r->conf.force_lowercase_filenames) {
450 		buffer_to_lower(path);
451 	}
452 	if (buffer_is_blank(path)) {
453 		r->http_status = 502;
454 		valid = 0;
455 	}
456 
457 	/* check that path is under xdocroot(s)
458 	 * - xdocroot should have trailing slash appended at config time
459 	 * - r->conf.force_lowercase_filenames is not a server-wide setting,
460 	 *   and so can not be definitively applied to xdocroot at config time*/
461 	if (xdocroot && xdocroot->used) {
462 		const buffer * const xval = !r->conf.force_lowercase_filenames
463 		  ? array_match_value_prefix(xdocroot, path)
464 		  : array_match_value_prefix_nc(xdocroot, path);
465 		if (NULL == xval) {
466 			log_error(r->conf.errh, __FILE__, __LINE__,
467 			  "X-Sendfile (%s) not under configured x-sendfile-docroot(s)", path->ptr);
468 			r->http_status = 403;
469 			valid = 0;
470 		}
471 	}
472 
473 	if (valid) http_response_send_file(r, path, NULL);
474 
475 	if (r->http_status >= 400 && status < 300) {
476 		r->handler_module = NULL;
477 	} else if (0 != status && 200 != status) {
478 		r->http_status = status;
479 	}
480 }
481 
482 
483 static void http_response_xsendfile2(request_st * const r, const buffer * const value, const array * const xdocroot) {
484     const char *pos = value->ptr;
485     buffer * const b = r->tmp_buf;
486     const int status = r->http_status;
487 
488     /* reset Content-Length, if set by backend */
489     if (light_btst(r->resp_htags, HTTP_HEADER_CONTENT_LENGTH)) {
490         http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH,
491                                    CONST_STR_LEN("Content-Length"));
492     }
493 
494     while (*pos) {
495         const char *filename, *range;
496         stat_cache_entry *sce;
497         off_t begin_range, end_range, range_len;
498 
499         while (' ' == *pos) pos++;
500         if (!*pos) break;
501 
502         filename = pos;
503         if (NULL == (range = strchr(pos, ' '))) {
504             /* missing range */
505             log_error(r->conf.errh, __FILE__, __LINE__,
506               "Couldn't find range after filename: %s", filename);
507             r->http_status = 502;
508             break;
509         }
510         buffer_copy_string_len(b, filename, range - filename);
511 
512         /* find end of range */
513         for (pos = ++range; *pos && *pos != ' ' && *pos != ','; pos++) ;
514 
515         buffer_urldecode_path(b);
516         if (!buffer_is_valid_UTF8(b)) {
517             log_error(r->conf.errh, __FILE__, __LINE__,
518               "X-Sendfile2 invalid UTF-8 after url-decode: %s", b->ptr);
519             r->http_status = 502;
520             break;
521         }
522         buffer_path_simplify(b);
523         if (r->conf.force_lowercase_filenames) {
524             buffer_to_lower(b);
525         }
526         if (buffer_is_blank(b)) {
527             r->http_status = 502;
528             break;
529         }
530         if (xdocroot && xdocroot->used) {
531             const buffer * const xval = !r->conf.force_lowercase_filenames
532               ? array_match_value_prefix(xdocroot, b)
533               : array_match_value_prefix_nc(xdocroot, b);
534             if (NULL == xval) {
535                 log_error(r->conf.errh, __FILE__, __LINE__,
536                   "X-Sendfile2 (%s) not under configured x-sendfile-docroot(s)",
537                   b->ptr);
538                 r->http_status = 403;
539                 break;
540             }
541         }
542 
543         sce = stat_cache_get_entry_open(b, r->conf.follow_symlink);
544         if (NULL == sce) {
545             log_error(r->conf.errh, __FILE__, __LINE__,
546               "send-file error: couldn't get stat_cache entry for "
547               "X-Sendfile2: %s", b->ptr);
548             r->http_status = 404;
549             break;
550         } else if (!S_ISREG(sce->st.st_mode)) {
551             log_error(r->conf.errh, __FILE__, __LINE__,
552               "send-file error: wrong filetype for X-Sendfile2: %s", b->ptr);
553             r->http_status = 502;
554             break;
555         }
556         /* found the file */
557 
558         /* parse range */
559         end_range = sce->st.st_size - 1;
560         {
561             char *rpos = NULL;
562             errno = 0;
563             begin_range = strtoll(range, &rpos, 10);
564             if (errno != 0 || begin_range < 0 || rpos == range)
565                 goto range_failed;
566             if ('-' != *rpos++) goto range_failed;
567             if (rpos != pos) {
568                 range = rpos;
569                 end_range = strtoll(range, &rpos, 10);
570                 if (errno != 0 || end_range < 0 || rpos == range)
571                     goto range_failed;
572             }
573             if (rpos != pos) goto range_failed;
574 
575             goto range_success;
576 
577 range_failed:
578             log_error(r->conf.errh, __FILE__, __LINE__,
579               "Couldn't decode range after filename: %s", filename);
580             r->http_status = 502;
581             break;
582 
583 range_success: ;
584         }
585 
586         /* no parameters accepted */
587 
588         while (*pos == ' ') pos++;
589         if (*pos != '\0' && *pos != ',') {
590             r->http_status = 502;
591             break;
592         }
593 
594         range_len = end_range - begin_range + 1;
595         if (range_len < 0) {
596             r->http_status = 502;
597             break;
598         }
599         if (range_len != 0) {
600             http_chunk_append_file_ref_range(r, sce, begin_range, range_len);
601         }
602 
603         if (*pos == ',') pos++;
604     }
605 
606     if (r->http_status >= 400 && status < 300) {
607 	r->handler_module = NULL;
608     } else if (0 != status && 200 != status) {
609         r->http_status = status;
610     }
611 }
612 
613 
614 void http_response_backend_error (request_st * const r) {
615 	if (r->resp_body_started) {
616 		/*(response might have been already started, kill the connection)*/
617 		/*(mode == DIRECT to avoid later call to http_response_backend_done())*/
618 		r->handler_module = NULL;  /*(avoid sending final chunked block)*/
619 		r->keep_alive = 0;
620 		r->resp_body_finished = 1;
621 	} /*(else error status set later by http_response_backend_done())*/
622 }
623 
624 void http_response_backend_done (request_st * const r) {
625 	/* (not CON_STATE_ERROR and not CON_STATE_RESPONSE_END,
626 	 *  i.e. not called from handle_connection_close or handle_request_reset
627 	 *  hooks, except maybe from errdoc handler, which later resets state)*/
628 	switch (r->state) {
629 	case CON_STATE_HANDLE_REQUEST:
630 	case CON_STATE_READ_POST:
631 		if (!r->resp_body_started) {
632 			/* Send an error if we haven't sent any data yet */
633 			if (r->http_status < 500 && r->http_status != 400)
634 				r->http_status = 500;
635 			r->handler_module = NULL;
636 			break;
637 		}
638 		__attribute_fallthrough__
639 	case CON_STATE_WRITE:
640 		if (!r->resp_body_finished) {
641 			if (r->http_version == HTTP_VERSION_1_1)
642 				http_chunk_close(r);
643 		  #if 0
644 			/* This is a lot of work to make it possible for an HTTP/1.0 client
645 			 * to detect that response is truncated (after lighttpd made an
646 			 * HTTP/1.1 request to backend, and backend gave a Transfer-Encoding
647 			 * chunked response instead of sending Content-Length, and lighttpd
648 			 * is streaming response to client).  An HTTP/1.0 client is probably
649 			 * not checking for truncated response, or else client should really
650 			 * prefer HTTP/1.1 or better.  If lighttpd were streaming response,
651 			 * there would be no Content-Length and lighttpd would have sent
652 			 * Connection: close.  Alternatively, since not streaming (if these
653 			 * conditions are true), could send an HTTP status error instead of
654 			 * sending partial content with a bogus Content-Length.  If we do
655 			 * not send an HTTP error status, then response_start hooks may add
656 			 * caching headers (e.g. mod_expire, mod_setenv).  If in future we
657 			 * send HTTP error status, might special-case HEAD requests and
658 			 * clear response body so that response headers (w/o Content-Length)
659 			 * can be sent.  For now, we have chosen to send partial content,
660 			 * including generating an incorrect Content-Length (later), and not
661 			 * to take any of these extra steps. */
662 			else if (__builtin_expect( (r->http_version == HTTP_VERSION_1_0), 0)
663 			         && r->gw_dechunk && !r->gw_dechunk->done
664 			         && !(r->conf.stream_response_body
665 			              & (FDEVENT_STREAM_RESPONSE
666 			                |FDEVENT_STREAM_RESPONSE_BUFMIN))) {
667 				r->keep_alive = 0; /* disable keep-alive, send bogus length */
668 				http_header_response_set(r, HTTP_HEADER_CONTENT_LENGTH,
669 				                         CONST_STR_LEN("Content-Length"),
670 				                         CONST_STR_LEN("9999999999999"));
671 				http_header_response_unset(r, HTTP_HEADER_ETAG,
672 				                           CONST_STR_LEN("ETag"));
673 				http_header_response_unset(r, HTTP_HEADER_LAST_MODIFIED,
674 				                           CONST_STR_LEN("Last-Modified"));
675 				http_header_response_unset(r, HTTP_HEADER_CACHE_CONTROL,
676 				                           CONST_STR_LEN("Cache-Control"));
677 				http_header_response_unset(r, HTTP_HEADER_EXPIRES,
678 				                           CONST_STR_LEN("Expires"));
679 			}
680 		  #endif
681 			r->resp_body_finished = 1;
682 		}
683 	default:
684 		break;
685 	}
686 }
687 
688 
689 void http_response_upgrade_read_body_unknown(request_st * const r) {
690     /* act as transparent proxy */
691     if (!(r->conf.stream_request_body & FDEVENT_STREAM_REQUEST))
692         r->conf.stream_request_body |=
693           (FDEVENT_STREAM_REQUEST_BUFMIN | FDEVENT_STREAM_REQUEST);
694     if (!(r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE))
695         r->conf.stream_response_body |=
696           (FDEVENT_STREAM_RESPONSE_BUFMIN | FDEVENT_STREAM_RESPONSE);
697     r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
698     r->reqbody_length = -2;
699     r->resp_body_scratchpad = -1;
700     r->keep_alive = 0;
701 }
702 
703 
704 __attribute_pure__
705 static int http_response_append_buffer_simple_accum(const request_st * const r, const off_t len) {
706     /*(check to accumulate small reads in buffer before flushing to temp file)*/
707     return
708       len < 32768 && r->write_queue.last && r->write_queue.last->file.is_temp;
709 }
710 
711 
712 static int http_response_append_buffer(request_st * const r, buffer * const mem, const int simple_accum) {
713     /* Note: this routine is separate from http_response_append_mem() to
714      * potentially avoid copying buffer by using http_chunk_append_buffer(). */
715 
716     if (r->resp_decode_chunked)
717         return http_chunk_decode_append_buffer(r, mem);
718 
719     if (r->resp_body_scratchpad > 0) {
720         off_t len = (off_t)buffer_clen(mem);
721         r->resp_body_scratchpad -= len;
722         if (r->resp_body_scratchpad > 0) {
723             if (simple_accum
724                 && http_response_append_buffer_simple_accum(r, len)) {
725                 r->resp_body_scratchpad += len;
726                 return 0; /*(accumulate small reads in buffer)*/
727             }
728         }
729         else { /* (r->resp_body_scratchpad <= 0) */
730             r->resp_body_finished = 1;
731             if (__builtin_expect( (r->resp_body_scratchpad < 0), 0)) {
732                 /*(silently truncate if data exceeds Content-Length)*/
733                 len += r->resp_body_scratchpad;
734                 r->resp_body_scratchpad = 0;
735                 buffer_truncate(mem, (uint32_t)len);
736             }
737         }
738     }
739     else if (0 == r->resp_body_scratchpad) {
740         /*(silently truncate if data exceeds Content-Length)*/
741         buffer_clear(mem);
742         return 0;
743     }
744     else if (simple_accum
745              && http_response_append_buffer_simple_accum(r, buffer_clen(mem))) {
746         return 0; /*(accumulate small reads in buffer)*/
747     }
748     return http_chunk_append_buffer(r, mem);
749 }
750 
751 
752 #ifdef HAVE_SPLICE
753 static int http_response_append_splice(request_st * const r, http_response_opts * const opts, buffer * const b, const int fd, unsigned int toread) {
754     /* check if worthwhile to splice() to avoid copying through userspace */
755     /*assert(opts->simple_accum);*//*(checked in caller)*/
756     if (r->resp_body_scratchpad >= toread
757         && (toread > 32768
758             || (toread >= 8192 /*(!http_response_append_buffer_simple_accum())*/
759                 && r->write_queue.last && r->write_queue.last->file.is_temp))) {
760 
761         if (!buffer_is_blank(b)) {
762             /*(flush small reads previously accumulated in b)*/
763             int rc = http_response_append_buffer(r, b, 0); /*(0 to flush)*/
764             chunk_buffer_yield(b); /*(improve large buf reuse)*/
765             if (__builtin_expect( (0 != rc), 0)) return -1; /* error */
766         }
767 
768         /*assert(opts->fdfmt == S_IFSOCK || opts->fdfmt == S_IFIFO);*/
769         ssize_t n = (opts->fdfmt == S_IFSOCK)
770           ? chunkqueue_append_splice_sock_tempfile(
771               &r->write_queue, fd, toread, r->conf.errh)
772           : chunkqueue_append_splice_pipe_tempfile(
773               &r->write_queue, fd, toread, r->conf.errh);
774         if (__builtin_expect( (n >= 0), 1)) {
775             if (0 == (r->resp_body_scratchpad -= n))
776                 r->resp_body_finished = 1;
777             return 1; /* success */
778         }
779         else if (n != -EINVAL)
780             return -1; /* error */
781         /*(fall through; target filesystem w/o splice() support)*/
782     }
783     return 0; /* not handled */
784 }
785 #endif
786 
787 
788 static int http_response_append_mem(request_st * const r, const char * const mem, size_t len) {
789     if (r->resp_decode_chunked)
790         return http_chunk_decode_append_mem(r, mem, len);
791 
792     if (r->resp_body_scratchpad > 0) {
793         r->resp_body_scratchpad -= (off_t)len;
794         if (r->resp_body_scratchpad <= 0) {
795             r->resp_body_finished = 1;
796             if (__builtin_expect( (r->resp_body_scratchpad < 0), 0)) {
797                 /*(silently truncate if data exceeds Content-Length)*/
798                 len = (size_t)(r->resp_body_scratchpad + (off_t)len);
799                 r->resp_body_scratchpad = 0;
800             }
801         }
802     }
803     else if (0 == r->resp_body_scratchpad) {
804         /*(silently truncate if data exceeds Content-Length)*/
805         return 0;
806     }
807     return http_chunk_append_mem(r, mem, len);
808 }
809 
810 
811 int http_response_transfer_cqlen(request_st * const r, chunkqueue * const cq, size_t len) {
812     /*(intended for use as callback from modules setting opts->parse(),
813      * e.g. mod_fastcgi and mod_ajp13)
814      *(do not set r->resp_body_finished here since those protocols handle it)*/
815     if (0 == len) return 0;
816     if (__builtin_expect( (!r->resp_decode_chunked), 1)) {
817         const size_t olen = len;
818         if (r->resp_body_scratchpad >= 0) {
819             r->resp_body_scratchpad -= (off_t)len;
820             if (__builtin_expect( (r->resp_body_scratchpad < 0), 0)) {
821                 /*(silently truncate if data exceeds Content-Length)*/
822                 len = (size_t)(r->resp_body_scratchpad + (off_t)len);
823                 r->resp_body_scratchpad = 0;
824             }
825         }
826         int rc = http_chunk_transfer_cqlen(r, cq, len);
827         if (__builtin_expect( (0 != rc), 0))
828             return -1;
829         if (__builtin_expect( (olen != len), 0)) /*discard excess data, if any*/
830             chunkqueue_mark_written(cq, (off_t)(olen - len));
831     }
832     else {
833         /* specialized use by opts->parse() to decode chunked encoding;
834          * FastCGI, AJP13 packet data is all type MEM_CHUNK
835          * (This extra copy can be avoided if FastCGI backend does not send
836          *  Transfer-Encoding: chunked, which FastCGI is not supposed to do) */
837         uint32_t remain = (uint32_t)len, wr;
838         for (const chunk *c = cq->first; c && remain; c=c->next, remain-=wr) {
839             /*assert(c->type == MEM_CHUNK);*/
840             wr = buffer_clen(c->mem) - c->offset;
841             if (wr > remain) wr = remain;
842             if (0 != http_chunk_decode_append_mem(r, c->mem->ptr+c->offset, wr))
843                 return -1;
844         }
845         chunkqueue_mark_written(cq, len);
846     }
847     return 0;
848 }
849 
850 
851 static int http_response_process_headers(request_st * const restrict r, http_response_opts * const restrict opts, char * const restrict s, const unsigned short hoff[8192], const int is_nph) {
852     int i = 1;
853 
854     if (is_nph) {
855         /* non-parsed headers ... we parse them anyway */
856         /* (accept HTTP/2.0 and HTTP/3.0 from naive non-proxy backends) */
857         /* (http_header_str_to_code() expects certain chars after code) */
858         if (s[12] == '\r' || s[12] == '\n') s[12] = '\0';/*(caller checked 12)*/
859         if ((s[5] == '1' || opts->backend != BACKEND_PROXY) && s[6] == '.'
860             && (s[7] == '1' || s[7] == '0') && s[8] == ' ') {
861             /* after the space should be a status code for us */
862             int status = http_header_str_to_code(s+9);
863             if (status >= 100 && status < 1000) {
864               #ifdef __COVERITY__ /* Coverity false positive for tainted data */
865                 status = 200;/* http_header_str_to_code() validates, untaints */
866               #endif
867                 r->http_status = status;
868                 opts->local_redir = 0; /*(disable; status was set)*/
869                 i = 2;
870             } /* else we expected 3 digits and didn't get them */
871         }
872 
873         if (0 == r->http_status) {
874             log_error(r->conf.errh, __FILE__, __LINE__,
875               "invalid HTTP status line: %s", s);
876             r->http_status = 502; /* Bad Gateway */
877             r->handler_module = NULL;
878             return 0;
879         }
880     }
881     else if (__builtin_expect( (opts->backend == BACKEND_PROXY), 0)) {
882         /* invalid response Status-Line from HTTP proxy */
883         r->http_status = 502; /* Bad Gateway */
884         r->handler_module = NULL;
885         return 0;
886     }
887 
888     for (; i < hoff[0]; ++i) {
889         const char *k = s+hoff[i], *value;
890         char *end = s+hoff[i+1]-1; /*('\n')*/
891 
892         /* parse the headers */
893         if (NULL == (value = memchr(k, ':', end - k))) {
894             /* we expect: "<key>: <value>\r\n" */
895             continue;
896         }
897 
898         const uint32_t klen = (uint32_t)(value - k);
899         if (0 == klen) continue; /*(already ignored when writing response)*/
900         const enum http_header_e id = http_header_hkey_get(k, klen);
901 
902         do { ++value; } while (*value == ' ' || *value == '\t'); /* skip LWS */
903         /* strip the \r?\n */
904         if (end > value && end[-1] == '\r') --end;
905         /*(XXX: not done; could remove trailing whitespace)*/
906 
907         if (opts->authorizer && (0 == r->http_status || 200 == r->http_status)){
908             if (id == HTTP_HEADER_STATUS) {
909                 end[0] = '\0';
910                 int status = http_header_str_to_code(value);
911                 if (status >= 100 && status < 1000) {
912                   #ifdef __COVERITY__ /* Coverity false positive for tainted */
913                     status = 200;/* http_header_str_to_code() validates */
914                   #endif
915                     r->http_status = status;
916                     opts->local_redir = 0; /*(disable; status was set)*/
917                 }
918                 else {
919                     r->http_status = 502; /* Bad Gateway */
920                     break; /*(do not unset r->handler_module)*/
921                 }
922             }
923             else if (id == HTTP_HEADER_OTHER && klen > 9 && (k[0] & 0xdf) == 'V'
924                      && buffer_eq_icase_ssn(k, CONST_STR_LEN("Variable-"))) {
925                 http_header_env_append(r, k + 9, klen - 9, value, end - value);
926             }
927             continue;
928         }
929 
930         switch (id) {
931           case HTTP_HEADER_STATUS:
932             if (opts->backend != BACKEND_PROXY) {
933                 end[0] = '\0';
934                 int status = http_header_str_to_code(value);
935                 if (status >= 100 && status < 1000) {
936                   #ifdef __COVERITY__ /* Coverity false positive for tainted */
937                     status = 200;/* http_header_str_to_code() validates */
938                   #endif
939                     r->http_status = status;
940                     opts->local_redir = 0; /*(disable; status was set)*/
941                 }
942                 else {
943                     r->http_status = 502;
944                     r->handler_module = NULL;
945                 }
946                 continue; /* do not send Status to client */
947             } /*(else pass w/o parse for BACKEND_PROXY)*/
948             break;
949           case HTTP_HEADER_UPGRADE:
950             /*(technically, should also verify Connection: upgrade)*/
951             /*(flag only for mod_proxy and mod_cgi (for now))*/
952             if (opts->backend != BACKEND_PROXY && opts->backend != BACKEND_CGI)
953                 continue;
954             if (r->http_version >= HTTP_VERSION_2 && !r->h2_connect_ext)
955                 continue;
956             break;
957           case HTTP_HEADER_CONNECTION:
958             if (opts->backend == BACKEND_PROXY) continue;
959             /*(simplistic attempt to honor backend request to close)*/
960             if (http_header_str_contains_token(value, end - value,
961                                                CONST_STR_LEN("close")))
962                 r->keep_alive = 0;
963             if (r->http_version >= HTTP_VERSION_2) continue;
964             break;
965           case HTTP_HEADER_CONTENT_LENGTH:
966             if (*value == '+') ++value;
967             if (!r->resp_decode_chunked
968                 && !light_btst(r->resp_htags, HTTP_HEADER_CONTENT_LENGTH)) {
969                 const char *err = end;
970                 while (err > value && (err[-1] == ' ' || err[-1] == '\t')) --err;
971                 if (err <= value) continue; /*(might error 502 Bad Gateway)*/
972                 uint32_t vlen = (uint32_t)(err - value);
973                 r->resp_body_scratchpad =
974                   (off_t)li_restricted_strtoint64(value, vlen, &err);
975                 if (err != value + vlen) {
976                     /*(invalid Content-Length value from backend;
977                      * read from backend until backend close, hope for the best)
978                      *(might choose to treat this as 502 Bad Gateway) */
979                     r->resp_body_scratchpad = -1;
980                 }
981             }
982             else {
983                 /* ignore Content-Length if Transfer-Encoding: chunked
984                  * ignore subsequent (multiple) Content-Length
985                  * (might choose to treat this as 502 Bad Gateway) */
986                 continue;
987             }
988             break;
989           case HTTP_HEADER_TRANSFER_ENCODING:
990             if (light_btst(r->resp_htags, HTTP_HEADER_CONTENT_LENGTH)) {
991                 /* ignore Content-Length if Transfer-Encoding: chunked
992                  * (might choose to treat this as 502 Bad Gateway) */
993                 r->resp_body_scratchpad = -1;
994                 http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH,
995                                            CONST_STR_LEN("Content-Length"));
996             }
997             /*(assumes "Transfer-Encoding: chunked"; does not verify)*/
998             r->resp_decode_chunked = 1;
999             r->gw_dechunk = calloc(1, sizeof(response_dechunk));
1000             force_assert(r->gw_dechunk);
1001             continue;
1002           case HTTP_HEADER_HTTP2_SETTINGS:
1003             /* RFC7540 3.2.1
1004              *   A server MUST NOT send this header field. */
1005             /* (not bothering to remove HTTP2-Settings from Connection) */
1006             continue;
1007           case HTTP_HEADER_OTHER:
1008             /* ignore invalid headers with whitespace between label and ':'
1009              * (if less strict behavior is desired, check and correct above
1010              *  this switch() statement, but not for BACKEND_PROXY) */
1011             if (k[klen-1] == ' ' || k[klen-1] == '\t')
1012                 continue;
1013             break;
1014           default:
1015             break;
1016         }
1017 
1018         if (end - value)
1019             http_header_response_insert(r, id, k, klen, value, end - value);
1020     }
1021 
1022     /* CGI/1.1 rev 03 - 7.2.1.2 */
1023     /* (proxy requires Status-Line, so never true for proxy)*/
1024     if (0 == r->http_status && light_btst(r->resp_htags, HTTP_HEADER_LOCATION)){
1025         r->http_status = 302;
1026     }
1027 
1028     return 0;
1029 }
1030 
1031 
1032 static http_response_send_1xx_cb http_response_send_1xx_h1;
1033 static http_response_send_1xx_cb http_response_send_1xx_h2;
1034 
1035 void
1036 http_response_send_1xx_cb_set (http_response_send_1xx_cb fn, int vers)
1037 {
1038     if (vers >= HTTP_VERSION_2)
1039         http_response_send_1xx_h2 = fn;
1040     else if (vers == HTTP_VERSION_1_1)
1041         http_response_send_1xx_h1 = fn;
1042 }
1043 
1044 
1045 int
1046 http_response_send_1xx (request_st * const r)
1047 {
1048     http_response_send_1xx_cb http_response_send_1xx_fn = NULL;
1049     if (r->http_version >= HTTP_VERSION_2)
1050         http_response_send_1xx_fn = http_response_send_1xx_h2;
1051     else if (r->http_version == HTTP_VERSION_1_1)
1052         http_response_send_1xx_fn = http_response_send_1xx_h1;
1053 
1054     if (http_response_send_1xx_fn && !http_response_send_1xx_fn(r, r->con))
1055         return 0; /* error occurred */
1056 
1057     http_response_header_clear(r);
1058     return 1; /* 1xx response handled */
1059 }
1060 
1061 
1062 __attribute_cold__
1063 __attribute_noinline__
1064 static int
1065 http_response_check_1xx (request_st * const r, buffer * const restrict b, uint32_t hlen, uint32_t dlen)
1066 {
1067     /* pass through unset r->http_status (not 1xx) or 101 Switching Protocols */
1068     if (0 == r->http_status || 101 == r->http_status)
1069         return 0; /* pass through as-is; do not loop for addtl response hdrs */
1070 
1071     /* discard 1xx response from b; already processed
1072      * (but further response might follow in b, so preserve addtl data) */
1073     if (dlen)
1074         memmove(b->ptr, b->ptr+hlen, dlen);
1075     buffer_truncate(b, dlen);
1076 
1077     /* Note: while GW_AUTHORIZER mode is not expected to return 1xx, as a
1078      * feature, 1xx responses from authorizer are passed back to client */
1079 
1080     return http_response_send_1xx(r);
1081     /* 0: error, 1: 1xx response handled; loop for next response headers */
1082 }
1083 
1084 
1085 __attribute_noinline__
1086 handler_t http_response_parse_headers(request_st * const r, http_response_opts * const opts, buffer * const b) {
1087     /**
1088      * possible formats of response headers:
1089      *
1090      * proxy or NPH (non-parsed headers):
1091      *
1092      *   HTTP/1.0 200 Ok\n
1093      *   Header: Value\n
1094      *   \n
1095      *
1096      * CGI:
1097      *
1098      *   Header: Value\n
1099      *   Status: 200\n
1100      *   \n
1101      *
1102      * and different mixes of \n and \r\n combinations
1103      *
1104      * Some users also forget about CGI and just send a response
1105      * and hope we handle it. No headers, no header-content separator
1106      */
1107     const char *bstart;
1108     uint32_t blen;
1109 
1110     do {
1111         uint32_t header_len, is_nph = 0;
1112         unsigned short hoff[8192]; /* max num header lines + 3; 16k on stack */
1113         hoff[0] = 1; /* number of lines */
1114         hoff[1] = 0; /* base offset for all lines */
1115         hoff[2] = 0; /* offset from base for 2nd line; init 0 to detect '\n' */
1116         blen = buffer_clen(b);
1117         header_len = http_header_parse_hoff(b->ptr, blen, hoff);
1118         if ((header_len ? header_len : blen) > MAX_HTTP_RESPONSE_FIELD_SIZE) {
1119             log_error(r->conf.errh, __FILE__, __LINE__,
1120               "response headers too large for %s", r->uri.path.ptr);
1121             r->http_status = 502; /* Bad Gateway */
1122             r->handler_module = NULL;
1123             return HANDLER_FINISHED;
1124         }
1125         if (hoff[2]) { /*(at least one newline found if offset is non-zero)*/
1126             /*("HTTP/1.1 200 " is at least 13 chars + \r\n; 12 w/o final ' ')*/
1127             is_nph = hoff[2] >= 12 && 0 == memcmp(b->ptr, "HTTP/", 5);
1128             if (!is_nph) {
1129                 const char * colon = memchr(b->ptr, ':', hoff[2]-1);
1130                 if (__builtin_expect( (NULL == colon), 0)) {
1131                     if (hoff[2] <= 2 && (1 == hoff[2] || b->ptr[0] == '\r')) {
1132                         /* no HTTP headers */
1133                     }
1134                     else if (opts->backend == BACKEND_CGI) {
1135                         /* no HTTP headers, but body (special-case for CGI)*/
1136                         /* no colon found; does not appear to be HTTP headers */
1137                         if (0 != http_chunk_append_buffer(r, b)) {
1138                             return HANDLER_ERROR;
1139                         }
1140                         r->http_status = 200; /* OK */
1141                         r->resp_body_started = 1;
1142                         return HANDLER_GO_ON;
1143                     }
1144                     else {
1145                         /* invalid response headers */
1146                         r->http_status = 502; /* Bad Gateway */
1147                         r->handler_module = NULL;
1148                         return HANDLER_FINISHED;
1149                     }
1150                 }
1151             }
1152         } /* else no newline yet; partial header line?) */
1153         if (0 == header_len) /* headers incomplete */
1154             return HANDLER_GO_ON;
1155 
1156         /* the body starts after the EOL */
1157         bstart = b->ptr + header_len;
1158         blen -= header_len;
1159 
1160         if (0 != http_response_process_headers(r, opts, b->ptr, hoff, is_nph))
1161             return HANDLER_ERROR;
1162 
1163     } while (r->http_status < 200
1164              && http_response_check_1xx(r, b, bstart - b->ptr, blen));
1165 
1166     r->resp_body_started = 1;
1167 
1168     if (opts->authorizer
1169         && (r->http_status == 0 || r->http_status == 200)) {
1170         return HANDLER_GO_ON;
1171     }
1172 
1173     if (NULL == r->handler_module) {
1174         return HANDLER_FINISHED;
1175     }
1176 
1177     if (opts->local_redir && r->http_status >= 300 && r->http_status < 400
1178         && 0 == blen) {
1179         /* (Might not have begun to receive body yet, but do skip local-redir
1180          *  if we already have started receiving a response body (blen > 0)) */
1181         /*(light_btst(r->resp_htags, HTTP_HEADER_LOCATION))*/
1182         handler_t rc = http_cgi_local_redir(r);
1183         if (rc != HANDLER_GO_ON) return rc;
1184     }
1185 
1186     if (opts->xsendfile_allow) {
1187         buffer *vb;
1188         /* X-Sendfile2 is deprecated; historical for fastcgi */
1189         if (opts->backend == BACKEND_FASTCGI
1190             && NULL != (vb = http_header_response_get(r, HTTP_HEADER_OTHER,
1191                                                       CONST_STR_LEN("X-Sendfile2")))) {
1192             http_response_xsendfile2(r, vb, opts->xsendfile_docroot);
1193             /* http_header_response_unset() shortcut for HTTP_HEADER_OTHER */
1194             buffer_clear(vb); /*(do not send to client)*/
1195             if (NULL == r->handler_module)
1196                 r->resp_body_started = 0;
1197             return HANDLER_FINISHED;
1198         } else if (NULL != (vb = http_header_response_get(r, HTTP_HEADER_OTHER,
1199                                                           CONST_STR_LEN("X-Sendfile")))
1200                    || (opts->backend == BACKEND_FASTCGI /* X-LIGHTTPD-send-file is deprecated; historical for fastcgi */
1201                        && NULL != (vb = http_header_response_get(r, HTTP_HEADER_OTHER,
1202                                                                  CONST_STR_LEN("X-LIGHTTPD-send-file"))))) {
1203             http_response_xsendfile(r, vb, opts->xsendfile_docroot);
1204             /* http_header_response_unset() shortcut for HTTP_HEADER_OTHER */
1205             buffer_clear(vb); /*(do not send to client)*/
1206             if (NULL == r->handler_module)
1207                 r->resp_body_started = 0;
1208             return HANDLER_FINISHED;
1209         }
1210     }
1211 
1212     if (blen > 0) {
1213         /* modules which set opts->parse() (e.g. mod_ajp13, mod_fastcgi) must
1214          * not pass buffer with excess data to this routine (and do not do so
1215          * due to framing of response headers separately from response body) */
1216         int rc = http_response_append_mem(r, bstart, blen);
1217         if (__builtin_expect( (0 != rc), 0))
1218             return HANDLER_ERROR;
1219     }
1220 
1221     /* (callback for response headers complete) */
1222     return (opts->headers) ? opts->headers(r, opts) : HANDLER_GO_ON;
1223 }
1224 
1225 
1226 handler_t http_response_read(request_st * const r, http_response_opts * const opts, buffer * const b, fdnode * const fdn) {
1227     const int fd = fdn->fd;
1228     ssize_t n;
1229     size_t avail;
1230     /*size_t total = 0;*/
1231     do {
1232         unsigned int toread = 0;
1233         avail = buffer_string_space(b);
1234 
1235         if (0 == fdevent_ioctl_fionread(fd, opts->fdfmt, (int *)&toread)) {
1236 
1237           #ifdef HAVE_SPLICE
1238             /* check if worthwhile to splice() to avoid copying to userspace */
1239             if (opts->simple_accum) {
1240                 int rc = http_response_append_splice(r, opts, b, fd, toread);
1241                 if (rc) {
1242                     if (__builtin_expect( (rc > 0), 1))
1243                         break;
1244                     return HANDLER_ERROR;
1245                 } /*(fall through to handle traditionally)*/
1246             }
1247           #endif
1248 
1249             if (avail < toread) {
1250                 uint32_t blen = buffer_clen(b);
1251                 if (toread + blen < 4096)
1252                     toread = 4095 - blen;
1253                 else if (toread > opts->max_per_read)
1254                     toread = opts->max_per_read;
1255                 /* reduce amount read for response headers to reduce extra data
1256                  * copying for initial data following response headers
1257                  * (see http_response_parse_headers())
1258                  * (This seems reasonable to do even if opts->parse is set)
1259                  * (default chunk buffer is 8k; typical response headers < 8k)
1260                  * (An alternative might be the opposite: read extra, e.g. 128k,
1261                  *  if data available, in order to write to temp files sooner)*/
1262                 if (toread > 8192 && !r->resp_body_started) toread = 8192;
1263             }
1264             else if (0 == toread) {
1265               #if 0
1266                 return (fdevent_fdnode_interest(fdn) & FDEVENT_IN)
1267                   ? HANDLER_FINISHED  /* read finished */
1268                   : HANDLER_GO_ON;    /* optimistic read; data not ready */
1269               #else
1270                 if (!(fdevent_fdnode_interest(fdn) & FDEVENT_IN)) {
1271                     if (!(r->conf.stream_response_body
1272                           & FDEVENT_STREAM_RESPONSE_POLLRDHUP))
1273                         return HANDLER_GO_ON;/*optimistic read; data not ready*/
1274                 }
1275                 if (0 == avail) /* let read() below indicate if EOF or EAGAIN */
1276                     toread = 1024;
1277               #endif
1278             }
1279         }
1280         else if (avail < 1024) {
1281             toread = 4095 - avail;
1282         }
1283 
1284         if (r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
1285             off_t cqlen = chunkqueue_length(&r->write_queue);
1286             if (cqlen + (off_t)toread > 65536 - 4096) {
1287                 if (!r->con->is_writable) {
1288                     /*(defer removal of FDEVENT_IN interest since
1289                      * connection_state_machine() might be able to send data
1290                      * immediately, unless !con->is_writable, where
1291                      * connection_state_machine() might not loop back to call
1292                      * mod_proxy_handle_subrequest())*/
1293                     fdevent_fdnode_event_clr(r->con->srv->ev, fdn, FDEVENT_IN);
1294                 }
1295                 if (cqlen >= 65536-1) {
1296                     if (buffer_is_blank(b))
1297                         chunk_buffer_yield(b); /*(improve large buf reuse)*/
1298                     return HANDLER_GO_ON;
1299                 }
1300                 toread = 65536 - 1 - (unsigned int)cqlen;
1301                 /* Note: heuristic is fuzzy in that it limits how much to read
1302                  * from backend based on how much is pending to write to client.
1303                  * Modules where data from backend is framed (e.g. FastCGI) may
1304                  * want to limit how much is buffered from backend while waiting
1305                  * for a complete data frame or data packet from backend. */
1306             }
1307         }
1308 
1309         if (avail < toread) {
1310             /*(add avail+toread to reduce allocations when ioctl EOPNOTSUPP)*/
1311             avail = toread < opts->max_per_read && avail
1312               ? avail-1+toread
1313               : toread;
1314             avail = chunk_buffer_prepare_append(b, avail);
1315         }
1316 
1317         n = read(fd, b->ptr+buffer_clen(b), avail);
1318 
1319         if (n < 0) {
1320             switch (errno) {
1321               case EAGAIN:
1322              #ifdef EWOULDBLOCK
1323              #if EWOULDBLOCK != EAGAIN
1324               case EWOULDBLOCK:
1325              #endif
1326              #endif
1327               case EINTR:
1328                 if (buffer_is_blank(b))
1329                     chunk_buffer_yield(b); /*(improve large buf reuse)*/
1330                 return HANDLER_GO_ON;
1331               default:
1332                 log_perror(r->conf.errh, __FILE__, __LINE__,
1333                   "read() %d %d", r->con->fd, fd);
1334                 return HANDLER_ERROR;
1335             }
1336         }
1337 
1338         buffer_commit(b, (size_t)n);
1339       #ifdef __COVERITY__
1340         /* Coverity Scan overlooks the effect of buffer_commit() */
1341         b->ptr[buffer_clen(b)+n] = '\0';
1342       #endif
1343 
1344         if (NULL != opts->parse) {
1345             handler_t rc = opts->parse(r, opts, b, (size_t)n);
1346             if (rc != HANDLER_GO_ON) return rc;
1347         } else if (0 == n) {
1348             if (buffer_is_blank(b))
1349                 chunk_buffer_yield(b); /*(improve large buf reuse)*/
1350             else if (opts->simple_accum) {
1351                 /*(flush small reads previously accumulated in b)*/
1352                 int rc = http_response_append_buffer(r, b, 0); /*(0 to flush)*/
1353                 chunk_buffer_yield(b); /*(improve large buf reuse)*/
1354                 if (__builtin_expect( (0 != rc), 0)) {
1355                     /* error writing to tempfile;
1356                      * truncate response or send 500 if nothing sent yet */
1357                     return HANDLER_ERROR;
1358                 }
1359             }
1360             /* note: no further data is sent to backend after read EOF on socket
1361              * (not checking for half-closed TCP socket)
1362              * (backend should read all data desired prior to closing socket,
1363              *  though might send app-level close data frame, if applicable) */
1364             return HANDLER_FINISHED; /* read finished */
1365         } else if (0 == r->resp_body_started) {
1366             /* split header from body */
1367             handler_t rc = http_response_parse_headers(r, opts, b);
1368             if (rc != HANDLER_GO_ON) return rc;
1369             /* accumulate response in b until headers completed (or error)*/
1370             if (r->resp_body_started) {
1371                 buffer_clear(b);
1372                 /* check if Content-Length provided and response body received
1373                  * (done here instead of http_response_process_headers() since
1374                  *  backends which set opts->parse() might handle differently)*/
1375                 if (0 == r->resp_body_scratchpad)
1376                     r->resp_body_finished = 1;
1377                 /* enable simple accumulation of small reads in some situations
1378                  * no Content-Length (will read to EOF)
1379                  * Content-Length (will read until r->resp_body_scratchpad == 0)
1380                  * not chunked-encoding
1381                  * not bufmin streaming
1382                  * (no custom parse routine set for opts->parse()) */
1383                 else if (!r->resp_decode_chunked /* && NULL == opts->parse */
1384                          && !(r->conf.stream_response_body
1385                               & FDEVENT_STREAM_RESPONSE_BUFMIN))
1386                     opts->simple_accum = 1;
1387             }
1388         } else {
1389             /* flush b (do not accumulate small reads) if streaming and might
1390              * write to client since there is a chance that r->write_queue is
1391              * fully written to client (no more temp files) and then we do not
1392              * want to hold onto buffered data in b for an indeterminate time
1393              * until next read of data from backend */
1394             int simple_accum = opts->simple_accum
1395                             && (!(r->conf.stream_response_body
1396                                   & FDEVENT_STREAM_RESPONSE)
1397                                 || !r->con->is_writable);
1398             int rc = http_response_append_buffer(r, b, simple_accum);
1399             if (__builtin_expect( (0 != rc), 0)) {
1400                 /* error writing to tempfile;
1401                  * truncate response or send 500 if nothing sent yet */
1402                 return HANDLER_ERROR;
1403             }
1404             /*buffer_clear(b);*//*http_response_append_buffer() clears*/
1405             /* small reads might accumulate in b; not necessarily cleared */
1406         }
1407 
1408         if (r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
1409             if (chunkqueue_length(&r->write_queue) > 65536 - 4096) {
1410                 /*(defer removal of FDEVENT_IN interest since
1411                  * connection_state_machine() might be able to send
1412                  * data immediately, unless !con->is_writable, where
1413                  * connection_state_machine() might not loop back to
1414                  * call the subrequest handler)*/
1415                 if (!r->con->is_writable)
1416                     fdevent_fdnode_event_clr(r->con->srv->ev, fdn, FDEVENT_IN);
1417                 break;
1418             }
1419         }
1420     } while (!r->resp_body_started); /*(loop to read large response headers)*/
1421     /*while (0);*//*(extra logic might benefit systems without FIONREAD)*/
1422     /*while ((size_t)n == avail && (total += (size_t)n) < opts->max_per_read);*/
1423     /* else emptied kernel read buffer or partial read or reached read limit */
1424 
1425     if (buffer_is_blank(b)) chunk_buffer_yield(b); /*(improve large buf reuse)*/
1426 
1427     return (!r->resp_body_finished ? HANDLER_GO_ON : HANDLER_FINISHED);
1428 }
1429