1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #20273 from benpicco/coap_find_uri_query

nanocoap: implement coap_find_uri_query()
This commit is contained in:
benpicco 2024-01-20 11:27:15 +00:00 committed by GitHub
commit 8dbe2ef7e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 68 additions and 0 deletions

View File

@ -861,6 +861,23 @@ static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
(uint8_t *)target, max_len, '&');
}
/**
* @brief Find a URI query option of the packet
*
* This function searches for a query option of the form "?key=value"
* and would, when present, return the pointer to "value" when searching
* for "key".
*
* @param[in] pkt pkt to work on
* @param[in] key key string to look for
* @param[out] value found value if present, may be NULL
* @param[out] len length of value if present, may be NULL if value is NULL
*
* @returns true if the key was found, false if not
*/
bool coap_find_uri_query(coap_pkt_t *pkt, const char *key,
const char **value, size_t *len);
/**
* @brief Iterate over a packet's options
*

View File

@ -439,6 +439,45 @@ int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_
return (blkopt & 0x8) ? 1 : 0;
}
bool coap_find_uri_query(coap_pkt_t *pkt, const char *key, const char **value, size_t *len)
{
uint8_t *opt_pos = NULL;
while (1) {
int len_query;
const void *key_data = coap_iterate_option(pkt, COAP_OPT_URI_QUERY,
&opt_pos, &len_query);
if (key_data == NULL) {
return false;
}
const char *separator = memchr(key_data, '=', len_query);
size_t len_key = separator
? (separator - (char *)key_data)
: len_query;
if (memcmp(key, key_data, len_key)) {
continue;
}
if (value == NULL) {
return true;
}
assert(len);
if (separator) {
*value = separator + 1;
*len = len_query - len_key - 1;
} else {
*value = NULL;
*len = 0;
}
return true;
}
return false;
}
bool coap_has_unprocessed_critical_options(const coap_pkt_t *pkt)
{
for (unsigned i = 0; i < sizeof(pkt->opt_crit); ++i){

View File

@ -368,7 +368,19 @@ static void test_nanocoap__get_multi_query(void)
coap_get_uri_query_string(&pkt, query, sizeof(query));
/* skip initial '&' from coap_get_uri_query_string() */
TEST_ASSERT_EQUAL_STRING((char *)qs, &query[1]);
const char *val;
size_t val_len;
TEST_ASSERT(coap_find_uri_query(&pkt, "ab", &val, &val_len));
TEST_ASSERT_EQUAL_INT(3, val_len);
TEST_ASSERT_EQUAL_INT(0, memcmp(val, "cde", val_len));
TEST_ASSERT(coap_find_uri_query(&pkt, "f", &val, &val_len));
TEST_ASSERT_EQUAL_INT(0, val_len);
TEST_ASSERT_EQUAL_INT((uintptr_t)NULL, (uintptr_t)val);
TEST_ASSERT(!coap_find_uri_query(&pkt, "cde", &val, &val_len));
TEST_ASSERT(coap_find_uri_query(&pkt, "ab", NULL, 0));
}
/*
* Builds on get_multi_query test, to use coap_opt_add_uri_query2().
*/