diff --git a/configure.in b/configure.in index 0a4942cb..3673d53d 100644 --- a/configure.in +++ b/configure.in @@ -18,7 +18,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. AC_PREREQ([2.67]) -AC_INIT([libcoap], [0.06]) +AC_INIT([libcoap], [0.07]) AC_CONFIG_SRCDIR([coap.h]) # Checks for programs. diff --git a/examples/client.c b/examples/client.c index d0730089..c13efb0f 100644 --- a/examples/client.c +++ b/examples/client.c @@ -247,9 +247,9 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { /* add URI components from optlist */ for (option = optlist; option; option = option->next ) { switch (COAP_OPTION_KEY(*(coap_option *)option->data)) { - case COAP_OPTION_URI_SCHEME : case COAP_OPTION_URI_AUTHORITY : case COAP_OPTION_URI_PATH : + case COAP_OPTION_URI_QUERY : coap_add_option ( pdu, COAP_OPTION_KEY(*(coap_option *)option->data), COAP_OPTION_LENGTH(*(coap_option *)option->data), COAP_OPTION_DATA(*(coap_option *)option->data) ); @@ -461,12 +461,6 @@ void cmdline_uri(char *arg) { coap_split_uri((unsigned char *)arg, &uri ); - if (uri.scheme.length && - (uri.scheme.length != strlen(COAP_DEFAULT_SCHEME) - || memcmp(uri.scheme.s, COAP_DEFAULT_SCHEME, uri.scheme.length))) - coap_insert( &optlist, new_option_node(COAP_OPTION_URI_SCHEME, - uri.scheme.length, uri.scheme.s), - order_opts); #if 0 /* need authority only for proxy requests */ if (uri.na.length) coap_insert( &optlist, new_option_node(COAP_OPTION_URI_AUTHORITY, @@ -477,6 +471,11 @@ cmdline_uri(char *arg) { coap_insert( &optlist, new_option_node(COAP_OPTION_URI_PATH, uri.path.length, uri.path.s), order_opts); + + if (uri.query.length) + coap_insert( &optlist, new_option_node(COAP_OPTION_URI_QUERY, + uri.query.length, uri.query.s), + order_opts); } void diff --git a/examples/server.c b/examples/server.c index 815460b4..8636e2c2 100644 --- a/examples/server.c +++ b/examples/server.c @@ -549,10 +549,6 @@ handle_delete(coap_context_t *ctx, coap_queue_t *node, void *data) { /* create the response */ pdu = new_response(ctx, node, COAP_RESPONSE_200); - if (uri.scheme.length) - coap_add_option(pdu, COAP_OPTION_URI_SCHEME, - uri.scheme.length, uri.scheme.s); - if (uri.na.length) coap_add_option(pdu, COAP_OPTION_URI_AUTHORITY, uri.na.length, uri.na.s); @@ -566,6 +562,10 @@ handle_delete(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_add_option(pdu, COAP_OPTION_TOKEN, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); + if (uri.query.length) + coap_add_option(pdu, COAP_OPTION_URI_QUERY, + uri.query.length, uri.query.s); + coap_delete_resource(ctx, coap_uri_hash(&uri)); return pdu; diff --git a/net.c b/net.c index 959ec925..af3405ab 100644 --- a/net.c +++ b/net.c @@ -556,7 +556,7 @@ coap_dispatch( coap_context_t *context ) { response = coap_new_pdu(); if (response) { response->hdr->type = COAP_MESSAGE_RST; - response->hdr->code = COAP_RESPONSE_400; + response->hdr->code = COAP_RESPONSE_X_242; response->hdr->id = node->pdu->hdr->id; /* add rejected option */ diff --git a/pdu.c b/pdu.c index e388a824..504053a4 100644 --- a/pdu.c +++ b/pdu.c @@ -153,11 +153,11 @@ coap_check_critical(coap_pdu_t *pdu, coap_opt_t **option) { if (opt_code & 0x01) { switch (opt_code) { /* skip known options */ case COAP_OPTION_CONTENT_TYPE : - case COAP_OPTION_URI_SCHEME : case COAP_OPTION_URI_AUTHORITY : case COAP_OPTION_URI_PATH : case COAP_OPTION_TOKEN : case COAP_OPTION_BLOCK : + case COAP_OPTION_URI_QUERY : break; default: /* return first unknown critical option */ fprintf(stderr, @@ -219,15 +219,15 @@ coap_get_request_uri(coap_pdu_t *pdu, coap_uri_t *result) { memset(result, 0, sizeof(*result)); - if ((opt = coap_check_option(pdu, COAP_OPTION_URI_SCHEME))) - COAP_SET_STR(&result->scheme, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt)); - if ((opt = coap_check_option(pdu, COAP_OPTION_URI_AUTHORITY))) COAP_SET_STR(&result->na, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt)); if ((opt = coap_check_option(pdu, COAP_OPTION_URI_PATH))) COAP_SET_STR(&result->path, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt)); + if ((opt = coap_check_option(pdu, COAP_OPTION_URI_QUERY))) + COAP_SET_STR(&result->query, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt)); + return 1; } diff --git a/pdu.h b/pdu.h index a323ccee..9193c5e6 100644 --- a/pdu.h +++ b/pdu.h @@ -59,6 +59,8 @@ #define COAP_OPTION_URI_AUTHORITY 5 /* C, String, 1-270 B, "" */ #define COAP_OPTION_LOCATION 6 /* E, String, 1-270 B, - */ #define COAP_OPTION_URI_PATH 9 /* C, String, 1-270 B, "" */ +#define COAP_OPTION_TOKEN 11 /* C, Sequence of Bytes, 1-2 B, - */ +#define COAP_OPTION_URI_QUERY 15 /* C, String, 1-270 B, "" */ /* option types from draft-hartke-coap-observe-01 */ @@ -67,7 +69,6 @@ /* selected option types from draft-bormann-coap-misc-04 */ #define COAP_OPTION_ACCEPT 8 /* E Sequence of Bytes, 1-n B, - */ -#define COAP_OPTION_TOKEN 11 /* C, Sequence of Bytes, 1-n B, - */ #define COAP_OPTION_BLOCK 13 /* C, unsigned integer, 1--3 B, 0 */ #define COAP_OPTION_NOOP 14 /* no-op for fenceposting */ @@ -82,7 +83,11 @@ #define COAP_RESPONSE_405 165 /* 405 Method Not Allowed */ #define COAP_RESPONSE_415 175 /* 415 Unsupported Media Type */ #define COAP_RESPONSE_500 200 /* 500 Internal Server Error */ +#define COAP_RESPONSE_503 203 /* 503 Service Unavailable */ #define COAP_RESPONSE_504 204 /* 504 Gateway Timeout */ +#define COAP_RESPONSE_X_240 240 /* Token Option required by server */ +#define COAP_RESPONSE_X_241 241 /* Uri-Authority Option required by server */ +#define COAP_RESPONSE_X_242 242 /* Critical Option not supported */ /* CoAP media type encoding */ diff --git a/subscribe.c b/subscribe.c index 9799fe04..d50ccfa3 100644 --- a/subscribe.c +++ b/subscribe.c @@ -48,11 +48,6 @@ notify(coap_context_t *context, coap_resource_t *res, pdu->hdr->code = code; /* FIXME: content-type and data (how about block?) */ - if (res->uri->scheme.length) - coap_add_option (pdu, COAP_OPTION_URI_SCHEME, - res->uri->scheme.length, - res->uri->scheme.s ); - if (res->uri->na.length) coap_add_option (pdu, COAP_OPTION_URI_AUTHORITY, res->uri->na.length, @@ -73,6 +68,11 @@ notify(coap_context_t *context, coap_resource_t *res, sub->token.s); } + if (res->uri->query.length) + coap_add_option (pdu, COAP_OPTION_URI_QUERY, + res->uri->query.length, + res->uri->query.s ); + if (res->data) { length = (unsigned char *)pdu->hdr + COAP_MAX_PDU_SIZE - pdu->data; ct = res->mediatype; diff --git a/uri.c b/uri.c index 249c6123..481f61c9 100644 --- a/uri.c +++ b/uri.c @@ -22,8 +22,14 @@ #include #include "mem.h" +#include "debug.h" +#include "pdu.h" #include "uri.h" +#ifndef MIN +# define MIN(x,y) (x) < (y) ? (x) : (y) +#endif + int coap_split_uri(unsigned char *str, coap_uri_t *uri) { unsigned char *p; @@ -40,20 +46,24 @@ coap_split_uri(unsigned char *str, coap_uri_t *uri) { if ( *p != ':' ) { /* no scheme, reset p */ p = str; - } else { /* scheme found, look for network authority */ - COAP_SET_STR(&uri->scheme, p - str, str); + } else { /* scheme found, check if it is "coap" */ + if (memcmp(str, COAP_DEFAULT_SCHEME, + MIN(p - str, sizeof(COAP_DEFAULT_SCHEME) - 1)) != 0) { + debug("unknown URI scheme '%s'\n",str); + return -1; + } *p++ = '\0'; + + /* look for network authority */ if ( strncmp( (char *)p, "//", 2 ) == 0 ) { /* have network authority */ p += 2; uri->na.s = p; /* skip NA and port so that p and str finally point to path */ - while ( *p && *p != '/' ) + while ( *p && *p != '/' && *p != '?') ++p; uri->na.length = p - uri->na.s; - if ( *p ) - *p++ = '\0'; str = p; #if 0 @@ -78,7 +88,7 @@ coap_split_uri(unsigned char *str, coap_uri_t *uri) { } else str = p; - /* str now points to the path */ + /* str now points to the path or query if path is empty*/ } /* split path and query */ @@ -86,26 +96,26 @@ coap_split_uri(unsigned char *str, coap_uri_t *uri) { if ( *str == '\0' ) return 0; -#if 1 - uri->path.s = *str == '/' ? ++str : str; - uri->path.length = strlen((char *)uri->path.s); -#else - if (*str != '?') - uri->path.s = str++; + if (*str != '?') { + if (*str == '/') /* skip leading '/' */ + *str++ = '\0'; + uri->path.s = str; + } while (*str && *str != '?') str++; if (*str == '?') { - uri->path.length = str - uri->path.s; *str++ = '\0'; - if (*str) { /* we do not want to point query to an empty string */ + if (*str) { uri->query.s = str; - uri->query.length = strlen(uri->query.s); + uri->query.length = strlen((char *)uri->query.s); } - } -#endif + } + + if (uri->path.s) + uri->path.length = strlen((char *)uri->path.s); return 0; } @@ -132,33 +142,34 @@ coap_clone_uri(const coap_uri_t *uri) { if ( !uri ) return NULL; - result = (coap_uri_t *)coap_malloc( uri->scheme.length + uri->na.length + + result = (coap_uri_t *)coap_malloc( uri->query.length + uri->na.length + uri->path.length + sizeof(coap_uri_t) + 1); if ( !result ) return NULL; memset( result, 0, sizeof(coap_uri_t) ); - if ( uri->scheme.length ) { - result->scheme.s = URI_DATA(result); - result->scheme.length = uri->scheme.length; - - memcpy(result->scheme.s, uri->scheme.s, uri->scheme.length); - } if ( uri->na.length ) { - result->na.s = URI_DATA(result) + uri->scheme.length; + result->na.s = URI_DATA(result); result->na.length = uri->na.length; memcpy(result->na.s, uri->na.s, uri->na.length); } if ( uri->path.length ) { - result->path.s = URI_DATA(result) + uri->scheme.length + uri->na.length; + result->path.s = URI_DATA(result) + uri->na.length; result->path.length = uri->path.length; memcpy(result->path.s, uri->path.s, uri->path.length); } + if ( uri->query.length ) { + result->query.s = URI_DATA(result) + uri->na.length + uri->path.length; + result->query.length = uri->query.length; + + memcpy(result->query.s, uri->query.s, uri->query.length); + } + return result; } diff --git a/uri.h b/uri.h index 87d0261c..a5970179 100644 --- a/uri.h +++ b/uri.h @@ -23,9 +23,9 @@ #include "str.h" typedef struct { - str scheme; /* URI scheme */ str na; /* network authority */ str path; /* path */ + str query; /* query part */ } coap_uri_t; /**