Coverage Report

Created: 2026-10-22 06:50

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