Bug Summary

File:lib/lwan-request.c
Warning:line 1710, column 13
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lwan-request.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -fno-plt -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/buildbot/lwan-worker/clang-analyze/build/src/lib -resource-dir /usr/lib/clang/13.0.1 -include /home/buildbot/lwan-worker/clang-analyze/build/lwan-build-config.h -D _FILE_OFFSET_BITS=64 -D _TIME_BITS=64 -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib/missing -I /usr/include/python3.10 -I /usr/include/luajit-2.1 -I /usr/include/valgrind -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib -I /home/buildbot/lwan-worker/clang-analyze/build -internal-isystem /usr/lib/clang/13.0.1/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../x86_64-pc-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-unused-parameter -Wno-free-nonheap-object -std=gnu99 -fdebug-compilation-dir=/home/buildbot/lwan-worker/clang-analyze/build/src/lib -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/buildbot/lwan-worker/clang-analyze/CLANG/2022-06-05-005737-3677223-1 -x c /home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c
1/*
2 * lwan - simple web server
3 * Copyright (c) 2012-2014 L. A. F. Pereira <l@tia.mat.br>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#define _GNU_SOURCE
21#include <arpa/inet.h>
22#include <assert.h>
23#include <errno(*__errno_location ()).h>
24#include <fcntl.h>
25#include <inttypes.h>
26#include <limits.h>
27#include <stddef.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <strings.h>
32#include <sys/mman.h>
33#include <sys/socket.h>
34#include <sys/stat.h>
35#include <sys/types.h>
36#include <sys/vfs.h>
37#include <unistd.h>
38
39#include "lwan-private.h"
40
41#include "base64.h"
42#include "list.h"
43#include "lwan-config.h"
44#include "lwan-http-authorize.h"
45#include "lwan-io-wrappers.h"
46#include "sha1.h"
47
48#define HEADER_VALUE_SEPARATOR_LEN(sizeof(": ") - 1) (sizeof(": ") - 1)
49#define HEADER_TERMINATOR_LEN(sizeof("\r\n") - 1) (sizeof("\r\n") - 1)
50#define MIN_REQUEST_SIZE(sizeof("GET / HTTP/1.1\r\n\r\n") - 1) (sizeof("GET / HTTP/1.1\r\n\r\n") - 1)
51
52enum lwan_read_finalizer {
53 FINALIZER_DONE,
54 FINALIZER_TRY_AGAIN,
55 FINALIZER_TIMEOUT,
56};
57
58struct proxy_header_v2 {
59 uint8_t sig[12];
60 uint8_t cmd_ver;
61 uint8_t fam;
62 uint16_t len;
63 union {
64 struct {
65 in_addr_t src_addr;
66 in_addr_t dst_addr;
67 uint16_t src_port;
68 uint16_t dst_port;
69 } ip4;
70 struct {
71 struct in6_addr src_addr;
72 struct in6_addr dst_addr;
73 uint16_t src_port;
74 uint16_t dst_port;
75 } ip6;
76 } addr;
77};
78
79static char decode_hex_digit(char ch) __attribute__((pure));
80static char *ignore_leading_whitespace(char *buffer) __attribute__((pure));
81
82
83static bool_Bool
84parse_ascii_port(char *port, unsigned short *out)
85{
86 unsigned long parsed;
87 char *end_ptr;
88
89 errno(*__errno_location ()) = 0;
90 parsed = strtoul(port, &end_ptr, 10);
91
92 if (UNLIKELY(errno != 0)__builtin_expect((((*__errno_location ()) != 0)), (0)))
93 return false0;
94
95 if (UNLIKELY(*end_ptr != '\0')__builtin_expect(((*end_ptr != '\0')), (0)))
96 return false0;
97
98 if (UNLIKELY((unsigned long)(unsigned short)parsed != parsed)__builtin_expect((((unsigned long)(unsigned short)parsed != parsed
)), (0))
)
99 return false0;
100
101 *out = htons((unsigned short)parsed);
102 return true1;
103}
104
105static char *
106strsep_char(char *strp, const char *end, char delim)
107{
108 char *ptr;
109
110 if (UNLIKELY(!strp)__builtin_expect(((!strp)), (0)))
111 return NULL((void*)0);
112
113 if (UNLIKELY(strp > end)__builtin_expect(((strp > end)), (0)))
114 return NULL((void*)0);
115
116 ptr = strchr(strp, delim);
117 if (UNLIKELY(!ptr)__builtin_expect(((!ptr)), (0)))
118 return NULL((void*)0);
119
120 *ptr = '\0';
121 return ptr + 1;
122}
123
124static char *
125parse_proxy_protocol_v1(struct lwan_request *request, char *buffer)
126{
127 static const size_t line_size = 108;
128 char *end, *protocol, *src_addr, *dst_addr, *src_port, *dst_port;
129 unsigned int size;
130 struct lwan_proxy *const proxy = request->proxy;
131
132 end = memchr(buffer, '\r', line_size);
133 if (UNLIKELY(!end || end[1] != '\n')__builtin_expect(((!end || end[1] != '\n')), (0)))
134 return NULL((void*)0);
135 *end = '\0';
136 size = (unsigned int) (end + 2 - buffer);
137
138 protocol = buffer + sizeof("PROXY ") - 1;
139 src_addr = strsep_char(protocol, end, ' ');
140 dst_addr = strsep_char(src_addr, end, ' ');
141 src_port = strsep_char(dst_addr, end, ' ');
142 dst_port = strsep_char(src_port, end, ' ');
143
144 if (UNLIKELY(!dst_port)__builtin_expect(((!dst_port)), (0)))
145 return NULL((void*)0);
146
147 STRING_SWITCH(protocol)switch (string_as_uint32(protocol)) {
148 case STR4_INT('T', 'C', 'P', '4')((uint32_t)(('T') | ('C') << 8 | ('P') << 16 | ('4'
) << 24))
: {
149 struct sockaddr_in *from = &proxy->from.ipv4;
150 struct sockaddr_in *to = &proxy->to.ipv4;
151
152 from->sin_family = to->sin_family = AF_INET2;
153
154 if (UNLIKELY(inet_pton(AF_INET, src_addr, &from->sin_addr) <= 0)__builtin_expect(((inet_pton(2, src_addr, &from->sin_addr
) <= 0)), (0))
)
155 return NULL((void*)0);
156 if (UNLIKELY(inet_pton(AF_INET, dst_addr, &to->sin_addr) <= 0)__builtin_expect(((inet_pton(2, dst_addr, &to->sin_addr
) <= 0)), (0))
)
157 return NULL((void*)0);
158 if (UNLIKELY(!parse_ascii_port(src_port, &from->sin_port))__builtin_expect(((!parse_ascii_port(src_port, &from->
sin_port))), (0))
)
159 return NULL((void*)0);
160 if (UNLIKELY(!parse_ascii_port(dst_port, &to->sin_port))__builtin_expect(((!parse_ascii_port(dst_port, &to->sin_port
))), (0))
)
161 return NULL((void*)0);
162
163 break;
164 }
165 case STR4_INT('T', 'C', 'P', '6')((uint32_t)(('T') | ('C') << 8 | ('P') << 16 | ('6'
) << 24))
: {
166 struct sockaddr_in6 *from = &proxy->from.ipv6;
167 struct sockaddr_in6 *to = &proxy->to.ipv6;
168
169 from->sin6_family = to->sin6_family = AF_INET610;
170
171 if (UNLIKELY(inet_pton(AF_INET6, src_addr, &from->sin6_addr) <= 0)__builtin_expect(((inet_pton(10, src_addr, &from->sin6_addr
) <= 0)), (0))
)
172 return NULL((void*)0);
173 if (UNLIKELY(inet_pton(AF_INET6, dst_addr, &to->sin6_addr) <= 0)__builtin_expect(((inet_pton(10, dst_addr, &to->sin6_addr
) <= 0)), (0))
)
174 return NULL((void*)0);
175 if (UNLIKELY(!parse_ascii_port(src_port, &from->sin6_port))__builtin_expect(((!parse_ascii_port(src_port, &from->
sin6_port))), (0))
)
176 return NULL((void*)0);
177 if (UNLIKELY(!parse_ascii_port(dst_port, &to->sin6_port))__builtin_expect(((!parse_ascii_port(dst_port, &to->sin6_port
))), (0))
)
178 return NULL((void*)0);
179
180 break;
181 }
182 default:
183 return NULL((void*)0);
184 }
185
186 request->flags |= REQUEST_PROXIED;
187 return buffer + size;
188}
189
190static char *parse_proxy_protocol_v2(struct lwan_request *request, char *buffer)
191{
192 struct proxy_header_v2 *hdr = (struct proxy_header_v2 *)buffer;
193 struct lwan_request_parser_helper *helper = request->helper;
194 const unsigned int proto_signature_length = 16;
195 unsigned int size;
196 struct lwan_proxy *const proxy = request->proxy;
197
198 enum { LOCAL = 0x20, PROXY = 0x21, TCP4 = 0x11, TCP6 = 0x21 };
199
200 size = proto_signature_length + (unsigned int)ntohs(hdr->len);
201 if (UNLIKELY(size > (unsigned int)sizeof(*hdr))__builtin_expect(((size > (unsigned int)sizeof(*hdr))), (0
))
)
202 return NULL((void*)0);
203 if (UNLIKELY(size >= helper->buffer->len)__builtin_expect(((size >= helper->buffer->len)), (0
))
)
204 return NULL((void*)0);
205
206 if (LIKELY(hdr->cmd_ver == PROXY)__builtin_expect((!!(hdr->cmd_ver == PROXY)), (1))) {
207 if (hdr->fam == TCP4) {
208 struct sockaddr_in *from = &proxy->from.ipv4;
209 struct sockaddr_in *to = &proxy->to.ipv4;
210
211 to->sin_family = from->sin_family = AF_INET2;
212
213 from->sin_addr.s_addr = hdr->addr.ip4.src_addr;
214 from->sin_port = hdr->addr.ip4.src_port;
215
216 to->sin_addr.s_addr = hdr->addr.ip4.dst_addr;
217 to->sin_port = hdr->addr.ip4.dst_port;
218 } else if (hdr->fam == TCP6) {
219 struct sockaddr_in6 *from = &proxy->from.ipv6;
220 struct sockaddr_in6 *to = &proxy->to.ipv6;
221
222 from->sin6_family = to->sin6_family = AF_INET610;
223
224 from->sin6_addr = hdr->addr.ip6.src_addr;
225 from->sin6_port = hdr->addr.ip6.src_port;
226
227 to->sin6_addr = hdr->addr.ip6.dst_addr;
228 to->sin6_port = hdr->addr.ip6.dst_port;
229 } else {
230 return NULL((void*)0);
231 }
232 } else if (hdr->cmd_ver == LOCAL) {
233 struct sockaddr_in *from = &proxy->from.ipv4;
234 struct sockaddr_in *to = &proxy->to.ipv4;
235
236 from->sin_family = to->sin_family = AF_UNSPEC0;
237 } else {
238 return NULL((void*)0);
239 }
240
241 request->flags |= REQUEST_PROXIED;
242 return buffer + size;
243}
244
245static ALWAYS_INLINEinline __attribute__((always_inline)) char *identify_http_method(struct lwan_request *request,
246 char *buffer)
247{
248 const uint32_t first_four = string_as_uint32(buffer);
249
250#define GENERATE_IF(upper, lower, mask, constant, probability) \
251 if (__builtin_expect_with_probability(first_four == (constant), 1, \
252 probability)) { \
253 request->flags |= (mask); \
254 return buffer + sizeof(#upper); \
255 }
256
257 FOR_EACH_REQUEST_METHOD(GENERATE_IF)GENERATE_IF(GET, get, (1 << 0), (((uint32_t)(('G') | ('E'
) << 8 | ('T') << 16 | (' ') << 24))), 0.6)
GENERATE_IF(POST, post, (1 << 3 | 1 << 1 | 1 <<
0), (((uint32_t)(('P') | ('O') << 8 | ('S') << 16
| ('T') << 24))), 0.2) GENERATE_IF(HEAD, head, (1 <<
1), (((uint32_t)(('H') | ('E') << 8 | ('A') << 16
| ('D') << 24))), 0.2) GENERATE_IF(OPTIONS, options, (
1 << 2), (((uint32_t)(('O') | ('P') << 8 | ('T') <<
16 | ('I') << 24))), 0.1) GENERATE_IF(DELETE, delete, (
1 << 1 | 1 << 2), (((uint32_t)(('D') | ('E') <<
8 | ('L') << 16 | ('E') << 24))), 0.1) GENERATE_IF
(PUT, put, (1 << 3 | 1 << 2 | 1 << 0), (((uint32_t
)(('P') | ('U') << 8 | ('T') << 16 | (' ') <<
24))), 0.1)
258
259#undef GENERATE_IF
260
261 return NULL((void*)0);
262}
263
264static ALWAYS_INLINEinline __attribute__((always_inline)) char decode_hex_digit(char ch)
265{
266 static const char hex_digit_tbl[256] = {
267 ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4, ['5'] = 5,
268 ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9, ['a'] = 10, ['b'] = 11,
269 ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15, ['A'] = 10, ['B'] = 11,
270 ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
271 };
272 return hex_digit_tbl[(unsigned char)ch];
273}
274
275static ssize_t url_decode(char *str)
276{
277 if (UNLIKELY(!str)__builtin_expect(((!str)), (0)))
278 return -EINVAL22;
279
280 char *ch, *decoded;
281 for (decoded = ch = str; *ch; ch++) {
282 if (*ch == '%') {
283 char tmp =
284 (char)(decode_hex_digit(ch[1]) << 4 | decode_hex_digit(ch[2]));
285
286 if (UNLIKELY(!tmp)__builtin_expect(((!tmp)), (0)))
287 return -EINVAL22;
288
289 *decoded++ = tmp;
290 ch += 2;
291 } else if (*ch == '+') {
292 *decoded++ = ' ';
293 } else {
294 *decoded++ = *ch;
295 }
296 }
297
298 *decoded = '\0';
299 return (ssize_t)(decoded - str);
300}
301
302static int key_value_compare(const void *a, const void *b)
303{
304 return strcmp(((const struct lwan_key_value *)a)->key,
305 ((const struct lwan_key_value *)b)->key);
306}
307
308static void
309reset_key_value_array(void *data)
310{
311 struct lwan_key_value_array *array = data;
312
313 lwan_key_value_array_reset(array);
314}
315
316static void parse_key_values(struct lwan_request *request,
317 struct lwan_value *helper_value,
318 struct lwan_key_value_array *array,
319 ssize_t (*decode_value)(char *value),
320 const char separator)
321{
322 struct lwan_key_value *kv;
323 char *ptr = helper_value->value;
324 const char *end = helper_value->value + helper_value->len;
325 struct coro_defer *reset_defer;
326
327 if (!helper_value->len)
328 return;
329
330 lwan_key_value_array_init(array);
331 reset_defer = coro_defer(request->conn->coro, reset_key_value_array, array);
332
333 do {
334 char *key, *value;
335
336 while (*ptr == ' ' || *ptr == separator)
337 ptr++;
338 if (UNLIKELY(*ptr == '\0')__builtin_expect(((*ptr == '\0')), (0)))
339 break;
340
341 key = ptr;
342 ptr = strsep_char(key, end, separator);
343
344 value = strsep_char(key, end, '=');
345 if (UNLIKELY(!value)__builtin_expect(((!value)), (0))) {
346 value = "";
347 } else if (UNLIKELY(decode_value(value) < 0)__builtin_expect(((decode_value(value) < 0)), (0))) {
348 /* Disallow values that failed decoding, but allow empty values */
349 goto error;
350 }
351
352 if (UNLIKELY(decode_value(key) <= 0)__builtin_expect(((decode_value(key) <= 0)), (0))) {
353 /* Disallow keys that failed decoding, or empty keys */
354 goto error;
355 }
356
357 kv = lwan_key_value_array_append(array);
358 if (UNLIKELY(!kv)__builtin_expect(((!kv)), (0)))
359 goto error;
360
361 kv->key = key;
362 kv->value = value;
363 } while (ptr);
364
365 lwan_key_value_array_sort(array, key_value_compare);
366
367 return;
368
369error:
370 coro_defer_fire_and_disarm(request->conn->coro, reset_defer);
371}
372
373static ssize_t
374identity_decode(char *input __attribute__((unused)))
375{
376 return 1;
377}
378
379static void parse_cookies(struct lwan_request *request)
380{
381 const char *cookies = lwan_request_get_header(request, "Cookie");
382
383 if (!cookies)
384 return;
385
386 struct lwan_value header = {.value = (char *)cookies,
387 .len = strlen(cookies)};
388 parse_key_values(request, &header, &request->helper->cookies,
389 identity_decode, ';');
390}
391
392static void parse_query_string(struct lwan_request *request)
393{
394 struct lwan_request_parser_helper *helper = request->helper;
395
396 parse_key_values(request, &helper->query_string, &helper->query_params,
397 url_decode, '&');
398}
399
400static void parse_form_data(struct lwan_request *request)
401{
402 struct lwan_request_parser_helper *helper = request->helper;
403 static const char content_type[] = "application/x-www-form-urlencoded";
404
405 if (helper->content_type.len < sizeof(content_type) - 1)
406 return;
407 if (UNLIKELY(strncmp(helper->content_type.value, content_type,__builtin_expect(((strncmp(helper->content_type.value, content_type
, sizeof(content_type) - 1))), (0))
408 sizeof(content_type) - 1))__builtin_expect(((strncmp(helper->content_type.value, content_type
, sizeof(content_type) - 1))), (0))
)
409 return;
410
411 parse_key_values(request, &helper->body_data, &helper->post_params,
412 url_decode, '&');
413}
414
415static void find_query_string(struct lwan_request *request, const char *space)
416{
417 struct lwan_request_parser_helper *helper = request->helper;
418
419 char *query_string = memchr(request->url.value, '?', request->url.len);
420 if (query_string) {
421 *query_string = '\0';
422 helper->query_string.value = query_string + 1;
423 helper->query_string.len = (size_t)(space - query_string - 1);
424 request->url.len -= helper->query_string.len + 1;
425 request->flags |= REQUEST_HAS_QUERY_STRING;
426 }
427}
428
429static char *
430identify_http_path(struct lwan_request *request, char *buffer)
431{
432 struct lwan_request_parser_helper *helper = request->helper;
433 static const size_t minimal_request_line_len = sizeof("/ HTTP/1.0") - 1;
434 char *space, *end_of_line;
435 ptrdiff_t end_len;
436
437 if (UNLIKELY(*buffer != '/')__builtin_expect(((*buffer != '/')), (0)))
438 return NULL((void*)0);
439
440 end_len = buffer - helper->buffer->value;
441 if (UNLIKELY((size_t)end_len >= helper->buffer->len)__builtin_expect((((size_t)end_len >= helper->buffer->
len)), (0))
)
442 return NULL((void*)0);
443
444 end_of_line = memchr(buffer, '\r', helper->buffer->len - (size_t)end_len);
445 if (UNLIKELY(!end_of_line)__builtin_expect(((!end_of_line)), (0)))
446 return NULL((void*)0);
447 if (UNLIKELY((size_t)(end_of_line - buffer) < minimal_request_line_len)__builtin_expect((((size_t)(end_of_line - buffer) < minimal_request_line_len
)), (0))
)
448 return NULL((void*)0);
449 *end_of_line = '\0';
450
451 space = end_of_line - sizeof("HTTP/X.X");
452
453 request->url.value = buffer;
454 request->url.len = (size_t)(space - buffer);
455 find_query_string(request, space);
456 request->original_url = request->url;
457
458 *space++ = '\0';
459
460 STRING_SWITCH_LARGE(space)switch (string_as_uint64(space)) {
461 case STR8_INT('H','T','T','P','/','1','.','0')((uint64_t)((uint32_t)(('H') | ('T') << 8 | ('T') <<
16 | ('P') << 24)) | (uint64_t)((uint32_t)(('/') | ('1'
) << 8 | ('.') << 16 | ('0') << 24)) <<
32)
:
462 request->flags |= REQUEST_IS_HTTP_1_0;
463 break;
464 case STR8_INT('H','T','T','P','/','1','.','1')((uint64_t)((uint32_t)(('H') | ('T') << 8 | ('T') <<
16 | ('P') << 24)) | (uint64_t)((uint32_t)(('/') | ('1'
) << 8 | ('.') << 16 | ('1') << 24)) <<
32)
:
465 break;
466 default:
467 return NULL((void*)0);
468 }
469
470 return end_of_line + 1;
471}
472
473__attribute__((noinline)) static void set_header_value(
474 struct lwan_value *header, char *end, char *p, size_t header_len)
475{
476 p += header_len;
477
478 if (LIKELY(string_as_uint16(p) == STR2_INT(':', ' '))__builtin_expect((!!(string_as_uint16(p) == ((uint16_t)((':')
| (' ') << 8)))), (1))
) {
479 *end = '\0';
480 char *value = p + sizeof(": ") - 1;
481
482 header->value = value;
483 header->len = (size_t)(end - value);
484 }
485}
486
487#define HEADER_LENGTH(hdr) \
488 ({ \
489 if (UNLIKELY(end - sizeof(hdr) + 1 < p)__builtin_expect(((end - sizeof(hdr) + 1 < p)), (0))) \
490 continue; \
491 sizeof(hdr) - 1; \
492 })
493
494#define SET_HEADER_VALUE(dest, hdr) \
495 do { \
496 const size_t header_len = HEADER_LENGTH(hdr); \
497 set_header_value(&(helper->dest), end, p, header_len); \
498 } while (0)
499
500static ALWAYS_INLINEinline __attribute__((always_inline)) ssize_t find_headers(char **header_start,
501 struct lwan_value *request_buffer,
502 char **next_request)
503{
504 char *buffer = request_buffer->value;
505 char *buffer_end = buffer + request_buffer->len;
506 ssize_t n_headers = 0;
507 char *next_header;
508
509 for (char *next_chr = buffer + 1;;) {
510 next_header = memchr(next_chr, '\r', (size_t)(buffer_end - next_chr));
511
512 if (UNLIKELY(!next_header)__builtin_expect(((!next_header)), (0)))
513 return -1;
514
515 if (next_chr == next_header) {
516 if (buffer_end - next_chr >= (ptrdiff_t)HEADER_TERMINATOR_LEN(sizeof("\r\n") - 1)) {
517 STRING_SWITCH_SMALL (next_header)switch (string_as_uint16(next_header)) {
518 case STR2_INT('\r', '\n')((uint16_t)(('\r') | ('\n') << 8)):
519 *next_request = next_header + HEADER_TERMINATOR_LEN(sizeof("\r\n") - 1);
520 }
521 }
522 goto out;
523 }
524
525 /* Is there at least a space for a minimal (H)eader and a (V)alue? */
526 if (LIKELY(next_header - next_chr >= (ptrdiff_t)(sizeof("H: V") - 1))__builtin_expect((!!(next_header - next_chr >= (ptrdiff_t)
(sizeof("H: V") - 1))), (1))
) {
527 header_start[n_headers++] = next_chr;
528
529 if (UNLIKELY(n_headers >= N_HEADER_START - 1)__builtin_expect(((n_headers >= 64 - 1)), (0)))
530 return -1;
531 } else {
532 /* Better to abort early if there's no space. */
533 return -1;
534 }
535
536 next_chr = next_header + HEADER_TERMINATOR_LEN(sizeof("\r\n") - 1);
537 if (UNLIKELY(next_chr >= buffer_end)__builtin_expect(((next_chr >= buffer_end)), (0)))
538 return -1;
539 }
540
541out:
542 header_start[n_headers] = next_header;
543 return n_headers;
544}
545
546static bool_Bool parse_headers(struct lwan_request_parser_helper *helper,
547 char *buffer)
548{
549 char **header_start = helper->header_start;
550 ssize_t n_headers = 0;
551
552 n_headers = find_headers(header_start, helper->buffer,
553 &helper->next_request);
554 if (UNLIKELY(n_headers < 0)__builtin_expect(((n_headers < 0)), (0)))
555 return false0;
556
557 for (ssize_t i = 0; i < n_headers; i++) {
558 char *p = header_start[i];
559 char *end = header_start[i + 1] - HEADER_TERMINATOR_LEN(sizeof("\r\n") - 1);
560
561 STRING_SWITCH_L (p)switch (((string_as_uint32(p)) | (uint32_t)0x20202020)) {
562 case STR4_INT_L('A', 'c', 'c', 'e')((((uint32_t)(('A') | ('c') << 8 | ('c') << 16 | (
'e') << 24))) | (uint32_t)0x20202020)
:
563 p += HEADER_LENGTH("Accept");
564
565 STRING_SWITCH_L (p)switch (((string_as_uint32(p)) | (uint32_t)0x20202020)) {
566 case STR4_INT_L('-', 'E', 'n', 'c')((((uint32_t)(('-') | ('E') << 8 | ('n') << 16 | (
'c') << 24))) | (uint32_t)0x20202020)
:
567 SET_HEADER_VALUE(accept_encoding, "-Encoding");
568 break;
569 }
570 break;
571 case STR4_INT_L('C', 'o', 'n', 'n')((((uint32_t)(('C') | ('o') << 8 | ('n') << 16 | (
'n') << 24))) | (uint32_t)0x20202020)
:
572 SET_HEADER_VALUE(connection, "Connection");
573 break;
574 case STR4_INT_L('C', 'o', 'n', 't')((((uint32_t)(('C') | ('o') << 8 | ('n') << 16 | (
't') << 24))) | (uint32_t)0x20202020)
:
575 p += HEADER_LENGTH("Content");
576
577 STRING_SWITCH_L (p)switch (((string_as_uint32(p)) | (uint32_t)0x20202020)) {
578 case STR4_INT_L('-', 'T', 'y', 'p')((((uint32_t)(('-') | ('T') << 8 | ('y') << 16 | (
'p') << 24))) | (uint32_t)0x20202020)
:
579 SET_HEADER_VALUE(content_type, "-Type");
580 break;
581 case STR4_INT_L('-', 'L', 'e', 'n')((((uint32_t)(('-') | ('L') << 8 | ('e') << 16 | (
'n') << 24))) | (uint32_t)0x20202020)
:
582 SET_HEADER_VALUE(content_length, "-Length");
583 break;
584 }
585 break;
586 case STR4_INT_L('I', 'f', '-', 'M')((((uint32_t)(('I') | ('f') << 8 | ('-') << 16 | (
'M') << 24))) | (uint32_t)0x20202020)
:
587 SET_HEADER_VALUE(if_modified_since.raw, "If-Modified-Since");
588 break;
589 case STR4_INT_L('H', 'o', 's', 't')((((uint32_t)(('H') | ('o') << 8 | ('s') << 16 | (
't') << 24))) | (uint32_t)0x20202020)
:
590 SET_HEADER_VALUE(host, "Host");
591 break;
592 case STR4_INT_L('R', 'a', 'n', 'g')((((uint32_t)(('R') | ('a') << 8 | ('n') << 16 | (
'g') << 24))) | (uint32_t)0x20202020)
:
593 SET_HEADER_VALUE(range.raw, "Range");
594 break;
595 }
596 }
597
598 helper->n_header_start = (size_t)n_headers;
599 return true1;
600}
601#undef HEADER_LENGTH
602#undef SET_HEADER_VALUE
603
604ssize_t lwan_find_headers(char **header_start, struct lwan_value *buffer,
605 char **next_request)
606{
607 return find_headers(header_start, buffer, next_request);
608}
609
610static void parse_if_modified_since(struct lwan_request_parser_helper *helper)
611{
612 static const size_t header_len =
613 sizeof("Wed, 17 Apr 2019 13:59:27 GMT") - 1;
614 time_t parsed;
615
616 if (UNLIKELY(helper->if_modified_since.raw.len != header_len)__builtin_expect(((helper->if_modified_since.raw.len != header_len
)), (0))
)
617 return;
618
619 if (UNLIKELY(lwan_parse_rfc_time(helper->if_modified_since.raw.value,__builtin_expect(((lwan_parse_rfc_time(helper->if_modified_since
.raw.value, &parsed) < 0)), (0))
620 &parsed) < 0)__builtin_expect(((lwan_parse_rfc_time(helper->if_modified_since
.raw.value, &parsed) < 0)), (0))
)
621 return;
622
623 helper->if_modified_since.parsed = parsed;
624}
625
626static bool_Bool
627parse_off_without_sign(const char *ptr, char **end, off_t *off)
628{
629 unsigned long long val;
630
631 static_assert_Static_assert(sizeof(val) >= sizeof(off_t),
632 "off_t fits in a long long");
633
634 errno(*__errno_location ()) = 0;
635
636 val = strtoull(ptr, end, 10);
637 if (UNLIKELY(val == 0 && *end == ptr)__builtin_expect(((val == 0 && *end == ptr)), (0)))
638 return false0;
639 if (UNLIKELY(errno != 0)__builtin_expect((((*__errno_location ()) != 0)), (0)))
640 return false0;
641 if (UNLIKELY(val > OFF_MAX)__builtin_expect(((val > 9223372036854775807LL)), (0)))
642 return false0;
643
644 *off = (off_t)val;
645 return true1;
646}
647
648static void
649parse_range(struct lwan_request_parser_helper *helper)
650{
651 if (UNLIKELY(helper->range.raw.len <= (sizeof("bytes=") - 1))__builtin_expect(((helper->range.raw.len <= (sizeof("bytes="
) - 1))), (0))
)
652 return;
653
654 char *range = helper->range.raw.value;
655 if (UNLIKELY(strncmp(range, "bytes=", sizeof("bytes=") - 1))__builtin_expect(((strncmp(range, "bytes=", sizeof("bytes=") -
1))), (0))
)
656 return;
657
658 range += sizeof("bytes=") - 1;
659
660 off_t from, to;
661 char *end;
662
663 if (*range == '-') {
664 from = 0;
665
666 if (!parse_off_without_sign(range + 1, &end, &to))
667 goto invalid_range;
668 if (*end != '\0')
669 goto invalid_range;
670 } else if (lwan_char_isdigit(*range)) {
671 if (!parse_off_without_sign(range, &end, &from))
672 goto invalid_range;
673 if (*end != '-')
674 goto invalid_range;
675
676 range = end + 1;
677 if (*range == '\0') {
678 to = -1;
679 } else {
680 if (!parse_off_without_sign(range, &end, &to))
681 goto invalid_range;
682 if (*end != '\0')
683 goto invalid_range;
684 }
685 } else {
686invalid_range:
687 to = from = -1;
688 }
689
690 helper->range.from = from;
691 helper->range.to = to;
692}
693
694static void
695parse_accept_encoding(struct lwan_request *request)
696{
697 struct lwan_request_parser_helper *helper = request->helper;
698
699 if (!helper->accept_encoding.len)
700 return;
701
702 for (const char *p = helper->accept_encoding.value; *p; p++) {
703 STRING_SWITCH(p)switch (string_as_uint32(p)) {
704 case STR4_INT('d','e','f','l')((uint32_t)(('d') | ('e') << 8 | ('f') << 16 | ('l'
) << 24))
:
705 case STR4_INT(' ','d','e','f')((uint32_t)((' ') | ('d') << 8 | ('e') << 16 | ('f'
) << 24))
:
706 request->flags |= REQUEST_ACCEPT_DEFLATE;
707 break;
708 case STR4_INT('g','z','i','p')((uint32_t)(('g') | ('z') << 8 | ('i') << 16 | ('p'
) << 24))
:
709 case STR4_INT(' ','g','z','i')((uint32_t)((' ') | ('g') << 8 | ('z') << 16 | ('i'
) << 24))
:
710 request->flags |= REQUEST_ACCEPT_GZIP;
711 break;
712#if defined(LWAN_HAVE_ZSTD)
713 case STR4_INT('z','s','t','d')((uint32_t)(('z') | ('s') << 8 | ('t') << 16 | ('d'
) << 24))
:
714 case STR4_INT(' ','z','s','t')((uint32_t)((' ') | ('z') << 8 | ('s') << 16 | ('t'
) << 24))
:
715 request->flags |= REQUEST_ACCEPT_ZSTD;
716 break;
717#endif
718#if defined(LWAN_HAVE_BROTLI)
719 default:
720 while (lwan_char_isspace(*p))
721 p++;
722
723 STRING_SWITCH_SMALL(p)switch (string_as_uint16(p)) {
724 case STR2_INT('b', 'r')((uint16_t)(('b') | ('r') << 8)):
725 request->flags |= REQUEST_ACCEPT_BROTLI;
726 break;
727 }
728#endif
729 }
730
731 if (!(p = strchr(p, ',')))
732 break;
733 }
734}
735
736static ALWAYS_INLINEinline __attribute__((always_inline)) char *
737ignore_leading_whitespace(char *buffer)
738{
739 while (lwan_char_isspace(*buffer))
740 buffer++;
741 return buffer;
742}
743
744static ALWAYS_INLINEinline __attribute__((always_inline)) void parse_connection_header(struct lwan_request *request)
745{
746 struct lwan_request_parser_helper *helper = request->helper;
747 bool_Bool has_keep_alive = false0;
748 bool_Bool has_close = false0;
749
750 if (!helper->connection.len)
751 goto out;
752
753 for (const char *p = helper->connection.value; *p; p++) {
754 STRING_SWITCH_L(p)switch (((string_as_uint32(p)) | (uint32_t)0x20202020)) {
755 case STR4_INT_L('k','e','e','p')((((uint32_t)(('k') | ('e') << 8 | ('e') << 16 | (
'p') << 24))) | (uint32_t)0x20202020)
:
756 case STR4_INT_L(' ', 'k','e','e')((((uint32_t)((' ') | ('k') << 8 | ('e') << 16 | (
'e') << 24))) | (uint32_t)0x20202020)
:
757 has_keep_alive = true1;
758 break;
759 case STR4_INT_L('c','l','o','s')((((uint32_t)(('c') | ('l') << 8 | ('o') << 16 | (
's') << 24))) | (uint32_t)0x20202020)
:
760 case STR4_INT_L(' ', 'c','l','o')((((uint32_t)((' ') | ('c') << 8 | ('l') << 16 | (
'o') << 24))) | (uint32_t)0x20202020)
:
761 has_close = true1;
762 break;
763 case STR4_INT_L('u','p','g','r')((((uint32_t)(('u') | ('p') << 8 | ('g') << 16 | (
'r') << 24))) | (uint32_t)0x20202020)
:
764 case STR4_INT_L(' ', 'u','p','g')((((uint32_t)((' ') | ('u') << 8 | ('p') << 16 | (
'g') << 24))) | (uint32_t)0x20202020)
:
765 request->conn->flags |= CONN_IS_UPGRADE;
766 break;
767 }
768
769 if (!(p = strchr(p, ',')))
770 break;
771 }
772
773out:
774 if (LIKELY(!(request->flags & REQUEST_IS_HTTP_1_0))__builtin_expect((!!(!(request->flags & REQUEST_IS_HTTP_1_0
))), (1))
)
775 has_keep_alive = !has_close;
776
777 if (has_keep_alive) {
778 request->conn->flags |= CONN_IS_KEEP_ALIVE;
779 } else {
780 request->conn->flags &=
781 ~(CONN_IS_KEEP_ALIVE | CONN_SENT_CONNECTION_HEADER);
782 }
783}
784
785#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
786static void save_to_corpus_for_fuzzing(struct lwan_value buffer)
787{
788 struct lwan_value buffer_copy;
789 char corpus_name[PATH_MAX4096];
790 const char *crlfcrlf;
791 int fd;
792
793 if (!(crlfcrlf = memmem(buffer.value, buffer.len, "\r\n\r\n", 4)))
794 return;
795 buffer.len = (size_t)(crlfcrlf - buffer.value + 4);
796
797try_another_file_name:
798 buffer_copy = buffer;
799
800 snprintf(corpus_name, sizeof(corpus_name), "corpus-request-%d", rand());
801
802 fd = open(corpus_name, O_WRONLY01 | O_CLOEXEC02000000 | O_CREAT0100 | O_EXCL0200, 0644);
803 if (fd < 0)
804 goto try_another_file_name;
805
806 while (buffer_copy.len) {
807 ssize_t r = write(fd, buffer_copy.value, buffer_copy.len);
808
809 if (r < 0) {
810 if (errno(*__errno_location ()) == EAGAIN11 || errno(*__errno_location ()) == EINTR4)
811 continue;
812
813 close(fd);
814 unlink(corpus_name);
815 goto try_another_file_name;
816 }
817
818 buffer_copy.value += r;
819 buffer_copy.len -= r;
820 }
821
822 close(fd);
823 lwan_status_debug("Request saved to %s", corpus_name)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 823, __FUNCTION__, "Request saved to %s", corpus_name)
;
824}
825#endif
826
827static enum lwan_http_status
828client_read(struct lwan_request *request,
829 struct lwan_value *buffer,
830 const size_t want_to_read,
831 enum lwan_read_finalizer (*finalizer)(const struct lwan_value *buffer,
832 size_t want_to_read,
833 const struct lwan_request *request,
834 int n_packets))
835{
836 struct lwan_request_parser_helper *helper = request->helper;
837 int n_packets = 0;
838
839 if (helper->next_request) {
840 const size_t next_request_len = (size_t)(helper->next_request - buffer->value);
841 size_t new_len;
842
843 if (__builtin_sub_overflow(buffer->len, next_request_len, &new_len)) {
844 helper->next_request = NULL((void*)0);
845 } else if (new_len) {
846 /* FIXME: This memmove() could be eventually removed if a better
847 * stucture (maybe a ringbuffer, reading with readv(), and each
848 * pointer is coro_strdup() if they wrap around?) were used for
849 * the request buffer. */
850 buffer->len = new_len;
851 memmove(buffer->value, helper->next_request, new_len);
852 goto try_to_finalize;
853 }
854 }
855
856 for (buffer->len = 0;; n_packets++) {
857 size_t to_read = (size_t)(want_to_read - buffer->len);
858
859 if (UNLIKELY(to_read == 0)__builtin_expect(((to_read == 0)), (0)))
860 return HTTP_TOO_LARGE;
861
862 ssize_t n = recv(request->fd, buffer->value + buffer->len, to_read, 0);
863 if (UNLIKELY(n <= 0)__builtin_expect(((n <= 0)), (0))) {
864 if (n < 0) {
865 switch (errno(*__errno_location ())) {
866 case EINTR4:
867 case EAGAIN11:
868yield_and_read_again:
869 coro_yield(request->conn->coro, CONN_CORO_WANT_READ);
870 continue;
871 }
872
873 /* Unexpected error before reading anything */
874 if (UNLIKELY(!buffer->len)__builtin_expect(((!buffer->len)), (0)))
875 return HTTP_BAD_REQUEST;
876 }
877
878 /* Client shut down orderly (n = 0), or unrecoverable error (n < 0);
879 * shut down coro. */
880 break;
881 }
882
883 buffer->len += (size_t)n;
884
885try_to_finalize:
886 switch (finalizer(buffer, want_to_read, request, n_packets)) {
887 case FINALIZER_DONE:
888 buffer->value[buffer->len] = '\0';
889#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
890 save_to_corpus_for_fuzzing(*buffer);
891#endif
892 return HTTP_OK;
893
894 case FINALIZER_TRY_AGAIN:
895 goto yield_and_read_again;
896
897 case FINALIZER_TIMEOUT:
898 return HTTP_TIMEOUT;
899 }
900 }
901
902 coro_yield(request->conn->coro, CONN_CORO_ABORT);
903 __builtin_unreachable();
904 return HTTP_INTERNAL_ERROR;
905}
906
907static enum lwan_read_finalizer
908read_request_finalizer_from_helper(const struct lwan_value *buffer,
909 struct lwan_request_parser_helper *helper,
910 int n_packets,
911 bool_Bool allow_proxy_reqs)
912{
913 static const size_t min_proxied_request_size =
914 MIN_REQUEST_SIZE(sizeof("GET / HTTP/1.1\r\n\r\n") - 1) + sizeof(struct proxy_header_v2);
915
916 if (LIKELY(buffer->len >= MIN_REQUEST_SIZE)__builtin_expect((!!(buffer->len >= (sizeof("GET / HTTP/1.1\r\n\r\n"
) - 1))), (1))
) {
917 STRING_SWITCH (buffer->value + buffer->len - 4)switch (string_as_uint32(buffer->value + buffer->len - 4
))
{
918 case STR4_INT('\r', '\n', '\r', '\n')((uint32_t)(('\r') | ('\n') << 8 | ('\r') << 16 |
('\n') << 24))
:
919 return FINALIZER_DONE;
920 }
921 }
922
923 char *crlfcrlf = memmem(buffer->value, buffer->len, "\r\n\r\n", 4);
924 if (LIKELY(crlfcrlf)__builtin_expect((!!(crlfcrlf)), (1))) {
925 if (LIKELY(helper->next_request)__builtin_expect((!!(helper->next_request)), (1))) {
926 helper->next_request = NULL((void*)0);
927 return FINALIZER_DONE;
928 }
929
930 const size_t crlfcrlf_to_base = (size_t)(crlfcrlf - buffer->value);
931 if (crlfcrlf_to_base >= MIN_REQUEST_SIZE(sizeof("GET / HTTP/1.1\r\n\r\n") - 1) - 4)
932 return FINALIZER_DONE;
933
934 if (buffer->len > min_proxied_request_size && allow_proxy_reqs) {
935 /* FIXME: Checking for PROXYv2 protocol header here is a layering
936 * violation. */
937 STRING_SWITCH_LARGE (crlfcrlf + 4)switch (string_as_uint64(crlfcrlf + 4)) {
938 case STR8_INT(0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54, 0x0a)((uint64_t)((uint32_t)((0x00) | (0x0d) << 8 | (0x0a) <<
16 | (0x51) << 24)) | (uint64_t)((uint32_t)((0x55) | (
0x49) << 8 | (0x54) << 16 | (0x0a) << 24)) <<
32)
:
939 return FINALIZER_DONE;
940 }
941 }
942 }
943
944 /* Yield a timeout error to avoid clients being intentionally slow and
945 * hogging the server. (Clients can't only connect and do nothing, they
946 * need to send data, otherwise the timeout queue timer will kick in and
947 * close the connection. Limit the number of packets to avoid them sending
948 * just a byte at a time.) See lwan_calculate_n_packets() to see how this is
949 * calculated. */
950 if (UNLIKELY(n_packets > helper->error_when_n_packets)__builtin_expect(((n_packets > helper->error_when_n_packets
)), (0))
)
951 return FINALIZER_TIMEOUT;
952
953 return FINALIZER_TRY_AGAIN;
954}
955
956static inline enum lwan_read_finalizer
957read_request_finalizer(const struct lwan_value *buffer,
958 size_t want_to_read __attribute__((unused)),
959 const struct lwan_request *request,
960 int n_packets)
961{
962 return read_request_finalizer_from_helper(
963 buffer, request->helper, n_packets,
964 request->flags & REQUEST_ALLOW_PROXY_REQS);
965}
966
967static ALWAYS_INLINEinline __attribute__((always_inline)) enum lwan_http_status
968read_request(struct lwan_request *request)
969{
970 return client_read(request, request->helper->buffer,
971 DEFAULT_BUFFER_SIZE4096 - 1 /* -1 for NUL byte */,
972 read_request_finalizer);
973}
974
975static enum lwan_read_finalizer
976body_data_finalizer(const struct lwan_value *buffer,
977 size_t want_to_read,
978 const struct lwan_request *request,
979 int n_packets)
980{
981 const struct lwan_request_parser_helper *helper = request->helper;
982
983 if (want_to_read == buffer->len)
984 return FINALIZER_DONE;
985
986 /* For POST requests, the body can be larger, and due to small MTUs on
987 * most ethernet connections, responding with a timeout solely based on
988 * number of packets doesn't work. Use keepalive timeout instead. */
989 if (UNLIKELY(time(NULL) > helper->error_when_time)__builtin_expect(((time(((void*)0)) > helper->error_when_time
)), (0))
)
990 return FINALIZER_TIMEOUT;
991
992 /* In addition to time, also estimate the number of packets based on an
993 * usual MTU value and the request body size. */
994 if (UNLIKELY(n_packets > helper->error_when_n_packets)__builtin_expect(((n_packets > helper->error_when_n_packets
)), (0))
)
995 return FINALIZER_TIMEOUT;
996
997 return FINALIZER_TRY_AGAIN;
998}
999
1000static const char *is_dir(const char *v)
1001{
1002 struct stat st;
1003
1004 if (!v)
1005 return NULL((void*)0);
1006
1007 if (*v != '/')
1008 return NULL((void*)0);
1009
1010 if (stat(v, &st) < 0)
1011 return NULL((void*)0);
1012
1013 if (!S_ISDIR(st.st_mode)((((st.st_mode)) & 0170000) == (0040000)))
1014 return NULL((void*)0);
1015
1016 if (!(st.st_mode & S_ISVTX01000)) {
1017 lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1020, __FUNCTION__, "Using %s as temporary directory, but it doesn't have "
"the sticky bit set.", v)
1018 "Using %s as temporary directory, but it doesn't have "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1020, __FUNCTION__, "Using %s as temporary directory, but it doesn't have "
"the sticky bit set.", v)
1019 "the sticky bit set.",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1020, __FUNCTION__, "Using %s as temporary directory, but it doesn't have "
"the sticky bit set.", v)
1020 v)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1020, __FUNCTION__, "Using %s as temporary directory, but it doesn't have "
"the sticky bit set.", v)
;
1021 }
1022
1023 return v;
1024}
1025
1026static const char *is_dir_good_for_tmp(const char *v)
1027{
1028 struct statfs sb;
1029
1030 v = is_dir(v);
1031 if (!v)
1032 return NULL((void*)0);
1033
1034 if (!statfs(v, &sb) && sb.f_type == TMPFS_MAGIC0x01021994) {
1035 lwan_status_warning("%s is a tmpfs filesystem, "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1036, __FUNCTION__, "%s is a tmpfs filesystem, " "not considering it"
, v)
1036 "not considering it", v)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1036, __FUNCTION__, "%s is a tmpfs filesystem, " "not considering it"
, v)
;
1037 return NULL((void*)0);
1038 }
1039
1040 return v;
1041}
1042
1043static const char *temp_dir;
1044static const size_t body_buffer_temp_file_thresh = 1<<20;
1045
1046static const char *
1047get_temp_dir(void)
1048{
1049 const char *tmpdir;
1050
1051 tmpdir = is_dir_good_for_tmp(secure_getenv("TMPDIR"));
1052 if (tmpdir)
1053 return tmpdir;
1054
1055 tmpdir = is_dir_good_for_tmp(secure_getenv("TMP"));
1056 if (tmpdir)
1057 return tmpdir;
1058
1059 tmpdir = is_dir_good_for_tmp(secure_getenv("TEMP"));
1060 if (tmpdir)
1061 return tmpdir;
1062
1063 tmpdir = is_dir_good_for_tmp("/var/tmp");
1064 if (tmpdir)
1065 return tmpdir;
1066
1067 tmpdir = is_dir_good_for_tmp(P_tmpdir"/tmp");
1068 if (tmpdir)
1069 return tmpdir;
1070
1071 lwan_status_warning("Temporary directory could not be determined. POST "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1073, __FUNCTION__, "Temporary directory could not be determined. POST "
"or PUT requests over %zu bytes bytes will fail.", body_buffer_temp_file_thresh
)
1072 "or PUT requests over %zu bytes bytes will fail.",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1073, __FUNCTION__, "Temporary directory could not be determined. POST "
"or PUT requests over %zu bytes bytes will fail.", body_buffer_temp_file_thresh
)
1073 body_buffer_temp_file_thresh)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1073, __FUNCTION__, "Temporary directory could not be determined. POST "
"or PUT requests over %zu bytes bytes will fail.", body_buffer_temp_file_thresh
)
;
1074 return NULL((void*)0);
1075}
1076
1077__attribute__((constructor)) static void initialize_temp_dir(void)
1078{
1079 temp_dir = get_temp_dir();
1080}
1081
1082static int create_temp_file(void)
1083{
1084 char template[PATH_MAX4096];
1085 mode_t prev_mask;
1086 int ret;
1087
1088 if (UNLIKELY(!temp_dir)__builtin_expect(((!temp_dir)), (0)))
1089 return -ENOENT2;
1090
1091#if defined(O_TMPFILE(020000000 | 0200000))
1092 int fd = open(temp_dir,
1093 O_TMPFILE(020000000 | 0200000) | O_CREAT0100 | O_RDWR02 | O_EXCL0200 | O_CLOEXEC02000000 |
1094 O_NOFOLLOW0400000 | O_NOATIME01000000,
1095 S_IRUSR0400 | S_IWUSR0200);
1096 if (LIKELY(fd >= 0)__builtin_expect((!!(fd >= 0)), (1)))
1097 return fd;
1098#endif
1099
1100 ret = snprintf(template, sizeof(template), "%s/lwanXXXXXX", temp_dir);
1101 if (UNLIKELY(ret < 0 || ret >= (int)sizeof(template))__builtin_expect(((ret < 0 || ret >= (int)sizeof(template
))), (0))
)
1102 return -EOVERFLOW75;
1103
1104 prev_mask = umask_for_tmpfile(S_IRUSR | S_IWUSR)({ (void)(0400 | 0200); 0U; });
1105 ret = mkostemp(template, O_CLOEXEC02000000);
1106 umask_for_tmpfile(prev_mask)({ (void)(prev_mask); 0U; });
1107
1108 if (LIKELY(ret >= 0)__builtin_expect((!!(ret >= 0)), (1)))
1109 unlink(template);
1110
1111 return ret;
1112}
1113
1114struct file_backed_buffer {
1115 void *ptr;
1116 size_t size;
1117};
1118
1119static void
1120free_body_buffer(void *data)
1121{
1122 struct file_backed_buffer *buf = data;
1123
1124 munmap(buf->ptr, buf->size);
1125 free(buf);
1126}
1127
1128static void*
1129alloc_body_buffer(struct coro *coro, size_t size, bool_Bool allow_file)
1130{
1131 struct file_backed_buffer *buf;
1132 void *ptr = (void *)MAP_FAILED((void *) -1);
1133 int fd;
1134
1135 if (LIKELY(size < body_buffer_temp_file_thresh)__builtin_expect((!!(size < body_buffer_temp_file_thresh))
, (1))
) {
1136 ptr = coro_malloc(coro, size);
1137
1138 if (LIKELY(ptr)__builtin_expect((!!(ptr)), (1)))
1139 return ptr;
1140 }
1141
1142 if (UNLIKELY(!allow_file)__builtin_expect(((!allow_file)), (0)))
1143 return NULL((void*)0);
1144
1145 fd = create_temp_file();
1146 if (UNLIKELY(fd < 0)__builtin_expect(((fd < 0)), (0)))
1147 return NULL((void*)0);
1148
1149 if (UNLIKELY(ftruncate(fd, (off_t)size) < 0)__builtin_expect(((ftruncate(fd, (off_t)size) < 0)), (0))) {
1150 close(fd);
1151 return NULL((void*)0);
1152 }
1153
1154 if (MAP_HUGETLB0x40000) {
1155 ptr = mmap(NULL((void*)0), size, PROT_READ0x1 | PROT_WRITE0x2,
1156 MAP_SHARED0x01 | MAP_HUGETLB0x40000, fd, 0);
1157 }
1158 if (UNLIKELY(ptr == MAP_FAILED)__builtin_expect(((ptr == ((void *) -1))), (0)))
1159 ptr = mmap(NULL((void*)0), size, PROT_READ0x1 | PROT_WRITE0x2, MAP_SHARED0x01, fd, 0);
1160 close(fd);
1161 if (UNLIKELY(ptr == MAP_FAILED)__builtin_expect(((ptr == ((void *) -1))), (0)))
1162 return NULL((void*)0);
1163
1164 buf = coro_malloc_full(coro, sizeof(*buf), free_body_buffer);
1165 if (UNLIKELY(!buf)__builtin_expect(((!buf)), (0))) {
1166 munmap(ptr, size);
1167 return NULL((void*)0);
1168 }
1169
1170 buf->ptr = ptr;
1171 buf->size = size;
1172 return ptr;
1173}
1174
1175static enum lwan_http_status
1176get_remaining_body_data_length(struct lwan_request *request,
1177 const size_t max_size,
1178 size_t *total,
1179 size_t *have)
1180{
1181 struct lwan_request_parser_helper *helper = request->helper;
1182 long long parsed_size;
1183
1184 if (UNLIKELY(!helper->content_length.value)__builtin_expect(((!helper->content_length.value)), (0)))
1185 return HTTP_BAD_REQUEST;
1186
1187 parsed_size = parse_long_long(helper->content_length.value, -1);
1188 if (UNLIKELY(parsed_size < 0)__builtin_expect(((parsed_size < 0)), (0)))
1189 return HTTP_BAD_REQUEST;
1190 if (UNLIKELY((size_t)parsed_size >= max_size)__builtin_expect((((size_t)parsed_size >= max_size)), (0)))
1191 return HTTP_TOO_LARGE;
1192 if (UNLIKELY(!parsed_size)__builtin_expect(((!parsed_size)), (0)))
1193 return HTTP_OK;
1194
1195 *total = (size_t)parsed_size;
1196
1197 if (!helper->next_request) {
1198 *have = 0;
1199 return HTTP_PARTIAL_CONTENT;
1200 }
1201
1202 char *buffer_end = helper->buffer->value + helper->buffer->len;
1203
1204 *have = (size_t)(buffer_end - helper->next_request);
1205
1206 if (*have < *total)
1207 return HTTP_PARTIAL_CONTENT;
1208
1209 helper->body_data.value = helper->next_request;
1210 helper->body_data.len = *total;
1211 helper->next_request += *total;
1212 return HTTP_OK;
1213}
1214
1215static int read_body_data(struct lwan_request *request)
1216{
1217 /* Holy indirection, Batman! */
1218 const struct lwan_config *config = &request->conn->thread->lwan->config;
1219 struct lwan_request_parser_helper *helper = request->helper;
1220 enum lwan_http_status status;
1221 size_t total, have, max_data_size;
1222 bool_Bool allow_temp_file;
1223 char *new_buffer;
1224
1225 switch (lwan_request_get_method(request)) {
1226 case REQUEST_METHOD_POST:
1227 allow_temp_file = config->allow_post_temp_file;
1228 max_data_size = config->max_post_data_size;
1229 break;
1230 case REQUEST_METHOD_PUT:
1231 allow_temp_file = config->allow_put_temp_file;
1232 max_data_size = config->max_put_data_size;
1233 break;
1234 default:
1235 return -HTTP_NOT_ALLOWED;
1236 }
1237
1238 status =
1239 get_remaining_body_data_length(request, max_data_size, &total, &have);
1240 if (status != HTTP_PARTIAL_CONTENT)
1241 return -(int)status;
1242
1243 new_buffer =
1244 alloc_body_buffer(request->conn->coro, total + 1, allow_temp_file);
1245 if (UNLIKELY(!new_buffer)__builtin_expect(((!new_buffer)), (0)))
1246 return -HTTP_INTERNAL_ERROR;
1247
1248 if (!(request->flags & REQUEST_IS_HTTP_1_0)) {
1249 /* §8.2.3 https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html */
1250 const char *expect = lwan_request_get_header(request, "Expect");
1251
1252 if (expect && strncmp(expect, "100-", 4) == 0) {
1253 static const char continue_header[] = "HTTP/1.1 100 Continue\r\n\r\n";
1254
1255 lwan_send(request, continue_header, sizeof(continue_header) - 1, 0);
1256 }
1257 }
1258
1259 helper->body_data.value = new_buffer;
1260 helper->body_data.len = total;
1261 if (have) {
1262 new_buffer = mempcpy(new_buffer, helper->next_request, have);
1263 total -= have;
1264 }
1265 helper->next_request = NULL((void*)0);
1266
1267 helper->error_when_time = time(NULL((void*)0)) + config->keep_alive_timeout;
1268 helper->error_when_n_packets = lwan_calculate_n_packets(total);
1269
1270 struct lwan_value buffer = {.value = new_buffer, .len = total};
1271 return (int)client_read(request, &buffer, total, body_data_finalizer);
1272}
1273
1274static char *
1275parse_proxy_protocol(struct lwan_request *request, char *buffer)
1276{
1277 STRING_SWITCH(buffer)switch (string_as_uint32(buffer)) {
1278 case STR4_INT('P','R','O','X')((uint32_t)(('P') | ('R') << 8 | ('O') << 16 | ('X'
) << 24))
:
1279 return parse_proxy_protocol_v1(request, buffer);
1280 case STR4_INT('\x0D','\x0A','\x0D','\x0A')((uint32_t)(('\x0D') | ('\x0A') << 8 | ('\x0D') <<
16 | ('\x0A') << 24))
:
1281 return parse_proxy_protocol_v2(request, buffer);
1282 }
1283
1284 return buffer;
1285}
1286
1287static enum lwan_http_status parse_http_request(struct lwan_request *request)
1288{
1289 struct lwan_request_parser_helper *helper = request->helper;
1290 char *buffer = helper->buffer->value;
1291
1292 if (request->flags & REQUEST_ALLOW_PROXY_REQS) {
1293 /* REQUEST_ALLOW_PROXY_REQS will be cleared in lwan_process_request() */
1294
1295 buffer = parse_proxy_protocol(request, buffer);
1296 if (UNLIKELY(!buffer)__builtin_expect(((!buffer)), (0)))
1297 return HTTP_BAD_REQUEST;
1298 }
1299
1300 buffer = ignore_leading_whitespace(buffer);
1301
1302 if (UNLIKELY(buffer > helper->buffer->value + helper->buffer->len -__builtin_expect(((buffer > helper->buffer->value + helper
->buffer->len - (sizeof("GET / HTTP/1.1\r\n\r\n") - 1))
), (0))
1303 MIN_REQUEST_SIZE)__builtin_expect(((buffer > helper->buffer->value + helper
->buffer->len - (sizeof("GET / HTTP/1.1\r\n\r\n") - 1))
), (0))
)
1304 return HTTP_BAD_REQUEST;
1305
1306 char *path = identify_http_method(request, buffer);
1307 if (UNLIKELY(!path)__builtin_expect(((!path)), (0)))
1308 return HTTP_NOT_ALLOWED;
1309
1310 buffer = identify_http_path(request, path);
1311 if (UNLIKELY(!buffer)__builtin_expect(((!buffer)), (0)))
1312 return HTTP_BAD_REQUEST;
1313
1314 if (UNLIKELY(!parse_headers(helper, buffer))__builtin_expect(((!parse_headers(helper, buffer))), (0)))
1315 return HTTP_BAD_REQUEST;
1316
1317 ssize_t decoded_len = url_decode(request->url.value);
1318 if (UNLIKELY(decoded_len < 0)__builtin_expect(((decoded_len < 0)), (0)))
1319 return HTTP_BAD_REQUEST;
1320 request->original_url.len = request->url.len = (size_t)decoded_len;
1321
1322 parse_connection_header(request);
1323
1324 return HTTP_OK;
1325}
1326
1327static enum lwan_http_status
1328prepare_websocket_handshake(struct lwan_request *request, char **encoded)
1329{
1330 static const unsigned char websocket_uuid[] =
1331 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
1332 unsigned char digest[20];
1333 sha1_context ctx;
1334
1335 if (UNLIKELY(request->flags & RESPONSE_SENT_HEADERS)__builtin_expect(((request->flags & RESPONSE_SENT_HEADERS
)), (0))
)
1336 return HTTP_INTERNAL_ERROR;
1337
1338 if (UNLIKELY(!(request->conn->flags & CONN_IS_UPGRADE))__builtin_expect(((!(request->conn->flags & CONN_IS_UPGRADE
))), (0))
)
1339 return HTTP_BAD_REQUEST;
1340
1341 const char *upgrade = lwan_request_get_header(request, "Upgrade");
1342 if (UNLIKELY(!upgrade || !streq(upgrade, "websocket"))__builtin_expect(((!upgrade || !streq(upgrade, "websocket")))
, (0))
)
1343 return HTTP_BAD_REQUEST;
1344
1345 const char *sec_websocket_key =
1346 lwan_request_get_header(request, "Sec-WebSocket-Key");
1347 if (UNLIKELY(!sec_websocket_key)__builtin_expect(((!sec_websocket_key)), (0)))
1348 return HTTP_BAD_REQUEST;
1349
1350 const size_t sec_websocket_key_len = strlen(sec_websocket_key);
1351 if (base64_encoded_len(16) != sec_websocket_key_len)
1352 return HTTP_BAD_REQUEST;
1353 if (UNLIKELY(!base64_validate((void *)sec_websocket_key, sec_websocket_key_len))__builtin_expect(((!base64_validate((void *)sec_websocket_key
, sec_websocket_key_len))), (0))
)
1354 return HTTP_BAD_REQUEST;
1355
1356 sha1_init(&ctx);
1357 sha1_update(&ctx, (void *)sec_websocket_key, sec_websocket_key_len);
1358 sha1_update(&ctx, websocket_uuid, sizeof(websocket_uuid) - 1);
1359 sha1_finalize(&ctx, digest);
1360
1361 *encoded = (char *)base64_encode(digest, sizeof(digest), NULL((void*)0));
1362 return LIKELY(*encoded)__builtin_expect((!!(*encoded)), (1)) ? HTTP_SWITCHING_PROTOCOLS : HTTP_INTERNAL_ERROR;
1363}
1364
1365enum lwan_http_status
1366lwan_request_websocket_upgrade(struct lwan_request *request)
1367{
1368 char header_buf[DEFAULT_HEADERS_SIZE2048];
1369 size_t header_buf_len;
1370 char *encoded;
1371
1372 enum lwan_http_status r = prepare_websocket_handshake(request, &encoded);
1373 if (r != HTTP_SWITCHING_PROTOCOLS)
1374 return r;
1375
1376 request->flags |= RESPONSE_NO_CONTENT_LENGTH;
1377 header_buf_len = lwan_prepare_response_header_full(
1378 request, HTTP_SWITCHING_PROTOCOLS, header_buf, sizeof(header_buf),
1379 (struct lwan_key_value[]){
1380 /* Connection: Upgrade is implicit if conn->flags & CONN_IS_UPGRADE */
1381 {.key = "Sec-WebSocket-Accept", .value = encoded},
1382 {.key = "Upgrade", .value = "websocket"},
1383 {},
1384 });
1385 free(encoded);
1386 if (UNLIKELY(!header_buf_len)__builtin_expect(((!header_buf_len)), (0)))
1387 return HTTP_INTERNAL_ERROR;
1388
1389 request->conn->flags |= CONN_IS_WEBSOCKET;
1390 lwan_send(request, header_buf, header_buf_len, 0);
1391
1392 return HTTP_SWITCHING_PROTOCOLS;
1393}
1394
1395static inline bool_Bool request_has_body(const struct lwan_request *request)
1396{
1397 /* 3rd bit set in method: request method has body. See lwan.h,
1398 * definition of FOR_EACH_REQUEST_METHOD() for more info. */
1399 return lwan_request_get_method(request) & 1 << 3;
1400}
1401
1402static enum lwan_http_status
1403maybe_read_body_data(const struct lwan_url_map *url_map,
1404 struct lwan_request *request)
1405{
1406 int status = 0;
1407
1408 if (url_map->flags & HANDLER_EXPECTS_BODY_DATA) {
1409 status = read_body_data(request);
1410 if (status > 0)
1411 return (enum lwan_http_status)status;
1412 }
1413
1414 /* Instead of trying to read the body here, which will require
1415 * us to allocate and read potentially a lot of bytes, force
1416 * this connection to be closed as soon as we send a "not allowed"
1417 * response. */
1418 request->conn->flags &= ~CONN_IS_KEEP_ALIVE;
1419
1420 if (status < 0) {
1421 status = -status;
1422 return (enum lwan_http_status)status;
1423 }
1424
1425 return HTTP_NOT_ALLOWED;
1426}
1427
1428static enum lwan_http_status prepare_for_response(const struct lwan_url_map *url_map,
1429 struct lwan_request *request)
1430{
1431 request->url.value += url_map->prefix_len;
1432 request->url.len -= url_map->prefix_len;
1433 while (*request->url.value == '/' && request->url.len > 0) {
1434 request->url.value++;
1435 request->url.len--;
1436 }
1437
1438 if (UNLIKELY(url_map->flags & HANDLER_MUST_AUTHORIZE)__builtin_expect(((url_map->flags & HANDLER_MUST_AUTHORIZE
)), (0))
) {
1439 if (!lwan_http_authorize_urlmap(request, url_map))
1440 return HTTP_NOT_AUTHORIZED;
1441 }
1442
1443 if (UNLIKELY(request_has_body(request))__builtin_expect(((request_has_body(request))), (0)))
1444 return maybe_read_body_data(url_map, request);
1445
1446 return HTTP_OK;
1447}
1448
1449static bool_Bool handle_rewrite(struct lwan_request *request)
1450{
1451 struct lwan_request_parser_helper *helper = request->helper;
1452
1453 request->flags &= ~RESPONSE_URL_REWRITTEN;
1454
1455 find_query_string(request, request->url.value + request->url.len);
1456
1457 helper->urls_rewritten++;
1458 if (UNLIKELY(helper->urls_rewritten > 4)__builtin_expect(((helper->urls_rewritten > 4)), (0))) {
1459 lwan_default_response(request, HTTP_INTERNAL_ERROR);
1460 return false0;
1461 }
1462
1463 return true1;
1464}
1465
1466const char *lwan_request_get_method_str(const struct lwan_request *request)
1467{
1468#define GENERATE_CASE_STMT(upper, lower, mask, constant, probability) \
1469 case REQUEST_METHOD_##upper: \
1470 return #upper;
1471
1472 switch (lwan_request_get_method(request)) {
1473 FOR_EACH_REQUEST_METHOD(GENERATE_CASE_STMT)GENERATE_CASE_STMT(GET, get, (1 << 0), (((uint32_t)(('G'
) | ('E') << 8 | ('T') << 16 | (' ') << 24)
)), 0.6) GENERATE_CASE_STMT(POST, post, (1 << 3 | 1 <<
1 | 1 << 0), (((uint32_t)(('P') | ('O') << 8 | (
'S') << 16 | ('T') << 24))), 0.2) GENERATE_CASE_STMT
(HEAD, head, (1 << 1), (((uint32_t)(('H') | ('E') <<
8 | ('A') << 16 | ('D') << 24))), 0.2) GENERATE_CASE_STMT
(OPTIONS, options, (1 << 2), (((uint32_t)(('O') | ('P')
<< 8 | ('T') << 16 | ('I') << 24))), 0.1) GENERATE_CASE_STMT
(DELETE, delete, (1 << 1 | 1 << 2), (((uint32_t)(
('D') | ('E') << 8 | ('L') << 16 | ('E') <<
24))), 0.1) GENERATE_CASE_STMT(PUT, put, (1 << 3 | 1 <<
2 | 1 << 0), (((uint32_t)(('P') | ('U') << 8 | (
'T') << 16 | (' ') << 24))), 0.1)
1474 default:
1475 return "UNKNOWN";
1476 }
1477#undef GENERATE_CASE_STMT
1478}
1479
1480#ifndef NDEBUG
1481static void log_request(struct lwan_request *request,
1482 enum lwan_http_status status,
1483 double time_to_read_request,
1484 double time_to_process_request)
1485{
1486 char ip_buffer[INET6_ADDRSTRLEN46];
1487
1488 lwan_status_debug(lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
5
Calling 'lwan_request_get_remote_address'
1489 "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
1490 lwan_request_get_remote_address(request, ip_buffer),lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
1491 request->conn->thread->date.date, lwan_request_get_id(request),lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
1492 lwan_request_get_method_str(request), request->original_url.value,lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
1493 request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" : "1.1", status,lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
1494 request->response.mime_type, time_to_read_request,lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
1495 time_to_process_request)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1495, __FUNCTION__, "%s [%s] %016lx \"%s %s HTTP/%s\" %d %s (r:%.3fms p:%.3fms)"
, lwan_request_get_remote_address(request, ip_buffer), request
->conn->thread->date.date, lwan_request_get_id(request
), lwan_request_get_method_str(request), request->original_url
.value, request->flags & REQUEST_IS_HTTP_1_0 ? "1.0" :
"1.1", status, request->response.mime_type, time_to_read_request
, time_to_process_request)
;
1496}
1497#else
1498#define log_request(...)
1499#endif
1500
1501#ifndef NDEBUG
1502static struct timespec current_precise_monotonic_timespec(void)
1503{
1504 struct timespec now;
1505
1506 if (UNLIKELY(clock_gettime(CLOCK_MONOTONIC, &now) < 0)__builtin_expect(((clock_gettime(1, &now) < 0)), (0))) {
1507 lwan_status_perror("clock_gettime")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1507, __FUNCTION__, "clock_gettime")
;
1508 return (struct timespec){};
1509 }
1510
1511 return now;
1512}
1513
1514static double elapsed_time_ms(const struct timespec then)
1515{
1516 const struct timespec now = current_precise_monotonic_timespec();
1517 struct timespec diff = {
1518 .tv_sec = now.tv_sec - then.tv_sec,
1519 .tv_nsec = now.tv_nsec - then.tv_nsec,
1520 };
1521
1522 if (diff.tv_nsec < 0) {
1523 diff.tv_sec--;
1524 diff.tv_nsec += 1000000000l;
1525 }
1526
1527 return (double)diff.tv_sec / 1000.0 + (double)diff.tv_nsec / 1000000.0;
1528}
1529#endif
1530
1531void lwan_process_request(struct lwan *l, struct lwan_request *request)
1532{
1533 enum lwan_http_status status;
1534 struct lwan_url_map *url_map;
1535
1536#ifndef NDEBUG
1537 struct timespec request_read_begin_time = current_precise_monotonic_timespec();
1538#endif
1539 status = read_request(request);
1540
1541#ifndef NDEBUG
1542 double time_to_read_request = elapsed_time_ms(request_read_begin_time);
1543
1544 struct timespec request_begin_time = current_precise_monotonic_timespec();
1545#endif
1546 if (UNLIKELY(status != HTTP_OK)__builtin_expect(((status != HTTP_OK)), (0))) {
1
Taking false branch
1547 /* If read_request() returns any error at this point, it's probably
1548 * better to just send an error response and abort the coroutine and
1549 * let the client handle the error instead: we don't have
1550 * information to even log the request because it has not been
1551 * parsed yet at this stage. Even if there are other requests waiting
1552 * in the pipeline, this seems like the safer thing to do. */
1553 request->conn->flags &= ~CONN_IS_KEEP_ALIVE;
1554 lwan_default_response(request, status);
1555 coro_yield(request->conn->coro, CONN_CORO_ABORT);
1556 __builtin_unreachable();
1557 }
1558
1559 status = parse_http_request(request);
1560 if (UNLIKELY(status != HTTP_OK)__builtin_expect(((status != HTTP_OK)), (0)))
2
Taking true branch
1561 goto log_and_return;
3
Control jumps to line 1584
1562
1563lookup_again:
1564 url_map = lwan_trie_lookup_prefix(&l->url_map_trie, request->url.value);
1565 if (UNLIKELY(!url_map)__builtin_expect(((!url_map)), (0))) {
1566 status = HTTP_NOT_FOUND;
1567 goto log_and_return;
1568 }
1569
1570 status = prepare_for_response(url_map, request);
1571 if (UNLIKELY(status != HTTP_OK)__builtin_expect(((status != HTTP_OK)), (0)))
1572 goto log_and_return;
1573
1574 status = url_map->handler(request, &request->response, url_map->data);
1575 if (UNLIKELY(url_map->flags & HANDLER_CAN_REWRITE_URL)__builtin_expect(((url_map->flags & HANDLER_CAN_REWRITE_URL
)), (0))
) {
1576 if (request->flags & RESPONSE_URL_REWRITTEN) {
1577 if (LIKELY(handle_rewrite(request))__builtin_expect((!!(handle_rewrite(request))), (1)))
1578 goto lookup_again;
1579 return;
1580 }
1581 }
1582
1583log_and_return:
1584 lwan_response(request, status);
1585
1586 log_request(request, status, time_to_read_request, elapsed_time_ms(request_begin_time));
4
Calling 'log_request'
1587}
1588
1589static inline void *
1590value_lookup(const struct lwan_key_value_array *array, const char *key)
1591{
1592 const struct lwan_array *la = (const struct lwan_array *)array;
1593
1594 if (LIKELY(la->elements)__builtin_expect((!!(la->elements)), (1))) {
1595 struct lwan_key_value k = { .key = (char *)key };
1596 struct lwan_key_value *entry;
1597
1598 entry = bsearch(&k, la->base, la->elements, sizeof(k), key_value_compare);
1599 if (LIKELY(entry)__builtin_expect((!!(entry)), (1)))
1600 return entry->value;
1601 }
1602
1603 return NULL((void*)0);
1604}
1605
1606const char *lwan_request_get_query_param(struct lwan_request *request,
1607 const char *key)
1608{
1609 return value_lookup(lwan_request_get_query_params(request), key);
1610}
1611
1612const char *lwan_request_get_post_param(struct lwan_request *request,
1613 const char *key)
1614{
1615 return value_lookup(lwan_request_get_post_params(request), key);
1616}
1617
1618const char *lwan_request_get_cookie(struct lwan_request *request,
1619 const char *key)
1620{
1621 return value_lookup(lwan_request_get_cookies(request), key);
1622}
1623
1624const char *
1625lwan_request_get_header_from_helper(struct lwan_request_parser_helper *helper,
1626 const char *header)
1627{
1628 const size_t header_len = strlen(header);
1629 const size_t header_len_with_separator =
1630 header_len + HEADER_VALUE_SEPARATOR_LEN(sizeof(": ") - 1);
1631
1632 assert(strchr(header, ':') == NULL)((void) sizeof ((strchr(header, ':') == ((void*)0)) ? 1 : 0),
__extension__ ({ if (strchr(header, ':') == ((void*)0)) ; else
__assert_fail ("strchr(header, ':') == NULL", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1632, __extension__ __PRETTY_FUNCTION__); }))
;
1633
1634 for (size_t i = 0; i < helper->n_header_start; i++) {
1635 const char *start = helper->header_start[i];
1636 char *end = helper->header_start[i + 1] - HEADER_TERMINATOR_LEN(sizeof("\r\n") - 1);
1637
1638 if (UNLIKELY((size_t)(end - start) < header_len_with_separator)__builtin_expect((((size_t)(end - start) < header_len_with_separator
)), (0))
)
1639 continue;
1640
1641 STRING_SWITCH_SMALL (start + header_len)switch (string_as_uint16(start + header_len)) {
1642 case STR2_INT(':', ' ')((uint16_t)((':') | (' ') << 8)):
1643 if (!strncasecmp(start, header, header_len)) {
1644 *end = '\0';
1645 return start + header_len_with_separator;
1646 }
1647 }
1648 }
1649
1650 return NULL((void*)0);
1651}
1652
1653inline const char *lwan_request_get_header(struct lwan_request *request,
1654 const char *header)
1655{
1656 return lwan_request_get_header_from_helper(request->helper, header);
1657}
1658
1659const char *lwan_request_get_host(struct lwan_request *request)
1660{
1661 const struct lwan_request_parser_helper *helper = request->helper;
1662
1663 return helper->host.len ? helper->host.value : NULL((void*)0);
1664}
1665
1666ALWAYS_INLINEinline __attribute__((always_inline)) int
1667lwan_connection_get_fd(const struct lwan *lwan, const struct lwan_connection *conn)
1668{
1669 return (int)(intptr_t)(conn - lwan->conns);
1670}
1671
1672const char *
1673lwan_request_get_remote_address_and_port(struct lwan_request *request,
1674 char buffer[static INET6_ADDRSTRLEN46],
1675 uint16_t *port)
1676{
1677 struct sockaddr_storage non_proxied_addr = {.ss_family = AF_UNSPEC0};
1678 struct sockaddr_storage *sock_addr;
1679
1680 *port = 0;
1681
1682 if (request->flags & REQUEST_PROXIED) {
7
Assuming the condition is false
8
Taking false branch
1683 sock_addr = (struct sockaddr_storage *)&request->proxy->from;
1684
1685 if (UNLIKELY(sock_addr->ss_family == AF_UNSPEC)__builtin_expect(((sock_addr->ss_family == 0)), (0))) {
1686 static const char unspecified[] = "*unspecified*";
1687
1688 static_assert_Static_assert(sizeof(unspecified) <= INET6_ADDRSTRLEN46,
1689 "Enough space for unspecified address family");
1690 return memcpy(buffer, unspecified, sizeof(unspecified));
1691 }
1692 } else {
1693 socklen_t sock_len = sizeof(non_proxied_addr);
1694
1695 sock_addr = &non_proxied_addr;
1696
1697 if (UNLIKELY(getpeername(request->fd, (struct sockaddr *)sock_addr,__builtin_expect(((getpeername(request->fd, (struct sockaddr
*)sock_addr, &sock_len) < 0)), (0))
9
Assuming the condition is false
10
Taking false branch
1698 &sock_len) < 0)__builtin_expect(((getpeername(request->fd, (struct sockaddr
*)sock_addr, &sock_len) < 0)), (0))
) {
1699 return NULL((void*)0);
1700 }
1701 }
1702
1703 if (sock_addr->ss_family
10.1
Field 'ss_family' is not equal to AF_INET
== AF_INET2) {
11
Taking false branch
1704 struct sockaddr_in *sin = (struct sockaddr_in *)sock_addr;
1705 *port = ntohs(sin->sin_port);
1706 return inet_ntop(AF_INET2, &sin->sin_addr, buffer, INET6_ADDRSTRLEN46);
1707 }
1708
1709 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sock_addr;
1710 *port = ntohs(sin6->sin6_port);
12
1st function call argument is an uninitialized value
1711 return inet_ntop(AF_INET610, &sin6->sin6_addr, buffer, INET6_ADDRSTRLEN46);
1712}
1713
1714const char *
1715lwan_request_get_remote_address(struct lwan_request *request,
1716 char buffer[static INET6_ADDRSTRLEN46])
1717{
1718 uint16_t port;
1719 return lwan_request_get_remote_address_and_port(request, buffer, &port);
6
Calling 'lwan_request_get_remote_address_and_port'
1720}
1721
1722static void remove_sleep(void *data1, void *data2)
1723{
1724 static const enum lwan_connection_flags suspended_sleep =
1725 CONN_SUSPENDED | CONN_HAS_REMOVE_SLEEP_DEFER;
1726 struct timeouts *wheel = data1;
1727 struct timeout *timeout = data2;
1728 struct lwan_request *request =
1729 container_of(timeout, struct lwan_request, timeout)((struct lwan_request *) ((char *)(timeout) - __builtin_offsetof
(struct lwan_request, timeout)) + ((typeof(*(timeout)) *)0 !=
(typeof(((struct lwan_request *)0)->timeout) *)0))
;
1730
1731 if ((request->conn->flags & suspended_sleep) == suspended_sleep)
1732 timeouts_del(wheel, timeout);
1733
1734 request->conn->flags &= ~CONN_HAS_REMOVE_SLEEP_DEFER;
1735}
1736
1737void lwan_request_sleep(struct lwan_request *request, uint64_t ms)
1738{
1739 struct lwan_connection *conn = request->conn;
1740 struct timeouts *wheel = conn->thread->wheel;
1741 struct timespec now;
1742 struct coro_defer *defer = NULL((void*)0);
1743
1744 /* We need to update the timer wheel right now because
1745 * a request might have requested to sleep a long time
1746 * before it was being serviced -- causing the timeout
1747 * to essentially be a no-op. */
1748 if (UNLIKELY(clock_gettime(monotonic_clock_id, &now) < 0)__builtin_expect(((clock_gettime(monotonic_clock_id, &now
) < 0)), (0))
)
1749 lwan_status_critical("Could not get monotonic time")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1749, __FUNCTION__, "Could not get monotonic time")
;
1750 timeouts_update(wheel, (timeout_t)(now.tv_sec * 1000 + now.tv_nsec / 1000000));
1751
1752 request->timeout = (struct timeout) {};
1753 timeouts_add(wheel, &request->timeout, ms);
1754
1755 if (!(conn->flags & CONN_HAS_REMOVE_SLEEP_DEFER)) {
1756 defer = coro_defer2(conn->coro, remove_sleep, wheel, &request->timeout);
1757 conn->flags |= CONN_HAS_REMOVE_SLEEP_DEFER;
1758 }
1759
1760 coro_yield(conn->coro, CONN_CORO_SUSPEND);
1761
1762 if (defer)
1763 coro_defer_fire_and_disarm(conn->coro, defer);
1764}
1765
1766ALWAYS_INLINEinline __attribute__((always_inline)) int
1767lwan_request_get_range(struct lwan_request *request, off_t *from, off_t *to)
1768{
1769 struct lwan_request_parser_helper *helper = request->helper;
1770
1771 if (!(request->flags & REQUEST_PARSED_RANGE)) {
1772 parse_range(helper);
1773 request->flags |= REQUEST_PARSED_RANGE;
1774 }
1775
1776 if (LIKELY(helper->range.raw.len)__builtin_expect((!!(helper->range.raw.len)), (1))) {
1777 *from = helper->range.from;
1778 *to = helper->range.to;
1779 return 0;
1780 }
1781
1782 return -ENOENT2;
1783}
1784
1785ALWAYS_INLINEinline __attribute__((always_inline)) int
1786lwan_request_get_if_modified_since(struct lwan_request *request, time_t *value)
1787{
1788 struct lwan_request_parser_helper *helper = request->helper;
1789
1790 if (!(request->flags & REQUEST_PARSED_IF_MODIFIED_SINCE)) {
1791 parse_if_modified_since(helper);
1792 request->flags |= REQUEST_PARSED_IF_MODIFIED_SINCE;
1793 }
1794
1795 if (LIKELY(helper->if_modified_since.raw.len)__builtin_expect((!!(helper->if_modified_since.raw.len)), (
1))
) {
1796 *value = helper->if_modified_since.parsed;
1797 return 0;
1798 }
1799
1800 return -ENOENT2;
1801}
1802
1803ALWAYS_INLINEinline __attribute__((always_inline)) const struct lwan_value *
1804lwan_request_get_request_body(struct lwan_request *request)
1805{
1806 return &request->helper->body_data;
1807}
1808
1809ALWAYS_INLINEinline __attribute__((always_inline)) const struct lwan_value *
1810lwan_request_get_content_type(struct lwan_request *request)
1811{
1812 return &request->helper->content_type;
1813}
1814
1815ALWAYS_INLINEinline __attribute__((always_inline)) const struct lwan_key_value_array *
1816lwan_request_get_cookies(struct lwan_request *request)
1817{
1818 if (!(request->flags & REQUEST_PARSED_COOKIES)) {
1819 parse_cookies(request);
1820 request->flags |= REQUEST_PARSED_COOKIES;
1821 }
1822
1823 return &request->helper->cookies;
1824}
1825
1826ALWAYS_INLINEinline __attribute__((always_inline)) const struct lwan_key_value_array *
1827lwan_request_get_query_params(struct lwan_request *request)
1828{
1829 if (!(request->flags & REQUEST_PARSED_QUERY_STRING)) {
1830 parse_query_string(request);
1831 request->flags |= REQUEST_PARSED_QUERY_STRING;
1832 }
1833
1834 return &request->helper->query_params;
1835}
1836
1837ALWAYS_INLINEinline __attribute__((always_inline)) const struct lwan_key_value_array *
1838lwan_request_get_post_params(struct lwan_request *request)
1839{
1840 if (!(request->flags & REQUEST_PARSED_FORM_DATA)) {
1841 parse_form_data(request);
1842 request->flags |= REQUEST_PARSED_FORM_DATA;
1843 }
1844
1845 return &request->helper->post_params;
1846}
1847
1848ALWAYS_INLINEinline __attribute__((always_inline)) enum lwan_request_flags
1849lwan_request_get_accept_encoding(struct lwan_request *request)
1850{
1851 if (!(request->flags & REQUEST_PARSED_ACCEPT_ENCODING)) {
1852 parse_accept_encoding(request);
1853 request->flags |= REQUEST_PARSED_ACCEPT_ENCODING;
1854 }
1855
1856 return request->flags & REQUEST_ACCEPT_MASK;
1857}
1858
1859#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1860static int useless_coro_for_fuzzing(struct coro *c __attribute__((unused)),
1861 void *data __attribute__((unused)))
1862{
1863 return 0;
1864}
1865
1866static bool_Bool request_seems_complete(struct lwan_request_parser_helper *helper)
1867{
1868 return read_request_finalizer_from_helper(helper->buffer, helper, 1,
1869 false0) == FINALIZER_DONE;
1870}
1871
1872__attribute__((used)) int fuzz_parse_http_request(const uint8_t *data,
1873 size_t length)
1874{
1875 static struct coro_switcher switcher;
1876 static struct coro *coro;
1877 static char *header_start[N_HEADER_START64];
1878 static char data_copy[32767] = {0};
1879
1880 if (length > sizeof(data_copy))
1881 length = sizeof(data_copy);
1882 memcpy(data_copy, data, length);
1883
1884 if (!coro) {
1885 coro = coro_new(&switcher, useless_coro_for_fuzzing, NULL((void*)0));
1886
1887 lwan_job_thread_init();
1888 lwan_http_authorize_init();
1889 }
1890
1891 struct lwan_request_parser_helper helper = {
1892 .buffer = &(struct lwan_value){.value = data_copy, .len = length},
1893 .header_start = header_start,
1894 .error_when_n_packets = 2,
1895 };
1896 struct lwan_connection conn = {.coro = coro};
1897 struct lwan_proxy proxy = {};
1898 struct lwan_request request = {
1899 .helper = &helper,
1900 .conn = &conn,
1901 .flags = REQUEST_ALLOW_PROXY_REQS,
1902 .proxy = &proxy,
1903 };
1904
1905 /* If the finalizer isn't happy with a request, there's no point in
1906 * going any further with parsing it. */
1907 if (!request_seems_complete(&helper))
1908 return 0;
1909
1910 /* client_read() NUL-terminates the string */
1911 data_copy[length - 1] = '\0';
1912
1913 if (parse_http_request(&request) != HTTP_OK)
1914 return 0;
1915
1916 off_t trash1;
1917 time_t trash2;
1918 char *trash3;
1919 size_t gen = coro_deferred_get_generation(coro);
1920
1921 /* Only pointers were set in helper struct; actually parse them here. */
1922 parse_accept_encoding(&request);
1923
1924 /* Requesting these items will force them to be parsed, and also
1925 * exercise the lookup function. */
1926 LWAN_NO_DISCARD(lwan_request_get_header(&request, "Non-Existing-Header"))do { __typeof__(lwan_request_get_header(&request, "Non-Existing-Header"
)) no_discard_ = lwan_request_get_header(&request, "Non-Existing-Header"
); __asm__ __volatile__("" ::"g"(no_discard_) : "memory"); } while
(0)
;
1927
1928 /* Usually existing short header */
1929 LWAN_NO_DISCARD(lwan_request_get_header(&request, "Host"))do { __typeof__(lwan_request_get_header(&request, "Host")
) no_discard_ = lwan_request_get_header(&request, "Host")
; __asm__ __volatile__("" ::"g"(no_discard_) : "memory"); } while
(0)
;
1930
1931 LWAN_NO_DISCARD(lwan_request_get_cookie(&request, "Non-Existing-Cookie"))do { __typeof__(lwan_request_get_cookie(&request, "Non-Existing-Cookie"
)) no_discard_ = lwan_request_get_cookie(&request, "Non-Existing-Cookie"
); __asm__ __volatile__("" ::"g"(no_discard_) : "memory"); } while
(0)
;
1932 /* Set by some tests */
1933 LWAN_NO_DISCARD(lwan_request_get_cookie(&request, "FOO"))do { __typeof__(lwan_request_get_cookie(&request, "FOO"))
no_discard_ = lwan_request_get_cookie(&request, "FOO"); __asm__
__volatile__("" ::"g"(no_discard_) : "memory"); } while (0)
;
1934
1935 LWAN_NO_DISCARD(do { __typeof__(lwan_request_get_query_param(&request, "Non-Existing-Query-Param"
)) no_discard_ = lwan_request_get_query_param(&request, "Non-Existing-Query-Param"
); __asm__ __volatile__("" ::"g"(no_discard_) : "memory"); } while
(0)
1936 lwan_request_get_query_param(&request, "Non-Existing-Query-Param"))do { __typeof__(lwan_request_get_query_param(&request, "Non-Existing-Query-Param"
)) no_discard_ = lwan_request_get_query_param(&request, "Non-Existing-Query-Param"
); __asm__ __volatile__("" ::"g"(no_discard_) : "memory"); } while
(0)
;
1937
1938 LWAN_NO_DISCARD(do { __typeof__(lwan_request_get_post_param(&request, "Non-Existing-Post-Param"
)) no_discard_ = lwan_request_get_post_param(&request, "Non-Existing-Post-Param"
); __asm__ __volatile__("" ::"g"(no_discard_) : "memory"); } while
(0)
1939 lwan_request_get_post_param(&request, "Non-Existing-Post-Param"))do { __typeof__(lwan_request_get_post_param(&request, "Non-Existing-Post-Param"
)) no_discard_ = lwan_request_get_post_param(&request, "Non-Existing-Post-Param"
); __asm__ __volatile__("" ::"g"(no_discard_) : "memory"); } while
(0)
;
1940
1941 lwan_request_get_range(&request, &trash1, &trash1);
1942 LWAN_NO_DISCARD(trash1)do { __typeof__(trash1) no_discard_ = trash1; __asm__ __volatile__
("" ::"g"(no_discard_) : "memory"); } while (0)
;
1943
1944 lwan_request_get_if_modified_since(&request, &trash2);
1945 LWAN_NO_DISCARD(trash2)do { __typeof__(trash2) no_discard_ = trash2; __asm__ __volatile__
("" ::"g"(no_discard_) : "memory"); } while (0)
;
1946
1947 enum lwan_http_status handshake =
1948 prepare_websocket_handshake(&request, &trash3);
1949 LWAN_NO_DISCARD(trash3)do { __typeof__(trash3) no_discard_ = trash3; __asm__ __volatile__
("" ::"g"(no_discard_) : "memory"); } while (0)
;
1950 if (handshake == HTTP_SWITCHING_PROTOCOLS)
1951 free(trash3);
1952
1953 LWAN_NO_DISCARD(lwan_http_authorize(&request, "Fuzzy Realm", "/dev/null"))do { __typeof__(lwan_http_authorize(&request, "Fuzzy Realm"
, "/dev/null")) no_discard_ = lwan_http_authorize(&request
, "Fuzzy Realm", "/dev/null"); __asm__ __volatile__("" ::"g"(
no_discard_) : "memory"); } while (0)
;
1954
1955 coro_deferred_run(coro, gen);
1956
1957 return 0;
1958}
1959#endif
1960
1961static inline int64_t
1962make_async_yield_value(int fd, enum lwan_connection_coro_yield event)
1963{
1964 return (int64_t)(((uint64_t)fd << 32 | event));
1965}
1966
1967static inline void async_await_fd(struct coro *coro,
1968 int fd,
1969 enum lwan_connection_coro_yield events)
1970{
1971 assert(events >= CONN_CORO_ASYNC_AWAIT_READ &&((void) sizeof ((events >= CONN_CORO_ASYNC_AWAIT_READ &&
events <= CONN_CORO_ASYNC_AWAIT_READ_WRITE) ? 1 : 0), __extension__
({ if (events >= CONN_CORO_ASYNC_AWAIT_READ && events
<= CONN_CORO_ASYNC_AWAIT_READ_WRITE) ; else __assert_fail
("events >= CONN_CORO_ASYNC_AWAIT_READ && events <= CONN_CORO_ASYNC_AWAIT_READ_WRITE"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1972, __extension__ __PRETTY_FUNCTION__); }))
1972 events <= CONN_CORO_ASYNC_AWAIT_READ_WRITE)((void) sizeof ((events >= CONN_CORO_ASYNC_AWAIT_READ &&
events <= CONN_CORO_ASYNC_AWAIT_READ_WRITE) ? 1 : 0), __extension__
({ if (events >= CONN_CORO_ASYNC_AWAIT_READ && events
<= CONN_CORO_ASYNC_AWAIT_READ_WRITE) ; else __assert_fail
("events >= CONN_CORO_ASYNC_AWAIT_READ && events <= CONN_CORO_ASYNC_AWAIT_READ_WRITE"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-request.c"
, 1972, __extension__ __PRETTY_FUNCTION__); }))
;
1973
1974 return (void)coro_yield(coro, make_async_yield_value(fd, events));
1975}
1976
1977void lwan_request_await_read(struct lwan_request *r, int fd)
1978{
1979 return async_await_fd(r->conn->coro, fd, CONN_CORO_ASYNC_AWAIT_READ);
1980}
1981
1982void lwan_request_await_write(struct lwan_request *r, int fd)
1983{
1984 return async_await_fd(r->conn->coro, fd, CONN_CORO_ASYNC_AWAIT_WRITE);
1985}
1986
1987void lwan_request_await_read_write(struct lwan_request *r, int fd)
1988{
1989 return async_await_fd(r->conn->coro, fd, CONN_CORO_ASYNC_AWAIT_READ_WRITE);
1990}
1991
1992ssize_t lwan_request_async_read_flags(
1993 struct lwan_request *request, int fd, void *buf, size_t len, int flags)
1994{
1995 while (true1) {
1996 ssize_t r = recv(fd, buf, len, MSG_DONTWAITMSG_DONTWAIT | MSG_NOSIGNALMSG_NOSIGNAL | flags);
1997
1998 if (r < 0) {
1999 switch (errno(*__errno_location ())) {
2000 case EWOULDBLOCK11:
2001 lwan_request_await_read(request, fd);
2002 /* Fallthrough */
2003 case EINTR4:
2004 continue;
2005 case EPIPE32:
2006 return -errno(*__errno_location ());
2007 }
2008 }
2009
2010 return r;
2011 }
2012}
2013
2014ssize_t lwan_request_async_read(struct lwan_request *request,
2015 int fd,
2016 void *buf,
2017 size_t len)
2018{
2019 return lwan_request_async_read_flags(request, fd, buf, len, 0);
2020}
2021
2022ssize_t lwan_request_async_write(struct lwan_request *request,
2023 int fd,
2024 const void *buf,
2025 size_t len)
2026{
2027 while (true1) {
2028 ssize_t r = send(fd, buf, len, MSG_DONTWAITMSG_DONTWAIT|MSG_NOSIGNALMSG_NOSIGNAL);
2029
2030 if (r < 0) {
2031 switch (errno(*__errno_location ())) {
2032 case EWOULDBLOCK11:
2033 lwan_request_await_write(request, fd);
2034 /* Fallthrough */
2035 case EINTR4:
2036 continue;
2037 case EPIPE32:
2038 return -errno(*__errno_location ());
2039 }
2040 }
2041
2042 return r;
2043 }
2044}
2045
2046ssize_t lwan_request_async_writev(struct lwan_request *request,
2047 int fd,
2048 struct iovec *iov,
2049 int iov_count)
2050{
2051 ssize_t total_written = 0;
2052 int curr_iov = 0;
2053
2054 for (int tries = 10; tries;) {
2055 const int remaining_len = (int)(iov_count - curr_iov);
2056 ssize_t written;
2057
2058 if (remaining_len == 1) {
2059 const struct iovec *vec = &iov[curr_iov];
2060 return lwan_request_async_write(request, fd, vec->iov_base,
2061 vec->iov_len);
2062 }
2063
2064 written = writev(fd, iov + curr_iov, (size_t)remaining_len);
2065 if (UNLIKELY(written < 0)__builtin_expect(((written < 0)), (0))) {
2066 /* FIXME: Consider short writes as another try as well? */
2067 tries--;
2068
2069 switch (errno(*__errno_location ())) {
2070 case EAGAIN11:
2071 case EINTR4:
2072 goto try_again;
2073 default:
2074 goto out;
2075 }
2076 }
2077
2078 total_written += written;
2079
2080 while (curr_iov < iov_count &&
2081 written >= (ssize_t)iov[curr_iov].iov_len) {
2082 written -= (ssize_t)iov[curr_iov].iov_len;
2083 curr_iov++;
2084 }
2085
2086 if (curr_iov == iov_count)
2087 return total_written;
2088
2089 iov[curr_iov].iov_base = (char *)iov[curr_iov].iov_base + written;
2090 iov[curr_iov].iov_len -= (size_t)written;
2091
2092 try_again:
2093 lwan_request_await_write(request, fd);
2094 }
2095
2096out:
2097 coro_yield(request->conn->coro, CONN_CORO_ABORT);
2098 __builtin_unreachable();
2099}