Parse URLs much like Node's url module.
#include <url.h>
#include <url.h>
int
main (void) {
url_data_t *parsed = url_parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash");
url_free(parsed);
}
The url_data_t *
holds the parsed values and can be written to stdout
with url_inspect(parsed);
url_inspect(parsed);
output
#url =>
.href: "http://user:pass@subdomain.host.com:8080/p/a/t/h?query=string#hash"
.protocol: "http"
.host: "subdomain.host.com"
.auth: "user:pass"
.hostname: "subdomain.host.com:8080"
.pathname: "/p/a/t/h"
.search: "?query=string"
.path: "/p/a/t/h?query=string#hash"
.hash: "#hash"
.query: "query=string"
.port: "8080"
Parses a url into parts and returns a url_data_t *
pointer`.
url_data_t *
url_parse (char *url);
example
url_data_t *parsed = url_parse("http://google.com");
Returns the protocol part of a url.
char *
url_get_protocol (char *url);
example
char *protocol = url_get_protocol("https://github.com"); // https
Returns the auth part of a url.
char *
url_get_auth (char *url);
example
char *auth = url_get_auth("sftp://user:password@domain.com:5000/path/to/directory"); // user:password
Returns the hostname part of a url.
char *
url_get_hostname (char *url);
example
char *hostname = url_get_hostname("http://john@website.host.com:3000/index.html"); // website.host.com:3000
Returns the host part of a url.
char *
url_get_host (char *url);
example
char *host = url_get_host("https://cache.us.site.com:4444/pixel.gif"); // cache.us.site.com
Returns the pathname part of a url.
char *
url_get_pathname (char *url);
example
char *pathname = url_get_pathname("http://github.com/jwerle/url.h"); // /jwerle/url.h
Returns the path part of a url.
char *
url_get_path (char *url);
example
char *path = url_get_path("https://socialnetwork.com/login?user=jwerle"); // /login?user=jwerle
Returns the search part of a url.
char *
url_get_search (char *url);
example
char *search = url_get_search("https://www.google.com/search?q=hack"); // ?q=hack
char *
url_get_query (char *url);
example
char *query = url_get_query("http://site.com/home?ref=stream&id=12345"); // ref=stream&id=12345
Returns the hash part of a url.
char *
url_get_hash (char *url);
example
char *hash = url_get_hash("http://single-page-app.com/#page=home&id=12345"); // #page=home&id=12345
Returns the port part of a url.
char *
url_get_port (char *url);
example
char *port = url_get_port("https://site.com:9000"); // 9000
Frees a url_data_t
pointer.
void
url_free (url_data_t *data);
example
url_free(parsed);
Checks if given input is a valid url protocol.
bool
url_is_protocol (char *str);
example
assert(url_is_protocol("https"));
assert(! url_is_protocol("foo"));
Parses and prints all parts of a url.
void
url_inspect (char *url);
example
url_inspect("https://google.com/search?q=github");
output
#url =>
.href: "https://google.com/search?q=github"
.protocol: "https"
.host: "google.com"
.auth: ""
.hostname: "google.com"
.pathname: "/search"
.search: "?q=github"
.path: "/search?q=github"
.hash: ""
.query: "q=github"
.port: ""
Prints all parts of a parsed url from a url_data_t
pointer.
void
url_data_inspect (url_data_t *data);
example
url_data_inspect(parsed);
output
#url =>
.href: "http://user:pass@subdomain.host.com:8080/p/a/t/h?query=string#hash"
.protocol: "http"
.host: "subdomain.host.com"
.auth: "user:pass"
.hostname: "subdomain.host.com:8080"
.pathname: "/p/a/t/h"
.search: "?query=string"
.path: "/p/a/t/h?query=string#hash"
.hash: "#hash"
.query: "query=string"
.port: "8080"
Checks if a given protocol is an ssh protocol like ssh
or git
bool
url_is_ssh (char *str);
example
bool is_ssh = url_is_ssh(url_get_protocol(url));
MIT