libcoap 4.3.5-develop-19cef11
coap_uri.c File Reference

URI handling functions. More...

#include "coap3/coap_libcoap_build.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+ Include dependency graph for coap_uri.c:

Go to the source code of this file.

Data Structures

struct  cnt_str
 

Macros

#define hexchar_to_dec(c)
 Calculates decimal value from hexadecimal ASCII character given in c. More...
 
#define min(a, b)   ((a) < (b) ? (a) : (b))
 
#define URI_DATA(uriobj)   ((unsigned char *)(uriobj) + sizeof(coap_uri_t))
 

Typedefs

typedef enum coap_uri_check_t coap_uri_check_t
 
typedef void(* segment_handler_t) (const uint8_t *, size_t, void *)
 

Enumerations

enum  coap_uri_check_t { COAP_URI_CHECK_URI , COAP_URI_CHECK_PROXY }
 

Functions

COAP_STATIC_INLINE const uint8_t * strnchr (const uint8_t *s, size_t len, unsigned char c)
 A length-safe version of strchr(). More...
 
static int coap_split_uri_sub (const uint8_t *str_var, size_t len, coap_uri_t *uri, coap_uri_check_t check_proxy)
 
int coap_split_uri (const uint8_t *str_var, size_t len, coap_uri_t *uri)
 Parses a given string into URI components. More...
 
int coap_split_proxy_uri (const uint8_t *str_var, size_t len, coap_uri_t *uri)
 Parses a given string into URI components. More...
 
static void coap_replace_upper_lower (coap_optlist_t *optlist)
 
int coap_uri_into_options (const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt, uint8_t *_buf COAP_UNUSED, size_t buflen COAP_UNUSED)
 
int coap_uri_into_optlist (const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt)
 Takes a coap_uri_t and then adds CoAP options into the optlist_chain. More...
 
int coap_host_is_unix_domain (const coap_str_const_t *host)
 Determines from the host whether this is a Unix Domain socket request. More...
 
static void decode_segment (const uint8_t *seg, size_t length, unsigned char *buf)
 Decodes percent-encoded characters while copying the string seg of size length to buf. More...
 
static int check_segment (const uint8_t *s, size_t length, size_t *segment_size)
 Runs through the given path (or query) segment and checks if percent-encodings are correct. More...
 
static int make_decoded_option (const uint8_t *s, size_t length, unsigned char *buf, size_t buflen, size_t *optionsize)
 Writes a coap option from given string s to buf. More...
 
static int dots (const uint8_t *s, size_t len)
 Checks if path segment s consists of one or two dots. More...
 
static void backup_segment (void *data)
 
static size_t coap_split_path_impl (const uint8_t *path, size_t len, segment_handler_t h, void *data)
 Splits the given string into segments. More...
 
static void write_option (const uint8_t *s, size_t len, void *data)
 
int coap_split_path (const uint8_t *s, size_t length, unsigned char *buf, size_t *buflen)
 Splits the given URI path into segments. More...
 
void coap_replace_percents (coap_optlist_t *optlist)
 replace any % hex definitions with the actual character. More...
 
static void backup_optlist (coap_optlist_t **optlist_begin)
 
int coap_path_into_optlist (const uint8_t *s, size_t length, coap_option_num_t optnum, coap_optlist_t **optlist_chain)
 Splits the given URI path into '/' separate segments, and then adds the Uri-Path / Location-Path option for each segment to the optlist_chain. More...
 
int coap_split_query (const uint8_t *s, size_t length, unsigned char *buf, size_t *buflen)
 Splits the given URI query into segments. More...
 
int coap_query_into_optlist (const uint8_t *s, size_t length, coap_option_num_t optnum, coap_optlist_t **optlist_chain)
 Splits the given URI query into '&' separate segments, and then adds the Uri-Query / Location-Query option for each segment to the optlist_chain. More...
 
coap_uri_tcoap_new_uri (const uint8_t *uri, unsigned int length)
 Creates a new coap_uri_t object from the specified URI. More...
 
