url.c
5.42 KB
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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <url0.h>
#include <registrar.h>
#include <cpystr.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
/* Forward prototypes */
static int get_scheme (const url_t, char *, size_t, size_t *);
static int get_user (const url_t, char *, size_t, size_t *);
static int get_passwd (const url_t, char *, size_t, size_t *);
static int get_host (const url_t, char *, size_t, size_t *);
static int get_port (const url_t, long *);
static int get_path (const url_t, char *, size_t, size_t *);
static int get_query (const url_t, char *, size_t, size_t *);
static int get_id (const url_t, int *);
int
url_init (url_t * purl, const char *name)
{
int status = EINVAL;
struct url_registrar *ureg;
struct mailbox_registrar *mreg;
registrar_t reg = NULL;
size_t name_len;
int id;
/* Sanity checks */
if (name == NULL || *name == '\0')
return status;
name_len = strlen (name);
/* Search for a known scheme */
while (registrar_list (&ureg, &mreg, &id, ®) == 0)
{
size_t scheme_len;
if (ureg && ureg->scheme &&
name_len > (scheme_len = strlen (ureg->scheme)) &&
memcmp (name, ureg->scheme, scheme_len) == 0)
{
status = 0;
break;
}
}
/* Found one initialize it */
if (status == 0)
{
status = ureg->_init (purl, name);
if (status == 0)
{
url_t url = *purl;
url->id = id;
if (url->_get_id == NULL)
url->_get_id = get_id;
if (url->_get_scheme == NULL)
url->_get_scheme = get_scheme;
if (url->_get_user == NULL)
url->_get_user = get_user;
if (url->_get_passwd == NULL)
url->_get_passwd = get_passwd;
if (url->_get_host == NULL)
url->_get_host = get_host;
if (url->_get_port == NULL)
url->_get_port = get_port;
if (url->_get_path == NULL)
url->_get_path = get_path;
if (url->_get_query == NULL)
url->_get_query = get_query;
}
}
return status;
}
void
url_destroy (url_t *purl)
{
if (purl && *purl)
{
struct url_registrar *ureg;
int id;
url_get_id (*purl, &id);
registrar_get (id, &ureg, NULL);
ureg->_destroy(purl);
(*purl) = NULL;
}
}
int (url_get_scheme) (const url_t url, char *scheme, size_t len, size_t *n)
{
return (url) ? url->_get_scheme(url, scheme, len, n) : EINVAL;
}
int (url_get_user) (const url_t url, char *user, size_t len, size_t *n)
{
return (url) ? url->_get_user(url, user, len, n) : EINVAL;
}
int (url_get_passwd) (const url_t url, char *passwd, size_t len, size_t *n)
{
return (url) ? url->_get_passwd(url, passwd, len, n) : EINVAL;
}
int (url_get_host) (const url_t url, char *host, size_t len, size_t *n)
{
return (url) ? url->_get_host(url, host, len, n) : EINVAL;
}
int (url_get_port) (const url_t url, long *port)
{
return (url) ? url->_get_port(url, port) : EINVAL;
}
int (url_get_path) (const url_t url, char *path, size_t len, size_t *n)
{
return (url) ? url->_get_path(url, path, len, n) : EINVAL;
}
int (url_get_query) (const url_t url, char *query, size_t len, size_t *n)
{
return (url) ? url->_get_query(url, query, len, n) : EINVAL;
}
int (url_get_id) (const url_t url, int *id)
{
return (url) ? url->_get_id (url, id) : EINVAL;
}
/* Simple stub functions they all call _cpystr */
static int
get_scheme (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->scheme, len);
if (n)
*n = i;
return 0;
}
static int
get_user (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->user, len);
if (n)
*n = i;
return 0;
}
/* FIXME: We should not store passwd in clear, but rather
have a simple encoding, and decoding mechanism */
static int
get_passwd (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->passwd, len);
if (n)
*n = i;
return 0;
}
static int
get_host (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->host, len);
if (n)
*n = i;
return 0;
}
static int
get_port (const url_t u, long * p)
{
*p = u->port;
return 0;
}
static int
get_path (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr(s, u->path, len);
if (n)
*n = i;
return 0;
}
static int
get_query (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr(s, u->query, len);
if (n)
*n = i;
return 0;
}
static int
get_id (const url_t u, int *id)
{
if (id)
*id = u->id;
return 0;
}