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.
34 static struct mapping_t UrlMap[] = {
48 /* decode url escaping */
49 char *url_decode(char *p)
57 if (*q == '%' && hexval(q[1]) >= 0 && hexval(q[2]) >= 0) {
58 *p++ = (hexval(q[1]) << 4) | hexval(q[2]);
69 url_scheme_t url_check_scheme(const char *s)
75 if (!s || !(t = strchr(s, ':')))
78 m_strncpy(sbuf, sizeof(sbuf), s, t - s);
79 i = mutt_getvaluebyname(sbuf, UrlMap);
80 return i == -1 ? U_UNKNOWN : i;
83 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
84 * these are pointers into src, which is altered with '\0's. Port of 0
85 * means no port given.
86 * FIXME: THIS IS TASTELESS
88 static char *ciss_parse_userhost(ciss_url_t *ciss, char *src)
99 if (strncmp(src, "//", 2))
104 if ((path = strchr(src, '/')))
107 if ((t = strrchr(src, '@'))) {
109 if ((p = strchr (src, ':'))) {
112 url_decode (ciss->pass);
115 url_decode (ciss->user);
121 if ((p = strchr (t, ':'))) {
123 ciss->port = atoi (p);
129 url_decode(ciss->host);
133 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
134 * which is modified by this call (duplicate it first if you need to).
135 * FIXME: THIS IS TASTELESS
137 int url_parse_ciss(ciss_url_t *ciss, char *src)
139 ciss->scheme = url_check_scheme(src);
140 if (ciss->scheme == U_UNKNOWN)
143 ciss->path = ciss_parse_userhost(ciss, strchr(src, ':') + 1);
144 url_decode(ciss->path);
149 /* url_ciss_tostring: output the URL string for a given CISS object. */
150 int url_ciss_tostring(ciss_url_t *ciss, char *dst, ssize_t len, int flags)
154 if (ciss->scheme == U_UNKNOWN)
157 snprintf(dst, len, "%s:", mutt_getnamebyvalue(ciss->scheme, UrlMap));
160 l = m_strcat(dst, len, "//");
163 if ((flags & U_DECODE_PASSWD) && ciss->pass) {
164 l += snprintf(dst + l, len - l, "%s:%s@", ciss->user, ciss->pass);
166 l += snprintf(dst + l, len - l, "%s@", ciss->user);
171 l += snprintf(dst + l, len - l, "%s:%hu/", ciss->host, ciss->port);
173 l += snprintf(dst + l, len - l, "%s/", ciss->host);
177 l += m_strcpy(dst + l, len - l, ciss->path);