mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-25 06:23:53 +01:00
Merge pull request #16707 from benpicco/sys/uri_parser-constify
uri_parser: constify result
This commit is contained in:
commit
17b9c5adbb
@ -82,14 +82,15 @@ static int _connection_send(lwm2m_client_connection_t *conn, uint8_t *buffer,
|
||||
lwm2m_client_data_t *client_data);
|
||||
|
||||
/**
|
||||
* @brief Tries to find an interface in the host string. If not, it will check
|
||||
* @brief Tries to find an interface by interface string. If not, it will check
|
||||
* if there only exists one interface, and will use it
|
||||
* @param[in] host host string
|
||||
* @param[in] iface interface string
|
||||
* @param[in] iface_len interface string length
|
||||
*
|
||||
* @return pointer to the interface to use on success
|
||||
* @return NULL on error
|
||||
*/
|
||||
static netif_t *_get_interface(char *host);
|
||||
static netif_t *_get_interface(const char *iface, size_t len);
|
||||
|
||||
void *lwm2m_connect_server(uint16_t sec_obj_inst_id, void *user_data)
|
||||
{
|
||||
@ -220,10 +221,9 @@ static int _connection_send(lwm2m_client_connection_t *conn, uint8_t *buffer,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static netif_t *_get_interface(char *host)
|
||||
static netif_t *_get_interface(const char *iface, size_t iface_len)
|
||||
{
|
||||
netif_t *netif = NULL;
|
||||
char *iface = ipv6_addr_split_iface(host);
|
||||
|
||||
if (iface == NULL) {
|
||||
/* get the number of net interfaces */
|
||||
@ -240,7 +240,7 @@ static netif_t *_get_interface(char *host)
|
||||
}
|
||||
}
|
||||
else {
|
||||
netif = netif_get_by_name(iface);
|
||||
netif = netif_get_by_name_buffer(iface, iface_len);
|
||||
}
|
||||
|
||||
return netif;
|
||||
@ -252,7 +252,7 @@ static lwm2m_client_connection_t *_connection_create(uint16_t sec_obj_inst_id,
|
||||
lwm2m_client_connection_t *conn = NULL;
|
||||
char uri[MAX_URI_LENGTH];
|
||||
size_t uri_len = ARRAY_SIZE(uri);
|
||||
char *port;
|
||||
const char *port;
|
||||
bool is_bootstrap;
|
||||
|
||||
DEBUG("Creating connection\n");
|
||||
@ -330,7 +330,7 @@ static lwm2m_client_connection_t *_connection_create(uint16_t sec_obj_inst_id,
|
||||
* if not, check the number of interfaces and default to the first if there
|
||||
* is only one defined. */
|
||||
if (ipv6_addr_is_link_local((ipv6_addr_t *)&conn->remote.addr.ipv6)) {
|
||||
netif_t *netif = _get_interface(parsed_uri.host);
|
||||
netif_t *netif = _get_interface(parsed_uri.zoneid, parsed_uri.zoneid_len);
|
||||
if (netif == NULL) {
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
@ -39,8 +39,8 @@ extern "C" {
|
||||
* @brief container that holds all results
|
||||
*/
|
||||
typedef struct {
|
||||
char *scheme; /**< scheme */
|
||||
char *userinfo; /**< userinfo */
|
||||
const char *scheme; /**< scheme */
|
||||
const char *userinfo; /**< userinfo */
|
||||
|
||||
/**
|
||||
* @brief host part
|
||||
@ -49,7 +49,7 @@ typedef struct {
|
||||
* '[' and ']' as well as the zoneid (with leading '%'), if
|
||||
* present.
|
||||
*/
|
||||
char *host;
|
||||
const char *host;
|
||||
|
||||
/**
|
||||
* @brief Pointer to the start of the address, if @ref host is an
|
||||
@ -58,18 +58,18 @@ typedef struct {
|
||||
* @note @ref ipv6addr does not include the brackets '[' and ']'
|
||||
* and the zoneid part.
|
||||
*/
|
||||
char *ipv6addr;
|
||||
const char *ipv6addr;
|
||||
|
||||
/**
|
||||
* @brief zoneid if @ref host is IPv6 address, NULL otherwise
|
||||
*
|
||||
* @see https://tools.ietf.org/html/rfc6874
|
||||
*/
|
||||
char *zoneid;
|
||||
const char *zoneid;
|
||||
|
||||
char *port; /**< port */
|
||||
char *path; /**< path */
|
||||
char *query; /**< query */
|
||||
const char *port; /**< port */
|
||||
const char *path; /**< path */
|
||||
const char *query; /**< query */
|
||||
uint16_t scheme_len; /**< length of @ref scheme */
|
||||
uint16_t userinfo_len; /**< length of @ref userinfo */
|
||||
uint16_t host_len; /**< length of @ref host */
|
||||
@ -84,8 +84,8 @@ typedef struct {
|
||||
* @brief Container to represent a query parameter
|
||||
*/
|
||||
typedef struct {
|
||||
char *name; /**< name of the query parameter */
|
||||
char *value; /**< value of the query parameter */
|
||||
const char *name; /**< name of the query parameter */
|
||||
const char *value; /**< value of the query parameter */
|
||||
uint16_t name_len; /**< length of @ref name */
|
||||
uint16_t value_len; /**< length of @ref value */
|
||||
} uri_parser_query_param_t;
|
||||
|
||||
@ -26,7 +26,7 @@
|
||||
#include "debug.h"
|
||||
|
||||
/* strchr for non-Null-terminated strings (buffers) */
|
||||
static char *_strchrb(char *start, char *stop, char c)
|
||||
static const char *_strchrb(const char *start, const char *stop, char c)
|
||||
{
|
||||
for (; start < stop; start++) {
|
||||
if (*start == c) {
|
||||
@ -36,8 +36,8 @@ static char *_strchrb(char *start, char *stop, char c)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *_consume_scheme(uri_parser_result_t *result, char *uri,
|
||||
char *uri_end, bool *has_authority)
|
||||
static const char *_consume_scheme(uri_parser_result_t *result, const char *uri,
|
||||
const char *uri_end, bool *has_authority)
|
||||
{
|
||||
assert(uri);
|
||||
|
||||
@ -49,7 +49,7 @@ static char *_consume_scheme(uri_parser_result_t *result, char *uri,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *p = _strchrb(uri, uri_end, ':');
|
||||
const char *p = _strchrb(uri, uri_end, ':');
|
||||
|
||||
result->scheme = uri;
|
||||
result->scheme_len = p - uri;
|
||||
@ -65,11 +65,11 @@ static char *_consume_scheme(uri_parser_result_t *result, char *uri,
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
void _consume_userinfo(uri_parser_result_t *result, char *uri,
|
||||
char *authority_end)
|
||||
void _consume_userinfo(uri_parser_result_t *result, const char *uri,
|
||||
const char *authority_end)
|
||||
{
|
||||
/* check for userinfo within authority */
|
||||
char *userinfo_end = _strchrb(uri, authority_end, '@');
|
||||
const char *userinfo_end = _strchrb(uri, authority_end, '@');
|
||||
|
||||
/* check if match */
|
||||
if (userinfo_end) {
|
||||
@ -88,14 +88,14 @@ void _consume_userinfo(uri_parser_result_t *result, char *uri,
|
||||
}
|
||||
}
|
||||
|
||||
bool _consume_port(uri_parser_result_t *result, char *ipv6_end,
|
||||
char *authority_end)
|
||||
bool _consume_port(uri_parser_result_t *result, const char *ipv6_end,
|
||||
const char *authority_end)
|
||||
{
|
||||
/* check for port after host part */
|
||||
char *port_begin = NULL;
|
||||
const char *port_begin = NULL;
|
||||
/* repeat until last ':' in authority section */
|
||||
/* if ipv6 address, check after ipv6 end marker */
|
||||
char *p = (ipv6_end ? ipv6_end : result->host);
|
||||
const char *p = (ipv6_end ? ipv6_end : result->host);
|
||||
while (p != NULL && (p < authority_end)) {
|
||||
port_begin = p;
|
||||
p = _strchrb(p + 1, authority_end, ':');
|
||||
@ -116,13 +116,13 @@ bool _consume_port(uri_parser_result_t *result, char *ipv6_end,
|
||||
return true;
|
||||
}
|
||||
|
||||
static char *_consume_authority(uri_parser_result_t *result, char *uri,
|
||||
char *uri_end)
|
||||
static const char *_consume_authority(uri_parser_result_t *result, const char *uri,
|
||||
const char *uri_end)
|
||||
{
|
||||
assert(uri);
|
||||
|
||||
/* search until first '/' */
|
||||
char *authority_end = _strchrb(uri, uri_end, '/');
|
||||
const char *authority_end = _strchrb(uri, uri_end, '/');
|
||||
if (!authority_end) {
|
||||
authority_end = uri_end;
|
||||
}
|
||||
@ -137,7 +137,7 @@ static char *_consume_authority(uri_parser_result_t *result, char *uri,
|
||||
return authority_end;
|
||||
}
|
||||
|
||||
char *ipv6_end = NULL;
|
||||
const char *ipv6_end = NULL;
|
||||
/* validate IPv6 form */
|
||||
if (result->host[0] == '[') {
|
||||
ipv6_end = _strchrb(result->host, uri_end, ']');
|
||||
@ -146,7 +146,7 @@ static char *_consume_authority(uri_parser_result_t *result, char *uri,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *zoneid_start = _strchrb(result->host, ipv6_end, '%');
|
||||
const char *zoneid_start = _strchrb(result->host, ipv6_end, '%');
|
||||
if (zoneid_start) {
|
||||
/* skip % */
|
||||
result->zoneid = zoneid_start + 1;
|
||||
@ -175,8 +175,8 @@ static char *_consume_authority(uri_parser_result_t *result, char *uri,
|
||||
return authority_end;
|
||||
}
|
||||
|
||||
static char *_consume_path(uri_parser_result_t *result, char *uri,
|
||||
char *uri_end)
|
||||
static const char *_consume_path(uri_parser_result_t *result, const char *uri,
|
||||
const char *uri_end)
|
||||
{
|
||||
assert(uri);
|
||||
|
||||
@ -184,7 +184,7 @@ static char *_consume_path(uri_parser_result_t *result, char *uri,
|
||||
result->path_len = (uri_end - uri);
|
||||
|
||||
/* check for query start '?' */
|
||||
char *path_end = _strchrb(uri, uri_end, '?');
|
||||
const char *path_end = _strchrb(uri, uri_end, '?');
|
||||
|
||||
/* no query string found, return! */
|
||||
if (!path_end) {
|
||||
@ -201,8 +201,8 @@ static char *_consume_path(uri_parser_result_t *result, char *uri,
|
||||
return (result->query + result->query_len);
|
||||
}
|
||||
|
||||
static int _parse_relative(uri_parser_result_t *result, char *uri,
|
||||
char *uri_end)
|
||||
static int _parse_relative(uri_parser_result_t *result, const char *uri,
|
||||
const char *uri_end)
|
||||
{
|
||||
uri = _consume_path(result, uri, uri_end);
|
||||
/* uri should point to uri_end, otherwise there's something left
|
||||
@ -214,8 +214,8 @@ static int _parse_relative(uri_parser_result_t *result, char *uri,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _parse_absolute(uri_parser_result_t *result, char *uri,
|
||||
char *uri_end)
|
||||
static int _parse_absolute(uri_parser_result_t *result, const char *uri,
|
||||
const char *uri_end)
|
||||
{
|
||||
bool has_authority;
|
||||
|
||||
@ -246,7 +246,7 @@ static int _parse_absolute(uri_parser_result_t *result, char *uri,
|
||||
|
||||
bool uri_parser_is_absolute(const char *uri, size_t uri_len)
|
||||
{
|
||||
char *colon = _strchrb((char *)uri, (char *)(uri + uri_len), ':');
|
||||
const char *colon = _strchrb(uri, uri + uri_len, ':');
|
||||
|
||||
/* potentially absolute, if ':' exists */
|
||||
if (colon) {
|
||||
@ -292,10 +292,10 @@ int uri_parser_process(uri_parser_result_t *result, const char *uri,
|
||||
memset(result, 0, sizeof(*result));
|
||||
|
||||
if (uri_parser_is_absolute(uri, uri_len)) {
|
||||
return _parse_absolute(result, (char *)uri, (char *)(uri + uri_len));
|
||||
return _parse_absolute(result, uri, uri + uri_len);
|
||||
}
|
||||
else {
|
||||
return _parse_relative(result, (char *)uri, (char *)(uri + uri_len));
|
||||
return _parse_relative(result, uri, uri + uri_len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -343,7 +343,7 @@ int uri_parser_split_query(const uri_parser_result_t *uri,
|
||||
else if ((idx + 1) < params_len) {
|
||||
/* c is an ampersand (&), so mark the next char as the next
|
||||
* parameter's name name */
|
||||
params[++idx].name = (char *)c + 1U;
|
||||
params[++idx].name = c + 1U;
|
||||
assert(params[idx].name_len == 0);
|
||||
}
|
||||
else {
|
||||
@ -361,7 +361,7 @@ int uri_parser_split_query(const uri_parser_result_t *uri,
|
||||
}
|
||||
params[idx].name_len = c - params[idx].name;
|
||||
/* pick next char as start of value */
|
||||
params[idx].value = (char *)c + 1U;
|
||||
params[idx].value = c + 1U;
|
||||
/* make sure the precondition on params is met */
|
||||
assert(params[idx].value_len == 0);
|
||||
break;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user