Raw
1 #ifndef URL_MATCH_H
2 #define URL_MATCH_H
3
4 #include "string-list.h"
5 #include "config.h"
6
7 struct url_info {
8 /* normalized url on success, must be freed, otherwise NULL */
9 char *url;
10 /* if !url, a brief reason for the failure, otherwise NULL */
11 const char *err;
12
13 /* the rest of the fields are only set if url != NULL */
14
15 size_t url_len; /* total length of url (which is now normalized) */
16 size_t scheme_len; /* length of scheme name (excluding final :) */
17 size_t user_off; /* offset into url to start of user name (0 => none) */
18 size_t user_len; /* length of user name; if user_off != 0 but
19 user_len == 0, an empty user name was given */
20 size_t passwd_off; /* offset into url to start of passwd (0 => none) */
21 size_t passwd_len; /* length of passwd; if passwd_off != 0 but
22 passwd_len == 0, an empty passwd was given */
23 size_t host_off; /* offset into url to start of host name (0 => none) */
24 size_t host_len; /* length of host name;
25 * file urls may have host_len == 0 */
26 size_t port_off; /* offset into url to start of port number (0 => none) */
27 size_t port_len; /* if a portnum is present (port_off != 0), it has
28 * this length (excluding the leading ':') starting
29 * from port_off (always 0 for file urls) */
30 size_t path_off; /* offset into url to the start of the url path;
31 * this will always point to a '/' character
32 * after the url has been normalized */
33 size_t path_len; /* length of path portion excluding any trailing
34 * '?...' and '#...' portion; will always be >= 1 */
35 };
36
37 char *url_normalize(const char *, struct url_info *);
38 char *url_parse(const char *, struct url_info *);
39
40 /*
41 * Like url_normalize(), but also allows '*' glob characters in the host
42 * portion. Use this when normalizing URL patterns from user configuration.
43 *
44 * Note that '*' is a valid path character per RFC 3986 (as a sub-delim),
45 * so glob patterns using '*' in the path are also accepted.
46 *
47 * Returns a newly allocated normalized string and fills out_info if
48 * non-NULL, or NULL if the pattern is invalid.
49 */
50 char *url_normalize_pattern(const char *url, struct url_info *out_info);
51
52 struct urlmatch_item {
53 size_t hostmatch_len;
54 size_t pathmatch_len;
55 char user_matched;
56 };
57
58 struct urlmatch_config {
59 struct string_list vars;
60 struct url_info url;
61 const char *section;
62 const char *key;
63
64 void *cb;
65 config_fn_t collect_fn;
66 config_fn_t cascade_fn;
67 /*
68 * Compare the two matches, the one just discovered and the existing
69 * best match and return a negative value if the found item is to be
70 * rejected or a non-negative value if it is to be accepted. If this
71 * field is set to NULL, use the default comparison technique, which
72 * checks to ses if found is better (according to the urlmatch
73 * specificity rules) than existing.
74 */
75 int (*select_fn)(const struct urlmatch_item *found, const struct urlmatch_item *existing);
76 /*
77 * An optional callback to allow e.g. for partial URLs; it shall
78 * return 1 or 0 depending whether `url` matches or not.
79 */
80 int (*fallback_match_fn)(const char *url, void *cb);
81 };
82
83 #define URLMATCH_CONFIG_INIT { \
84 .vars = STRING_LIST_INIT_DUP, \
85 }
86
87 int urlmatch_config_entry(const char *var, const char *value,
88 const struct config_context *ctx, void *cb);
89 void urlmatch_config_release(struct urlmatch_config *config);
90
91 #endif /* URL_MATCH_H */