File: url.c

package info (click to toggle)
html-xml-utils 7.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,488 kB
  • sloc: ansic: 11,213; sh: 7,996; lex: 243; makefile: 193; yacc: 125
file content (415 lines) | stat: -rwxr-xr-x 12,299 bytes parent folder | download | duplicates (2)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * Routines and data structures to parse URLs
 *
 * Assumes the strings are encoded in UTF-8
 *
 * Bug: URL_s_absolutize("foo/bar", "../") yields "" but should return "./"
 *
 * Copyright © 1994-2011 World Wide Web Consortium
 * See http://www.w3.org/Consortium/Legal/copyright-software
 *
 * Author: Bert Bos <bert@w3.org>
 * Created: 7 March 1999
 */
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#if STDC_HEADERS
# include <string.h>
#else
# ifndef HAVE_STRCHR
#  define strchr index
#  define strrchr rindex
# endif
# ifndef HAVE_STRSTR
#  include "strstr.e"
# endif
#endif
#include <ctype.h>
#include <regex.h>
#include <err.h>
#include <sysexits.h>
#if HAVE_LIBIDN2
# include <idn2.h>
#elif HAVE_LIBIDN
# include <idna.h>
#endif
#include "export.h"
#include "heap.e"
#include "types.e"

EXPORT typedef struct {
  string full;					/* Full URL as a string */
  string proto;					/* Protocol */
  string user;					/* User name */
  string password;				/* User's password */
  string machine;				/* Domain name or IP number */
  string port;					/* Port number or service */
  string path;					/* Path part of URL */
  string query;					/* Query part of URL */
  string fragment;				/* Fragment ID part of URL */
} *URL;


/* idn_to_ascii -- convert UTF-8 to Punycode, allocate on heap */
static string idn_to_ascii(const conststring s)
{
#if HAVE_LIBIDN2
  string p;
  int rc;

  rc = idn2_lookup_u8((uint8_t*)s, (uint8_t**)&p, IDN2_NFC_INPUT);
  if (rc == IDN2_OK) return p;
  warnx("%s", idn2_strerror(rc));
  return newstring(s);
#elif HAVE_LIBIDN
  string p;
  Idna_rc rc;

  rc = idna_to_ascii_8z(s, &p, 0);
  if (rc == IDNA_SUCCESS) return p;
  warnx("%s", idna_strerror(rc));
  return newstring(s);
#else
  conststring p;

  for (p = s; *p; p++)
    if ((unsigned char)(*p) < 33 || (unsigned char)(*p) > 127)
      warnx("Internationalized domain names in URLs require libidn.");
  return newstring(s);
#endif
}

/* utf8tohex -- convert UTF-8 to %HH hex encoding, allocate on heap */
static string utf8tohex(const conststring s)
{
  static string hex = "0123456789ABCDEF";
  string h;
  int i, j;

  if (!s) return NULL;
  newarray(h, 3 * strlen(s) + 1);		/* Usually too much */
  for (i = 0, j = 0; s[i]; i++) {
    if (s[i] & 0x80) {				/* Not ASCII */
      h[j++] = '%';
      h[j++] = hex[((unsigned char)s[i])/16];
      h[j++] = hex[((unsigned char)s[i])%16];
    } else if (s[i] == ' ') {			/* Also escape spaces */
      h[j++] = '%'; h[j++] = '2'; h[j++] = '0';
    } else
      h[j++] = s[i];
  }
  h[j] = '\0';
  return h;
}

/* URL_dispose -- free the memory used by a URL struct */
EXPORT void URL_dispose(URL url)
{
  if (url) {
    dispose(url->full);
    dispose(url->proto);
    dispose(url->user);
    dispose(url->password);
    dispose(url->machine);
    dispose(url->port);
    dispose(url->path);
    dispose(url->query);
    dispose(url->fragment);
    dispose(url);
  }
}

