From b325dfbdd4abea69e70f609429028bef81aa1bd3 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Sun, 11 Jul 2010 19:56:19 +0200 Subject: [PATCH] added helper functions for URI handling --- Makefile | 6 ++-- coap.h | 1 + uri.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ uri.h | 22 ++++++++++++++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 uri.c create mode 100644 uri.h diff --git a/Makefile b/Makefile index 8ff0f0c3..1367b024 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -PROGRAM:=coap +PROGRAM:=coap-server VERSION:=0.03 -SOURCES:= pdu.c net.c encode.c +SOURCES:= pdu.c net.c encode.c uri.c OBJECTS:= $(patsubst %.c, %.o, $(SOURCES)) -HEADERS:=coap.h debug.h pdu.h net.h encode.h +HEADERS:=coap.h debug.h pdu.h net.h encode.h uri.h CFLAGS:=-g -Wall -ansi -pedantic DISTDIR=$(PROGRAM)-$(VERSION) FILES:=main.c Makefile $(SOURCES) $(HEADERS) diff --git a/coap.h b/coap.h index 9b8a0dea..794a1e02 100644 --- a/coap.h +++ b/coap.h @@ -10,5 +10,6 @@ #include "pdu.h" #include "net.h" #include "encode.h" +#include "uri.h" #endif /* _COAP_H_ */ diff --git a/uri.c b/uri.c new file mode 100644 index 00000000..c09f3729 --- /dev/null +++ b/uri.c @@ -0,0 +1,90 @@ +/* uri.c -- helper functions for URI treatment + * + * (c) 2010 Olaf Bergmann + */ + +#include +#include +#include + +#include "uri.h" + +int +coap_split_uri( char *str, coap_uri_t *uri) { + char *p; + + if ( !str || !uri ) + return -1; + + memset( uri, 0, sizeof(coap_uri_t) ); + + /* find scheme */ + p = str; + while ( isalnum(*p) ) + ++p; + + if ( *p != ':' ) { /* no scheme, reset p */ + p = str; + } else { /* scheme found, look for network authority */ + *p++ = '\0'; + uri->scheme = str; + if ( strncmp( p, "//", 2 ) == 0 ) { /* have network authority */ + p += 2; + uri->na = p; + + /* skip NA and port so that p and str finally point to path */ + while ( *p && *p != '/' ) + ++p; + + if ( *p ) + *p++ = '\0'; + + str = p; +#if 0 + /* split server address and port */ + if ( *uri->na == '[' ) { /* IPv6 address reference */ + p = ++uri->na; + + while ( *p && *p != ']' ) + ++p; + *p++ = '\0'; + } else { /* IPv4 address or hostname */ + p = uri->na; + while ( *p && *p != ':' ) + ++p; + } + + if ( *p == ':' ) { /* handle port */ + *p++ = '\0'; + uri->port = p; + } +#endif + } else + str = p; + + /* str now points to the path */ + } + + /* split path and query */ + if ( *str == '\0' ) + return 0; + +#if 1 + uri->path = str; +#else + if (*str != '?') + uri->path = str++; + + while (*str && *str != '?') + str++; + + if (*str == '?') { + *str++ = '\0'; + + if (*str) /* we do not want to point query to an empty string */ + uri->query = str; + } +#endif + + return 0; +} diff --git a/uri.h b/uri.h new file mode 100644 index 00000000..5b865304 --- /dev/null +++ b/uri.h @@ -0,0 +1,22 @@ +/* uri.h -- helper functions for URI treatment + * + * (c) 2010 Olaf Bergmann + */ + +#ifndef _COAP_URI_H_ +#define _COAP_URI_H_ + +typedef struct { + char *scheme; /* URI scheme */ + char *na; /* network authority */ + char *path; /* path */ +} coap_uri_t; + +/** + * Splits given URI into pieces and fills the specified uri object accordingly. + * URI parts that are not available will be set to NULL in uri. The function + * returns -1 on error, 0 on success. Note that the passed str will be altered. + */ +int coap_split_uri( char *str, coap_uri_t *uri); + +#endif /* _COAP_URI_H_ */