Coverage Report

Created: 2026-12-01 06:33

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
213k
{
38
213k
  memset(parser, 0, sizeof(*parser));
39
213k
  parser->max_line_len = max_line_len;
40
213k
  curlx_dyn_init(&parser->scratch, max_line_len);
41
213k
}
42
43
void Curl_h1_req_parse_free(struct h1_req_parser *parser)
44
397k
{
45
397k
  if(parser) {
46
397k
    Curl_http_req_free(parser->req);
47
397k
    curlx_dyn_free(&parser->scratch);
48
397k
    parser->req = NULL;
49
397k
    parser->done = FALSE;
50
397k
  }
51
397k
}
52
53
static CURLcode trim_line(struct h1_req_parser *parser, int options)
54
1.73M
{
55
1.73M
  DEBUGASSERT(parser->line);
56
1.73M
  if(parser->line_len) {
57
1.73M
    if(parser->line[parser->line_len - 1] == '\n')
58
1.73M
      --parser->line_len;
59
1.73M
    if(parser->line_len) {
60
1.72M
      if(parser->line[parser->line_len - 1] == '\r')
61
1.25M
        --parser->line_len;
62
473k
      else if(options & H1_PARSE_OPT_STRICT)
63
0
        return CURLE_URL_MALFORMAT;
64
1.72M
    }
65
1.59k
    else if(options & H1_PARSE_OPT_STRICT)
66
0
      return CURLE_URL_MALFORMAT;
67
1.73M
  }
68
0
  else if(options & H1_PARSE_OPT_STRICT)
69
0
    return CURLE_URL_MALFORMAT;
70
71
1.73M
  if(parser->line_len > parser->max_line_len) {
72
0
    return CURLE_URL_MALFORMAT;
73
0
  }
74
1.73M
  return CURLE_OK;
75
1.73M
}
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
1.73M
{
81
1.73M
  const char *line_end;
82
83
1.73M
  DEBUGASSERT(!parser->line);
84
1.73M
  *pnread = 0;
85
1.73M
  line_end = memchr(buf, '\n', buflen);
86
1.73M
  if(!line_end)
87
1.59k
    return CURLE_AGAIN;
88
1.73M
  parser->line = (const char *)buf;
89
1.73M
  parser->line_len = line_end - parser->line + 1;
90
1.73M
  *pnread = parser->line_len;
91
1.73M
  return CURLE_OK;
92
1.73M
}
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
1.73M
{
98
1.73M
  CURLcode result;
99
100
1.73M
  *pnread = 0;
101
1.73M
  if(parser->line) {
102
1.54M
    parser->line = NULL;
103
1.54M
    parser->line_len = 0;
104
1.54M
    curlx_dyn_reset(&parser->scratch);
105
1.54M
  }
106
107
1.73M
  result = detect_line(parser, buf, buflen, pnread);
108
1.73M
  if(!result) {
109
1.73M
    if(curlx_dyn_len(&parser->scratch)) {
110
      /* append detected line to scratch to have the complete line */
111
1.25k
      result = curlx_dyn_addn(&parser->scratch, parser->line,
112
1.25k
                              parser->line_len);
113
1.25k
      if(result)
114
2
        return result;
115
1.24k
      parser->line = curlx_dyn_ptr(&parser->scratch);
116
1.24k
      parser->line_len = curlx_dyn_len(&parser->scratch);
117
1.24k
    }
118
1.73M
    result = trim_line(parser, options);
119
1.73M
    if(result)
120
0
      return result;
121
1.73M
  }
122
1.59k
  else if(result == CURLE_AGAIN) {
123
    /* no line end in `buf`, add it to our scratch */
124
1.59k
    result = curlx_dyn_addn(&parser->scratch, (const unsigned char *)buf,
125
1.59k
                            buflen);
126
1.59k
    *pnread = buflen;
127
1.59k
  }
128
1.73M
  return result;
129
1.73M
}
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
188k
{
136
188k
  const char *p, *m, *target, *hv, *scheme, *authority, *path;
137
188k
  size_t m_len, target_len, hv_len, scheme_len, authority_len, path_len;
138
188k
  size_t i;
139
188k
  CURLU *url = NULL;
140
188k
  CURLcode result = CURLE_URL_MALFORMAT; /* Use this as default fail */
141
142
188k
  DEBUGASSERT(!parser->req);
143
  /* line must match: "METHOD TARGET HTTP_VERSION" */
144
188k
  if(custom_method && custom_method[0] &&
145
2.80k
     !strncmp(custom_method, parser->line, strlen(custom_method))) {
146
2.79k
    p = parser->line + strlen(custom_method);
147
2.79k
  }
148
185k
  else {
149
185k
    p = memchr(parser->line, ' ', parser->line_len);
150
185k
    if(!p || p == parser->line)
151
180
      goto out;
152
185k
  }
153
154
188k
  m = parser->line;
155
188k
  m_len = p - parser->line;
156
188k
  target = p + 1;
157
188k
  target_len = hv_len = 0;
158
188k
  hv = NULL;
159
160
  /* URL may contain spaces so scan backwards */
161
1.50M
  for(i = parser->line_len; i > m_len; --i) {
162
1.50M
    if(parser->line[i] == ' ') {
163
187k
      hv = &parser->line[i + 1];
164
187k
      hv_len = parser->line_len - i;
165
187k
      target_len = (hv - target) - 1;
166
187k
      break;
167
187k
    }
168
1.50M
  }
169
  /* no SPACE found or empty TARGET or empty HTTP_VERSION */
170
188k
  if(!target_len || !hv_len)
171
288
    goto out;
172
173
187k
  (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
187k
  scheme = authority = path = NULL;
188
187k
  scheme_len = authority_len = path_len = 0;
189
190
187k
  if(target_len == 1 && target[0] == '*') {
191
    /* asterisk-form */
192
210
    path = target;
193
210
    path_len = target_len;
194
210
  }
195
187k
  else if(!strncmp("CONNECT", m, m_len)) {
196
    /* authority-form */
197
521
    authority = target;
198
521
    authority_len = target_len;
199
521
  }
200
187k
  else if(target[0] == '/') {
201
    /* origin-form */
202
170k
    path = target;
203
170k
    path_len = target_len;
204
170k
  }
205
17.0k
  else {
206
    /* origin-form OR absolute-form */
207
17.0k
    CURLUcode uc;
208
17.0k
    char tmp[H1_MAX_URL_LEN];
209
210
    /* default, unless we see an absolute URL */
211
17.0k
    path = target;
212
17.0k
    path_len = target_len;
213
214
    /* URL parser wants null-termination */
215
17.0k
    if(target_len >= sizeof(tmp))
216
3
      goto out;
217
17.0k
    memcpy(tmp, target, target_len);
218
17.0k
    tmp[target_len] = '\0';
219
    /* See if treating TARGET as an absolute URL makes sense */
220
17.0k
    if(Curl_is_absolute_url(tmp, NULL, 0, FALSE)) {
221
13.9k
      unsigned int url_options;
222
223
13.9k
      url = curl_url();
224
13.9k
      if(!url) {
225
6
        result = CURLE_OUT_OF_MEMORY;
226
6
        goto out;
227
6
      }
228
13.9k
      url_options = (CURLU_NON_SUPPORT_SCHEME |
229
13.9k
                     CURLU_PATH_AS_IS |
230
13.9k
                     CURLU_NO_DEFAULT_PORT);
231
13.9k
      if(!(options & H1_PARSE_OPT_STRICT))
232
13.9k
        url_options |= CURLU_ALLOW_SPACE;
233
13.9k
      uc = curl_url_set(url, CURLUPART_URL, tmp, url_options);
234
13.9k
      if(uc) {
235
2.29k
        goto out;
236
2.29k
      }
237
13.9k
    }
238
239
14.7k
    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
14.7k
  }
244
245
185k
  if(url) {
246
11.6k
    result = Curl_http_req_make2(&parser->req, m, m_len, url, scheme_default);
247
11.6k
  }
248
173k
  else {
249
173k
    if(!scheme && scheme_default) {
250
0
      scheme = scheme_default;
251
0
      scheme_len = strlen(scheme_default);
252
0
    }
253
173k
    result = Curl_http_req_make(&parser->req, m, m_len, scheme, scheme_len,
254
173k
                                authority, authority_len, path, path_len);
255
173k
  }
256
257
188k
out:
258
188k
  curl_url_cleanup(url);
259
188k
  return result;
260
185k
}
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
189k
{
268
189k
  CURLcode result = CURLE_OK;
269
189k
  size_t nread;
270
271
189k
  *pnread = 0;
272
273
189k
  DEBUGASSERT(buf);
274
189k
  if(!buf)
275
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
276
277
1.91M
  while(!parser->done) {
278
1.73M
    result = next_line(parser, buf, buflen, options, &nread);
279
1.73M
    if(result) {
280
2
      if(result == CURLE_AGAIN)
281
0
        result = CURLE_OK;
282
2
      goto out;
283
2
    }
284
285
    /* Consume this line */
286
1.73M
    *pnread += nread;
287
1.73M
    buf += nread;
288
1.73M
    buflen -= nread;
289
290
1.73M
    if(!parser->line) {
291
      /* consumed bytes, but line not complete */
292
1.59k
      if(!buflen)
293
1.59k
        goto out;
294
1.59k
    }
295
1.73M
    else if(!parser->req) {
296
188k
      result = start_req(parser, scheme_default, custom_method, options);
297
188k
      if(result)
298
2.97k
        goto out;
299
188k
    }
300
1.54M
    else if(parser->line_len == 0) {
301
      /* last, empty line, we are finished */
302
184k
      if(!parser->req) {
303
0
        result = CURLE_URL_MALFORMAT;
304
0
        goto out;
305
0
      }
306
184k
      parser->done = TRUE;
307
184k
      curlx_dyn_reset(&parser->scratch);
308
      /* last chance adjustments */
309
184k
    }
310
1.35M
    else {
311
1.35M
      result = Curl_dynhds_h1_add_line(&parser->req->headers,
312
1.35M
                                       parser->line, parser->line_len);
313
1.35M
      if(result)
314
754
        goto out;
315
1.35M
    }
316
1.73M
  }
317
318
189k
out:
319
189k
  return result;
320
189k
}
321
322
CURLcode Curl_h1_req_write_head(struct httpreq *req, int http_minor,
323
                                struct dynbuf *dbuf)
324
10.5k
{
325
10.5k
  CURLcode result;
326
327
10.5k
  result = curlx_dyn_addf(dbuf, "%s %s%s%s%s HTTP/1.%d\r\n",
328
10.5k
                          req->method,
329
10.5k
                          req->scheme ? req->scheme : "",
330
10.5k
                          req->scheme ? "://" : "",
331
10.5k
                          req->authority ? req->authority : "",
332
10.5k
                          req->path ? req->path : "",
333
10.5k
                          http_minor);
334
10.5k
  if(result)
335
6
    goto out;
336
337
10.5k
  result = Curl_dynhds_h1_dprint(&req->headers, dbuf);
338
10.5k
  if(result)
339
7
    goto out;
340
341
10.5k
  result = curlx_dyn_addn(dbuf, STRCONST("\r\n"));
342
343
10.5k
out:
344
10.5k
  return result;
345
10.5k
}
346
347
#endif /* !CURL_DISABLE_HTTP */