uri.[hc]: make 1st argument of coap_split_uri() const

Issue #37 [0] argued that coap_split_uri() should not change the
string that was passed as its first argument. The old behavior
was to normalize the scheme and the host part of the given URI by
converting both to lower case. This commit drops that normalization
step and leaves the original string as is. As a consequence,
implementations that employ virtual hosts now must canonicalize
the host part by themselves.

CAUTION: The constness is not (yet) reflected in the str components
of the result parameter of type coap_uri_t. Care must be taken not
to accidently change an immutable component of the coap_uri_t object.

[0] https://github.com/obgm/libcoap/issues/37
This commit is contained in:
Olaf Bergmann committed 2016-07-06 12:48:21 +02:00
1 parent 24500483f7
commit cb6d3c43ed
2 files changed
+15 -15

No files matched your search

+2 -4
View File
@@ -1,7 +1,7 @@
/*
* uri.h -- helper functions for URI treatment
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2010-2011,2016 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
@@ -75,10 +75,8 @@ int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key);
* @param uri The coap_uri_t object to store the result.
* @return @c 0 on success, or < 0 on error.
*
* @note The host name part will be converted to lower case by this
* function.
*/
int coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri);
int coap_split_uri(const unsigned char *str_var, size_t len, coap_uri_t *uri);
/**
* Splits the given URI path into segments. Each segment is preceded
+13 -11
View File
@@ -1,6 +1,6 @@
/* uri.c -- helper functions for URI treatment
*
* Copyright (C) 2010--2012,2015 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2010--2012,2015-2016 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
@@ -41,9 +41,12 @@ strnchr(unsigned char *s, size_t len, unsigned char c) {
return len ? s : NULL;
}
#define ISEQUAL_CI(a,b) \
((a) == (b) || (islower(b) && ((a) == ((b) - 0x20))))
int
coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) {
unsigned char *p, *q;
coap_split_uri(const unsigned char *str_var, size_t len, coap_uri_t *uri) {
const unsigned char *p, *q;
int secure = 0, res = 0;
if (!str_var || !uri)
@@ -60,7 +63,7 @@ coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) {
}
q = (unsigned char *)COAP_DEFAULT_SCHEME;
while (len && *q && tolower(*p) == *q) {
while (len && *q && ISEQUAL_CI(*p, *q)) {
++p; ++q; --len;
}
@@ -72,12 +75,12 @@ coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) {
}
/* There might be an additional 's', indicating the secure version: */
if (len && (secure = tolower(*p) == 's')) {
if (len && (secure = *p == 's')) {
++p; --len;
}
q = (unsigned char *)"://";
while (len && *q && tolower(*p) == *q) {
while (len && *q && *p == *q) {
++p; ++q; --len;
}
@@ -100,11 +103,10 @@ coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) {
goto error;
}
COAP_SET_STR(&uri->host, q - p, p);
COAP_SET_STR(&uri->host, q - p, (unsigned char *)p);
++q; --len;
} else { /* IPv4 address or FQDN */
while (len && *q != ':' && *q != '/' && *q != '?') {
*q = tolower(*q);
++q;
--len;
}
@@ -114,7 +116,7 @@ coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) {
goto error;
}
COAP_SET_STR(&uri->host, q - p, p);
COAP_SET_STR(&uri->host, q - p, (unsigned char *)p);
}
/* check for Uri-Port */
@@ -158,7 +160,7 @@ coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) {
}
if (p < q) {
COAP_SET_STR(&uri->path, q - p, p);
COAP_SET_STR(&uri->path, q - p, (unsigned char *)p);
p = q;
}
}
@@ -167,7 +169,7 @@ coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) {
if (len && *p == '?') {
++p;
--len;
COAP_SET_STR(&uri->query, len, p);
COAP_SET_STR(&uri->query, len, (unsigned char *)p);
len = 0;
}