1 /* uri.h -- helper functions for URI treatment
3 * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
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. */
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 */
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.
33 * @return New URI object or NULL on error.
35 coap_uri_t *coap_new_uri(const unsigned char *uri, unsigned int length);
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);
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.
47 * @param path The URI path to generate hash for.
48 * @param len The length of @p path.
49 * @param key The output buffer.
51 * @return @c 1 if @p key was set, @c 0 otherwise.
53 int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key);
56 * @defgroup uri_parse URI Parsing Functions
58 * CoAP PDUs contain normalized URIs with their path and query split into
59 * multiple segments. The functions in this module help splitting strings.
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.
69 * unsigned char *token;
70 * coap_parse_iterator_t pi;
71 * coap_parse_iterator_init(uri.path.s, uri.path.length, '/', "?#", 2, &pi);
73 * while ((token = coap_parse_next(&pi))) {
74 * ... do something with token ...
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;
88 * Initializes the given iterator @p pi.
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.
97 * @return The initialized iterator object @p pi.
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);
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.
112 * @param pi The iterator to update.
114 * @return The next token or @c NULL if no more tokens exist.
116 unsigned char *coap_parse_next(coap_parse_iterator_t *pi);
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
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.
131 * @note The host name part will be converted to lower case by this
135 coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri);
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.
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.
148 * @return The number of segments created or @c -1 on error.
150 int coap_split_path(const unsigned char *s, size_t length,
151 unsigned char *buf, size_t *buflen);
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.
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.
164 * @return The number of segments created or @c -1 on error.
166 * @bug This function does not reserve additional space for delta > 12.
168 int coap_split_query(const unsigned char *s, size_t length,
169 unsigned char *buf, size_t *buflen);
173 #endif /* _COAP_URI_H_ */