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