summaryrefslogtreecommitdiffabout
path: root/lib/url.c
blob: 11d220d4f7bce553447add4d5430453da39aaacd (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
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
/* This file is part of GNU Pies
   Copyright (C) 2009, 2010, 2013, 2015 Sergey Poznyakoff
  
   GNU Pies 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, or (at your option)
   any later version.

   GNU Pies 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 GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include "libpies.h"
#include <wordsplit.h>

/* scheme://[user[:password]@][host[:port]/]path[;arg=str[;arg=str...] 
*/

static int
alloc_string_len (char **sptr, const char *start, size_t len)
{
  *sptr = malloc (len + 1);
  if (!*sptr)
    return 1;
  memcpy (*sptr, start, len);
  (*sptr)[len] = 0;
  return 0;
}

static int
alloc_string (char **sptr, const char *start, const char *end)
{
  size_t len = end ? end - start : strlen (start);
  return alloc_string_len (sptr, start, len);
}

static int
url_parse_args (struct pies_url *url, char **str)
{
  struct wordsplit ws;

  ws.ws_delim = ";";
  if (wordsplit (*str, &ws, WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM))
    return 1;
  url->argc = ws.ws_wordc;
  url->argv = ws.ws_wordv;
  return 0;
}

static int
url_parse_path (struct pies_url *url, char **str)
{
  char *p;

  p = strchr (*str, ';');
  if (!p)
    p = *str + strlen (*str);
  if (alloc_string (&url->path, *str, p))
    return 1;
  *str = p;
  if (*p)
    ++*str;
  return url_parse_args (url, str);
}

/* On input str points at the beginning of host part */
static int
url_parse_host (struct pies_url *url, char **str)
{
  char *s = *str;
  size_t len = strcspn (s, "/:");

  if (s[len] == ':')
    {
      char *start = s + len + 1;
      char *q;
      unsigned long n = strtoul (start, &q, 10);
      if (n > USHRT_MAX)
	return 1;
      if ((*q && !strchr ("/;", *q)))
	{
	  char *proto = url->proto_s ? url->proto_s : "tcp";
	  size_t size = strcspn (start, "/;");
	  struct servent *serv;
	  
	  alloc_string_len (&url->port_s, start, size);
	  serv = getservbyname (url->port_s, proto);
	  if (!serv)
	    return 1;
	  url->port = ntohs (serv->s_port);
	  *str = start + size;
	}
      else
	{
	  alloc_string_len (&url->port_s, start, q - start);
	  url->port = n;
	  *str = q;
	}
    }
  else
    *str = s + len;
  if (alloc_string_len (&url->host, s, len))
    return 1;
  if (**str)
    {
      if (*(*str)++ == '/')
	return url_parse_path (url, str);
      else
	return url_parse_args (url, str);
    }
  return 0;
}

/* On input str points past the mech:// part */
static int
url_parse_user (struct pies_url *url, char **str)
{
  if (**str == '/')
    return url_parse_path (url, str);
  else
    {
      size_t len = strcspn (*str, ":;@/");
      char *p = *str + len;
      
      switch (*p)
	{
	case ';':
	case ':':
	  len = strcspn (p + 1, "@/:");
	  if (p[len + 1] == '@')
	    {
	      if (alloc_string_len (&url->passwd, p + 1, len))
		return 1;
	      if (alloc_string (&url->user, *str, p))
		return 1;
	      *str = p + len + 2;
	    }
	  break;
	  
	case '@':
	  if (alloc_string (&url->user, *str, p))
	    return 1;
	  url->passwd = NULL;
	  *str = p + 1;
	}
      return url_parse_host (url, str);
    }
}

static int
url_parse_scheme (struct pies_url *url, const char *str)
{
  size_t len;
  char *p;
  
  if (!str)
    {
      errno = EINVAL;
      return 1;
    }

  len = strcspn (str, ":+");
  if (!str[len])
    {
      errno = EINVAL;
      return 1;
    }

  alloc_string_len (&url->scheme, str, len);

  str += len;
  
  if (*str == '+')
    {
      struct protoent *proto;

      len = strcspn (++str, ":");
      if (str[len] == 0)
	{
	  errno = EINVAL;
	  return 1;
	}
      alloc_string_len (&url->proto_s, str, len);
      str += len;
      proto = getprotobyname (url->proto_s);
      if (!proto)
	{
	  errno = EINVAL;
	  return 1;
	}
      url->proto = proto->p_proto;
    }
  else
    url->proto = 0;
  
  /* Skip slashes */
  p = (char*) str + 1;
  if (memcmp (p, "//", 2))
    {
      errno = EINVAL;
      return 1;
    }
  p += 2;
  return url_parse_user (url, &p);
}

void
pies_url_free_user (struct pies_url *url)
{
  if (url->user)
    {
      free (url->user);
      url->user = NULL;
    }
}

void
pies_url_free_passwd (struct pies_url *url)
{
  if (url->passwd)
    {
      memset (url->passwd, 0, strlen (url->passwd));
      free (url->passwd);
      url->passwd = NULL;
    }
}

void
pies_url_destroy (struct pies_url **purl)
{
  int i;
  if (purl && *purl)
    {
      struct pies_url *url = *purl;

      free (url->string);
      free (url->scheme);
      free (url->host);
      free (url->port_s);
      free (url->proto_s);
      free (url->path);
      pies_url_free_user (url);
      pies_url_free_passwd (url);
      for (i = 0; i < url->argc; i++)
	free (url->argv[i]);
      free (url->argv);
      free (url);
      *purl = NULL;
    }
}

static int
strasgn (char **dst, char *src)
{
  if (!src)
    *dst = NULL;
  else
    {
      *dst = strdup (src);
      if (!*dst)
	return 1;
    }
  return 0;
}
  
static int
do_url_copy (struct pies_url *dest, struct pies_url *src)
{
#define STRASGN(a,b) if (strasgn (a, b)) return -1;
  size_t i;
  
  STRASGN (&dest->string, src->string);
  STRASGN (&dest->scheme, src->scheme);
  STRASGN (&dest->host, src->host);
  STRASGN (&dest->port_s, src->port_s);
  dest->port = src->port;
  STRASGN (&dest->proto_s, src->proto_s);
  dest->proto = src->proto;
  STRASGN (&dest->path, src->path);
  STRASGN (&dest->user, src->user);
  STRASGN (&dest->passwd, src->passwd);
  dest->argv = calloc (src->argc + 1, sizeof (dest->argv[0]));
  if (!dest->argv)
    return -1;
  for (i = 0; i < src->argc; i++)
    {
      if (!(dest->argv[i] = strdup (src->argv[0])))
	{
	  dest->argc = i;
	  return -1;
	}
    }
  dest->argv[i] = NULL;
  dest->argc = i;
  return 0;
#undef STRASGN
}

int
pies_url_copy (struct pies_url **purl, struct pies_url *src)
{
  struct pies_url *dest = malloc (sizeof (*dest));
  
  if (!dest)
    return -1;
  memset (dest, 0, sizeof (*dest));
  if (do_url_copy (dest, src))
    {
      pies_url_destroy (&dest);
      return -1;
    }
  *purl = dest;
  return 0;
}

int
pies_basic_url_create (struct pies_url **purl, const char *str)
{
  int rc;
  struct pies_url *url;

  url = malloc (sizeof (*url));
  if (!url)
    return 1;
  memset (url, 0, sizeof (*url));
  rc = url_parse_scheme (url, str);
  if (rc)
    pies_url_destroy (&url);
  else
    {
      url->string = strdup (str);
      *purl = url;
    }
  return rc;
}

extern char **environ;

int
pies_url_create (struct pies_url **purl, const char *str)
{
  struct wordsplit ws;
  int rc;
  
  ws.ws_env = (const char**) environ;
  if (wordsplit (str, &ws,
		 WRDSF_NOCMD | WRDSF_QUOTE | WRDSF_NOSPLIT | WRDSF_ENV))
    return -1;
  rc = pies_basic_url_create (purl, ws.ws_wordv[0]);
  wordsplit_free (&ws);
  return rc;
}

const char *
pies_url_get_arg (struct pies_url *url, const char *argname)
{
  int i;
  size_t arglen = strlen (argname);
  for (i = 0; i < url->argc; i++)
    {
      size_t len = strcspn (url->argv[i], "=");
      if (len == arglen && memcmp (url->argv[i], argname, arglen) == 0)
	return url->argv[i] + len + 1;
    }
  return NULL;
}

Return to:

Send suggestions and report system problems to the System administrator.