Implementation of connectivity abstraction feature Release v0.3
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / 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 {
20     str host; /**< host part of the URI */
21     unsigned short port; /**< The port in host byte order */
22     str path; /**< Beginning of the first path segment.
23      Use coap_split_path() to create Uri-Path options */
24     str query; /**<  The query part if present */
25 } coap_uri_t;
26
27 /**
28  * Creates a new coap_uri_t object from the specified URI. Returns the new
29  * object or NULL on error. The memory allocated by the new coap_uri_t
30  * must be released using coap_free().
31  * @param uri The URI path to copy.
32  * @para length The length of uri.
33  *
34  * @return New URI object or NULL on error.
35  */
36 coap_uri_t *coap_new_uri(const unsigned char *uri, unsigned int length);
37
38 /**
39  * Clones the specified coap_uri_t object. Thie function allocates sufficient
40  * memory to hold the coap_uri_t structure and its contents. The object must
41  * be released with coap_free(). */
42 coap_uri_t *coap_clone_uri(const coap_uri_t *uri);
43
44 /**
45  * Calculates a hash over the given path and stores the result in
46  * @p key. This function returns @c 0 on error or @c 1 on success.
47  *
48  * @param path The URI path to generate hash for.
49  * @param len  The length of @p path.
50  * @param key  The output buffer.
51  *
52  * @return @c 1 if @p key was set, @c 0 otherwise.
53  */
54 int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key);
55
56 /**
57  * @defgroup uri_parse URI Parsing Functions
58  *
59  * CoAP PDUs contain normalized URIs with their path and query split into
60  * multiple segments. The functions in this module help splitting strings.
61  * @{
62  */
63
64 /**
65  * Iterator to for tokenizing a URI path or query. This structure must
66  * be initialized with coap_parse_iterator_init(). Call
67  * coap_parse_next() to walk through the tokens.
68  *
69  * @code
70  * unsigned char *token;
71  * coap_parse_iterator_t pi;
72  * coap_parse_iterator_init(uri.path.s, uri.path.length, '/', "?#", 2, &pi);
73  *
74  * while ((token = coap_parse_next(&pi))) {
75  *   ... do something with token ...
76  * }
77  * @endcode
78  */
79 typedef struct
80 {
81     size_t n; /**< number of remaining characters in buffer */
82     unsigned char separator; /**< segment separators */
83     unsigned char *delim; /**< delimiters where to split the string */
84     size_t dlen; /**< length of separator */
85     unsigned char *pos; /**< current position in buffer */
86     size_t segment_length; /**< length of current segment */
87 } coap_parse_iterator_t;
88
89 /**
90  * Initializes the given iterator @p pi.
91  *
92  * @param s         The string to tokenize.
93  * @param n         The length of @p s.
94  * @param separator The separator character that delimits tokens.
95  * @param delim     A set of characters that delimit @s.
96  * @param dlen      The length of @p delim.
97  * @param pi        The iterator object to initialize.
98  *
99  * @return The initialized iterator object @p pi.
100  */
101 coap_parse_iterator_t *
102 coap_parse_iterator_init(unsigned char *s, size_t n, unsigned char separator, unsigned char *delim,
103         size_t dlen, 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, unsigned char *buf, size_t *buflen);
151
152 /**
153  * Splits the given URI query into segments. Each segment is preceded
154  * by an option pseudo-header with delta-value 0 and the actual length
155  * of the respective query term.
156  *
157  * @param s      The query string to split.
158  * @param length The actual length of @p s.
159  * @param buf    Result buffer for parsed segments.
160  * @param buflen Maximum length of @p buf. Will be set to the actual number
161  * of bytes written into buf on success.
162  *
163  * @return The number of segments created or @c -1 on error.
164  *
165  * @bug This function does not reserve additional space for delta > 12.
166  */
167 int coap_split_query(const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen);
168
169 /** @} */
170
171 #endif /* _COAP_URI_H_ */