/* URL_new -- create a new URL struct; return NULL if not a valid URL */
EXPORT URL URL_new(const conststring url)
{
#define PROTO    "([^:/?#]+)"
  /*              2--------2						*/
#define USER     "([^/?#@:[]*)"
  /*              5----------5						*/
#define PASSWORD "([^/?#@[]*)"
  /*              7---------7						*/
#define HOST     "(([^/?#:[]+)|\\[([0-9a-fA-F:]*)])?"
  /*              89---------9----A-------------A-8			*/
#define PORT     "([^/?#]*)"
  /*              C-------C						*/
#define AUTH     "(" USER "(:" PASSWORD ")?@)?" HOST "(:" PORT ")?"
  /*              4--5--5--6---7------7--6--4   8--8  B---C--C--B	*/
#define PATH     "([^?#[]*)"
  /*              D------D						*/
#define QUERY    "([^#]*)"
  /*              F---F							*/
#define FRAGM    "(.*)"
  /*              H--H							*/

#define PAT  "(" PROTO ":)?(//" AUTH ")?" PATH "(\\?" QUERY ")?(#" FRAGM ")?"
  /*          1  2---2   1 3          3   D--D  E     F---F  E G   H---H  G */

   /*
   * 2 = proto, 5 = user, 7 = password, 9/A = machine, C = port, D = path,
   * F = query, H = fragment
   */
# define MAXSUB 18
  static regex_t re;
  static int initialized = 0;
  regmatch_t pm[MAXSUB];
  URL result;

  assert(url != NULL);
  
  /* Compile the regexp, only once */
  if (! initialized) {
    assert(regcomp(&re, PAT, REG_EXTENDED) == 0); /* Could be memory... */
    initialized = 1;
  }

  /* Match the URL against the pattern; return NULL if no match */
  if (regexec(&re, url, MAXSUB, pm, 0) != 0) return NULL;

  /* Store the various parts */
  new(result);
  result->full = newstring(url);
  result->proto = pm[2].rm_so == -1
    ? NULL : down(newnstring(url, pm[2].rm_eo));
  result->user = pm[5].rm_so == -1
    ? NULL : newnstring(url + pm[5].rm_so, pm[5].rm_eo - pm[5].rm_so);
  result->password = pm[7].rm_so == -1
    ? NULL : newnstring(url + pm[7].rm_so, pm[7].rm_eo - pm[7].rm_so);
  result->machine = pm[9].rm_so != -1
    ? newnstring(url + pm[9].rm_so, pm[9].rm_eo - pm[9].rm_so)
    : pm[10].rm_so != -1
    ? newnstring(url + pm[10].rm_so, pm[10].rm_eo - pm[10].rm_so)
    : NULL;
  result->port = pm[12].rm_so == -1
    ? NULL : newnstring(url + pm[12].rm_so, pm[12].rm_eo - pm[12].rm_so);
  result->path = pm[13].rm_so == -1
    ? NULL : newnstring(url + pm[13].rm_so, pm[13].rm_eo - pm[13].rm_so);
  result->query = pm[15].rm_so == -1
    ? NULL : newnstring(url + pm[15].rm_so, pm[15].rm_eo - pm[15].rm_so);
  result->fragment = pm[17].rm_so == -1
    ? NULL : newnstring(url + pm[17].rm_so, pm[17].rm_eo - pm[17].rm_so);
  return result;
}

/* merge -- merge a base path and a relative path */
static string  merge(const URL base, const string path)
{
  string s;
  int j;

  if (base->machine && (!base->path || !base->path[0])) {
    newarray(s, strlen(path) + 2);
    s[0] = '/';
    strcpy(s + 1, path);
  } else if (!base->path) {
    s = newstring(path);
  } else {
    for (j = strlen(base->path); j > 0 && base->path[j-1] != '/'; j--);
    newarray(s, j + strlen(path) + 1);
    memmove(s, base->path, j);
    strcpy(s + j, path);
  }
  return s;
}

/* remove_dot_segments -- remove /./ and /foo/../ */
static void remove_dot_segments(string path)
{
  int i = 0, len = strlen(path), j;

  while (len) {
    if (hasprefix(path + i, "/../")) {
      len -= 3;
      if (i == 0) {
	memmove(path + 1, path + 4, len);
      } else {
	for (j = i - 1; j > 0 && path[j-1] != '/'; j--) ;
	if (!hasprefix(path + j, "../")) {
	  memmove(path + j, path + i + 4, len);
	  i = j != 0 ? j - 1 : 0;
	} else {
	  i += 3;
	}
      }
    } else if (eq(path + i, "/..")) {
      len = 0;
      if (i == 0) {
	path[1] = '\0';
	i = 1;
      } else {
	for (j = i - 1; j > 0 && path[j-1] != '/'; j--) ;
	if (!hasprefix(path + j, "../")) {
	  path[j] = '\0';
	  i = j;
	} else {
	  i += 3;
	}
      }
    } else if (hasprefix(path + i, "/./")) {
      memmove(path + i, path + i + 2, len - 1);
      len -= 2;
    } else if (eq(path + i, "/.")) {
      path[i+1] = '\0';
      len--;
    } else {
      i++;
      len--;
    }
  }
}

