Coverage Report

Created: 2026-10-15 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http1.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_HTTP
27
28
#include "urldata.h"
29
#include "http.h"
30
#include "http1.h"
31
#include "urlapi-int.h"
32
33
34
#define H1_MAX_URL_LEN (8 * 1024)
35
36
void Curl_h1_req_parse_init(struct h1_req_parser *parser, size_t max_line_len)
37
9.76k
{
38
9.76k
  memset(parser, 0, sizeof(*parser));
39
9.76k
  parser->max_line_len = max_line_len;
40
9.76k
  curlx_dyn_init(&parser->scratch, max_line_len);
41
9.76k
}
42
43
void Curl_h1_req_parse_free(struct h1_req_parser *parser)
44
19.4k
{
45
19.4k
  if(parser) {
46
19.4k
    Curl_http_req_free(parser->req);
47
19.4k
    curlx_dyn_free(&parser->scratch);
48
19.4k
    parser->req = NULL;
49
19.4k
    parser->done = FALSE;
50
19.4k
  }
51
19.4k
}
52
53
static CURLcode trim_line(struct h1_req_parser *parser, int options)
54
137k
{
55
137k
  DEBUGASSERT(parser->line);
56
137k
  if(parser->line_len) {
57
137k
    if(parser->line[parser->line_len - 1] == '\n')
58
137k
      --parser->line_len;
59
137k
    if(parser->line_len) {
60
136k
      if(parser->line[parser->line_len - 1] == '\r')
61
62.6k
        --parser->line_len;
62
74.2k
      else if(options & H1_PARSE_OPT_STRICT)
63
0
        return CURLE_URL_MALFORMAT;
64
136k
    }
65
223
    else if(options & H1_PARSE_OPT_STRICT)
66
0
      return CURLE_URL_MALFORMAT;
67
137k
  }
68
0
  else if(options & H1_PARSE_OPT_STRICT)
69
0
    return CURLE_URL_MALFORMAT;
70
71
137k
  if(parser->line_len > parser->max_line_len) {
72
0
    return CURLE_URL_MALFORMAT;
73
0
  }
74
137k
  return CURLE_OK;
75
137k
}
76
77
static CURLcode detect_line(struct h1_req_parser *parser,
78
                            const uint8_t *buf, const size_t buflen,
79
                            size_t *pnread)
80
138k
{
81
138k
  const char *line_end;
82
83
138k
  DEBUGASSERT(!parser->line);
84
138k
  *pnread = 0;
85
138k
  line_end = memchr(buf, '\n', buflen);
86
138k
  if(!line_end)
87
865
    return CURLE_AGAIN;
88
137k
  parser->line = (const char *)buf;
89
137k
  parser->line_len = line_end - parser->line + 1;
90
137k
  *pnread = parser->line_len;
91
137k
  return CURLE_OK;
92
138k
}
93
94
static CURLcode next_line(struct h1_req_parser *parser,
95
                          const uint8_t *buf, const size_t buflen, int options,
96
                          size_t *pnread)
97
138k
{
98
138k
  CURLcode result;
99
100
138k
  *pnread = 0;
101
138k
  if(parser->line) {
102
127k
    parser->line = NULL;
103
127k
    parser->line_len = 0;
104
127k
    curlx_dyn_reset(&parser->scratch);
105
127k
  }
106
107
138k
  result = detect_line(parser, buf, buflen, pnread);
108
138k
  if(!result) {
109
137k
    if(curlx_dyn_len(&parser->scratch)) {
110
      /* append detected line to scratch to have the complete line */
111
313
      result = curlx_dyn_addn(&parser->scratch, parser->line,
112
313
                              parser->line_len);
113
313
      if(result)
114
0
        return result;
115
313
      parser->line = curlx_dyn_ptr(&parser->scratch);
116
313
      parser->line_len = curlx_dyn_len(&parser->scratch);
117
313
    }
118
137k
    result = trim_line(parser, options);
119
137k
    if(result)
120
0
      return result;
121
137k
  }
122
865
  else if(result == CURLE_AGAIN) {
123
    /* no line end in `buf`, add it to our scratch */
124
865
    result = curlx_dyn_addn(&parser->scratch, (const unsigned char *)buf,
125
865
                            buflen);
126
865
    *pnread = buflen;
127
865
  }
128
138k
  return result;
129
138k
}
130
131
static CURLcode start_req(struct h1_req_parser *parser,
132
                          const char *scheme_default,
133
                          const char *custom_method,
134
                          int options)
