added helper functions for URI handling

This commit is contained in:
Olaf Bergmann committed 2010-07-11 19:56:19 +02:00
1 parent 276473f990
commit b325dfbdd4
4 files changed
+116 -3

No files matched your search

+3 -3
View File
@@ -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)
+1
View File
@@ -10,5 +10,6 @@
#include "pdu.h"
#include "net.h"
#include "encode.h"
#include "uri.h"
#endif /* _COAP_H_ */
+90
View File
@@ -0,0 +1,90 @@
/* uri.c -- helper functions for URI treatment
*
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#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;
}
+22
View File
@@ -0,0 +1,22 @@
/* uri.h -- helper functions for URI treatment
*
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
*/
#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_ */