Line data Source code
1 : #ifndef HEADER_fd_src_waltz_http_fd_url_h 2 : #define HEADER_fd_src_waltz_http_fd_url_h 3 : 4 : /* fd_url.h provides an API for handling URLs. 5 : 6 : This API is by no means compliant. Works only for basic strings. */ 7 : 8 : #include "../../util/fd_util_base.h" 9 : 10 : /* fd_url_t holds a bunch of pointers into an URL string. */ 11 : 12 : struct fd_url { 13 : char const * scheme; 14 : ulong scheme_len; 15 : 16 : char const * host; 17 : ulong host_len; /* <=255 */ 18 : 19 : char const * port; 20 : ulong port_len; 21 : 22 : char const * tail; /* path, query, fragment */ 23 : ulong tail_len; 24 : }; 25 : 26 : typedef struct fd_url fd_url_t; 27 : 28 0 : #define FD_URL_SUCCESS 0 29 0 : #define FD_URL_ERR_SCHEME 1 30 0 : #define FD_URL_ERR_HOST_OVERSZ 2 31 0 : #define FD_URL_ERR_USERINFO 3 32 : 33 : FD_PROTOTYPES_BEGIN 34 : 35 : /* fd_url_parse_cstr is a basic URL parser. It is not RFC compliant. 36 : 37 : Non-exhaustive list of what this function cannot do: 38 : - Schemes other than http and https are not supported 39 : - userinfo (e.g. 'user:pass@') is not supported 40 : - Anything after the authority is ignored 41 : 42 : If opt_err!=NULL, on return *opt_err holds an FD_URL_ERR_{...} code. */ 43 : 44 : fd_url_t * 45 : fd_url_parse_cstr( fd_url_t * url, 46 : char const * url_str, 47 : ulong url_str_len, 48 : int * opt_err ); 49 : 50 : /* Shared validator/runtime URL gate. 51 : Accepts a http(s):// URL, fills fd_url_t `url` parameter. 52 : - Only `http://` and `https://` schemes are permitted. Anything 53 : else (including missing schemes or stray slashes) is rejected. 54 : The `context` string is echoed in the log so operators know which 55 : knob supplied the bad value. 56 : - If the URL omits an explicit port we default to 443/80 and then flip 57 : `is_ssl` based on the scheme so downstream sockets know whether 58 : to open TLS. 59 : - Host names larger than 255 bytes are rejected 60 : The function does not enforce the host being non-empty; that is left to 61 : the caller because some control paths treat an empty host differently 62 : (e.g. surfacing a custom error message). 63 : Returns 0 on success, -1 on failure (and logs a warning). */ 64 : 65 : int 66 : fd_url_parse_endpoint( fd_url_t * url, 67 : char const * url_str, 68 : ulong url_str_len, 69 : ushort * tcp_port, 70 : _Bool * is_ssl, 71 : char const * context ); 72 : 73 : /* fd_url_unescape undoes % escapes in-place. Returns the unescaped 74 : length on success, or 0 on failure (invalid hex digit or truncated 75 : percent encoding). */ 76 : 77 : ulong 78 : fd_url_unescape( char * msg, 79 : ulong len ); 80 : 81 : FD_PROTOTYPES_END 82 : 83 : #endif /* HEADER_fd_src_waltz_http_fd_url_h */