coap_uri_tcoap_clone_uri (const coap_uri_t *uri)
 Clones the specified coap_uri_t object. More...
 
void coap_delete_uri (coap_uri_t *uri)
 Removes the specified coap_uri_t object. More...
 
static int is_unescaped_in_path (const uint8_t c)
 
static int is_unescaped_in_query (const uint8_t c)
 
coap_string_tcoap_get_query (const coap_pdu_t *request)
 Extract query string from request PDU according to escape rules in 6.5.8. More...
 
coap_string_tcoap_get_uri_path (const coap_pdu_t *request)
 Extract uri_path string from request PDU. More...
 

Variables

coap_uri_info_t coap_uri_scheme [COAP_URI_SCHEME_LAST]
 

Detailed Description

URI handling functions.

Definition in file coap_uri.c.

Macro Definition Documentation

◆ hexchar_to_dec

#define hexchar_to_dec (   c)

Calculates decimal value from hexadecimal ASCII character given in c.

The caller must ensure that c actually represents a valid heaxdecimal character, e.g. with isxdigit(3).

Definition at line 402 of file coap_uri.c.

◆ min

#define min (   a,
 
)    ((a) < (b) ? (a) : (b))

Definition at line 524 of file coap_uri.c.

◆ URI_DATA

#define URI_DATA (   uriobj)    ((unsigned char *)(uriobj) + sizeof(coap_uri_t))

Definition at line 849 of file coap_uri.c.

Typedef Documentation

◆ coap_uri_check_t

◆ segment_handler_t

typedef void(* segment_handler_t) (const uint8_t *, size_t, void *)

Definition at line 527 of file coap_uri.c.

Enumeration Type Documentation

◆ coap_uri_check_t

Enumerator
COAP_URI_CHECK_URI 
COAP_URI_CHECK_PROXY 

Definition at line 46 of file coap_uri.c.

Function Documentation

◆ backup_optlist()

static void backup_optlist ( coap_optlist_t **  optlist_begin)
static

Definition at line 710 of file coap_uri.c.

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

◆ backup_segment()

static void backup_segment ( void *  data)
static

Definition at line 582 of file coap_uri.c.

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

◆ check_segment()

static int check_segment ( const uint8_t *  s,
size_t  length,
size_t *  segment_size 
)
static

Runs through the given path (or query) segment and checks if percent-encodings are correct.

This function returns 0 on success and -1 on error.

Definition at line 441 of file coap_uri.c.

+ Here is the caller graph for this function:

◆ coap_clone_uri()

coap_uri_t * coap_clone_uri ( const coap_uri_t uri)

Clones the specified coap_uri_t object.

This function allocates sufficient memory to hold the coap_uri_t structure and its contents. The object should be released with delete_uri().

Parameters
uriThe coap_uri_t structure to copy.
Returns
New URI object or NULL on error.

Definition at line 873 of file coap_uri.c.

+ Here is the call graph for this function:

◆ coap_delete_uri()

void coap_delete_uri ( coap_uri_t uri)

Removes the specified coap_uri_t object.

Parameters
uriThe coap_uri_t structure to remove.

Definition at line 915 of file coap_uri.c.

+ Here is the call graph for this function:

◆ coap_host_is_unix_domain()

int coap_host_is_unix_domain ( const coap_str_const_t host)

Determines from the host whether this is a Unix Domain socket request.

Parameters
hostThe host object.
Returns
0 on failure, or 1 on success.

Definition at line 384 of file coap_uri.c.

+ Here is the caller graph for this function:

◆ coap_new_uri()

coap_uri_t * coap_new_uri ( const uint8_t *  uri,
unsigned int  length 
)

Creates a new coap_uri_t object from the specified URI.

Returns the new object or NULL on error. The memory allocated by the new coap_uri_t should be released using coap_delete_uri().

Parameters
uriThe URI path to copy.
lengthThe length of uri.
Returns
New URI object or NULL on error.

Definition at line 852 of file coap_uri.c.

+ Here is the call graph for this function:

◆ coap_replace_upper_lower()

static void coap_replace_upper_lower ( coap_optlist_t optlist)
static

Definition at line 281 of file coap_uri.c.

+ Here is the caller graph for this function:

◆ coap_split_path_impl()

static size_t coap_split_path_impl ( const uint8_t *  path,
size_t  len,
segment_handler_t  h,
void *  data 
)
static

Splits the given string into segments.

You should call one of the functions coap_split_path() or coap_split_query() instead.

Parameters
pathThe URI path string to be tokenized.
lenThe length of path.
hA handler that is called with every token.
dataOpaque data that is passed to h when called.
Returns
The number of characters that have been parsed from s.

Definition at line 611 of file coap_uri.c.

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

◆ coap_split_uri_sub()

static int coap_split_uri_sub ( const uint8_t *  str_var,
size_t  len,
coap_uri_t uri,
coap_uri_check_t  check_proxy 
)
static

Definition at line 63 of file coap_uri.c.

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

◆ coap_uri_into_options()

int coap_uri_into_options ( const coap_uri_t uri,
const coap_address_t dst,
coap_optlist_t **  optlist_chain,
int  create_port_host_opt,
uint8_t *_buf  COAP_UNUSED,
size_t buflen  COAP_UNUSED 
)

Definition at line 292 of file coap_uri.c.

+ Here is the call graph for this function:

◆ decode_segment()

static void decode_segment ( const uint8_t *  seg,
size_t  length,
unsigned char *  buf 
)
static

Decodes percent-encoded characters while copying the string seg of size length to buf.

The caller of this function must ensure that the percent-encodings are correct (i.e. the character '' is always followed by two hex digits. and that buf provides sufficient space to hold the result. This function is supposed to be called by make_decoded_option() only.

Parameters
segThe segment to decode and copy.
lengthLength of seg.
bufThe result buffer.

Definition at line 417 of file coap_uri.c.

+ Here is the caller graph for this function:

◆ dots()

static int dots ( const uint8_t *  s,
size_t  len 
)
static

Checks if path segment s consists of one or two dots.

returns 1 if . , 2 if .. else 0.

Definition at line 535 of file coap_uri.c.

+ Here is the caller graph for this function:

◆ is_unescaped_in_path()

static int is_unescaped_in_path ( const uint8_t  c)
static

Definition at line 920 of file coap_uri.c.

+ Here is the caller graph for this function:

◆ is_unescaped_in_query()

static int is_unescaped_in_query ( const uint8_t  c)
static

Definition at line 929 of file coap_uri.c.

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

◆ make_decoded_option()

static int make_decoded_option ( const uint8_t *  s,
size_t  length,
unsigned char *  buf,
size_t  buflen,
size_t *  optionsize 
)
static

Writes a coap option from given string s to buf.

s should point to a (percent-encoded) path or query segment of a coap_uri_t object. The created option will have type 0, and the length parameter will be set according to the size of the decoded string. On success, this function returns 0 and sets optionsize to the option's size. On error the function returns a value less than zero. This function must be called from coap_split_path_impl() only.

Parameters
sThe string to decode.
lengthThe size of the percent-encoded string s.
bufThe buffer to store the new coap option.
buflenThe maximum size of buf.
optionsizeThe option's size.
Returns
0 on success and -1 on error.

Definition at line 484 of file coap_uri.c.

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

◆ strnchr()

COAP_STATIC_INLINE const uint8_t * strnchr ( const uint8_t *  s,
size_t  len,
unsigned char  c 
)

A length-safe version of strchr().

This function returns a pointer to the first occurrence of c in s, or NULL if not found.

Parameters
sThe string to search for c.
lenThe length of s.
cThe character to search.
Returns
A pointer to the first occurence of c, or NULL if not found.

Definition at line 39 of file coap_uri.c.

+ Here is the caller graph for this function:

◆ write_option()

static void write_option ( const uint8_t *  s,
size_t  len,
void *  data 
)
static

Definition at line 665 of file coap_uri.c.

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