135
9.71k
{
136
9.71k
  const char *p, *m, *target, *hv, *scheme, *authority, *path;
137
9.71k
  size_t m_len, target_len, hv_len, scheme_len, authority_len, path_len;
138
9.71k
  size_t i;
139
9.71k
  CURLU *url = NULL;
140
9.71k
  CURLcode result = CURLE_URL_MALFORMAT; /* Use this as default fail */
141
142
9.71k
  DEBUGASSERT(!parser->req);
143
  /* line must match: "METHOD TARGET HTTP_VERSION" */
144
9.71k
  if(custom_method && custom_method[0] &&
145
171
     !strncmp(custom_method, parser->line, strlen(custom_method))) {
146
160
    p = parser->line + strlen(custom_method);
147
160
  }
148
9.55k
  else {
149
9.55k
    p = memchr(parser->line, ' ', parser->line_len);
150
9.55k
    if(!p || p == parser->line)
151
4
      goto out;
152
9.55k
  }
153
154
9.71k
  m = parser->line;
155
9.71k
  m_len = p - parser->line;
156
9.71k
  target = p + 1;
157
9.71k
  target_len = hv_len = 0;
158
9.71k
  hv = NULL;
159
160
  /* URL may contain spaces so scan backwards */
161
82.6k
  for(i = parser->line_len; i > m_len; --i) {
162
82.6k
    if(parser->line[i] == ' ') {
163
9.70k
      hv = &parser->line[i + 1];
164
9.70k
      hv_len = parser->line_len - i;
165
9.70k
      target_len = (hv - target) - 1;
166
9.70k
      break;
167
9.70k
    }
168
82.6k
  }
169
  /* no SPACE found or empty TARGET or empty HTTP_VERSION */
170
9.71k
  if(!target_len || !hv_len)
171
10
    goto out;
172
173
9.70k
  (void)hv;
174
175
  /* The TARGET can be (rfc 9112, ch. 3.2):
176
   * origin-form:     path + optional query
177
   * absolute-form:   absolute URI
178
   * authority-form:  host+port for CONNECT
179
   * asterisk-form:   '*' for OPTIONS
180
   *
181
   * from TARGET, we derive `scheme` `authority` `path`
182
   * origin-form            --        --          TARGET
183
   * absolute-form          URL*      URL*        URL*
184
   * authority-form         --        TARGET      --
185
   * asterisk-form          --        --          TARGET
186
   */
187
9.70k
  scheme = authority = path = NULL;
188
9.70k
  scheme_len = authority_len = path_len = 0;
189
190
9.70k
  if(target_len == 1 && target[0] == '*') {
191
    /* asterisk-form */
192
10
    path = target;
193
10
    path_len = target_len;
194
10
  }
195
9.69k
  else if(!strncmp("CONNECT", m, m_len)) {
196
    /* authority-form */
197
54
    authority = target;
198
54
    authority_len = target_len;
199
54
  }
200
9.64k
  else if(target[0] == '/') {
201
    /* origin-form */
202
9.10k
    path = target;
203
9.10k
    path_len = target_len;
204
9.10k
  }
205
534
  else {
206
    /* origin-form OR absolute-form */
207
534
    CURLUcode uc;
208
534
    char tmp[H1_MAX_URL_LEN];
209
210
    /* default, unless we see an absolute URL */
211
534
    path = target;
212
534
    path_len = target_len;
213
214
    /* URL parser wants null-termination */
215
534
    if(target_len >= sizeof(tmp))
216
1
      goto out;
217
533
    memcpy(tmp, target, target_len);
218
533
    tmp[target_len] = '\0';
219
    /* See if treating TARGET as an absolute URL makes sense */
220
533
    if(Curl_is_absolute_url(tmp, NULL, 0, FALSE)) {
221
397
      unsigned int url_options;
222
223
397
      url = curl_url();
224
397
      if(!url) {
225
0
        result = CURLE_OUT_OF_MEMORY;
226
0
        goto out;
227
0
      }
228
397
      url_options = (CURLU_NON_SUPPORT_SCHEME |
229
397
                     CURLU_PATH_AS_IS |
230
397
                     CURLU_NO_DEFAULT_PORT);
231
397
      if(!(options & H1_PARSE_OPT_STRICT))
232
397
        url_options |= CURLU_ALLOW_SPACE;
233
397
      uc = curl_url_set(url, CURLUPART_URL, tmp, url_options);
234
397
      if(uc) {
235
7
        goto out;
236
7
      }
237
397
    }
238
239
526
    if(!url && (options & H1_PARSE_OPT_STRICT)) {
240
      /* we should have an absolute URL or have seen `/` earlier */
241
0
      goto out;
242
0
    }
243
526
  }
244
245
9.69k
  if(url) {
246
390
    result = Curl_http_req_make2(&parser->req, m, m_len, url, scheme_default);
247
390
  }
248
9.30k
  else {
249
9.30k
    if(!scheme && scheme_default) {
250
0
      scheme = scheme_default;
251
0
      scheme_len = strlen(scheme_default);
252
0
    }
253
9.30k
    result = Curl_http_req_make(&parser->req, m, m_len, scheme, scheme_len,
254
9.30k
                                authority, authority_len, path, path_len);
255
9.30k
  }
256
257
9.71k
out:
258
9.71k
  curl_url_cleanup(url);
259
9.71k
  return result;
260
9.69k
}
261
262
CURLcode Curl_h1_req_parse_read(struct h1_req_parser *parser,
263
                                const uint8_t *buf, size_t buflen,
264
                                const char *scheme_default,
265
                                const char *custom_method,
266
                                int options, size_t *pnread)
