Commit 7ce849a0 7ce849a0541ee9e7b4493f5b433687d4794eb624 by Sam Roberts

Use the url_parse() function to parse the URL.

1 parent 631205fb
...@@ -39,148 +39,43 @@ url_imap_destroy (url_t url) ...@@ -39,148 +39,43 @@ url_imap_destroy (url_t url)
39 } 39 }
40 40
41 /* 41 /*
42 IMAP URL 42 IMAP URL:
43 imap://[<user>;AUTH=<auth>@]<host>/ 43 imap://[<user>[;AUTH=<auth>]@]<host>[/<mailbox>]
44 else
45 imap://[<user>[:<pass>]@]<host>[/<mailbox>]
44 */ 46 */
47
45 int 48 int
46 _url_imap_init (url_t url) 49 _url_imap_init (url_t url)
47 { 50 {
48 const char *host_port; 51 int status = 0;
49 const char *indexe;
50 char *name = url->name;
51
52 /* reject the obvious */
53 if (name == NULL || strncmp (MU_IMAP_SCHEME, name, MU_IMAP_SCHEME_LEN) != 0)
54 return EINVAL;
55 52
56 /* do I need to decode url encoding '% hex hex' ? */
57
58 /* TYPE */
59 url->_destroy = url_imap_destroy; 53 url->_destroy = url_imap_destroy;
60 54
61 /* SCHEME */ 55 status = url_parse (url);
62 url->scheme = strdup (MU_IMAP_SCHEME);
63 if (url->scheme == NULL)
64 {
65 url_imap_destroy (url);
66 return ENOMEM;
67 }
68
69 name += MU_IMAP_SCHEME_LEN; /* pass the scheme */
70 56
71 host_port = strchr (name, '@'); 57 if (status)
72 if (host_port == NULL) 58 return status;
73 host_port = name;
74 59
75 /* looking for ";auth=auth-enc" */ 60 /* is it pop? */
76 for (indexe = name; indexe != host_port; indexe++) 61 if (strcmp ("imap", url->scheme) != 0)
77 { 62 return EINVAL;
78 /* Auth ? */
79 if (*indexe == ';')
80 {
81 /* make sure it's the token */
82 if (strncasecmp(indexe + 1, "auth=", 5) == 0)
83 break;
84 }
85 }
86 63
87 /* USER */ 64 /* fill in default port, if necesary */
88 url->user = malloc(indexe - name + 1); 65 if (url->port == 0)
89 if (url->user == NULL) 66 url->port = MU_IMAP_PORT;
90 {
91 url_imap_destroy (url);
92 return -1;
93 }
94 ((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0';
95 67
96 /* AUTH */ 68 /* fill in default auth, if necessary */
97 if (indexe == host_port) 69 if (!url->auth)
98 { 70 {
99 /* Use default AUTH '*' */
100 url->auth = malloc (1 + 1); 71 url->auth = malloc (1 + 1);
101 if (url->auth) 72 if (!url->auth)
102 { 73 return ENOMEM;
74
103 url->auth[0] = '*'; 75 url->auth[0] = '*';
104 url->auth[1] = '\0'; 76 url->auth[1] = '\0';
105 } 77 }
106 }
107 else
108 {
109 /* move pass AUTH= */
110 indexe += 6;
111 url->auth = malloc (host_port - indexe + 1);
112 if (url->auth)
113 {
114 ((char *)memcpy (url->auth, indexe, host_port - indexe))
115 [host_port - indexe] = '\0';
116 }
117 }
118 78
119 if (url->auth == NULL) 79 return status;
120 {
121 url_imap_destroy (url);
122 return -1;
123 }
124
125 /* HOST:PORT*/
126 if (*host_port == '@')
127 host_port++;
128
129 indexe = strchr (host_port, ':');
130 if (indexe)
131 {
132 char *s = NULL;
133 long int p = strtol (indexe + 1, &s, 10);
134 url->host = malloc (indexe - host_port + 1);
135 if (url->host)
136 {
137 ((char *)memcpy (url->host, host_port, indexe - host_port))
138 [indexe - host_port] = '\0';
139 }
140 url->port = (p == 0) ? MU_IMAP_PORT : p;
141 host_port = s;
142 }
143 else
144 url->port = MU_IMAP_PORT;
145
146
147 indexe = strchr (host_port, '/');
148
149 if (indexe == NULL)
150 {
151 if (url->host == NULL)
152 url->host = strdup (host_port);
153 }
154 else
155 {
156 char *question;
157 if (url->host == NULL)
158 {
159 url->host = malloc (indexe - host_port + 1);
160 if (url->host)
161 ((char *)memcpy (url->host, host_port, indexe - host_port))
162 [indexe - host_port] = '\0';
163 }
164 indexe++;
165 /* The query starts after a '?'. */
166 question = strchr (indexe, '?');
167 if (question == NULL)
168 url->path = strdup (indexe);
169 else
170 {
171 url->path = malloc (question - indexe + 1);
172 if (url->path)
173 ((char *)memcpy (url->path, indexe,
174 question - indexe))[question - indexe] = '\0';
175 url->query = strdup (question);
176 }
177 }
178
179 if (url->host == NULL)
180 {
181 url_imap_destroy (url);
182 return ENOMEM;
183 }
184
185 return 0;
186 } 80 }
81
......
...@@ -39,122 +39,35 @@ url_pop_destroy (url_t url) ...@@ -39,122 +39,35 @@ url_pop_destroy (url_t url)
39 } 39 }
40 40
41 /* 41 /*
42 POP URL 42 POP URL:
43 pop://[<user>;AUTH=<auth>@]<host>[:<port>] 43 pop://[<user>[;AUTH=<auth>]@]<host>[:<port>]
44 or:
45 pop://[<user>[:pass]@]<host>[:<port>]
44 */ 46 */
47
45 int 48 int
46 _url_pop_init (url_t url) 49 _url_pop_init (url_t url)
47 { 50 {
48 const char *host_port, *indexe; 51 int status = 0;
49 char *name = url->name;
50 52
51 /* reject the obvious */ 53 url->_destroy = url_pop_destroy;
52 if (name == NULL || strncmp (MU_POP_SCHEME, name, MU_POP_SCHEME_LEN) != 0)
53 return EINVAL;
54 54
55 /* do I need to decode url encoding '% hex hex' ? */ 55 status = url_parse(url);
56 56
57 /* TYPE */ 57 if(status)
58 url->_destroy = url_pop_destroy; 58 return status;
59
60 /* is it pop? */
61 if (strcmp ("pop", url->scheme) != 0)
62 return EINVAL;
63
64 /* not valid in a pop url */
65 if (url->path || url->query)
66 return EINVAL;
59 67
60 /* SCHEME */ 68 if (url->port == 0)
61 url->scheme = strdup (MU_POP_SCHEME);
62 if (url->scheme == NULL)
63 {
64 url_pop_destroy (url);
65 return ENOMEM;
66 }
67
68 name += MU_POP_SCHEME_LEN; /* pass the scheme */
69
70 host_port = strchr (name, '@');
71 if (host_port == NULL)
72 host_port= name;
73
74 /* looking for "user;auth=auth-enc" */
75 for (indexe = name; indexe != host_port; indexe++)
76 {
77 /* Auth ? */
78 if (*indexe == ';')
79 {
80 /* make sure it's the token */
81 if (strncasecmp(indexe + 1, "auth=", 5) == 0)
82 break;
83 }
84 }
85
86 /* USER */
87 url->user = malloc(indexe - name + 1);
88 if (url->user == NULL)
89 {
90 url_pop_destroy (url);
91 return -1;
92 }
93 ((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0';
94
95 /* AUTH */
96 if (indexe == host_port)
97 {
98 /* Use default AUTH '*' */
99 url->auth = malloc (1 + 1);
100 if (url->auth)
101 {
102 url->auth[0] = '*';
103 url->auth[1] = '\0';
104 }
105 }
106 else
107 {
108 /* move pass AUTH= */
109 indexe += 6;
110 url->auth = malloc (host_port - indexe + 1);
111 if (url->auth)
112 {
113 ((char *)memcpy (url->auth, indexe, host_port - indexe))
114 [host_port - indexe] = '\0';
115 }
116 }
117
118 if (url->auth == NULL)
119 {
120 url_pop_destroy (url);
121 return -1;
122 }
123
124 /* HOST:PORT */
125 if (*host_port == '@')
126 host_port++;
127
128 indexe = strchr (host_port, ':');
129 if (indexe == NULL)
130 {
131 url->host = strdup (host_port);
132 url->port = MU_POP_PORT; 69 url->port = MU_POP_PORT;
133 } 70
134 else 71 return status;
135 {
136 long p = strtol(indexe + 1, NULL, 10);
137 url->host = malloc (indexe - host_port + 1);
138 if (url->host)
139 {
140 ((char *)memcpy (url->host, host_port, indexe - host_port))
141 [indexe - host_port]='\0';
142 url->port = (p == 0) ? MU_POP_PORT : p;
143 }
144 }
145
146 if (url->host == NULL)
147 {
148 url_pop_destroy (url);
149 return ENOMEM;
150 }
151 else
152 {
153 /* playing smart and nuking any trailing slashes on the host */
154 size_t len = strlen (url->host);
155 if (url->host[len - 1] == '/')
156 url->host[len - 1] = '\0'; /* leak a bit */
157 }
158
159 return 0;
160 } 72 }
73
......