Updated Makefiles and CMakeLists.txt to point to resource, not oic-resource
[platform/upstream/iotivity.git] / csdk / libcoap-4.1.1 / uri.h
1 /* uri.h -- helper functions for URI treatment
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use. 
7  */
8
9 #ifndef _COAP_URI_H_
10 #define _COAP_URI_H_
11
12 #include "hashkey.h"
13 #include "str.h"
14
15 /** Representation of parsed URI. Components may be filled from a
16  * string with coap_split_uri() and can be used as input for
17  * option-creation functions. */
18 typedef struct {
19   str host;                     /**< host part of the URI */
20   unsigned short port;          /**< The port in host byte order */
21   str path;                     /**< Beginning of the first path segment. 
22                                    Use coap_split_path() to create Uri-Path options */
23   str query;                    /**<  The query part if present */
24 } coap_uri_t;
25
26 /**
27  * Creates a new coap_uri_t object from the specified URI. Returns the new
28  * object or NULL on error. The memory allocated by the new coap_uri_t
29  * must be released using coap_free().
30  * @param uri The URI path to copy.
31  * @para length The length of uri.
32  *
33  * @return New URI object or NULL on error.
34  */
35 coap_uri_t *coap_new_uri(const unsigned char *uri, unsigned int length);
36
37 /**
38  * Clones the specified coap_uri_t object. Thie function allocates sufficient
39  * memory to hold the coap_uri_t structure and its contents. The object must
40  * be released with coap_free(). */
41 coap_uri_t *coap_clone_uri(const coap_uri_t *uri);
42
43 /** 
44  * Calculates a hash over the given path and stores the result in 
45  * @p key. This function returns @c 0 on error or @c 1 on success.
46  * 
47  * @param path The URI path to generate hash for.
48  * @param len  The length of @p path.
49  * @param key  The output buffer.
50  * 
51  * @return @c 1 if @p key was set, @c 0 otherwise.
52  */
53 int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key);
54
55 /**
56  * @defgroup uri_parse URI Parsing Functions
57  *
58  * CoAP PDUs contain normalized URIs with their path and query split into
59  * multiple segments. The functions in this module help splitting strings.
60  * @{
61  */
62
63 /** 
64  * Iterator to for tokenizing a URI path or query. This structure must
65  * be initialized with coap_parse_iterator_init(). Call
66  * coap_parse_next() to walk through the tokens.
67  *
68  * @code
69  * unsigned char *token;
70  * coap_parse_iterator_t pi;
71  * coap_parse_iterator_init(uri.path.s, uri.path.length, '/', "?#", 2, &pi);
72  *
73  * while ((token = coap_parse_next(&pi))) {
74  *   ... do something with token ...
75  * }
76  * @endcode
77  */
78 typedef struct {
79   size_t n;                     /**< number of remaining characters in buffer */
80   unsigned char separator;      /**< segment separators */
81   unsigned char *delim;         /**< delimiters where to split the string */
82   size_t dlen;                  /**< length of separator */
83   unsigned char *pos;           /**< current position in buffer */
84   size_t segment_length;        /**< length of current segment */
85 } coap_parse_iterator_t;
86
87 /** 
88  * Initializes the given iterator @p pi. 
89  * 
90  * @param s         The string to tokenize.
91  * @param n         The length of @p s.
92  * @param separator The separator character that delimits tokens.
93  * @param delim     A set of characters that delimit @s.
94  * @param dlen      The length of @p delim.
95  * @param pi        The iterator object to initialize.
96  * 
97  * @return The initialized iterator object @p pi.
98  */
99 coap_parse_iterator_t *
100 coap_parse_iterator_init(unsigned char *s, size_t n, 
101                          unsigned char separator,
102                          unsigned char *delim, size_t dlen,
103                          coap_parse_iterator_t *pi);
104
105 /** 
106  * Updates the iterator @p pi to point to the next token. This
107  * function returns a pointer to that token or @c NULL if no more
108  * tokens exist. The contents of @p pi will be updated. In particular,
109  * @c pi->segment_length specifies the length of the current token, @c
110  * pi->pos points to its beginning.
111  * 
112  * @param pi The iterator to update.
113  * 
114  * @return The next token or @c NULL if no more tokens exist.
115  */
116 unsigned char *coap_parse_next(coap_parse_iterator_t *pi);
117
118 /** 
119  * Parses a given string into URI components. The identified syntactic
120  * components are stored in the result parameter @p uri. Optional URI
121  * components that are not specified will be set to { 0, 0 }, except
122  * for the port which is set to @c COAP_DEFAULT_PORT. This function
123  * returns @p 0 if parsing succeeded, a value less than zero
124  * otherwise.
125  * 
126  * @param str_var The string to split up.
127  * @param len     The actual length of @p str_var
128  * @param uri     The coap_uri_t object to store the result.
129  * @return @c 0 on success, or < 0 on error.
130  *
131  * @note The host name part will be converted to lower case by this
132  * function.
133  */
134 int
135 coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri);
136
137 /** 
138  * Splits the given URI path into segments. Each segment is preceded
139  * by an option pseudo-header with delta-value 0 and the actual length
140  * of the respective segment after percent-decoding.
141  * 
142  * @param s      The path string to split. 
143  * @param length The actual length of @p s.
144  * @param buf    Result buffer for parsed segments. 
145  * @param buflen Maximum length of @p buf. Will be set to the actual number
146  * of bytes written into buf on success.
147  * 
148  * @return The number of segments created or @c -1 on error.
149  */
150 int coap_split_path(const unsigned char *s, size_t length, 
151                     unsigned char *buf, size_t *buflen);
152
153 /** 
154  * Splits the given URI query into segments. Each segment is preceded
155  * by an option pseudo-header with delta-value 0 and the actual length
156  * of the respective query term.
157  * 
158  * @param s      The query string to split. 
159  * @param length The actual length of @p s.
160  * @param buf    Result buffer for parsed segments. 
161  * @param buflen Maximum length of @p buf. Will be set to the actual number
162  * of bytes written into buf on success.
163  * 
164  * @return The number of segments created or @c -1 on error.
165  *
166  * @bug This function does not reserve additional space for delta > 12.
167  */
168 int coap_split_query(const unsigned char *s, size_t length, 
169                      unsigned char *buf, size_t *buflen);
170
171 /** @} */
172
173 #endif /* _COAP_URI_H_ */