url_pop.c
2.81 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
#include <url.h>
#include <url0.h>
#include <stdlib.h>
#include <string.h>
#define POP_PORT 110
static int get_auth (const url_pop_t up, char * s, unsigned int n);
static int
get_auth (const url_pop_t up, char * s, unsigned int n)
{
if (up == NULL) return -1;
return _cpystr (up->auth, s, n);
}
int
(url_pop_get_auth) (const url_t url, char * auth, unsigned int n)
{
return ((url_pop_t) (url->data))->_get_auth(url->data, auth, n);
}
void
url_pop_destroy (url_t * url)
{
if (url && *url)
{
url_t u = *url;
if (u->scheme)
{
free (u->scheme);
}
if (u->user)
{
free (u->user);
}
if (u->passwd)
{
free (u->passwd);
}
if (u->host)
{
free (u->host);
}
if (u->data)
{
url_pop_t up = u->data;
if (up->auth)
{
free (up->auth);
}
free (u->data);
}
free (u);
u = NULL;
}
}
/*
POP URL
pop://<user>;AUTH=<auth>@<host>:<port>
*/
int
url_pop_create (url_t * url, const char * name)
{
const char * host_port, * index;
url_t u;
url_pop_t up;
/* reject the obvious */
if (name == NULL ||
strncmp ("pop://", name, 6) != 0 ||
(host_port = strchr (name, '@')) == NULL ||
strlen(name) < 9 /* 6(scheme)+1(user)+1(@)+1(host)*/) {
return -1;
}
/* do I need to decode url encoding '% hex hex' ? */
u = xcalloc(1, sizeof (*u));
u->data = up = xcalloc(1, sizeof(*up));
up->_get_auth = get_auth;
/* type */
u->type = URL_POP;
u->scheme = xstrdup ("pop://");
name += 6; /* pass the scheme */
/* looking for user;auth=auth-enc */
#if 1
for (index = name; index != host_port; index++)
{
/* Auth ? */
if (*index == ';')
{
if (strncasecmp(index +1, "auth=", 5) == 0 )
break;
}
}
#endif
/* USER */
if (index == name)
{
//free();
return -1;
}
u->user = malloc(index - name + 1);
((char *)memcpy(u->user, name, index - name))[index - name] = '\0';
/* AUTH */
if ((host_port - index) <= 6 /*strlen(";AUTH=")*/)
{
/* default AUth is '*'*/
up->auth = malloc (1 + 1);
up->auth[0] = '*';
up->auth[1] = '\0';
}
else
{
/* move pass AUTH= */
index += 6;
up->auth = malloc (host_port - index + 1);
((char *) memcpy (up->auth, index, host_port - index))
[host_port - index] = '\0';
}
/* HOST:PORT */
index = strchr (++host_port, ':');
if (index == NULL)
{
int len = strlen (host_port);
u->host = malloc (len + 1);
((char *)memcpy (u->host, host_port, len))[len] = '\0';
u->port = POP_PORT;
}
else
{
long p = strtol(index + 1, NULL, 10);
u->host = malloc (index - host_port + 1);
((char *)memcpy (u->host, host_port, index - host_port))
[index - host_port]='\0';
u->port = (p == 0) ? POP_PORT : p;
}
*url = u;
return 0;
}