diff --git a/configure.ac b/configure.ac index ff7e693f..18dcbf03 100644 --- a/configure.ac +++ b/configure.ac @@ -978,6 +978,7 @@ man/coap_resource.txt man/coap_session.txt man/coap_string.txt man/coap_tls_library.txt +man/coap_uri.txt man/coap-client.txt man/coap-server.txt man/coap-rd.txt diff --git a/examples/coap-client.c b/examples/coap-client.c index 534f8f7f..263dd31d 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -705,12 +705,6 @@ cmdline_hop_limit(char *arg) { return 1; } - -static uint16_t -get_default_port(const coap_uri_t *u) { - return coap_uri_scheme_is_secure(u) ? COAPS_DEFAULT_PORT : COAP_DEFAULT_PORT; -} - /** * Sets global URI options according to the URI passed as @p arg. * This function returns 0 on success or -1 on error. @@ -722,18 +716,15 @@ get_default_port(const coap_uri_t *u) { */ static int cmdline_uri(char *arg, int create_uri_opts) { - unsigned char portbuf[2]; #define BUFSIZE 100 - unsigned char _buf[BUFSIZE]; - unsigned char *buf = _buf; - size_t buflen; - int res; + unsigned char buf[BUFSIZE]; if (!proxy_scheme_option && proxy.host.length) { /* create Proxy-Uri from argument */ size_t len = strlen(arg); if (len > 1034) { - coap_log(LOG_ERR, "Absolute URI length must be <= 1034 bytes for a proxy\n"); + coap_log(LOG_ERR, + "Absolute URI length must be <= 1034 bytes for a proxy\n"); return -1; } @@ -748,13 +739,16 @@ cmdline_uri(char *arg, int create_uri_opts) { return -1; } - if (uri.scheme==COAP_URI_SCHEME_COAPS && !reliable && !coap_dtls_is_supported()) { + if (uri.scheme==COAP_URI_SCHEME_COAPS && !reliable && + !coap_dtls_is_supported()) { coap_log(LOG_EMERG, "coaps URI scheme not supported in this version of libcoap\n"); return -1; } - if ((uri.scheme==COAP_URI_SCHEME_COAPS_TCP || (uri.scheme==COAP_URI_SCHEME_COAPS && reliable)) && !coap_tls_is_supported()) { + if ((uri.scheme==COAP_URI_SCHEME_COAPS_TCP || + (uri.scheme==COAP_URI_SCHEME_COAPS && reliable)) && + !coap_tls_is_supported()) { coap_log(LOG_EMERG, "coaps+tcp URI scheme not supported in this version of libcoap\n"); return -1; @@ -767,46 +761,12 @@ cmdline_uri(char *arg, int create_uri_opts) { return -1; } - if (uri.port != get_default_port(&uri) && create_uri_opts) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_PORT, - coap_encode_var_safe(portbuf, sizeof(portbuf), - (uri.port & 0xffff)), - portbuf)); - } - - if (uri.path.length) { - buflen = BUFSIZE; - if (uri.path.length > BUFSIZE) - coap_log(LOG_WARNING, "URI path will be truncated (max buffer %d)\n", BUFSIZE); - res = coap_split_path(uri.path.s, uri.path.length, buf, &buflen); - - while (res--) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_PATH, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } - } - - if (uri.query.length) { - buflen = BUFSIZE; - buf = _buf; - res = coap_split_query(uri.query.s, uri.query.length, buf, &buflen); - - while (res--) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_QUERY, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } + if (coap_uri_into_options(&uri, &optlist, create_uri_opts, + buf, sizeof(buf)) < 0) { + coap_log(LOG_ERR, "Failed to create options for URI\n"); + return -1; } } - return 0; } diff --git a/examples/coap-server.c b/examples/coap-server.c index c13f7958..f35cf09a 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -1021,12 +1021,9 @@ hnd_proxy_uri(coap_resource_t *resource COAP_UNUSED, coap_pdu_t *pdu; coap_optlist_t *optlist = NULL; coap_opt_t *option; - unsigned char portbuf[2]; #define BUFSIZE 100 unsigned char _buf[BUFSIZE]; unsigned char *buf = _buf; - size_t buflen; - int res; coap_bin_const_t token = coap_pdu_get_token(request); memset(&uri, 0, sizeof(uri)); @@ -1128,45 +1125,10 @@ hnd_proxy_uri(coap_resource_t *resource COAP_UNUSED, proxy_uri = NULL; proxy_scheme_option = 0; - if (uri.port != (coap_uri_scheme_is_secure(&uri) ? - COAPS_DEFAULT_PORT : COAP_DEFAULT_PORT)) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_PORT, - coap_encode_var_safe(portbuf, sizeof(portbuf), - (uri.port & 0xffff)), - portbuf)); - } - - if (uri.path.length) { - buflen = BUFSIZE; - if (uri.path.length > BUFSIZE) - coap_log(LOG_WARNING, - "URI path will be truncated (max buffer %d)\n", BUFSIZE); - res = coap_split_path(uri.path.s, uri.path.length, buf, &buflen); - - while (res--) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_PATH, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } - } - - if (uri.query.length) { - buflen = BUFSIZE; - buf = _buf; - res = coap_split_query(uri.query.s, uri.query.length, buf, &buflen); - - while (res--) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_QUERY, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } + if (coap_uri_into_options(&uri, &optlist, 1, + buf, sizeof(buf)) < 0) { + coap_log(LOG_ERR, "Failed to create options for URI\n"); + goto cleanup; } } diff --git a/examples/lwip/client-coap.c b/examples/lwip/client-coap.c index ae2aa4eb..8e8ab75f 100644 --- a/examples/lwip/client-coap.c +++ b/examples/lwip/client-coap.c @@ -122,9 +122,7 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, coap_uri_t uri; char portbuf[8]; #define BUFSIZE 100 - unsigned char _buf[BUFSIZE]; - unsigned char *buf = _buf; - size_t buflen; + unsigned char buf[BUFSIZE]; int res; coap_set_log_level(log_level); @@ -135,10 +133,10 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, LWIP_ASSERT("Unsupported URI type", uri.scheme == COAP_URI_SCHEME_COAP); snprintf(portbuf, sizeof(portbuf), "%d", uri.port); - snprintf((char *)_buf, sizeof(_buf), "%*.*s", (int)uri.host.length, + snprintf((char *)buf, sizeof(buf), "%*.*s", (int)uri.host.length, (int)uri.host.length, (const char *)uri.host.s); /* resolve destination address where server should be sent */ - len = resolve_address((const char*)_buf, portbuf, &dst); + len = resolve_address((const char*)buf, portbuf, &dst); LWIP_ASSERT("Failed to resolve address", len > 0); main_coap_context = coap_new_context(NULL); @@ -162,45 +160,10 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, coap_session_max_pdu_size(session)); LWIP_ASSERT("Failed to create PDU", pdu != NULL); - if (uri.port != COAP_DEFAULT_PORT) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_PORT, - coap_encode_var_safe(_buf, 4, - (uri.port & 0xffff)), - _buf)); - } - if (uri.path.length) { - buflen = BUFSIZE; - if (uri.path.length > BUFSIZE) - coap_log(LOG_WARNING, "URI path will be truncated (max buffer %d)\n", BUFSIZE); - res = coap_split_path(uri.path.s, uri.path.length, buf, &buflen); + len = coap_uri_into_options(&uri, &optlist, 1, buf, sizeof(buf)); + LWIP_ASSERT("Failed to create options", len == 0); - while (res--) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_PATH, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } - } - - if (uri.query.length) { - buflen = BUFSIZE; - buf = _buf; - res = coap_split_query(uri.query.s, uri.query.length, buf, &buflen); - - while (res--) { - coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_URI_QUERY, - coap_opt_length(buf), - coap_opt_value(buf))); - - buf += coap_opt_size(buf); - } - } - - /* Add option list (sorted) to the PDU */ + /* Add option list (which will be sorted) to the PDU */ if (optlist) { res = coap_add_optlist_pdu(pdu, &optlist); LWIP_ASSERT("Failed to add options to PDU", res == 1); diff --git a/include/coap3/uri.h b/include/coap3/uri.h index d97de6f0..fa4bf25c 100644 --- a/include/coap3/uri.h +++ b/include/coap3/uri.h @@ -62,7 +62,7 @@ coap_uri_scheme_is_secure(const coap_uri_t *uri) { /** * 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 - * must be released using coap_free(). + * should be released using coap_delete_uri(). * * @param uri The URI path to copy. * @param length The length of uri. @@ -72,11 +72,23 @@ coap_uri_scheme_is_secure(const coap_uri_t *uri) { coap_uri_t *coap_new_uri(const uint8_t *uri, unsigned int length); /** - * Clones the specified coap_uri_t object. Thie function allocates sufficient - * memory to hold the coap_uri_t structure and its contents. The object must - * be released with coap_free(). */ + * 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(). + * + * @param uri The coap_uri_t structure to copy. + * + * @return New URI object or NULL on error. + */ coap_uri_t *coap_clone_uri(const coap_uri_t *uri); +/** + * Removes the specified coap_uri_t object. + * + * @param uri The coap_uri_t structure to remove. + */ +void coap_delete_uri(coap_uri_t *uri); + /** * @ingroup application_api * @defgroup uri_parse URI Parsing Functions @@ -109,7 +121,7 @@ int coap_split_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri); * port which is set to default port for the protocol. This function returns * @p 0 if parsing succeeded, a value less than zero otherwise. * Note: This function enforces that the given string is in Proxy-Uri format - * as well as supports different schema such as http. + * as well as supports different schema such as http and https. * * @param str_var The string to split up. * @param len The actual length of @p str_var @@ -120,6 +132,26 @@ int coap_split_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri); */ int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri); +/** + * Takes a coap_uri_t and then adds CoAP options into the @p optlist_chain. + * If the port is not the default port and create_port_opt is not 0, then + * the Port option is added in. Any path or query are broken down into the + * individual segment Path or Query options and added to the @p optlist_chain. + * + * @param uri The coap_uri_t object. + * @param optlist_chain Where to store the chain of options. + * @param buf Scratch buffer area (needs to be bigger than + * uri->path.length and uri->query.length) + * @param buflen Size of scratch buffer. + * @param create_port_opt @c 1 if port option to be added (if non-default) + * else @c 0 + * + * @return @c 0 on success, or < 0 on error. + * + */ +int coap_uri_into_options(coap_uri_t *uri, coap_optlist_t **optlist_chain, + int create_port_opt, uint8_t *buf, size_t buflen); + /** * Splits the given URI path into segments. Each segment is preceded * by an option pseudo-header with delta-value 0 and the actual length diff --git a/libcoap-3.map b/libcoap-3.map index a99d1fa1..b899285d 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -68,6 +68,7 @@ global: coap_delete_resource; coap_delete_str_const; coap_delete_string; + coap_delete_uri; coap_dtls_get_log_level; coap_dtls_is_supported; coap_dtls_set_log_level; @@ -247,6 +248,7 @@ global: coap_ticks_to_rt; coap_ticks_to_rt_us; coap_tls_is_supported; + coap_uri_into_options; coap_write_block_b_opt; coap_write_block_opt; local: diff --git a/libcoap-3.sym b/libcoap-3.sym index e8607f83..322a4f1e 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -66,6 +66,7 @@ coap_delete_pdu coap_delete_resource coap_delete_str_const coap_delete_string +coap_delete_uri coap_dtls_get_log_level coap_dtls_is_supported coap_dtls_set_log_level @@ -245,5 +246,6 @@ coap_ticks_from_rt_us coap_ticks_to_rt coap_ticks_to_rt_us coap_tls_is_supported +coap_uri_into_options coap_write_block_b_opt coap_write_block_opt diff --git a/man/Makefile.am b/man/Makefile.am index ce8aeb14..411f81ee 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -38,7 +38,8 @@ TXT3 = coap_async.txt \ coap_resource.txt \ coap_session.txt \ coap_string.txt \ - coap_tls_library.txt + coap_tls_library.txt \ + coap_uri.txt MAN3 = $(TXT3:%.txt=%.3) diff --git a/man/coap_pdu_setup.txt.in b/man/coap_pdu_setup.txt.in index a18b5cd8..33aa2e26 100644 --- a/man/coap_pdu_setup.txt.in +++ b/man/coap_pdu_setup.txt.in @@ -346,7 +346,7 @@ and hence be illegal. This is done by using *coap_encode_var_safe*() or *Function: coap_insert_optlist()* The *coap_insert_optlist*() function adds the _optlist_ entry onto the -_optlist_chain_ and then sorts the _optlist_chain_ before returning. +end of the _optlist_chain_. The initial _optlist_chain_ entry needs to be set to NULL before this function is first called. The *coap_delete_optlist*() function has to be called to free off all the _optlist_chain_ entries. @@ -699,11 +699,11 @@ FURTHER INFORMATION ------------------- See -"RFC7252: The Constrained Application Protocol (CoAP)" +"https://tools.ietf.org/html/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" -"RFC7959: Block-Wise Transfers in the Constrained Application Protocol (CoAP)" +"https://tools.ietf.org/html/rfc7959[RFC7959: Block-Wise Transfers in the Constrained Application Protocol (CoAP)]" -"RFC9175: CoAP: Echo, Request-Tag, and Token Processing" +"https://tools.ietf.org/html/rfc9175[RFC9175: CoAP: Echo, Request-Tag, and Token Processing]" for further information. diff --git a/man/coap_uri.txt.in b/man/coap_uri.txt.in new file mode 100644 index 00000000..b3706d8e --- /dev/null +++ b/man/coap_uri.txt.in @@ -0,0 +1,238 @@ +// -*- mode:doc; -*- +// vim: set syntax=asciidoc,tw=0: + +coap_uri(3) +============ +:doctype: manpage +:man source: coap_uri +:man version: @PACKAGE_VERSION@ +:man manual: libcoap Manual + +NAME +---- +coap_uri, +coap_split_uri, +coap_split_proxy_uri +- Work with CoAP URIs + +SYNOPSIS +-------- +*#include * + +*int coap_split_uri(const uint8_t *_uri_def_, size_t _length_, +coap_uri_t *_uri_);* + +*int coap_split_proxy_uri(const uint8_t *_uri_def_, size_t _length_, +coap_uri_t *_uri_);* + +*coap_uri_t *coap_new_uri(const uint8_t *_uri_def_, unsigned int _length_);* + +*coap_uri_t *coap_clone_uri(const coap_uri_t *_uri_);* + +*void coap_delete_uri(coap_uri_t *_uri_);* + +*int coap_uri_into_options(coap_uri_t *_uri_, +coap_optlist_t **_optlist_chain_, int _create_port_opt_, +uint8_t *_buf_, size_t _buflen_);* + +For specific (D)TLS library support, link with +*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, +*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* +or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with +*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. + +DESCRIPTION +----------- +This man page describes the functionality available to work with CoAP URIs and +break them down into the component parts. + +A CoAP URI is of the form + +<(optional):port><(optional)path><(optional)?query> + +where scheme can be one of coap://, coaps://, coap+tcp:// and coaps+tcp://. + +The parsed uri structure is of the form + +[source, c] +---- +typedef struct { + coap_str_const_t host; /* host part of the URI */ + uint16_t port; /* The port in host byte order */ + coap_str_const_t path; /* Beginning of the path if present. + Use coap_split_path() to create Uri-Path options */ + coap_str_const_t query; /* Beginning of the query if present + Use coap_split_query() to create Uri-Query options */ + enum coap_uri_scheme_t scheme; /* The parsed scheme specifier. */ +} coap_uri_t; +---- + +FUNCTIONS +--------- + +*Function: coap_split_uri()* + +The *coap_split_uri*() function is used to parse the provided _uri_def_ of +length _length_ into the component parts held in the _uri_ structure. These +component parts are the host, port, path and query and the CoAP URI scheme. + +*Function: coap_split_proxy_uri()* + +The *coap_split_proxy_uri*() function is used to parse the provided _uri_def_ of +length _length_ into the component parts held in the _uri_ structure. These +component parts are the host, port, path and query and the URI scheme. The +schemes also includes support for http:// and https:// as the proxy may need to +be a coap-to-http proxy. + +*Function: coap_new_uri()* + +The *coap_new_uri*() function creates a new coap_uri_t structure and populates +it using *coap_split_uri*() with _uri_def_ and _length_ as input. The returned +coap_uri_t structure needs to be freed off using *coap_delete_uri*(). + +*Function: coap_clone_uri()* + +The *coap_clone_uri*() function duplicates a _uri_ coap_uri_t structure. +The returned coap_uri_t structure needs to be freed off using +*coap_delete_uri*(). + +*Function: coap_delete_uri()* + +The *coap_delete_uri*() function frees off a previously created _uri_ +coap_uri_t structure. + +*Function: coap_uri_into_options()* + +The *coap_uri_into_options*() function takes the _uri_ structure and then adds +CoAP options derived from this information and adds them to _optlist_chain_. +The initial _optlist_chain_ entry should be set to NULL before +this function is called (unless *coap_insert_optlist*(3) has been previously +used). + +If the port is not the default port and _create_port_opt_ is not 0, then +the Port option is added to _optlist_chain_. + +If there is a path, then this is broken down into individual Path options for +each segment which are then added to _optlist_chain_. + +Likewise, if +there is a query, individual Query options for each segment are then added to +_optlist_chain_. + +_buf_ provides a scratch buffer to use, of size _buflen_ bytes. _buf_ needs +to be as big as the path or query lengths. + +*NOTE:* It is the responsibility of the application to free off the entries +added to _optlist_chain_ using *coap_delete_optlist*(3). + +RETURN VALUES +------------- +*coap_split_uri*(), *coap_split_proxy_uri*(), and +*coap_uri_into_options*() return 0 on success, else < 0 on failure. + +*coap_new_uri*() and *coap_clone_uri*() return a newly allocated coap_uri_t +structure or NULL on failure. + +EXAMPLES +-------- +*Setup PDU and Transmit* + +[source, c] +---- +#include + +/* + * Returns 0 failure, 1 success + */ +static int +parse_and_send_uri(coap_session_t *session, const char *do_uri) { + coap_uri_t uri; + coap_optlist_t *optlist = NULL; + coap_pdu_t *pdu; + coap_proto_t proto = coap_session_get_proto(session); + int res; + coap_mid_t mid; +#define BUFSIZE 100 + unsigned char buf[BUFSIZE]; + + /* Parse the URI */ + res = coap_split_uri((const uint8_t*)do_uri, strlen(do_uri), &uri); + if (res != 0) + return 0; + + /* Check the scheme matches the session type */ + switch (uri.scheme) { + case COAP_URI_SCHEME_COAP: + if (proto != COAP_PROTO_UDP) + return 0; + break; + case COAP_URI_SCHEME_COAPS: + if (proto != COAP_PROTO_DTLS) + return 0; + break; + case COAP_URI_SCHEME_COAP_TCP: + if (proto != COAP_PROTO_TCP) + return 0; + break; + case COAP_URI_SCHEME_COAPS_TCP: + if (proto != COAP_PROTO_TLS) + return 0; + break; + /* Proxy support only */ + case COAP_URI_SCHEME_HTTP: + case COAP_URI_SCHEME_HTTPS: + default: + return 0; + } + + /* construct CoAP message */ + pdu = coap_pdu_init(COAP_MESSAGE_CON, + COAP_REQUEST_CODE_GET, + coap_new_message_id(session), + coap_session_max_pdu_size(session)); + if (pdu == NULL) + return 0; + + /* Create all the necessary options from the URI */ + res = coap_uri_into_options(&uri, &optlist, 1, buf, sizeof(buf)); + if (res != 0) + return 0; + + /* Add option list (which will get sorted) to the PDU */ + if (optlist) { + res = coap_add_optlist_pdu(pdu, &optlist); + coap_delete_optlist(optlist); + if (res != 1) + return 0; + } + + /* and send the PDU */ + mid = coap_send(session, pdu); + if (mid == COAP_INVALID_MID) + return 0; + return 1; +} + +---- + +SEE ALSO +-------- +*coap_endpoint_client*(3) and *coap_pdu_setup*(3). + +FURTHER INFORMATION +------------------- +See + +"https://tools.ietf.org/html/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" + +for further information. + +BUGS +---- +Please report bugs on the mailing list for libcoap: +libcoap-developers@lists.sourceforge.net or raise an issue on GitHub at +https://github.com/obgm/libcoap/issues + +AUTHORS +------- +The libcoap project diff --git a/src/uri.c b/src/uri.c index 21b8a53d..5dd31335 100644 --- a/src/uri.c +++ b/src/uri.c @@ -247,6 +247,63 @@ coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri) { return coap_split_uri_sub(str_var, len, uri, COAP_URI_CHECK_PROXY); } +int +coap_uri_into_options(coap_uri_t *uri, + coap_optlist_t **optlist_chain, int create_port_opt, + uint8_t *_buf, size_t _buflen) { + int res; + unsigned char *buf = _buf; + size_t buflen = _buflen; + + if (uri->port != (coap_uri_scheme_is_secure(uri) ? + COAPS_DEFAULT_PORT : COAP_DEFAULT_PORT) && create_port_opt) { + coap_insert_optlist(optlist_chain, + coap_new_optlist(COAP_OPTION_URI_PORT, + coap_encode_var_safe(buf, 4, + (uri->port & 0xffff)), + buf)); + } + + if (uri->path.length) { + if (uri->path.length > buflen) + coap_log(LOG_WARNING, "URI path will be truncated (max buffer %zu)\n", + buflen); + res = coap_split_path(uri->path.s, uri->path.length, buf, &buflen); + if (res < 0) + return -1; + + while (res--) { + coap_insert_optlist(optlist_chain, + coap_new_optlist(COAP_OPTION_URI_PATH, + coap_opt_length(buf), + coap_opt_value(buf))); + + buf += coap_opt_size(buf); + } + } + + if (uri->query.length) { + buflen = _buflen; + buf = _buf; + if (uri->query.length > buflen) + coap_log(LOG_WARNING, "URI query will be truncated (max buffer %zu)\n", + buflen); + res = coap_split_query(uri->query.s, uri->query.length, buf, &buflen); + if (res < 0) + return -1; + + while (res--) { + coap_insert_optlist(optlist_chain, + coap_new_optlist(COAP_OPTION_URI_QUERY, + coap_opt_length(buf), + coap_opt_value(buf))); + + buf += coap_opt_size(buf); + } + } + return 0; +} + /** * Calculates decimal value from hexadecimal ASCII character given in * @p c. The caller must ensure that @p c actually represents a valid @@ -544,6 +601,11 @@ coap_clone_uri(const coap_uri_t *uri) { return result; } +void +coap_delete_uri(coap_uri_t *uri) { + coap_free(uri); +} + COAP_STATIC_INLINE int is_unescaped_in_path(const uint8_t c) { return ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' )