Skip to content

Commit

Permalink
uri.[hc]: make 1st argument of coap_split_uri() const
Browse files Browse the repository at this point in the history
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
obgm committed Jul 6, 2016
1 parent 2450048 commit cb6d3c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
6 changes: 2 additions & 4 deletions include/coap/uri.h
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.
Expand Down Expand Up @@ -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
Expand Down
24 changes: 13 additions & 11 deletions src/uri.c
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.
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}

Expand Down

0 comments on commit cb6d3c4

Please sign in to comment.