mirror of
https://github.com/git-for-windows/git.git
synced 2026-06-11 08:30:32 -05:00
Define url_parse, a general parsing function that supports all Git URLs
including scp style URLs such as hostname:~user/repo.
It is adapted from the algorithm in connect.c's parse_connect_url
and reuses the shared enum url_scheme and url_get_scheme function
that previous commits made available in url.h. The new parser and
the connect path agree on scheme classification. url_parse has the
same interface as url_normalize and uses the same data structures.
Both functions accept the same URL forms with one deliberate
exception. Bare local paths such as "/abs/path", "./rel"
or "repo" are accepted by parse_connect_url as URL_SCHEME_LOCAL,
but rejected by url_parse because url_normalize requires a URL
with a scheme://host form. A consumer that wants to handle both
URLs and local paths needs to dispatch on url_is_local_not_ssh
before calling url_parse, just as the connect path does internally.
The duplication with parse_connect_url is intentional.
The two functions have different contracts:
- parse_connect_url
Calls die() on an unknown scheme
and returns NUL-terminated host/path
strings for the connect path
- url_parse
Returns NULL on failure while populating
out_info->err, and exposes components
as offset/length pairs into the normalized
URL buffer, matching url_normalize.
Reconciling both is possible, but not in the scope
of the current patch set.
Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
80 lines
2.8 KiB
C
80 lines
2.8 KiB
C
#ifndef URL_MATCH_H
|
|
#define URL_MATCH_H
|
|
|
|
#include "string-list.h"
|
|
#include "config.h"
|
|
|
|
struct url_info {
|
|
/* normalized url on success, must be freed, otherwise NULL */
|
|
char *url;
|
|
/* if !url, a brief reason for the failure, otherwise NULL */
|
|
const char *err;
|
|
|
|
/* the rest of the fields are only set if url != NULL */
|
|
|
|
size_t url_len; /* total length of url (which is now normalized) */
|
|
size_t scheme_len; /* length of scheme name (excluding final :) */
|
|
size_t user_off; /* offset into url to start of user name (0 => none) */
|
|
size_t user_len; /* length of user name; if user_off != 0 but
|
|
user_len == 0, an empty user name was given */
|
|
size_t passwd_off; /* offset into url to start of passwd (0 => none) */
|
|
size_t passwd_len; /* length of passwd; if passwd_off != 0 but
|
|
passwd_len == 0, an empty passwd was given */
|
|
size_t host_off; /* offset into url to start of host name (0 => none) */
|
|
size_t host_len; /* length of host name;
|
|
* file urls may have host_len == 0 */
|
|
size_t port_off; /* offset into url to start of port number (0 => none) */
|
|
size_t port_len; /* if a portnum is present (port_off != 0), it has
|
|
* this length (excluding the leading ':') starting
|
|
* from port_off (always 0 for file urls) */
|
|
size_t path_off; /* offset into url to the start of the url path;
|
|
* this will always point to a '/' character
|
|
* after the url has been normalized */
|
|
size_t path_len; /* length of path portion excluding any trailing
|
|
* '?...' and '#...' portion; will always be >= 1 */
|
|
};
|
|
|
|
char *url_normalize(const char *, struct url_info *);
|
|
char *url_parse(const char *, struct url_info *);
|
|
|
|
struct urlmatch_item {
|
|
size_t hostmatch_len;
|
|
size_t pathmatch_len;
|
|
char user_matched;
|
|
};
|
|
|
|
struct urlmatch_config {
|
|
struct string_list vars;
|
|
struct url_info url;
|
|
const char *section;
|
|
const char *key;
|
|
|
|
void *cb;
|
|
config_fn_t collect_fn;
|
|
config_fn_t cascade_fn;
|
|
/*
|
|
* Compare the two matches, the one just discovered and the existing
|
|
* best match and return a negative value if the found item is to be
|
|
* rejected or a non-negative value if it is to be accepted. If this
|
|
* field is set to NULL, use the default comparison technique, which
|
|
* checks to ses if found is better (according to the urlmatch
|
|
* specificity rules) than existing.
|
|
*/
|
|
int (*select_fn)(const struct urlmatch_item *found, const struct urlmatch_item *existing);
|
|
/*
|
|
* An optional callback to allow e.g. for partial URLs; it shall
|
|
* return 1 or 0 depending whether `url` matches or not.
|
|
*/
|
|
int (*fallback_match_fn)(const char *url, void *cb);
|
|
};
|
|
|
|
#define URLMATCH_CONFIG_INIT { \
|
|
.vars = STRING_LIST_INIT_DUP, \
|
|
}
|
|
|
|
int urlmatch_config_entry(const char *var, const char *value,
|
|
const struct config_context *ctx, void *cb);
|
|
void urlmatch_config_release(struct urlmatch_config *config);
|
|
|
|
#endif /* URL_MATCH_H */
|