uri.c File Reference

Contains functions for uri, url parsing utility. More...

`#include <assert.h>`
`#include "config.h"`
`#include "upnpapi.h"`
`#include "uri.h"`
`#include "posix_overwrites.h"`
Include dependency graph for uri.c:

## Functions

static int | is_reserved (char in)
| Returns a 1 if a char is a RESERVED char as defined in http://www.ietf.org/rfc/rfc2396.txt RFC explaining URIs).
int | is_mark (char in)
| Returns a 1 if a char is a MARK char as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).
int | is_unreserved (char in)
| Returns a 1 if a char is an UNRESERVED char as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).
int | is_escaped (const char *in)
| Returns a 1 if a char[3] sequence is ESCAPED as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).
int | replace_escaped (char *in, size_t index, size_t *max)
| Replaces an escaped sequence with its unescaped version as in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs)
static size_t | parse_uric (const char *in, size_t max, token *out)
| Parses a string of uric characters starting at in[0] as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).
static void | copy_token (const token *in, const char *in_base, token *out, char *out_base)
| Tokens are generally pointers into other strings. This copies the offset and size from a token (in) relative to one string (in_base) into a token (out) relative to another string (out_base).
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.
int | token_string_casecmp (token *in1, const char *in2)
| Compares buffer in the token object with the buffer in in2.
int | token_cmp (token *in1, token *in2)
| Compares two tokens.
static int | parse_hostport (const char *in, unsigned short int defaultPort, hostport_type *out)
| Parses a string representing a host and port (e.g. "127.127.0.1:80" or "localhost") and fills out a hostport_type struct with internet address and a token representing the full host and port.
static size_t | parse_scheme (const char *in, size_t max, token *out)
| parses a uri scheme starting at in[0] as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).
int | remove_escaped_chars (char *in, size_t *size)
| Removes http escaped characters such as: "%20" and replaces them with their character representation. i.e. "hello%20foo" -> "hello foo".
static UPNP_INLINE int | is_end_path (char c)
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 returning a NEW (dynamically allocated with malloc) full url.
int | parse_uri (const char *in, size_t max, uri_type *out)
| Parses a uri as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).

## Detailed Description

Contains functions for uri, url parsing utility.

## Function Documentation

## copy_token()

static void copy_token | ( | const token * | in,
|  | const char * | in_base,
|  | token * | out,
|  | char * | out_base
| ) |  |

static

Tokens are generally pointers into other strings. This copies the offset and size from a token (in) relative to one string (in_base) into a token (out) relative to another string (out_base).

Parameters

  [in] | in | Source token.
  [in] | in_base | .
  [out] | out | Destination token.
  [in] | out_base | .

Referenced by copy_URL_list().

## 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
  - HTTP_SUCCESS - On Success.
  - UPNP_E_OUTOF_MEMORY - On Failure to allocate memory.

Parameters

  [in] | in | Source URL list.
  [out] | out | Destination URL list.

References copy_token(), HOSTPORT::text, UPNP_E_OUTOF_MEMORY, and URL_LIST::URLs.

Referenced by copy_subscription().

## 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] | list | URL list object.

References URL_LIST::URLs.

Referenced by freeSubscription().

## is_escaped()

int is_escaped | ( | const char * | in | ) |

Returns a 1 if a char[3] sequence is ESCAPED as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).

Size of array is NOT checked (MUST be checked by caller).

Returns
  1 if char is a ESCAPED char.

Parameters

  [in] | in | Char sequence to be matched for ESCAPED characters.

Referenced by parse_uric().

## is_mark()

int is_mark | ( | char | in | ) |

Returns a 1 if a char is a MARK char as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).

Returns
  1 if char is a MARKED char.

Parameters

  [in] | in | Char to be matched for MARKED characters.

Referenced by is_unreserved().

## is_reserved()

static int is_reserved | ( | char | in | ) |

static

Returns a 1 if a char is a RESERVED char as defined in http://www.ietf.org/rfc/rfc2396.txt RFC explaining URIs).

Returns
  1 if char is a RESERVED char.

Parameters

  [in] | in | Char to be matched for RESERVED characters.

References RESERVED.

Referenced by parse_uric().

## is_unreserved()

int is_unreserved | ( | char | in | ) |

Returns a 1 if a char is an UNRESERVED char as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).

Returns
  1 if char is a UNRESERVED char.

Parameters

  [in] | in | Char to be matched for UNRESERVED characters.

References is_mark().

Referenced by parse_uric().

## parse_hostport()

static int parse_hostport | ( | const char * | in,
|  | unsigned short int | defaultPort,
|  | hostport_type * | out
| ) |  |

static

Parses a string representing a host and port (e.g. "127.127.0.1:80" or "localhost") and fills out a hostport_type struct with internet address and a token representing the full host and port.

