GNUnet  0.20.0
uri.c File Reference
#include "platform.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gnunet_uri_lib.h"
Include dependency graph for uri.c:

Go to the source code of this file.

Functions

static int natoi (const char *str, size_t len)
 Copyright (C) 2016,2017 Jack Engqvist Johansson. More...
 
static int is_relative (const char *url)
 Check if a URL is relative (no scheme and hostname). More...
 
static char * parse_scheme (char *str)
 Parse the scheme of a URL by inserting a null terminator after the scheme. More...
 
static char * find_and_terminate (char *str, char find)
 Find a character in a string, replace it with '\0' and return the next character in the string. More...
 
static char * find_fragment (char *str)
 
static char * find_query (char *str)
 
static char * find_path (char *str)
 
int GNUNET_uri_parse (struct GNUNET_Uri *url, char *u)
 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

◆ natoi()

static int natoi ( const char *  str,
size_t  len 
)
inlinestatic

Copyright (C) 2016,2017 Jack Engqvist Johansson.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Parse a non null terminated string into an integer.

str: the string containing the number. len: Number of characters to parse.

Definition at line 36 of file uri.c.

38 {
39  int i, r = 0;
40  for (i = 0; i < len; i++) {
41  r *= 10;
42  r += str[i] - '0';
43  }
44 
45  return r;
46 }
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...

References len.

Referenced by GNUNET_uri_parse().

Here is the caller graph for this function:

◆ is_relative()

static int is_relative ( const char *  url)
inlinestatic

Check if a URL is relative (no scheme and hostname).

url: the string containing the URL to check.

Returns 1 if relative, otherwise 0.

Definition at line 57 of file uri.c.

58 {
59  return (*url == '/') ? 1 : 0;
60 }

Referenced by GNUNET_uri_parse().

Here is the caller graph for this function:

◆ parse_scheme()

static char* parse_scheme ( char *  str)
inlinestatic

Parse the scheme of a URL by inserting a null terminator after the scheme.

str: the string containing the URL to parse. Will be modified.

Returns a pointer to the hostname on success, otherwise NULL.

Definition at line 71 of file uri.c.

72 {
73  char *s;
74 
75  /* If not found or first in string, return error */
76  s = strchr (str, ':');
77  if (s == NULL || s == str) {
78  return NULL;
79  }
80 
81  /* If not followed by two slashes, return error */
82  if (s[1] == '\0' || s[1] != '/' || s[2] == '\0' || s[2] != '/') {
83  return NULL;
84  }
85 
86  *s = '\0'; // Replace ':' with NULL
87 
88  return s + 3;
89 }

Referenced by GNUNET_uri_parse().

Here is the caller graph for this function:

◆ find_and_terminate()

static char* find_and_terminate ( char *  str,
char  find 
)
inlinestatic

Find a character in a string, replace it with '\0' and return the next character in the string.

str: the string to search in. find: the character to search for.

Returns a pointer to the character after the one to search for. If not found, NULL is returned.

Definition at line 103 of file uri.c.

105 {
106  str = strchr(str, find);
107  if (NULL == str) {
108  return NULL;
109  }
110 
111  *str = '\0';
112  return str + 1;
113 }

Referenced by find_fragment(), find_path(), and find_query().

Here is the caller graph for this function:

◆ find_fragment()

static char* find_fragment ( char *  str)
inlinestatic

Definition at line 120 of file uri.c.

121 {
122  return find_and_terminate (str, '#');
123 }
static char * find_and_terminate(char *str, char find)
Find a character in a string, replace it with '\0' and return the next character in the string.
Definition: uri.c:103

References find_and_terminate().

Referenced by GNUNET_uri_parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_query()

static char* find_query ( char *  str)
inlinestatic

Definition at line 127 of file uri.c.

128 {
129  return find_and_terminate (str, '?');
130 }

References find_and_terminate().

Referenced by GNUNET_uri_parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_path()

static char* find_path ( char *  str)
inlinestatic

Definition at line 134 of file uri.c.

135 {
136  return find_and_terminate (str, '/');
137 }

References find_and_terminate().

Referenced by GNUNET_uri_parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 160 of file uri.c.

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

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.

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 261 of file uri.c.

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

◆ 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 313 of file uri.c.

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

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