UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
uri.cpp File Reference

Modify and parse URIs. More...

#include <upnp.hpp>
#include <uri.hpp>
#include <UPnPsdk/port_sock.hpp>
#include <umock/netdb.hpp>
+ Include dependency graph for uri.cpp:

Go to the source code of this file.

Functions

int replace_escaped (char *in, size_t index, size_t *max)
 Replaces one single escaped character within a string with its unescaped version.
 
int copy_URL_list (URL_list *in, URL_list *out)
 Copies one URL_list into another.
 
void free_URL_list (URL_list *list)
 Frees the memory associated with a URL_list.
 
void print_uri (uri_type *in)
 Function useful in debugging for printing a parsed uri.
 
void print_token (token *in)
 Function useful in debugging for printing a token.
 
int token_string_casecmp (token *in1, const char *in2)
 Compares buffer in the token object with the buffer in in2 case insensitive.
 
int token_cmp (token *in1, token *in2)
 Compares two tokens.
 
int remove_escaped_chars (char *in, size_t *size)
 Removes http escaped characters such as: "%20" and replaces them with their character representation.
 
int remove_dots (char *buf, size_t size)
 Removes ".", and ".." from a path.
 
char * resolve_rel_url (char *base_url, char *rel_url)
 Resolves a relative url with a base url.
 
int parse_uri (const char *in, size_t max, uri_type *out)
 Parses a uri as defined in RFC 2396 (explaining URIs).
 
Scope restricted to file
int anonymous_namespace{uri.cpp}::is_reserved (const unsigned char in)
 Check for a RESERVED character.
 
int anonymous_namespace{uri.cpp}::is_mark (const unsigned char in)
 Check for a MARK character.
 
int anonymous_namespace{uri.cpp}::is_unreserved (const unsigned char in)
 Check for an UNRESERVED character.
 
int anonymous_namespace{uri.cpp}::is_escaped (const unsigned char *in)
 Check that a char[3] sequence is ESCAPED.
 
size_t anonymous_namespace{uri.cpp}::parse_uric (const char *in, size_t max, token *out)
 Parses a string of uric characters starting at in[0].
 
void anonymous_namespace{uri.cpp}::copy_token (const token *in, const char *in_base, token *out, char *out_base)
 Copy the offset and size from a token to another token.
 
int anonymous_namespace{uri.cpp}::parse_hostport (const char *in, unsigned short int defaultPort, hostport_type *out)
 Parses a string with host and port and fills a hostport structure.
 
size_t anonymous_namespace{uri.cpp}::parse_scheme (const char *in, size_t max, token *out)
 parses a uri scheme starting at in[0].
 
int anonymous_namespace{uri.cpp}::is_end_path (char c)
 Check if end of a path.
 

Variables

UPnPsdk_EXTERN unsigned gIF_INDEX
 Contains network interface index of the link local address gIF_IPV6 that is used as its scope_id.
 

Detailed Description

Modify and parse URIs.

Definition in file uri.cpp.

Function Documentation

◆ replace_escaped()

int replace_escaped ( char *  in,
size_t  index,
size_t *  max 
)

Replaces one single escaped character within a string with its unescaped version.

This is spezified in RFC 2396 (explaining URIs). The index must exactly point to the '%' character, otherwise the function will return unsuccessful. Size of array is NOT checked (MUST be checked by caller).

Note
This function modifies the string and the max size. If the sequence is an escaped sequence it is replaced, the other characters in the string are shifted over, and NULL characters are placed at the end of the string.
Returns
1 - if an escaped character was converted
0 - otherwise
Parameters
[in,out]inString of characters.
[in]indexIndex at which to start checking the characters; must point to ''.
[in,out]maxMaximal size of the string buffer will be reduced by 2 if a character is converted.

Definition at line 396 of file uri.cpp.

+ Here is the caller graph for this function:

◆ copy_URL_list()

int copy_URL_list ( URL_list in,
URL_list out 
)

Copies one URL_list into another.

This includes dynamically allocating the out->URLs field (the full string), and the structures used to hold the parsedURLs. This memory MUST be freed by the caller through: free_URL_list(&out).

Returns
On success: HTTP_SUCCESS
On error: UPNP_E_OUTOF_MEMORY - On Failure to allocate memory.
Parameters
[in]inSource URL list.
[out]outDestination URL list.

Definition at line 431 of file uri.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_URL_list()

void free_URL_list ( URL_list list)

Frees the memory associated with a URL_list.

Frees the dynamically allocated members of of list. Does NOT free the pointer to the list itself ( i.e. does NOT free(list)).

Parameters
[in]listURL list object.

Definition at line 480 of file uri.cpp.

+ Here is the caller graph for this function:

◆ print_uri()

void print_uri ( uri_type in)

