-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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] #37
- Loading branch information
Showing
2 changed files
with
15 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* uri.h -- helper functions for URI treatment | ||
* | ||
* Copyright (C) 2010-2011 Olaf Bergmann <[email protected]> | ||
* Copyright (C) 2010-2011,2016 Olaf Bergmann <[email protected]> | ||
* | ||
* 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* uri.c -- helper functions for URI treatment | ||
* | ||
* Copyright (C) 2010--2012,2015 Olaf Bergmann <[email protected]> | ||
* Copyright (C) 2010--2012,2015-2016 Olaf Bergmann <[email protected]> | ||
* | ||
* 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; | ||
} | ||
|
||
|