urlstr.c
4.15 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012, 2014-2015 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/sys/url.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#include <mailutils/opool.h>
#define AUTH_PFX ";AUTH="
static int
url_reconstruct_to_pool (mu_url_t url, mu_opool_t pool)
{
if (url->flags & MU_URL_SCHEME)
{
int i;
mu_opool_appendz (pool, url->scheme);
mu_opool_append (pool, "://", 3);
if (url->flags & MU_URL_USER)
mu_opool_appendz (pool, url->user);
if (url->flags & MU_URL_SECRET)
mu_opool_append (pool, ":***", 4); /* FIXME: How about MU_URL_PARSE_HIDEPASS? */
if (url->flags & MU_URL_AUTH)
{
mu_opool_append (pool, AUTH_PFX, sizeof AUTH_PFX - 1);
mu_opool_appendz (pool, url->auth);
}
if (url->flags & MU_URL_HOST)
{
if (url->flags & (MU_URL_USER|MU_URL_SECRET|MU_URL_AUTH))
mu_opool_append_char (pool, '@');
mu_opool_appendz (pool, url->host);
if (url->flags & MU_URL_PORT)
{
mu_opool_append_char (pool, ':');
mu_opool_appendz (pool, url->portstr);
}
}
else if (url->flags & (MU_URL_USER|MU_URL_SECRET|MU_URL_AUTH))
return MU_ERR_URL_MISS_PARTS;
if (url->flags & MU_URL_PATH)
{
if (url->flags & MU_URL_HOST)
mu_opool_append_char (pool, '/');
mu_opool_appendz (pool, url->path);
}
if (url->flags & MU_URL_PARAM)
{
for (i = 0; i < url->fvcount; i++)
{
mu_opool_append_char (pool, ';');
mu_opool_append (pool, url->fvpairs[i],
strlen (url->fvpairs[i]));
}
}
if (url->flags & MU_URL_QUERY)
{
mu_opool_append_char (pool, '?');
mu_opool_append (pool, url->qargv[0],
strlen (url->qargv[0]));
for (i = 1; i < url->qargc; i++)
{
mu_opool_append_char (pool, '&');
mu_opool_append (pool, url->qargv[i],
strlen (url->qargv[i]));
}
}
return 0;
}
else if (url->flags == MU_URL_PATH)
{
mu_opool_appendz (pool, url->path);
return 0;
}
return MU_ERR_URL_MISS_PARTS;
}
int
mu_url_sget_name (const mu_url_t url, const char **retptr)
{
if (!url)
return EINVAL;
if (!url->name)
{
mu_opool_t pool;
int rc;
char *ptr, *newname;
size_t size;
rc = mu_opool_create (&pool, 0);
if (rc)
return rc;
rc = url_reconstruct_to_pool (url, pool);
if (rc)
{
mu_opool_destroy (&pool);
return rc;
}
ptr = mu_opool_finish (pool, &size);
newname = realloc (url->name, size + 1);
if (!newname)
{
mu_opool_destroy (&pool);
return ENOMEM;
}
memcpy (newname, ptr, size);
newname[size] = 0;
url->name = newname;
mu_opool_destroy (&pool);
}
if (retptr)
*retptr = url->name;
return 0;
}
int
mu_url_aget_name (const mu_url_t url, char **ret)
{
char *s;
const char *ptr;
int rc = mu_url_sget_name (url, &ptr);
if (rc)
return rc;
s = strdup (ptr);
if (!s)
return errno;
*ret = s;
return 0;
}
int
mu_url_get_name (const mu_url_t url, char *buf, size_t size, size_t *n)
{
size_t i;
const char *ptr;
int rc = mu_url_sget_name (url, &ptr);
if (rc)
return rc;
i = mu_cpystr (buf, ptr, size);
if (n)
*n = i;
return 0;
}
const char *
mu_url_to_string (const mu_url_t url)
{
const char *ptr;
if (mu_url_sget_name (url, &ptr))
return "";
return ptr;
}