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 */
24 unsigned char secure; /**< uses coaps URI schema */
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.
34 * @return New URI object or NULL on error.
36 coap_uri_t *coap_new_uri(const unsigned char *uri, unsigned int length);
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);
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.
48 * @param path The URI path to generate hash for.
49 * @param len The length of @p path.
50 * @param key The output buffer.
52 * @return @c 1 if @p key was set, @c 0 otherwise.
54 int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key);
57 * @defgroup uri_parse URI Parsing Functions
59 * CoAP PDUs contain normalized URIs with their path and query split into
60 * multiple segments. The functions in this module help splitting strings.
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.
70 * unsigned char *token;
71 * coap_parse_iterator_t pi;
72 * coap_parse_iterator_init(uri.path.s, uri.path.length, '/', "?#", 2, &pi);
74 * while ((token = coap_parse_next(&pi))) {
75 * ... do something with token ...
80 size_t n; /**< number of remaining characters in buffer */
81 unsigned char separator; /**< segment separators */
82 unsigned char *delim; /**< delimiters where to split the string */
83 size_t dlen; /**< length of separator */
84 unsigned char *pos; /**< current position in buffer */
85 size_t segment_length; /**< length of current segment */
86 } coap_parse_iterator_t;
89 * Initializes the given iterator @p pi.
91 * @param s The string to tokenize.
92 * @param n The length of @p s.
93 * @param separator The separator character that delimits tokens.
94 * @param delim A set of characters that delimit @s.
95 * @param dlen The length of @p delim.
96 * @param pi The iterator object to initialize.
98 * @return The initialized iterator object @p pi.
100 coap_parse_iterator_t *
101 coap_parse_iterator_init(unsigned char *s, size_t n,
102 unsigned char separator,
103 unsigned char *delim, size_t dlen,
104 coap_parse_iterator_t *pi);
107 * Updates the iterator @p pi to point to the next token. This
108 * function returns a pointer to that token or @c NULL if no more
109 * tokens exist. The contents of @p pi will be updated. In particular,
110 * @c pi->segment_length specifies the length of the current token, @c
111 * pi->pos points to its beginning.
113 * @param pi The iterator to update.
115 * @return The next token or @c NULL if no more tokens exist.
117 unsigned char *coap_parse_next(coap_parse_iterator_t *pi);
120 * Parses a given string into URI components. The identified syntactic
121 * components are stored in the result parameter @p uri. Optional URI
122 * components that are not specified will be set to { 0, 0 }, except
123 * for the port which is set to @c COAP_DEFAULT_PORT. This function
124 * returns @p 0 if parsing succeeded, a value less than zero
127 * @param str_var The string to split up.
128 * @param len The actual length of @p str_var
129 * @param uri The coap_uri_t object to store the result.
130 * @return @c 0 on success, or < 0 on error.
132 * @note The host name part will be converted to lower case by this
136 coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri);
139 * Splits the given URI path into segments. Each segment is preceded
140 * by an option pseudo-header with delta-value 0 and the actual length
141 * of the respective segment after percent-decoding.
143 * @param s The path string to split.
144 * @param length The actual length of @p s.
145 * @param buf Result buffer for parsed segments.
146 * @param buflen Maximum length of @p buf. Will be set to the actual number
147 * of bytes written into buf on success.
149 * @return The number of segments created or @c -1 on error.
151 int coap_split_path(const unsigned char *s, size_t length,
152 unsigned char *buf, size_t *buflen);
155 * Splits the given URI query into segments. Each segment is preceded
156 * by an option pseudo-header with delta-value 0 and the actual length
157 * of the respective query term.
159 * @param s The query string to split.
160 * @param length The actual length of @p s.
161 * @param buf Result buffer for parsed segments.
162 * @param buflen Maximum length of @p buf. Will be set to the actual number
163 * of bytes written into buf on success.
165 * @return The number of segments created or @c -1 on error.
167 * @bug This function does not reserve additional space for delta > 12.
169 int coap_split_query(const unsigned char *s, size_t length,
170 unsigned char *buf, size_t *buflen);
174 #endif /* _COAP_URI_H_ */