Coverage Report

Created: 2026-04-12 06:59

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