Coverage Report

Created: 2025-07-11 06:33

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