/src/dovecot/src/lib-url/http-url.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "str.h" |
5 | | #include "strfuncs.h" |
6 | | #include "net.h" |
7 | | #include "uri-util.h" |
8 | | |
9 | | #include "http-url-private.h" |
10 | | |
11 | | static bool http_url_parse_authority_form(struct http_url_parser *url_parser); |
12 | | |
13 | | static bool |
14 | | http_url_parse_scheme(struct http_url_parser *url_parser, const char **scheme_r) |
15 | 0 | { |
16 | 0 | struct uri_parser *parser = &url_parser->parser; |
17 | |
|
18 | 0 | *scheme_r = NULL; |
19 | 0 | if ((url_parser->flags & HTTP_URL_PARSE_SCHEME_EXTERNAL) != 0) |
20 | 0 | return TRUE; |
21 | | |
22 | 0 | if (uri_parse_scheme(parser, scheme_r) <= 0) { |
23 | 0 | parser->cur = parser->begin; |
24 | 0 | return FALSE; |
25 | 0 | } |
26 | | |
27 | 0 | return TRUE; |
28 | 0 | } |
29 | | |
30 | | static bool http_url_parse_unknown_scheme(struct http_url_parser *url_parser) |
31 | 0 | { |
32 | 0 | struct uri_parser *parser = &url_parser->parser; |
33 | |
|
34 | 0 | if (url_parser->request_target) { |
35 | | /* Valid as non-HTTP scheme, but also try to parse as authority |
36 | | */ |
37 | 0 | parser->cur = parser->begin; |
38 | 0 | if (!http_url_parse_authority_form(url_parser)) { |
39 | | /* indicate non-http-url */ |
40 | 0 | url_parser->url = NULL; |
41 | 0 | url_parser->req_format = |
42 | 0 | HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE; |
43 | 0 | } |
44 | 0 | return TRUE; |
45 | 0 | } |
46 | 0 | parser->error = "Not an HTTP URL"; |
47 | 0 | return FALSE; |
48 | 0 | } |
49 | | |
50 | | static bool |
51 | | http_url_parse_userinfo(struct http_url_parser *url_parser, |
52 | | struct uri_authority *auth, |
53 | | const char **user_r, const char **password_r) |
54 | 0 | { |
55 | 0 | struct uri_parser *parser = &url_parser->parser; |
56 | 0 | const char *p; |
57 | |
|
58 | 0 | *user_r = *password_r = NULL; |
59 | |
|
60 | 0 | if (auth->enc_userinfo == NULL) |
61 | 0 | return TRUE; |
62 | | |
63 | 0 | if ((url_parser->flags & HTTP_URL_ALLOW_USERINFO_PART) == 0) { |
64 | | /* RFC 7230, Section 2.7.1: http URI Scheme |
65 | | |
66 | | A sender MUST NOT generate the userinfo subcomponent (and its |
67 | | "@" delimiter) when an "http" URI reference is generated |
68 | | within a message as a request target or header field value. |
69 | | Before making use of an "http" URI reference received from an |
70 | | untrusted source, a recipient SHOULD parse for userinfo and |
71 | | treat its presence as an error; it is likely being used to |
72 | | obscure the authority for the sake of phishing attacks. |
73 | | */ |
74 | 0 | parser->error = "HTTP URL does not allow `userinfo@' part"; |
75 | 0 | return FALSE; |
76 | 0 | } |
77 | | |
78 | 0 | p = strchr(auth->enc_userinfo, ':'); |
79 | 0 | if (p == NULL) { |
80 | 0 | if (!uri_data_decode(parser, auth->enc_userinfo, NULL, user_r)) |
81 | 0 | return FALSE; |
82 | 0 | } else { |
83 | 0 | if (!uri_data_decode(parser, auth->enc_userinfo, p, user_r)) |
84 | 0 | return FALSE; |
85 | 0 | if (!uri_data_decode(parser, p + 1, NULL, password_r)) |
86 | 0 | return FALSE; |
87 | 0 | } |
88 | 0 | return TRUE; |
89 | 0 | } |
90 | | |
91 | | static bool http_url_parse_authority(struct http_url_parser *url_parser) |
92 | 0 | { |
93 | 0 | struct uri_parser *parser = &url_parser->parser; |
94 | 0 | struct http_url *url = url_parser->url; |
95 | 0 | struct uri_authority auth; |
96 | 0 | const char *user = NULL, *password = NULL; |
97 | 0 | int ret; |
98 | |
|
99 | 0 | if ((ret = uri_parse_host_authority(parser, &auth)) < 0) |
100 | 0 | return FALSE; |
101 | 0 | if (auth.host.name == NULL || *auth.host.name == '\0') { |
102 | | /* RFC 7230, Section 2.7.1: http URI Scheme |
103 | | |
104 | | A sender MUST NOT generate an "http" URI with an empty host |
105 | | identifier. A recipient that processes such a URI reference |
106 | | MUST reject it as invalid. |
107 | | */ |
108 | 0 | parser->error = "HTTP URL does not allow empty host identifier"; |
109 | 0 | return FALSE; |
110 | 0 | } |
111 | 0 | if (ret > 0) { |
112 | 0 | if (!http_url_parse_userinfo(url_parser, &auth, |
113 | 0 | &user, &password)) |
114 | 0 | return FALSE; |
115 | 0 | } |
116 | 0 | if (url != NULL) { |
117 | 0 | uri_host_copy(parser->pool, &url->host, &auth.host); |
118 | 0 | url->port = auth.port; |
119 | 0 | url->user = p_strdup(parser->pool, user); |
120 | 0 | url->password = p_strdup(parser->pool, password); |
121 | 0 | } |
122 | 0 | return TRUE; |
123 | 0 | } |
124 | | |
125 | | static bool http_url_parse_authority_form(struct http_url_parser *url_parser) |
126 | 0 | { |
127 | 0 | struct uri_parser *parser = &url_parser->parser; |
128 | |
|
129 | 0 | if (!http_url_parse_authority(url_parser)) |
130 | 0 | return FALSE; |
131 | 0 | if (parser->cur != parser->end) |
132 | 0 | return FALSE; |
133 | 0 | url_parser->req_format = HTTP_REQUEST_TARGET_FORMAT_AUTHORITY; |
134 | 0 | return TRUE; |
135 | 0 | } |
136 | | |
137 | | static void |
138 | | http_url_parse_path_copy_from_base(struct http_url_parser *url_parser) |
139 | 0 | { |
140 | 0 | struct uri_parser *parser = &url_parser->parser; |
141 | 0 | struct http_url *url = url_parser->url, *base = url_parser->base; |
142 | |
|
143 | 0 | if (!url_parser->relative || url == NULL) |
144 | 0 | return; |
145 | | |
146 | 0 | if (base->path != NULL) |
147 | 0 | url->path = p_strdup(parser->pool, base->path); |
148 | 0 | else if (base->enc_path != NULL) { |
149 | 0 | const char *enc; |
150 | 0 | bool result; |
151 | |
|
152 | 0 | result = uri_data_decode(parser, base->enc_path, |
153 | 0 | NULL, &enc); |
154 | 0 | i_assert(result); |
155 | 0 | url->path = p_strdup(parser->pool, enc); |
156 | 0 | } |
157 | 0 | if (base->enc_path != NULL) { |
158 | 0 | if (strcmp(url->path, base->enc_path) == 0) |
159 | 0 | url->enc_path = url->path; |
160 | 0 | else |
161 | 0 | url->enc_path = p_strdup(parser->pool, base->enc_path); |
162 | 0 | } else if (base->path != NULL) { |
163 | 0 | string_t *enc = t_str_new(256); |
164 | 0 | uri_append_path_data(enc, NULL, base->path); |
165 | 0 | if (strcmp(url->path, str_c(enc)) == 0) |
166 | 0 | url->enc_path = url->path; |
167 | 0 | else |
168 | 0 | url->enc_path = p_strdup(parser->pool, str_c(enc)); |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | static int |
173 | | http_url_parse_path(struct http_url_parser *url_parser) |
174 | 0 | { |
175 | 0 | struct uri_parser *parser = &url_parser->parser; |
176 | 0 | struct http_url *url = url_parser->url, *base = url_parser->base; |
177 | 0 | const char *const *path; |
178 | 0 | int path_relative; |
179 | 0 | string_t *enc_fullpath = NULL, *fullpath = NULL; |
180 | 0 | int ret; |
181 | | |
182 | | /* path-abempty / path-absolute / path-noscheme / path-empty */ |
183 | 0 | if ((ret = uri_parse_path(parser, &path_relative, &path)) < 0) |
184 | 0 | return -1; |
185 | | |
186 | | /* Resolve path */ |
187 | 0 | if (ret == 0) { |
188 | 0 | http_url_parse_path_copy_from_base(url_parser); |
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | 0 | if (url != NULL) { |
193 | 0 | enc_fullpath = t_str_new(256); |
194 | 0 | fullpath = t_str_new(256); |
195 | 0 | } |
196 | |
|
197 | 0 | if (url != NULL && url_parser->relative && path_relative > 0 && |
198 | 0 | (base->enc_path != NULL || base->path != NULL)) { |
199 | 0 | const char *base_path = base->enc_path; |
200 | |
|
201 | 0 | if (base_path == NULL) { |
202 | 0 | string_t *enc = t_str_new(256); |
203 | 0 | uri_append_path_data(enc, NULL, base->path); |
204 | 0 | base_path = str_c(enc); |
205 | 0 | } |
206 | |
|
207 | 0 | const char *pbegin = base_path; |
208 | 0 | const char *pend = base_path + strlen(base_path); |
209 | 0 | const char *p = pend - 1; |
210 | |
|
211 | 0 | i_assert(*pbegin == '/'); |
212 | | |
213 | | /* Discard trailing segments of base path based on how many |
214 | | effective leading '..' segments were found in the relative |
215 | | path. |
216 | | */ |
217 | 0 | while (path_relative > 0 && p > pbegin) { |
218 | 0 | while (p > pbegin && *p != '/') p--; |
219 | 0 | if (p >= pbegin) { |
220 | 0 | pend = p; |
221 | 0 | path_relative--; |
222 | 0 | } |
223 | 0 | if (p > pbegin) p--; |
224 | 0 | } |
225 | |
|
226 | 0 | if (pend > pbegin) { |
227 | 0 | const char *enc; |
228 | 0 | bool result; |
229 | |
|
230 | 0 | str_append_data(enc_fullpath, pbegin, pend - pbegin); |
231 | 0 | result = uri_data_decode(parser, str_c(enc_fullpath), |
232 | 0 | NULL, &enc); |
233 | 0 | i_assert(result); |
234 | 0 | str_append(fullpath, enc); |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | /* Append relative path */ |
239 | 0 | while (*path != NULL) { |
240 | 0 | const char *part; |
241 | |
|
242 | 0 | if (!uri_data_decode(parser, *path, NULL, &part)) |
243 | 0 | return -1; |
244 | | |
245 | 0 | if (url != NULL) { |
246 | 0 | str_append_c(enc_fullpath, '/'); |
247 | 0 | str_append(enc_fullpath, *path); |
248 | 0 | str_append_c(fullpath, '/'); |
249 | 0 | str_append(fullpath, part); |
250 | 0 | } |
251 | 0 | path++; |
252 | 0 | } |
253 | | |
254 | 0 | if (url != NULL) { |
255 | 0 | url->enc_path = p_strdup(parser->pool, str_c(enc_fullpath)); |
256 | 0 | if (strcmp(str_c(fullpath), url->enc_path) == 0) |
257 | 0 | url->path = url->enc_path; |
258 | 0 | else |
259 | 0 | url->path = p_strdup(parser->pool, str_c(fullpath)); |
260 | 0 | } |
261 | 0 | return 1; |
262 | 0 | } |
263 | | |
264 | | static bool |
265 | | http_url_parse_query(struct http_url_parser *url_parser, bool have_path) |
266 | 0 | { |
267 | 0 | struct uri_parser *parser = &url_parser->parser; |
268 | 0 | struct http_url *url = url_parser->url, *base = url_parser->base; |
269 | 0 | const char *query; |
270 | 0 | int ret; |
271 | |
|
272 | 0 | if ((ret = uri_parse_query(parser, &query)) < 0) |
273 | 0 | return FALSE; |
274 | 0 | if (url == NULL) |
275 | 0 | return TRUE; |
276 | | |
277 | 0 | if (ret > 0) |
278 | 0 | url->enc_query = p_strdup(parser->pool, query); |
279 | 0 | else if (url_parser->relative && !have_path) |
280 | 0 | url->enc_query = p_strdup(parser->pool, base->enc_query); |
281 | 0 | return TRUE; |
282 | 0 | } |
283 | | |
284 | | static bool |
285 | | http_url_parse_fragment(struct http_url_parser *url_parser, bool have_path) |
286 | 0 | { |
287 | 0 | struct uri_parser *parser = &url_parser->parser; |
288 | 0 | struct http_url *url = url_parser->url, *base = url_parser->base; |
289 | 0 | const char *fragment; |
290 | 0 | int ret; |
291 | |
|
292 | 0 | if ((ret = uri_parse_fragment(parser, &fragment)) < 0) |
293 | 0 | return FALSE; |
294 | 0 | if (ret > 0 && |
295 | 0 | (url_parser->flags & HTTP_URL_ALLOW_FRAGMENT_PART) == 0) { |
296 | 0 | parser->error = |
297 | 0 | "URL fragment not allowed for HTTP URL in this context"; |
298 | 0 | return FALSE; |
299 | 0 | } |
300 | 0 | if (url == NULL) |
301 | 0 | return TRUE; |
302 | | |
303 | 0 | if (ret > 0) |
304 | 0 | url->enc_fragment = p_strdup(parser->pool, fragment); |
305 | 0 | else if (url_parser->relative && !have_path) |
306 | 0 | url->enc_fragment = p_strdup(parser->pool, base->enc_fragment); |
307 | 0 | return TRUE; |
308 | 0 | } |
309 | | |
310 | | bool http_url_do_parse(struct http_url_parser *url_parser) |
311 | 0 | { |
312 | 0 | struct uri_parser *parser = &url_parser->parser; |
313 | 0 | struct http_url *url = url_parser->url, *base = url_parser->base; |
314 | 0 | bool relative = TRUE, have_scheme = FALSE, have_authority = FALSE, |
315 | 0 | have_path = FALSE; |
316 | 0 | const char *scheme; |
317 | 0 | int ret; |
318 | | |
319 | | /* RFC 7230, Appendix B: |
320 | | |
321 | | http-URI = "http://" authority path-abempty [ "?" query ] |
322 | | [ "#" fragment ] |
323 | | https-URI = "https://" authority path-abempty [ "?" query ] |
324 | | [ "#" fragment ] |
325 | | partial-URI = relative-part [ "?" query ] |
326 | | |
327 | | request-target = origin-form / absolute-form / authority-form / |
328 | | asterisk-form |
329 | | |
330 | | origin-form = absolute-path [ "?" query ] |
331 | | absolute-form = absolute-URI |
332 | | authority-form = authority |
333 | | asterisk-form = "*" |
334 | | ; Not parsed here |
335 | | |
336 | | absolute-path = 1*( "/" segment ) |
337 | | |
338 | | RFC 3986, Appendix A: (implemented in uri-util.h) |
339 | | |
340 | | absolute-URI = scheme ":" hier-part [ "?" query ] |
341 | | |
342 | | hier-part = "//" authority path-abempty |
343 | | / path-absolute |
344 | | / path-rootless |
345 | | / path-empty |
346 | | |
347 | | relative-part = "//" authority path-abempty |
348 | | / path-absolute |
349 | | / path-noscheme |
350 | | / path-empty |
351 | | |
352 | | authority = [ userinfo "@" ] host [ ":" port ] |
353 | | |
354 | | path-abempty = *( "/" segment ) |
355 | | path-absolute = "/" [ segment-nz *( "/" segment ) ] |
356 | | path-noscheme = segment-nz-nc *( "/" segment ) |
357 | | path-rootless = segment-nz *( "/" segment ) |
358 | | path-empty = 0<pchar> |
359 | | |
360 | | segment = *pchar |
361 | | segment-nz = 1*pchar |
362 | | segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" ) |
363 | | ; non-zero-length segment without any colon ":" |
364 | | |
365 | | query = *( pchar / "/" / "?" ) |
366 | | fragment = *( pchar / "/" / "?" ) |
367 | | */ |
368 | | |
369 | | /* "http:" / "https:" */ |
370 | 0 | if (http_url_parse_scheme(url_parser, &scheme)) { |
371 | 0 | if (scheme == NULL) { |
372 | | /* Scheme externally parsed */ |
373 | 0 | } else if (strcasecmp(scheme, "https") == 0) { |
374 | 0 | if (url != NULL) |
375 | 0 | url->have_ssl = TRUE; |
376 | 0 | } else if (strcasecmp(scheme, "http") != 0) { |
377 | 0 | return http_url_parse_unknown_scheme(url_parser); |
378 | 0 | } |
379 | | |
380 | 0 | relative = FALSE; |
381 | 0 | have_scheme = TRUE; |
382 | 0 | } |
383 | | |
384 | | /* "//" authority ; or |
385 | | * ["//"] authority ; when parsing a request target |
386 | | */ |
387 | 0 | if (parser->cur < parser->end && parser->cur[0] == '/') { |
388 | 0 | if ((have_scheme || !url_parser->request_target) && |
389 | 0 | (parser->cur + 1) < parser->end && parser->cur[1] == '/') { |
390 | 0 | parser->cur += 2; |
391 | 0 | relative = FALSE; |
392 | 0 | have_authority = TRUE; |
393 | 0 | } else { |
394 | | /* start of absolute-path */ |
395 | 0 | } |
396 | 0 | } else if (url_parser->request_target && !have_scheme) { |
397 | 0 | if (!http_url_parse_authority_form(url_parser)) { |
398 | | /* not non-HTTP scheme and invalid as authority-form */ |
399 | 0 | parser->error = "Request target is invalid"; |
400 | 0 | return FALSE; |
401 | 0 | } |
402 | 0 | return TRUE; |
403 | 0 | } |
404 | | |
405 | 0 | if (have_scheme && !have_authority) { |
406 | 0 | parser->error = "Absolute HTTP URL requires `//' after `http:'"; |
407 | 0 | return FALSE; |
408 | 0 | } |
409 | | |
410 | 0 | if (have_authority) { |
411 | 0 | if (!http_url_parse_authority(url_parser)) |
412 | 0 | return FALSE; |
413 | 0 | } |
414 | | |
415 | | /* Relative URLs are only valid when we have a base URL */ |
416 | 0 | if (relative) { |
417 | 0 | if (base == NULL) { |
418 | 0 | parser->error = "Relative HTTP URL not allowed"; |
419 | 0 | return FALSE; |
420 | 0 | } else if (!have_authority && url != NULL) { |
421 | 0 | uri_host_copy(parser->pool, &url->host, &base->host); |
422 | 0 | url->port = base->port; |
423 | 0 | url->have_ssl = base->have_ssl; |
424 | 0 | url->user = p_strdup_empty(parser->pool, base->user); |
425 | 0 | url->password = p_strdup_empty(parser->pool, |
426 | 0 | base->password); |
427 | 0 | } |
428 | | |
429 | 0 | url_parser->relative = TRUE; |
430 | 0 | } |
431 | | |
432 | | /* path-abempty / path-absolute / path-noscheme / path-empty */ |
433 | 0 | ret = http_url_parse_path(url_parser); |
434 | 0 | if (ret < 0) |
435 | 0 | return FALSE; |
436 | 0 | have_path = (ret > 0); |
437 | | |
438 | | /* [ "?" query ] */ |
439 | 0 | if (!http_url_parse_query(url_parser, have_path)) |
440 | 0 | return FALSE; |
441 | | |
442 | | /* [ "#" fragment ] */ |
443 | 0 | if (!http_url_parse_fragment(url_parser, have_path)) |
444 | 0 | return FALSE; |
445 | | |
446 | | /* must be at end of URL now */ |
447 | 0 | i_assert(parser->cur == parser->end); |
448 | | |
449 | 0 | if (have_scheme) |
450 | 0 | url_parser->req_format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE; |
451 | 0 | return TRUE; |
452 | 0 | } |
453 | | |
454 | | /* Public API */ |
455 | | |
456 | | int http_url_parse(const char *url, struct http_url *base, |
457 | | enum http_url_parse_flags flags, pool_t pool, |
458 | | struct http_url **url_r, const char **error_r) |
459 | 0 | { |
460 | 0 | struct http_url_parser url_parser; |
461 | | |
462 | | /* base != NULL indicates whether relative URLs are allowed. However, |
463 | | certain flags may also dictate whether relative URLs are |
464 | | allowed/required. */ |
465 | 0 | i_assert((flags & HTTP_URL_PARSE_SCHEME_EXTERNAL) == 0 || base == NULL); |
466 | 0 | i_assert(base == NULL || !http_url_is_empty(base)); |
467 | | |
468 | 0 | i_zero(&url_parser); |
469 | 0 | uri_parser_init(&url_parser.parser, pool, url); |
470 | 0 | url_parser.parser.allow_pct_nul = (flags & HTTP_URL_ALLOW_PCT_NUL) != 0; |
471 | |
|
472 | 0 | url_parser.url = p_new(pool, struct http_url, 1); |
473 | 0 | url_parser.base = base; |
474 | 0 | url_parser.flags = flags; |
475 | |
|
476 | 0 | if (!http_url_do_parse(&url_parser)) { |
477 | 0 | *error_r = url_parser.parser.error; |
478 | 0 | return -1; |
479 | 0 | } |
480 | 0 | *url_r = url_parser.url; |
481 | | /* http_url_is_empty() relies on this being set on every success. */ |
482 | 0 | i_assert(url_parser.url != NULL && |
483 | 0 | url_parser.url->host.name != NULL); |
484 | 0 | return 0; |
485 | 0 | } |
486 | | |
487 | | /* |
488 | | * HTTP URL evaluation |
489 | | */ |
490 | | |
491 | | /* Must compare every field of struct http_url except host.ip, which is |
492 | | deliberately excluded: uri_do_parse_host() derives it from host.name, |
493 | | so comparing host.name already covers it. */ |
494 | | bool http_url_equals(const struct http_url *url1, const struct http_url *url2) |
495 | 0 | { |
496 | 0 | return null_strcmp(url1->host.name, url2->host.name) == 0 && |
497 | 0 | null_strcmp(url1->user, url2->user) == 0 && |
498 | 0 | null_strcmp(url1->password, url2->password) == 0 && |
499 | 0 | null_strcmp(url1->enc_path, url2->enc_path) == 0 && |
500 | 0 | null_strcmp(url1->path, url2->path) == 0 && |
501 | 0 | null_strcmp(url1->enc_query, url2->enc_query) == 0 && |
502 | 0 | null_strcmp(url1->enc_fragment, url2->enc_fragment) == 0 && |
503 | 0 | url1->port == url2->port && |
504 | 0 | url1->have_ssl == url2->have_ssl; |
505 | 0 | } |
506 | | |
507 | | /* |
508 | | * HTTP URL manipulation |
509 | | */ |
510 | | |
511 | | void http_url_init_authority_from(struct http_url *dest, |
512 | | const struct http_url *src) |
513 | 0 | { |
514 | 0 | i_zero(dest); |
515 | 0 | dest->host = src->host; |
516 | 0 | dest->port = src->port; |
517 | 0 | dest->have_ssl = src->have_ssl; |
518 | 0 | } |
519 | | |
520 | | void http_url_copy_authority(pool_t pool, struct http_url *dest, |
521 | | const struct http_url *src) |
522 | 0 | { |
523 | 0 | i_zero(dest); |
524 | 0 | uri_host_copy(pool, &dest->host, &src->host); |
525 | 0 | dest->port = src->port; |
526 | 0 | dest->have_ssl = src->have_ssl; |
527 | 0 | } |
528 | | |
529 | | struct http_url * |
530 | | http_url_clone_authority(pool_t pool, const struct http_url *src) |
531 | 0 | { |
532 | 0 | struct http_url *new_url; |
533 | |
|
534 | 0 | new_url = p_new(pool, struct http_url, 1); |
535 | 0 | http_url_copy_authority(pool, new_url, src); |
536 | |
|
537 | 0 | return new_url; |
538 | 0 | } |
539 | | |
540 | | void http_url_copy(pool_t pool, struct http_url *dest, |
541 | | const struct http_url *src) |
542 | 0 | { |
543 | 0 | http_url_copy_authority(pool, dest, src); |
544 | 0 | dest->enc_path = p_strdup(pool, src->enc_path); |
545 | 0 | dest->path = p_strdup(pool, src->path); |
546 | 0 | dest->enc_query = p_strdup(pool, src->enc_query); |
547 | 0 | dest->enc_fragment = p_strdup(pool, src->enc_fragment); |
548 | 0 | } |
549 | | |
550 | | void http_url_copy_with_userinfo(pool_t pool, struct http_url *dest, |
551 | | const struct http_url *src) |
552 | 0 | { |
553 | 0 | http_url_copy(pool, dest, src); |
554 | 0 | dest->user = p_strdup(pool, src->user); |
555 | 0 | dest->password = p_strdup(pool, src->password); |
556 | 0 | } |
557 | | |
558 | | struct http_url *http_url_clone(pool_t pool, const struct http_url *src) |
559 | 0 | { |
560 | 0 | struct http_url *new_url; |
561 | |
|
562 | 0 | new_url = p_new(pool, struct http_url, 1); |
563 | 0 | http_url_copy(pool, new_url, src); |
564 | |
|
565 | 0 | return new_url; |
566 | 0 | } |
567 | | |
568 | | struct http_url * |
569 | | http_url_clone_with_userinfo(pool_t pool, const struct http_url *src) |
570 | 0 | { |
571 | 0 | struct http_url *new_url; |
572 | |
|
573 | 0 | new_url = p_new(pool, struct http_url, 1); |
574 | 0 | http_url_copy_with_userinfo(pool, new_url, src); |
575 | |
|
576 | 0 | return new_url; |
577 | 0 | } |
578 | | |
579 | | /* |
580 | | * HTTP URL construction |
581 | | */ |
582 | | |
583 | | static void |
584 | | http_url_add_scheme(string_t *urlstr, const struct http_url *url) |
585 | 0 | { |
586 | | /* scheme */ |
587 | 0 | if (!url->have_ssl) |
588 | 0 | uri_append_scheme(urlstr, "http"); |
589 | 0 | else |
590 | 0 | uri_append_scheme(urlstr, "https"); |
591 | 0 | str_append(urlstr, "//"); |
592 | 0 | } |
593 | | |
594 | | static void |
595 | | http_url_add_authority(string_t *urlstr, const struct http_url *url) |
596 | 0 | { |
597 | | /* Userinfo is intentionally left out, so that credentials don't end |
598 | | up in logs and other output. */ |
599 | | /* host */ |
600 | 0 | uri_append_host(urlstr, &url->host); |
601 | | /* port */ |
602 | 0 | uri_append_port(urlstr, url->port); |
603 | 0 | } |
604 | | |
605 | | static void |
606 | | http_url_add_target(string_t *urlstr, const struct http_url *url) |
607 | 0 | { |
608 | 0 | if ((url->enc_path == NULL || *url->enc_path == '\0') && |
609 | 0 | (url->path == NULL || *url->path == '\0')) { |
610 | | /* Older syntax of RFC 2616 requires this slash at all times for |
611 | | an absolute URL. */ |
612 | 0 | str_append_c(urlstr, '/'); |
613 | 0 | } else if (url->enc_path != NULL && *url->enc_path != '\0') { |
614 | 0 | i_assert(*url->enc_path == '/'); |
615 | 0 | str_append(urlstr, url->enc_path); |
616 | 0 | } else { |
617 | 0 | i_assert(*url->path == '/'); |
618 | 0 | uri_append_path_data(urlstr, "", url->path); |
619 | 0 | } |
620 | | |
621 | | /* query (pre-encoded) */ |
622 | 0 | if (url->enc_query != NULL) { |
623 | 0 | str_append_c(urlstr, '?'); |
624 | 0 | str_append(urlstr, url->enc_query); |
625 | 0 | } |
626 | 0 | } |
627 | | |
628 | | const char *http_url_create(const struct http_url *url) |
629 | 0 | { |
630 | 0 | string_t *urlstr = t_str_new(512); |
631 | |
|
632 | 0 | http_url_add_scheme(urlstr, url); |
633 | 0 | http_url_add_authority(urlstr, url); |
634 | 0 | http_url_add_target(urlstr, url); |
635 | | |
636 | | /* fragment */ |
637 | 0 | if (url->enc_fragment != NULL) { |
638 | 0 | str_append_c(urlstr, '#'); |
639 | 0 | str_append(urlstr, url->enc_fragment); |
640 | 0 | } |
641 | |
|
642 | 0 | return str_c(urlstr); |
643 | 0 | } |
644 | | |
645 | | const char *http_url_create_host(const struct http_url *url) |
646 | 0 | { |
647 | 0 | string_t *urlstr = t_str_new(512); |
648 | |
|
649 | 0 | http_url_add_scheme(urlstr, url); |
650 | 0 | http_url_add_authority(urlstr, url); |
651 | |
|
652 | 0 | return str_c(urlstr); |
653 | 0 | } |
654 | | |
655 | | const char *http_url_create_authority(const struct http_url *url) |
656 | 0 | { |
657 | 0 | string_t *urlstr = t_str_new(256); |
658 | |
|
659 | 0 | http_url_add_authority(urlstr, url); |
660 | |
|
661 | 0 | return str_c(urlstr); |
662 | 0 | } |
663 | | |
664 | | const char *http_url_create_target(const struct http_url *url) |
665 | 0 | { |
666 | 0 | string_t *urlstr = t_str_new(256); |
667 | |
|
668 | 0 | http_url_add_target(urlstr, url); |
669 | |
|
670 | 0 | return str_c(urlstr); |
671 | 0 | } |
672 | | |
673 | | void http_url_escape_path(string_t *out, const char *data) |
674 | 0 | { |
675 | 0 | uri_append_query_data(out, "&;?=+", data); |
676 | 0 | } |
677 | | |
678 | | void http_url_escape_param(string_t *out, const char *data) |
679 | 0 | { |
680 | 0 | uri_append_query_data(out, "&;/?=+", data); |
681 | 0 | } |