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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* The GIMP Help Browser - URI functions
* Copyright (C) 2001 Jacob Schroeder <jacob@convergence.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <glib.h>
#include "uri.h"
/* #define URI_DEBUG 1 */
typedef enum
{
URI_UNKNOWN,
URI_ABSURI,
URI_NETPATH,
URI_ABSPATH,
URI_RELPATH,
URI_QUERY,
URI_EMPTY,
URI_FRAGMENT,
URI_INVALID
} UriType;
static UriType
uri_get_type (const gchar *uri)
{
gchar c;
const gchar *cptr;
UriType type = URI_UNKNOWN;
if (!uri)
return type;
cptr = uri;
c = *cptr++;
if (g_ascii_isalpha (c))
{
type = URI_RELPATH; /* assume relative path */
while ((c = *cptr++))
{
if (g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.')
continue;
if (c == ':')
{
/* it was a scheme */
type = URI_ABSURI;
}
break;
}
}
else
{
switch (c)
{
case '/':
if (*cptr == '/')
{
cptr++;
type = URI_NETPATH;
}
else
{
type = URI_ABSPATH;
}
break;
case '?':
type = URI_QUERY;
break;
case '#':
type = URI_FRAGMENT;
break;
case '\0':
type = URI_EMPTY;
break;
default:
type = URI_RELPATH;
break;
}
}
#ifdef URI_DEBUG
g_print ("uri_get_type (\"%s\") -> ", uri);
switch (type)
{
case URI_UNKNOWN: g_print ("unknown"); break;
case URI_ABSURI: g_print ("absuri"); break;
case URI_NETPATH: g_print ("netpath"); break;
case URI_ABSPATH: g_print ("abspath"); break;
case URI_RELPATH: g_print ("relpath"); break;
case URI_QUERY: g_print ("query"); break;
case URI_EMPTY: g_print ("empty"); break;
case URI_FRAGMENT: g_print ("fragment"); break;
case URI_INVALID: g_print ("invalid"); break;
}
g_print ("\n");
#endif
return type;
}
gchar *
uri_to_abs (const gchar *uri,
const gchar *base_uri)
{
gchar c;
const gchar *cptr;
gchar *retval = NULL;
UriType uri_type = URI_UNKNOWN;
UriType base_type = URI_UNKNOWN;
gint base_cnt = 0; /* no of chars to be copied from base URI */
gint uri_cnt = 0; /* no of chars to be copied from URI */
gint sep_cnt = 0; /* no of chars to be inserted between them */
const gchar *sep_str = ""; /* string to insert between base and uri */
const gchar *part;
const gchar *last_segment = NULL;
#ifdef URI_DEBUG
g_print ("uri_to_abs (\"%s\", \"%s\")\n", uri, base_uri);
#endif
/* this function does not use the algorithm that is being proposed
* in RFC 2396. Instead it analyses the first characters of each
* URI to determine its kind (abs, net, path, ...).
* After that it locates the missing parts in the base URI and then
* concats everything into a newly allocated string.
*/
/* determine the kind of the URIs */
uri_type = uri_get_type (uri);
if (uri_type != URI_ABSURI)
{
base_type = uri_get_type (base_uri);
if (base_type != URI_ABSURI)
return NULL; /* neither uri nor base uri are absolute */
}
/* find missing parts in base URI */
switch (uri_type)
{
case URI_ABSURI:
/* base uri not needed */
break;
case URI_QUERY:
/* ??? last segment? */
uri_type = URI_RELPATH;
case URI_NETPATH: /* base scheme */
case URI_ABSPATH: /* base scheme and authority */
case URI_RELPATH: /* base scheme, authority and path */
cptr = base_uri;
/* skip scheme */
while ((c = *cptr++) && c != ':')
; /* nada */
base_cnt = cptr - base_uri; /* incl : */
if (*cptr != '/')
{
/* completion not possible */
return NULL;
}
if (uri_type == URI_NETPATH)
break;
/* skip authority */
if (cptr[0] == '/' && cptr[1] == '/')
{
part = cptr;
cptr += 2;
while ((c = *cptr++) && c != '/' && c != '?' && c != '#')
; /* nada */
cptr--;
base_cnt += cptr - part;
}
if (uri_type == URI_ABSPATH)
break;
/* skip path */
if (*cptr != '/')
{
sep_cnt = 1;
sep_str = "/";
break;
}
part = cptr;
g_assert (*cptr == '/');
while ((c = *cptr++) && c != '?' && c != '#')
{
if (c == '/')
last_segment = cptr - 1;
};
g_assert (last_segment);
cptr = last_segment;
while ((c = *uri) && c == '.' && cptr > part)
{
gint shift_segment = 0;
c = uri[1];
if (c == '.' )
{
c = uri[2];
shift_segment = 1;
}
if (c == '/')
{
uri += 2;
}
else if (c == 0 || c == '?' || c == '#')
{
uri += 1;
}
else
{
break;
}
g_assert (*cptr == '/');
if (shift_segment)
{
uri += 1;
while (cptr > part && *--cptr != '/')
; /* nada */
}
}
base_cnt += cptr - part + 1;
break;
case URI_EMPTY:
case URI_FRAGMENT:
/* use whole base uri */
base_cnt = strlen (base_uri);
break;
case URI_UNKNOWN:
case URI_INVALID:
return NULL;
}
/* do not include fragment part from the URI reference */
for (cptr = uri; (c = *cptr) && c != '#'; cptr++)
; /* nada */
uri_cnt = cptr - uri;
/* allocate string and copy characters */
retval = g_new (gchar, base_cnt + sep_cnt + uri_cnt + 1);
if (base_cnt)
strncpy (retval, base_uri, base_cnt);
if (sep_cnt)
strncpy (retval + base_cnt, sep_str, sep_cnt);
if (uri_cnt)
strncpy (retval + base_cnt + sep_cnt, uri, uri_cnt);
retval[base_cnt + sep_cnt + uri_cnt] = '\0';
#ifdef URI_DEBUG
g_print (" -> \"%s\"\n", retval);
#endif
return retval;
}
#if 0
RFC 2396 URI Generic Syntax August 1998
A. Collected BNF for URI
URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
absoluteURI = scheme ":" ( hier_part | opaque_part )
relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
hier_part = ( net_path | abs_path ) [ "?" query ]
opaque_part = uric_no_slash *uric
uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
"&" | "=" | "+" | "$" | ","
net_path = "//" authority [ abs_path ]
abs_path = "/" path_segments
rel_path = rel_segment [ abs_path ]
rel_segment = 1*( unreserved | escaped |
";" | "@" | "&" | "=" | "+" | "$" | "," )
scheme = alpha *( alpha | digit | "+" | "-" | "." )
authority = server | reg_name
reg_name = 1*( unreserved | escaped | "$" | "," |
";" | ":" | "@" | "&" | "=" | "+" )
server = [ [ userinfo "@" ] hostport ]
userinfo = *( unreserved | escaped |
";" | ":" | "&" | "=" | "+" | "$" | "," )
hostport = host [ ":" port ]
host = hostname | IPv4address
hostname = *( domainlabel "." ) toplabel [ "." ]
domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
toplabel = alpha | alpha *( alphanum | "-" ) alphanum
IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
port = *digit
path = [ abs_path | opaque_part ]
path_segments = segment *( "/" segment )
segment = *pchar *( ";" param )
param = *pchar
pchar = unreserved | escaped |
":" | "@" | "&" | "=" | "+" | "$" | ","
query = *uric
fragment = *uric
uric = reserved | unreserved | escaped
reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
"$" | ","
unreserved = alphanum | mark
mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
"(" | ")"
escaped = "%" hex hex
hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
"a" | "b" | "c" | "d" | "e" | "f"
alphanum = alpha | digit
alpha = lowalpha | upalpha
lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
"j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
"s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
"J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
"S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
"8" | "9"
#endif
|