/* URL_absolutize -- make a relative URL absolute */
EXPORT URL URL_absolutize(const URL base, const URL url)
{
  URL abs;

  new(abs);

  /* RFC 3986, section 5.2.2 */
  if (url->proto) {
    abs->proto = newstring(url->proto);
    abs->user = newstring(url->user);
    abs->password = newstring(url->password);
    abs->machine = newstring(url->machine);
    abs->port = newstring(url->port);
    abs->path = newstring(url->path);
    remove_dot_segments(abs->path);
    abs->query = newstring(url->query);
  } else {
    if (url->machine) {
      abs->user = newstring(url->user);
      abs->password = newstring(url->password);
      abs->machine = newstring(url->machine);
      abs->port = newstring(url->port);
      abs->path = newstring(url->path);
      remove_dot_segments(abs->path);
      abs->query = newstring(url->query);
    } else {
      if (!url->path || !url->path[0]) {
	abs->path = newstring(base->path);
	if (url->query) {
	  abs->query = newstring(url->query);
	} else {
	  abs->query = newstring(base->query);
	}
      } else {
	if (url->path[0] == '/') {
	  abs->path = newstring(url->path);
	  remove_dot_segments(abs->path);
	} else {
	  abs->path = merge(base, url->path);
	  remove_dot_segments(abs->path);
	}
	abs->query = newstring(url->query);
      }
      abs->user = newstring(base->user);
      abs->password = newstring(base->password);
      abs->machine = newstring(base->machine);
      abs->port = newstring(base->port);
    }
    abs->proto = newstring(base->proto);
  }
  abs->fragment = newstring(url->fragment);

  newarray(abs->full, (abs->proto ? strlen(abs->proto) + 1 : 0)
	   + (abs->user ? strlen(abs->user) + 1 : 0)
	   + (abs->password ? strlen(abs->password) + 1 : 0)
	   + (abs->machine ? strlen(abs->machine) + 4 : 0)
	   + (abs->port ? strlen(abs->port) + 1 : 0)
	   + (abs->path ? strlen(abs->path) : 0)
	   + (abs->query ? strlen(abs->query) + 1 : 0)
	   + (abs->fragment ? strlen(abs->fragment) + 1 : 0)
	   + 1);
  sprintf(abs->full, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
	  abs->proto ? abs->proto : (string) "",
	  abs->proto ? (string) ":" : (string) "",
	  abs->machine ? (string) "//" : (string) "",
	  abs->user ? abs->user : (string) "",
	  abs->password ? (string) ":" : (string) "",
	  abs->password ? abs->password : (string) "",
	  abs->user ? (string) "@" : (string) "",
	  abs->machine && strchr(abs->machine, ':') ? "[" : "",
	  abs->machine ? abs->machine : (string) "",
	  abs->machine && strchr(abs->machine, ':') ? "]" : "",
	  abs->port ? (string) ":" : (string) "",
	  abs->port ? abs->port : (string) "",
	  abs->path ? abs->path : (string) "",
	  abs->query ? "?" : (string) "",
	  abs->query ? abs->query : (string) "",
	  abs->fragment ? (string) "#" : (string) "",
	  abs->fragment ? abs->fragment : (string) "");
  /* Instead of strchr() above, we could have an IPv6 flag. Necessary? */

  return abs;
}

/* URL_s_absolutize -- make a relative URL absolute */
EXPORT string URL_s_absolutize(const conststring base, const conststring url)
{
  URL url1 = URL_new(url), base1 = URL_new(base);
  URL abs = URL_absolutize(base1, url1);
  string result = newstring(abs->full);
  URL_dispose(abs);
  URL_dispose(url1);
  URL_dispose(base1);
  return result;
}

/* URL_to_ascii -- use punycode and %-escaping to turn an IRI into a URI */
EXPORT URL URL_to_ascii(const URL iri)
{
  URL url;

  new(url);
  url->proto = newstring(iri->proto);
  url->user = newstring(iri->user);
  url->password = newstring(iri->password);
  url->machine = idn_to_ascii(iri->machine);
  url->port = newstring(iri->port);
  url->path = utf8tohex(iri->path);
  url->query = utf8tohex(iri->query);
  url->fragment = utf8tohex(iri->fragment);

  newarray(url->full, (url->proto ? strlen(url->proto) + 1 : 0)
	   + (url->user ? strlen(url->user) + 1 : 0)
	   + (url->password ? strlen(url->password) + 1 : 0)
	   + (url->machine ? strlen(url->machine) + 4 : 0)
	   + (url->port ? strlen(url->port) + 1 : 0)
	   + (url->path ? strlen(url->path) : 0)
	   + (url->query ? strlen(url->query) + 1 : 0)
	   + (url->fragment ? strlen(url->fragment) + 1 : 0)
	   + 1);
  sprintf(url->full, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
	  url->proto ? url->proto : (string) "",
	  url->proto ? (string) ":" : (string) "",
	  url->machine ? (string) "//" : (string) "",
	  url->user ? url->user : (string) "",
	  url->password ? (string) ":" : (string) "",
	  url->password ? url->password : (string) "",
	  url->user ? (string) "@" : (string) "",
	  url->machine && strchr(url->machine, ':') ? "[" : "",
	  url->machine ? url->machine : (string) "",
	  url->machine && strchr(url->machine, ':') ? "]" : "",
	  url->port ? (string) ":" : (string) "",
	  url->port ? url->port : (string) "",
	  url->path ? url->path : (string) "",
	  url->query ? "?" : (string) "",
	  url->query ? url->query : (string) "",
	  url->fragment ? (string) "#" : (string) "",
	  url->fragment ? url->fragment : (string) "");
  /* Instead of strchr() above, we could have an IPv6 flag. Necessary? */

  return url;
}

/* URL_s_to_ascii -- use punycode and %-escaping to turn an IRI into a URI */
EXPORT string URL_s_to_ascii(const conststring iri)
{
  URL x = URL_new(iri);
  URL y = URL_to_ascii(x);
  string result = newstring(y->full);
  URL_dispose(y);
  URL_dispose(x);
  return result;
}