1 #ifndef HTTP_URL_H
2 #define HTTP_URL_H
3
4 #include "net.h"
5 #include "uri-util.h"
6
7 #include "http-common.h"
8
9 struct http_request_target;
10
11 struct http_url {
12 /* server */
13 struct uri_host host;
14 in_port_t port;
15
16 /* userinfo (not parsed by default) */
17 const char *user;
18 const char *password;
19
20 /* path */
21 const char *path;
22
23 /* ?query (still encoded) */
24 const char *enc_query;
25
26 /* #fragment (still encoded) */
27 const char *enc_fragment;
28
29 bool have_ssl:1;
30 };
31
32 /*
33 * HTTP URL parsing
34 */
35
36 enum http_url_parse_flags {
37 /* Scheme part 'http:' is already parsed externally. This implies that
38 this is an absolute HTTP URL. */
39 HTTP_URL_PARSE_SCHEME_EXTERNAL = 0x01,
40 /* Allow '#fragment' part in HTTP URL */
41 HTTP_URL_ALLOW_FRAGMENT_PART = 0x02,
42 /* Allow 'user:password@' part in HTTP URL */
43 HTTP_URL_ALLOW_USERINFO_PART = 0x04,
44 /* Allow URL to contain %00 */
45 HTTP_URL_ALLOW_PCT_NUL = 0x08,
46 };
47
48 int http_url_parse(const char *url, struct http_url *base,
49 enum http_url_parse_flags flags, pool_t pool,
50 struct http_url **url_r, const char **error_r);
51
52 int http_url_request_target_parse(const char *request_target,
53 const char *host_header,
54 const struct http_url *default_base,
55 pool_t pool,
56 struct http_request_target *target,
57 const char **error_r) ATTR_NULL(3);
58
59 /*
60 * HTTP URL evaluation
61 */
62
63 static inline in_port_t
http_url_get_port_default(const struct http_url * url,in_port_t default_port)64 http_url_get_port_default(const struct http_url *url, in_port_t default_port)
65 {
66 return (url->port != 0 ? url->port : default_port);
67 }
68
http_url_get_port(const struct http_url * url)69 static inline in_port_t http_url_get_port(const struct http_url *url)
70 {
71 return http_url_get_port_default(
72 url, (url->have_ssl ? HTTPS_DEFAULT_PORT : HTTP_DEFAULT_PORT));
73 }
74
75 /*
76 * HTTP URL manipulation
77 */
78
79 void http_url_init_authority_from(struct http_url *dest,
80 const struct http_url *src);
81 void http_url_copy_authority(pool_t pool, struct http_url *dest,
82 const struct http_url *src);
83 struct http_url *
84 http_url_clone_authority(pool_t pool, const struct http_url *src);
85
86 void http_url_copy(pool_t pool, struct http_url *dest,
87 const struct http_url *src);
88 void http_url_copy_with_userinfo(pool_t pool, struct http_url *dest,
89 const struct http_url *src);
90
91 struct http_url *http_url_clone(pool_t pool,const struct http_url *src);
92 struct http_url *
93 http_url_clone_with_userinfo(pool_t pool, const struct http_url *src);
94
95 /*
96 * HTTP URL construction
97 */
98
99 const char *http_url_create(const struct http_url *url);
100
101 const char *http_url_create_host(const struct http_url *url);
102 const char *http_url_create_authority(const struct http_url *url);
103 const char *http_url_create_target(const struct http_url *url);
104
105 void http_url_escape_path(string_t *out, const char *data);
106 void http_url_escape_param(string_t *out, const char *data);
107
108 #endif
109