GNUnet  0.11.x
Data Structures | Functions
gnunet_uri_lib.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GNUNET_Uri
 Copyright (C) 2016 Jack Engqvist Johansson. More...
 
struct  GNUNET_UriParam
 

Functions

int GNUNET_uri_parse (struct GNUNET_Uri *url, char *url_str)
 Parse a URL to a struct. More...
 
int GNUNET_uri_split_path (char *path, char **parts, int max_parts)
 Split a path into several strings. More...
 
int GNUNET_uri_parse_query (char *query, char delimiter, struct GNUNET_UriParam *params, int max_params)
 Parse a query string into a key/value struct. More...
 

Function Documentation

◆ GNUNET_uri_parse()

int GNUNET_uri_parse ( struct GNUNET_Uri url,
char *  u 
)

Parse a URL to a struct.

The URL string should be in one of the following formats:

Absolute URL: scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ] [ "#" fragment ]

Relative URL: path [ "?" query ] [ "#" fragment ]

The following parts will be parsed to the corresponding struct member.

*url: a pointer to the struct where to store the parsed values. *url_str: a pointer to the url to be parsed (null terminated). The string will be modified.

Returns 0 on success, otherwise -1.

Definition at line 159 of file uri.c.

References find_fragment(), find_path(), find_query(), GNUNET_Uri::fragment, GNUNET_Uri::host, is_relative(), natoi(), parse_scheme(), GNUNET_Uri::password, GNUNET_Uri::path, GNUNET_Uri::port, GNUNET_Uri::query, GNUNET_Uri::scheme, and GNUNET_Uri::username.

161 {
162  if (NULL == url || NULL == u) {
163  return -1;
164  }
165 
166  memset(url, 0, sizeof (struct GNUNET_Uri));
167 
168  /* (Fragment) */
169  url->fragment = find_fragment (u);
170 
171  /* (Query) */
172  url->query = find_query (u);
173 
174  /* Relative URL? Parse scheme and hostname */
175  if (!is_relative (u)) {
176  /* Scheme */
177  url->scheme = u;
178  u = parse_scheme (u);
179  if (u == NULL) {
180  return -1;
181  }
182 
183  /* Host */
184  if ('\0' == *u) {
185  return -1;
186  }
187  url->host = u;
188 
189  /* (Path) */
190  url->path = find_path (u);
191 
192  /* (Credentials) */
193  u = strchr (url->host, '@');
194  if (NULL != u) {
195  /* Missing credentials? */
196  if (u == url->host) {
197  return -1;
198  }
199 
200  url->username = url->host;
201  url->host = u + 1;
202  *u = '\0';
203 
204  u = strchr (url->username, ':');
205  if (NULL == u) {
206  return -1;
207  }
208 
209  url->password = u + 1;
210  *u = '\0';
211  }
212 
213  /* Missing hostname? */
214  if ('\0' == *url->host) {
215  return -1;
216  }
217 
218  /* (Port) */
219  u = strchr (url->host, ':');
220  if (NULL != u && (NULL == url->path || u < url->path)) {
221  *(u++) = '\0';
222  if ('\0' == *u) {
223  return -1;
224  }
225 
226  if (url->path) {
227  url->port = natoi (u, url->path - u - 1);
228  } else {
229  url->port = atoi (u);
230  }
231  }
232 
233  /* Missing hostname? */
234  if ('\0' == *url->host) {
235  return -1;
236  }
237  } else {
238  /* (Path) */
239  url->path = find_path (u);
240  }
241 
242  return 0;
243 }
char * query
char * scheme
char * fragment
static char * find_fragment(char *str)
Definition: uri.c:119
char * password
char * username
static char * find_query(char *str)
Definition: uri.c:126
static int natoi(const char *str, size_t len)
Copyright (C) 2016,2017 Jack Engqvist Johansson.
Definition: uri.c:35
static char * parse_scheme(char *str)
Parse the scheme of a URL by inserting a null terminator after the scheme.
Definition: uri.c:70
Copyright (C) 2016 Jack Engqvist Johansson.
static int is_relative(const char *url)
Check if a URL is relative (no scheme and hostname).
Definition: uri.c:56
static char * find_path(char *str)
Definition: uri.c:133
Here is the call graph for this function:

◆ GNUNET_uri_split_path()

int GNUNET_uri_split_path ( char *  path,
char **  parts,
int  max_parts 
)

Split a path into several strings.

No data is copied, the slashed are used as null terminators and then pointers to each path part will be stored in **parts. Double slashes will be treated as one.

*path: the path to split. The string will be modified. **parts: a pointer to an array of (char *) where to store the result. max_parts: max number of parts to parse.

Returns the number of parsed items. -1 on error.

Definition at line 260 of file uri.c.

263 {
264  int i = 0;
265 
266  if (NULL == path || '\0' == *path) {
267  return -1;
268  }
269 
270  do {
271  /* Forward to after slashes */
272  while (*path == '/') path++;
273 
274  if ('\0' == *path) {
275  break;
276  }
277 
278  parts[i++] = path;
279 
280  path = strchr (path, '/');
281  if (NULL == path) {
282  break;
283  }
284 
285  *(path++) = '\0';
286  } while (i < max_parts);
287 
288  return i;
289 }

◆ GNUNET_uri_parse_query()

int GNUNET_uri_parse_query ( char *  query,
char  delimiter,
struct GNUNET_UriParam params,
int  max_params 
)

Parse a query string into a key/value struct.

The query string should be a null terminated string of parameters separated by a delimiter. Each parameter are checked for the equal sign character. If it appears in the parameter, it will be used as a null terminator and the part that comes after it will be the value of the parameter.

No data are copied, the equal sign and delimiters are used as null terminators and then pointers to each parameter key and value will be stored in the yuarel_param struct.

*query: the query string to parse. The string will be modified. delimiter: the character that separates the key/value pairs from each other. *params: an array of (struct yuarel_param) where to store the result. max_values: max number of parameters to parse.

Returns the number of parsed items. -1 on error.

Definition at line 312 of file uri.c.

References key, GNUNET_UriParam::key, and GNUNET_UriParam::val.

316 {
317  int i = 0;
318 
319  if (NULL == query || '\0' == *query) {
320  return -1;
321  }
322 
323  params[i++].key = query;
324  while (i < max_params && NULL != (query = strchr (query, delimiter))) {
325  *query = '\0';
326  params[i].key = ++query;
327  params[i].val = NULL;
328 
329  /* Go back and split previous param */
330  if (i > 0) {
331  if ((params[i - 1].val = strchr (params[i - 1].key, '=')) != NULL) {
332  *(params[i - 1].val)++ = '\0';
333  }
334  }
335  i++;
336  }
337 
338  /* Go back and split last param */
339  if ((params[i - 1].val = strchr (params[i - 1].key, '=')) != NULL) {
340  *(params[i - 1].val)++ = '\0';
341  }
342 
343  return i;
344 }
struct GNUNET_HashCode key
The key used in the DHT.