summaryrefslogtreecommitdiff
path: root/src/util/uri.c
blob: fa383110d6be017ed439bc22032d28a38207f71b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
 * 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.
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gnunet_uri_lib.h"


/**
 * Parse a non null terminated string into an integer.
 *
 * str: the string containing the number.
 * len: Number of characters to parse.
 */
static inline int
natoi (const char *str,
       size_t len)
{
  int i, r = 0;
  for (i = 0; i < len; i++) {
    r *= 10;
    r += str[i] - '0';
  }

  return r;
}


/**
 * Check if a URL is relative (no scheme and hostname).
 *
 * url: the string containing the URL to check.
 *
 * Returns 1 if relative, otherwise 0.
 */
static inline int
is_relative (const char *url)
{
  return (*url == '/') ? 1 : 0;
}


/**
 * 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.
 */
static inline char *
parse_scheme (char *str)
{
  char *s;

  /* If not found or first in string, return error */
  s = strchr (str, ':');
  if (s == NULL || s == str) {
    return NULL;
  }

  /* If not followed by two slashes, return error */
  if (s[1] == '\0' || s[1] != '/' || s[2] == '\0' || s[2] != '/') {
    return NULL;
  }

  *s = '\0'; // Replace ':' with NULL

  return s + 3;
}


/**
 * 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.
 */
static inline char *
find_and_terminate (char *str,
                    char find)
{
  str = strchr(str, find);
  if (NULL == str) {
    return NULL;
  }

  *str = '\0';
  return str + 1;
}


/* Yes, the following functions could be implemented as preprocessor macros
     instead of inline functions, but I think that this approach will be more
     clean in this case. */
static inline char *
find_fragment (char *str)
{
  return find_and_terminate (str, '#');
}


static inline char *
find_query (char *str)
{
  return find_and_terminate (str, '?');
}


static inline char *
find_path (char *str)
{
  return find_and_terminate (str, '/');
}


/**
 * 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.
 */
int
GNUNET_uri_parse (struct GNUNET_Uri *url,
                  char *u)
{
  if (NULL == url || NULL == u) {
    return -1;
  }

  memset(url, 0, sizeof (struct GNUNET_Uri));

  /* (Fragment) */
  url->fragment = find_fragment (u);

  /* (Query) */
  url->query = find_query (u);

  /* Relative URL? Parse scheme and hostname */
  if (!is_relative (u)) {
    /* Scheme */
    url->scheme = u;
    u = parse_scheme (u);
    if (u == NULL) {
      return -1;
    }

    /* Host */
    if ('\0' == *u) {
      return -1;
    }
    url->host = u;

    /* (Path) */
    url->path = find_path (u);

    /* (Credentials) */
    u = strchr (url->host, '@');
    if (NULL != u) {
      /* Missing credentials? */
      if (u == url->host) {
        return -1;
      }

      url->username = url->host;
      url->host = u + 1;
      *u = '\0';

      u = strchr (url->username, ':');
      if (NULL == u) {
        return -1;
      }

      url->password = u + 1;
      *u = '\0';
    }

    /* Missing hostname? */
    if ('\0' == *url->host) {
      return -1;
    }

    /* (Port) */
    u = strchr (url->host, ':');
    if (NULL != u && (NULL == url->path || u < url->path)) {
      *(u++) = '\0';
      if ('\0' == *u) {
        return -1;
      }

      if (url->path) {
        url->port = natoi (u, url->path - u - 1);
      } else {
        url->port = atoi (u);
      }
    }

    /* Missing hostname? */
    if ('\0' == *url->host) {
      return -1;
    }
  } else {
    /* (Path) */
    url->path = find_path (u);
  }

  return 0;
}


/**
 * 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.
 */
int
GNUNET_uri_split_path (char *path,
                       char **parts,
                       int max_parts)
{
  int i = 0;

  if (NULL == path || '\0' == *path) {
    return -1;
  }

  do {
    /* Forward to after slashes */
    while (*path == '/') path++;

    if ('\0' == *path) {
      break;
    }

    parts[i++] = path;

    path = strchr (path, '/');
    if (NULL == path) {
      break;
    }

    *(path++) = '\0';
  } while (i < max_parts);

  return i;
}


/**
 * 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.
 */
int
GNUNET_uri_parse_query (char *query,
                        char delimiter,
                        struct GNUNET_UriParam *params,
                        int max_params)
{
  int i = 0;

  if (NULL == query || '\0' == *query) {
    return -1;
  }

  params[i++].key = query;
  while (i < max_params && NULL != (query = strchr (query, delimiter))) {
    *query = '\0';
    params[i].key = ++query;
    params[i].val = NULL;

    /* Go back and split previous param */
    if (i > 0) {
      if ((params[i - 1].val = strchr (params[i - 1].key, '=')) != NULL) {
        *(params[i - 1].val)++ = '\0';
      }
    }
    i++;
  }

  /* Go back and split last param */
  if ((params[i - 1].val = strchr (params[i - 1].key, '=')) != NULL) {
    *(params[i - 1].val)++ = '\0';
  }

  return i;
}