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