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.
39 #include <lib-lib/lib-lib.h>
41 static struct mapping_t UrlMap[] = {
55 /* decode url escaping */
56 char *url_decode(char *p)
64 if (*q == '%' && hexval(q[1]) >= 0 && hexval(q[2]) >= 0) {
65 *p++ = (hexval(q[1]) << 4) | hexval(q[2]);
76 url_scheme_t url_check_scheme(const char *s)
82 if (!s || !(t = strchr(s, ':')))
85 m_strncpy(sbuf, sizeof(sbuf), s, t - s);
86 i = mutt_getvaluebyname(sbuf, UrlMap);
87 return i == -1 ? U_UNKNOWN : i;
90 /* ciss_parse_userhost: fill in components of ciss with info from src. Note
91 * these are pointers into src, which is altered with '\0's. Port of 0
92 * means no port given.
93 * FIXME: THIS IS TASTELESS
95 static char *ciss_parse_userhost(ciss_url_t *ciss, char *src)
106 if (strncmp(src, "//", 2))
111 if ((path = strchr(src, '/')))
114 if ((t = strrchr(src, '@'))) {
116 if ((p = strchr (src, ':'))) {
119 url_decode (ciss->pass);
122 url_decode (ciss->user);
128 if ((p = strchr (t, ':'))) {
130 ciss->port = atoi (p);
136 url_decode(ciss->host);
140 /* url_parse_ciss: Fill in ciss_url_t. char* elements are pointers into src,
141 * which is modified by this call (duplicate it first if you need to).
142 * FIXME: THIS IS TASTELESS
144 int url_parse_ciss(ciss_url_t *ciss, char *src)
146 ciss->scheme = url_check_scheme(src);
147 if (ciss->scheme == U_UNKNOWN)
150 ciss->path = ciss_parse_userhost(ciss, strchr(src, ':') + 1);
151 url_decode(ciss->path);
156 /* url_ciss_tostring: output the URL string for a given CISS object. */
157 int url_ciss_tostring(ciss_url_t *ciss, char *dst, ssize_t len, int flags)
161 if (ciss->scheme == U_UNKNOWN)
164 snprintf(dst, len, "%s:", mutt_getnamebyvalue(ciss->scheme, UrlMap));
167 l = m_strcat(dst, len, "//");
170 if ((flags & U_DECODE_PASSWD) && ciss->pass) {
171 l += snprintf(dst + l, len - l, "%s:%s@", ciss->user, ciss->pass);
173 l += snprintf(dst + l, len - l, "%s@", ciss->user);
178 l += snprintf(dst + l, len - l, "%s:%hu/", ciss->host, ciss->port);
180 l += snprintf(dst + l, len - l, "%s/", ciss->host);
184 l += m_strcpy(dst + l, len - l, ciss->path);