2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
20 * Copyright notice from original mutt:
21 * Copyright (C) 2000 Thomas Roessler <roessler@does-not-exist.org>
23 * This file is part of mutt-ng, see http://www.muttng.org/.
24 * It's licensed under the GNU General Public License,
25 * please see the file GPL in the top level source directory.
29 * A simple URL parser.
38 #include <lib-lib/mem.h>
39 #include <lib-lib/ascii.h>
40 #include <lib-lib/mapping.h>
41 #include <lib-lib/url.h>
43 #include <lib-mime/mime.h>
47 static struct mapping_t UrlMap[] = {
61 /* decode url escaping */
62 char *url_decode(char *p)
70 if (*q == '%' && hexval(q[1]) >= 0 && hexval(q[2]) >= 0) {
71 *p++ = (hexval(q[1]) << 4) | hexval(q[2]);
82 url_scheme_t url_check_scheme(const char *s)
88 if (!s || !(t = strchr(s, ':')))
91 m_strncpy(sbuf, sizeof(sbuf), s, t - s);
92 i = mutt_getvaluebyname(sbuf, UrlMap);
93 return i == -1 ? U_UNKNOWN : i;
96 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
97 * these are pointers into src, which is altered with '\0's. Port of 0
98 * means no port given.
99 * FIXME: THIS IS TASTELESS
101 static char *ciss_parse_userhost(ciss_url_t *ciss, char *src)
112 if (strncmp(src, "//", 2))
117 if ((path = strchr(src, '/')))
120 if ((t = strrchr(src, '@'))) {
122 if ((p = strchr (src, ':'))) {
125 url_decode (ciss->pass);
128 url_decode (ciss->user);
134 if ((p = strchr (t, ':'))) {
136 ciss->port = atoi (p);
142 url_decode(ciss->host);
146 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
147 * which is modified by this call (duplicate it first if you need to).
148 * FIXME: THIS IS TASTELESS
150 int url_parse_ciss(ciss_url_t *ciss, char *src)
152 ciss->scheme = url_check_scheme(src);
153 if (ciss->scheme == U_UNKNOWN)
156 ciss->path = ciss_parse_userhost(ciss, strchr(src, ':') + 1);
157 url_decode(ciss->path);
162 /* url_ciss_tostring: output the URL string for a given CISS object. */
163 int url_ciss_tostring(ciss_url_t *ciss, char *dst, ssize_t len, int flags)
167 if (ciss->scheme == U_UNKNOWN)
170 snprintf(dst, len, "%s:", mutt_getnamebyvalue(ciss->scheme, UrlMap));
173 l = m_strcat(dst, len, "//");
176 if ((flags & U_DECODE_PASSWD) && ciss->pass) {
177 l += snprintf(dst + l, len - l, "%s:%s@", ciss->user, ciss->pass);
179 l += snprintf(dst + l, len - l, "%s@", ciss->user);
184 l += snprintf(dst + l, len - l, "%s:%hu/", ciss->host, ciss->port);
186 l += snprintf(dst + l, len - l, "%s/", ciss->host);
190 l += m_strcpy(dst + l, len - l, ciss->path);