BSB Developer Docs
Loading...
Searching...
No Matches
url.h File Reference

URL parsing library. More...

#include <stdbool.h>
#include "slice.h"

Go to the source code of this file.

Typedefs

typedef struct Url Url
 Url opaque type.
 

Enumerations

enum  UrlPartId {
  UrlPartIdHref , UrlPartIdRequest , UrlPartIdOrigin , UrlPartIdProtocol ,
  UrlPartIdHost , UrlPartIdHostname , UrlPartIdPort , UrlPartIdPathname ,
  UrlPartIdSearch , UrlPartIdHash , UrlPartIdMax
}
 Enumeration of available URL part identifiers. More...
 

Functions

Url * url_alloc (void)
 Create an URL instance.
 
void url_free (Url *instance)
 Delete an URL instance.
 
bool url_parse (Url *instance, const char *source)
 Parse and encode an URL from a string.
 
const StringSlice * url_get_part (const Url *instance, UrlPartId part_id)
 Get a URL part via its identifier.
 

Detailed Description

URL parsing library.

Enumeration Type Documentation

◆ UrlPartId

enum UrlPartId

Enumeration of available URL part identifiers.

Enumerator
UrlPartIdHref 

Full URL.

UrlPartIdRequest 

Full URL, without hash (if present)

UrlPartIdOrigin 

Protocol + Hostname + Port (if specified)

UrlPartIdProtocol 

Protocol string.

UrlPartIdHost 

Hostname + Port (if specified)

UrlPartIdHostname 

Hostname or IP address.

UrlPartIdPort 

Port string (if specified)

UrlPartIdPathname 

Request path (if specified)

UrlPartIdSearch 

Request parameters (if specified)

UrlPartIdHash 

Fragment identifier (if specified)

UrlPartIdMax 

Special value, internal use.

Function Documentation

◆ url_alloc()

Url * url_alloc ( void )

Create an URL instance.

Returns
pointer to the created instance

◆ url_free()

void url_free ( Url * instance)

Delete an URL instance.

All part pointers obtained via url_get_part will become INVALID after this call.

Parameters
[in,out]instancepointer to the instance to be deleted

◆ url_get_part()

const StringSlice * url_get_part ( const Url * instance,
UrlPartId part_id )

Get a URL part via its identifier.

The return value is guaranteed to be non-NULL and valid until url_free is called.

If a part is not present, the returned slice will represent an empty string.

Parameters
[in]instancepointer to the instance to be queried
[in]part_idnumeric identifier of the part in question
Returns
pointer to the string slice representing the requested part

◆ url_parse()

bool url_parse ( Url * instance,
const char * source )

Parse and encode an URL from a string.

The following URL parts are required:

  • Protocol (e.g. "http://")
  • Hostname (e.g. "10.0.4.20" or "my.awesome.site")

All part pointers obtained via url_get_part will remain VALID after this call, but their underlying values might silently change.

Percent encoding will be performed where applicable. Non-ASCII hostnames are NOT supported. If a Unicode hostname is needed, it must be supplied in the punycode-encoded form.

Parameters
[in,out]instancepointer to the instance to contain the parsed data
[in]sourcepointer to a zero-terminated URL string
Returns
true if the URL could be parsed, false otherwise