5.8.1 URI¶
-
struct SerdURIView¶
A parsed view of a URI.
This representation is designed for fast streaming. It makes it possible to create relative URI references or resolve them into absolute URIs in-place without any string allocation.
Each component refers to slices in other strings, so a URI view must outlive any strings it was parsed from. Note that the components are not necessarily null-terminated.
The scheme, authority, path, query, and fragment simply point to the string value of those components, not including any delimiters. The path_prefix is a special component for storing relative or resolved paths. If it points to a string (usually a base URI the URI was resolved against), then this string is prepended to the path. Otherwise, the length is interpreted as the number of up-references (“../”) that must be prepended to the path.
-
SerdStringView scheme¶
Scheme.
-
SerdStringView authority¶
Authority.
-
SerdStringView path_prefix¶
Path prefix for relative/resolved paths.
-
SerdStringView path¶
Path suffix.
-
SerdStringView query¶
Query.
-
SerdStringView fragment¶
Fragment.
-
SerdStringView scheme¶
-
bool serd_uri_string_has_scheme(const char *string)¶
Return true iff
stringstarts with a valid URI scheme.
-
SerdURIView serd_parse_uri(const char *string)¶
Parse
stringand return a URI view that points into it.
-
char *serd_parse_file_uri(SerdAllocator *allocator, const char *uri, char **hostname)¶
Get the unescaped path and hostname from a file URI.
The returned path and
*hostnamemust be freed withserd_free().- Parameters
allocator – Allocator for the returned string.
uri – A file URI.
hostname – If non-NULL, set to the hostname, if present.
- Returns
A newly allocated path string that must be freed with
serd_free().
-
SerdURIView serd_resolve_uri(SerdURIView r, SerdURIView base)¶
Return reference
rresolved againstbase.This will make
ran absolute URI if possible.- Parameters
r – URI reference to make absolute, for example “child/path”.
base – Base URI, for example “http://example.org/base/”.
- Returns
An absolute URI, for example “http://example.org/base/child/path”, or
rif it is not a URI reference that can be resolved againstbase.
-
SerdURIView serd_relative_uri(SerdURIView r, SerdURIView base)¶
Return
ras a reference relative tobaseif possible.- Parameters
r – URI to make relative, for example “http://example.org/base/child/path”.
base – Base URI, for example “http://example.org/base”.
- Returns
A relative URI reference, for example “child/path”,
rif it can not be made relative tobase, or a null URI ifrcould be made relative to base, but the path prefix is already being used (most likely becauserwas previously a relative URI reference that was resolved against some base).
-
bool serd_uri_is_within(SerdURIView r, SerdURIView base)¶
Return whether
rcan be written as a reference relative tobase.For example, with
base“http://example.org/base/”, this returns true ifris also “http://example.org/base/”, or something like “http://example.org/base/child” (“child”) “http://example.org/base/child/grandchild#fragment” (“child/grandchild#fragment”), “http://example.org/base/child/grandchild?query” (“child/grandchild?query”), and so on.- Returns
True if
randbaseare equal or ifris a child ofbase.
-
size_t serd_uri_string_length(SerdURIView uri)¶
Return the length of
urias a string.This can be used to get the expected number of bytes that will be written by
serd_write_uri().- Returns
A string length in bytes, not including the null terminator.
-
size_t serd_write_uri(SerdURIView uri, SerdWriteFunc sink, void *stream)¶
Write
urias a string tosink.This will call
sinkseveral times to emit the URI.- Parameters
uri – URI to write as a string.
sink – Sink to write string output to.
stream – Opaque user argument to pass to
sink.
- Returns
The length of the written URI string (not including a null terminator), which may be less than
serd_uri_string_length(uri)on error.
-
size_t serd_write_file_uri(SerdStringView path, SerdStringView hostname, SerdWriteFunc sink, void *stream)¶
Write a file URI to
sinkfrom a path and optional hostname.Backslashes in Windows paths will be converted, and other characters will be percent encoded as necessary.
If
pathis relative,hostnameis ignored.- Parameters
path – File system path.
hostname – Optional hostname.
sink – Sink to write string output to.
stream – Opaque user argument to pass to
sink.
- Returns
The length of the written URI string (not including a null terminator).