Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

URL Parsing

Parse URLs into scheme, host, port, path, and query components. Used by TCP/UDP gclasses to decode listen/connect URLs.

Source code:

get_url_schema()

Extracts the schema (protocol) from a given URL and stores it in the provided buffer.

int get_url_schema(
    hgobj gobj,
    const char *uri,
    char *schema,    size_t schema_size
);

Parameters

KeyTypeDescription
gobjhgobjA handle to the GObj instance, used for logging errors.
uriconst char *The URL string from which the schema will be extracted.
schemachar *A buffer where the extracted schema will be stored.
schema_sizesize_tThe size of the schema buffer to prevent overflow.

Returns

Returns 0 on success if the schema is extracted successfully, or -1 if no schema is found or an error occurs.

Notes

Uses http_parser_parse_url() to parse the URL. If no schema is found, an error is logged using gobj_log_error().


parse_url()

Parses a given URL into its components, including schema, host, port, path, and query. Supports optional schema parsing.

int parse_url(
    hgobj gobj,
    const char *uri,
    char *schema, size_t schema_size,
    char *host, size_t host_size,
    char *port, size_t port_size,
    char *path, size_t path_size,
    char *query, size_t query_size,
    BOOL no_schema
);

Parameters

KeyTypeDescription
gobjhgobjHandle to the calling object, used for logging errors.
uriconst char *The URL string to be parsed.
schemachar *Buffer to store the extracted schema (e.g., ‘http’).
schema_sizesize_tSize of the schema buffer.
hostchar *Buffer to store the extracted host (e.g., ‘example.com’).
host_sizesize_tSize of the host buffer.
portchar *Buffer to store the extracted port (e.g., ‘8080’).
port_sizesize_tSize of the port buffer.
pathchar *Buffer to store the extracted path (e.g., ‘/index.html’).
path_sizesize_tSize of the path buffer.
querychar *Buffer to store the extracted query string (e.g., ‘id=123’).
query_sizesize_tSize of the query buffer.
no_schemaBOOLIf TRUE, the function does not parse the schema.

Returns

Returns 0 on success, or -1 if the URL cannot be parsed.

Notes

Uses http_parser_parse_url() to extract URL components. If no_schema is TRUE, the schema is ignored.