Function useful in debugging for printing a parsed uri.

This is only available when compiled with DEBUG enabled.

Parameters
[in]inURI object to print.

Definition at line 492 of file uri.cpp.

+ Here is the call graph for this function:

◆ print_token()

void print_token ( token in)

Function useful in debugging for printing a token.

This is only available when compiled with DEBUG enabled.

Parameters
[in]inToken object to print.

Definition at line 499 of file uri.cpp.

+ Here is the caller graph for this function:

◆ token_string_casecmp()

int token_string_casecmp ( token in1,
const char *  in2 
)

Compares buffer in the token object with the buffer in in2 case insensitive.

Returns
  • < 0, if string1 is less than string2.
  • == 0, if string1 is identical to string2 .
  • > 0, if string1 is greater than string2.
Parameters
[in]in1Token object whose buffer is to be compared.
[in]in2String of characters to compare with.

Definition at line 510 of file uri.cpp.

+ Here is the caller graph for this function:

◆ token_cmp()

int token_cmp ( token in1,
token in2 
)

Compares two tokens.

Returns
  • < 0, if string1 is less than string2.
  • == 0, if string1 is identical to string2 .
  • > 0, if string1 is greater than string2.
Parameters
[in]in1First token object whose buffer is to be compared.
[in]in2Second token object used for the comparison.

Definition at line 518 of file uri.cpp.

+ Here is the caller graph for this function:

◆ remove_escaped_chars()

int remove_escaped_chars ( char *  in,
size_t *  size 
)

Removes http escaped characters such as: "%20" and replaces them with their character representation.

For example: "hello%20foo" -> "hello foo". The input IS MODIFIED in place (shortened). Extra characters are replaced with NULL.

Returns
UPNP_E_SUCCESS.
Todo:
Optimize with prechecking the delimiter '%'.
Parameters
[in,out]inString of characters to be modified.
[in,out]sizeSize limit for the number of characters.

Definition at line 526 of file uri.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove_dots()

int remove_dots ( char *  buf,
size_t  size 
)

Removes ".", and ".." from a path.

If a ".." can not be resolved (i.e. the .. would go past the root of the path) an error is returned. The input IS modified in place. This function directly implements the "Remove Dot Segments" algorithm described in RFC 3986 section 5.2.4.

Examples:
 uchar path[30]="/../hello";
 uremove_dots(path, strlen(path)) -> UPNP_E_INVALID_URL
 uchar path[30]="/./hello";
 uremove_dots(path, strlen(path)) -> UPNP_E_SUCCESS,
 uin = "/hello"
 uchar path[30]="/./hello/foo/../goodbye" ->
 uUPNP_E_SUCCESS, in = "/hello/goodbye"
Returns
On success: UPNP_E_SUCCESS
On error:
  • UPNP_E_OUTOF_MEMORY - On failure to allocate memory.
  • UPNP_E_INVALID_URL - Failure to resolve URL.
Parameters
[in]bufString of characters from which "dots" have to be removed.
[in]sizeSize limit for the number of characters.

Definition at line 538 of file uri.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resolve_rel_url()

char * resolve_rel_url ( char *  base_url,
char *  rel_url 
)

Resolves a relative url with a base url.

  • If the base_url is a nullptr, then a copy of the rel_url is passed back.
  • If the rel_url is absolute then a copy of the rel_url is passed back.
  • If neither the base nor the rel_url are absolute then a nullptr is returned.
  • Otherwise it tries and resolves the relative url with the base as described in RFC 2396 (explaining URIs).

The resolution of '..' is NOT implemented, but '.' is resolved.

Returns
Pointer to a new with malloc dynamically allocated full URL or a nullptr.
Parameters
[in]base_urlBase URL.
[in]rel_urlRelative URL.

Definition at line 605 of file uri.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_uri()

int parse_uri ( const char *  in,
size_t  max,
uri_type out 
)

Parses a uri as defined in RFC 2396 (explaining URIs).

Handles absolute, relative, and opaque uris. Parses into the following pieces: scheme, hostport, pathquery, fragment (host with port and path with query are treated as one token). Strings in output uri_type are treated as token with character chain and size. They are not null ('\0') terminated.

Caller should check for the pieces they require.

Returns
On success: HTTP_SUCCESS
On error: UPNP_E_INVALID_URL
Parameters
[in]inCharacter string containing uri information to be parsed.
[in]maxNumber of characters (strlen()) of the input string.
[out]outOutput parameter which will have the parsed uri information.

Definition at line 733 of file uri.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ gIF_INDEX

UPnPsdk_EXTERN unsigned gIF_INDEX

Contains network interface index of the link local address gIF_IPV6 that is used as its scope_id.

Definition at line 50 of file uri.cpp.