Skip to content

Commit

Permalink
fixup! fixup! nanocoap: implement coap_find_uri_query()
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jan 19, 2024
1 parent 013011d commit 07e6579
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
* @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
* @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
*/
Expand Down
26 changes: 15 additions & 11 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,22 +451,26 @@ bool coap_find_uri_query(coap_pkt_t *pkt, const char *key, const char **value, s
return false;
}

const char *value_data = memchr(key_data, '=', len_query);
const char *separator = memchr(key_data, '=', len_query);
size_t len_key = separator
? (separator - (char *)key_data)
: len_query;

if (memcmp(key, key_data, value_data ? value_data - (char *)key_data : len_query)) {
if (memcmp(key, key_data, len_key)) {
continue;
}

if (value) {
*value = value_data
? value_data + 1
: NULL;
if (value == NULL) {
return true;
}
if (len) {
size_t len_key = value_data
? *value - (char *)key_data
: len_query;
*len = len_query - len_key;

assert(len);
if (separator) {
*value = separator + 1;
*len = len_query - len_key - 1;
} else {
*value = NULL;
*len = 0;
}
return true;
}
Expand Down

0 comments on commit 07e6579

Please sign in to comment.