267
10.5k
{
268
10.5k
  CURLcode result = CURLE_OK;
269
10.5k
  size_t nread;
270
271
10.5k
  *pnread = 0;
272
273
10.5k
  DEBUGASSERT(buf);
274
10.5k
  if(!buf)
275
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
276
277
147k
  while(!parser->done) {
278
138k
    result = next_line(parser, buf, buflen, options, &nread);
279
138k
    if(result) {
280
0
      if(result == CURLE_AGAIN)
281
0
        result = CURLE_OK;
282
0
      goto out;
283
0
    }
284
285
    /* Consume this line */
286
138k
    *pnread += nread;
287
138k
    buf += nread;
288
138k
    buflen -= nread;
289
290
138k
    if(!parser->line) {
291
      /* consumed bytes, but line not complete */
292
865
      if(!buflen)
293
865
        goto out;
294
865
    }
295
137k
    else if(!parser->req) {
296
9.71k
      result = start_req(parser, scheme_default, custom_method, options);
297
9.71k
      if(result)
298
22
        goto out;
299
9.71k
    }
300
127k
    else if(parser->line_len == 0) {
301
      /* last, empty line, we are finished */
302
9.67k
      if(!parser->req) {
303
0
        result = CURLE_URL_MALFORMAT;
304
0
        goto out;
305
0
      }
306
9.67k
      parser->done = TRUE;
307
9.67k
      curlx_dyn_reset(&parser->scratch);
308
      /* last chance adjustments */
309
9.67k
    }
310
117k
    else {
311
117k
      result = Curl_dynhds_h1_add_line(&parser->req->headers,
312
117k
                                       parser->line, parser->line_len);
313
117k
      if(result)
314
22
        goto out;
315
117k
    }
316
138k
  }
317
318
10.5k
out:
319
10.5k
  return result;
320
10.5k
}
321
322
CURLcode Curl_h1_req_write_head(struct httpreq *req, int http_minor,
323
                                struct dynbuf *dbuf)
324
1.04k
{
325
1.04k
  CURLcode result;
326
327
1.04k
  result = curlx_dyn_addf(dbuf, "%s %s%s%s%s HTTP/1.%d\r\n",
328
1.04k
                          req->method,
329
1.04k
                          req->scheme ? req->scheme : "",
330
1.04k
                          req->scheme ? "://" : "",
331
1.04k
                          req->authority ? req->authority : "",
332
1.04k
                          req->path ? req->path : "",
333
1.04k
                          http_minor);
334
1.04k
  if(result)
335
0
    goto out;
336
337
1.04k
  result = Curl_dynhds_h1_dprint(&req->headers, dbuf);
338
1.04k
  if(result)
339
1
    goto out;
340
341
1.04k
  result = curlx_dyn_addn(dbuf, STRCONST("\r\n"));
342
343
1.04k
out:
344
1.04k
  return result;
345
1.04k
}
346
347
#endif /* !CURL_DISABLE_HTTP */