Kyle J. McKay | 3402a8d | 2013-07-31 13:52:00 -0700 | [diff] [blame] | 1 | #ifndef URL_MATCH_H |
Elijah Newren | 64e68a3 | 2018-08-15 10:54:08 -0700 | [diff] [blame] | 2 | #define URL_MATCH_H |
| 3 | |
Kyle J. McKay | 3402a8d | 2013-07-31 13:52:00 -0700 | [diff] [blame] | 4 | #include "string-list.h" |
Glen Choo | e0f9a51 | 2023-06-28 19:26:21 +0000 | [diff] [blame] | 5 | #include "config.h" |
Kyle J. McKay | 3402a8d | 2013-07-31 13:52:00 -0700 | [diff] [blame] | 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) */ |
Patrick Steinhardt | 3ec6e6e | 2017-01-31 10:01:45 +0100 | [diff] [blame] | 24 | size_t host_len; /* length of host name; |
Kyle J. McKay | 3402a8d | 2013-07-31 13:52:00 -0700 | [diff] [blame] | 25 | * file urls may have host_len == 0 */ |
Patrick Steinhardt | 3ec6e6e | 2017-01-31 10:01:45 +0100 | [diff] [blame] | 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) */ |
Kyle J. McKay | 3402a8d | 2013-07-31 13:52:00 -0700 | [diff] [blame] | 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 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 37 | char *url_normalize(const char *, struct url_info *); |
Kyle J. McKay | 3402a8d | 2013-07-31 13:52:00 -0700 | [diff] [blame] | 38 | |
Junio C Hamano | 836b6fb | 2013-07-31 10:42:01 -0700 | [diff] [blame] | 39 | struct urlmatch_item { |
Patrick Steinhardt | af99049 | 2017-01-31 10:01:46 +0100 | [diff] [blame] | 40 | size_t hostmatch_len; |
| 41 | size_t pathmatch_len; |
Junio C Hamano | 836b6fb | 2013-07-31 10:42:01 -0700 | [diff] [blame] | 42 | char user_matched; |
| 43 | }; |
| 44 | |
| 45 | struct urlmatch_config { |
| 46 | struct string_list vars; |
| 47 | struct url_info url; |
| 48 | const char *section; |
| 49 | const char *key; |
| 50 | |
| 51 | void *cb; |
Glen Choo | e0f9a51 | 2023-06-28 19:26:21 +0000 | [diff] [blame] | 52 | config_fn_t collect_fn; |
| 53 | config_fn_t cascade_fn; |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 54 | /* |
| 55 | * Compare the two matches, the one just discovered and the existing |
| 56 | * best match and return a negative value if the found item is to be |
| 57 | * rejected or a non-negative value if it is to be accepted. If this |
| 58 | * field is set to NULL, use the default comparison technique, which |
| 59 | * checks to ses if found is better (according to the urlmatch |
| 60 | * specificity rules) than existing. |
| 61 | */ |
| 62 | int (*select_fn)(const struct urlmatch_item *found, const struct urlmatch_item *existing); |
Johannes Schindelin | 1229499 | 2020-04-24 22:35:49 +0000 | [diff] [blame] | 63 | /* |
| 64 | * An optional callback to allow e.g. for partial URLs; it shall |
| 65 | * return 1 or 0 depending whether `url` matches or not. |
| 66 | */ |
| 67 | int (*fallback_match_fn)(const char *url, void *cb); |
Junio C Hamano | 836b6fb | 2013-07-31 10:42:01 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Ævar Arnfjörð Bjarmason | 73ee449 | 2021-10-01 12:27:33 +0200 | [diff] [blame] | 70 | #define URLMATCH_CONFIG_INIT { \ |
| 71 | .vars = STRING_LIST_INIT_DUP, \ |
| 72 | } |
| 73 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 74 | int urlmatch_config_entry(const char *var, const char *value, |
| 75 | const struct config_context *ctx, void *cb); |
Ævar Arnfjörð Bjarmason | a41e8e7 | 2022-03-04 19:32:07 +0100 | [diff] [blame] | 76 | void urlmatch_config_release(struct urlmatch_config *config); |
Junio C Hamano | 836b6fb | 2013-07-31 10:42:01 -0700 | [diff] [blame] | 77 | |
Kyle J. McKay | 3402a8d | 2013-07-31 13:52:00 -0700 | [diff] [blame] | 78 | #endif /* URL_MATCH_H */ |