Parameters

  [in] | in | String of characters representing host and port.
  [out] | defaultPort | Output parameter where the host and port are represented as an internet address.

References gIF_INDEX, HOSTPORT::text, and UPNP_E_INVALID_URL.

Referenced by parse_uri().

## parse_scheme()

static size_t parse_scheme | ( | const char * | in,
|  | size_t | max,
|  | token * | out
| ) |  |

static

parses a uri scheme starting at in[0] as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).

(e.g. "http:" -> scheme= "http").

Note
  String MUST include ':' within the max charcters.

Returns

Parameters

  [in] | in | String of characters representing a scheme.
  [in] | max | Maximum number of characters.
  [out] | out | Output parameter whose buffer is filled in with the scheme.

Referenced by parse_uri().

## parse_uri()

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

Parses a uri as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).

Handles absolute, relative, and opaque uris. Parses into the following pieces: scheme, hostport, pathquery, fragment (path and query are treated as one token)

Caller should check for the pieces they require.

Returns

Parameters

  [in] | in | Character string containing uri information to be parsed.
  [in] | max | Maximum limit on the number of characters.
  [out] | out | Output parameter which will have the parsed uri information.

References parse_hostport(), parse_scheme(), parse_uric(), and token_string_casecmp().

Referenced by create_url_list(), FindServiceControlURLPath(), FindServiceEventURLPath(), http_FixStrUrl(), and resolve_rel_url().

## parse_uric()

static size_t parse_uric | ( | const char * | in,
|  | size_t | max,
|  | token * | out
| ) |  |

static

Parses a string of uric characters starting at in[0] as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).

Returns

Parameters

  [in] | in | String of characters.
  [in] | max | Maximum limit.
  [out] | out | Token object where the string of characters is copied.

References is_escaped(), is_reserved(), and is_unreserved().

Referenced by parse_uri().

## remove_dots()

int remove_dots | ( | char * | in,
|  | 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.)

Note
  Examples char path[30]="/../hello"; remove_dots(path, strlen(path)) -> UPNP_E_INVALID_URL char path[30]="/./hello"; remove_dots(path, strlen(path)) -> UPNP_E_SUCCESS, in = "/hello" char path[30]="/./hello/foo/../goodbye" -> UPNP_E_SUCCESS, in = "/hello/goodbye"

Returns
  - UPNP_E_SUCCESS - On Success.
  - UPNP_E_OUTOF_MEMORY - On failure to allocate memory.
  - UPNP_E_INVALID_URL - Failure to resolve URL.

Parameters

  [in] | buf | String of characters from which "dots" have to be removed.
  [in] | size | Size limit for the number of characters.

References UPNP_E_SUCCESS.

Referenced by process_request(), and resolve_rel_url().

## 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. i.e. "hello%20foo" -> "hello foo".

The input IS MODIFIED in place (shortened). Extra characters are replaced with NULL.

Returns
  UPNP_E_SUCCESS.

Parameters

  [in,out] | in | String of characters to be modified.
  [in,out] | size | Size limit for the number of characters.

References replace_escaped(), and UPNP_E_SUCCESS.

Referenced by process_request().

## replace_escaped()

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

Replaces an escaped sequence with its unescaped version as in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs)

Size of array is NOT checked (MUST be checked by caller)

Note
  This function modifies the string. 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

Parameters

  [in,out] | in | String of characters.
  [in] | index | Index at which to start checking the characters.
  [out] | max | .

Referenced by remove_escaped_chars().

## resolve_rel_url()

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

resolves a relative url with a base url returning a NEW (dynamically allocated with malloc) full url.

If the base_url is NULL, 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 NULL is returned. Otherwise it tries and resolves the relative url with the base as described in http://www.ietf.org/rfc/rfc2396.txt (RFCs explaining URIs).

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

Returns

Parameters

  [in] | base_url | Base URL.
  [in] | rel_url | Relative URL.

References parse_uri(), remove_dots(), HOSTPORT::text, and UPNP_E_SUCCESS.

Referenced by UpnpResolveURL(), and UpnpResolveURL2().

## 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] | in1 | First token object whose buffer is to be compared.
  [in] | in2 | Second token object used for the comparison.

Referenced by FindServiceControlURLPath(), and FindServiceEventURLPath().

## token_string_casecmp()

int token_string_casecmp | ( | token * | in1,
|  | const char * | in2
| ) |  |

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

Returns
  - < 0, if string1 is less than string2.
  - == 0, if string1 is identical to string2 .
  - > 0, if string1 is greater than string2.

Parameters

  [in] | in1 | Token object whose buffer is to be compared.
  [in] | in2 | String of characters to compare with.

Referenced by http_FixUrl(), http_OpenHttpConnection(), and parse_uri().