Forgot to set the path for cases where we don't have a querystring (which
[platform/upstream/libsoup.git] / libsoup / soup-uri.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* soup-uri.c : utility functions to parse URLs */
3
4 /* 
5  * Authors : 
6  *  Bertrand Guiheneuf <bertrand@helixcode.com>
7  *  Dan Winship <danw@helixcode.com>
8  *
9  * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
10  *
11  * This program is free software; you can redistribute it and/or 
12  * modify it under the terms of the GNU General Public License as 
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24  * USA
25  */
26
27
28
29 /* 
30  * Here we deal with URLs following the general scheme:
31  *   protocol://user;AUTH=mech:password@host:port/name
32  * where name is a path-like string (ie dir1/dir2/....) See RFC 1738
33  * for the complete description of Uniform Resource Locators. The
34  * ";AUTH=mech" addition comes from RFC 2384, "POP URL Scheme".
35  */
36
37 /* XXX TODO:
38  * recover the words between #'s or ?'s after the path
39  * % escapes
40  */
41
42 #include <string.h>
43 #include <stdlib.h>
44
45 #include "soup-uri.h"
46
47 static gint
48 soup_uri_get_default_port (gchar *proto)
49 {
50         gint len;
51
52         if (!proto) return -1;
53
54         len = strlen (proto);
55
56         if (strncasecmp (proto, "https", len) == 0)
57                 return 443;
58         else if (strncasecmp (proto, "http", len) == 0)
59                 return 80;
60         else if (strncasecmp (proto, "mailto", len) == 0)
61                 return 25;
62         else if (strncasecmp (proto, "ftp", len) == 0)
63                 return 21;
64         else
65                 return -1;
66 }
67
68 /**
69  * soup_uri_new: create a SoupUri object from a string
70  *
71  * @uri_string: The string containing the URL to scan
72  * 
73  * This routine takes a gchar and parses it as a
74  * URL of the form:
75  *   protocol://user;AUTH=mech:password@host:port/path?querystring
76  * There is no test on the values. For example,
77  * "port" can be a string, not only a number!
78  * The SoupUri structure fields are filled with
79  * the scan results. When a member of the 
80  * general URL can not be found, the corresponding
81  * SoupUri member is NULL.
82  * Fields filled in the SoupUri structure are allocated
83  * and url_string is not modified. 
84  * 
85  * Return value: a SoupUri structure containing the URL items.
86  **/
87 SoupUri *soup_uri_new (const gchar* uri_string)
88 {
89         SoupUri *g_uri;
90         char *semi, *colon, *at, *slash, *path, *query;
91         char **split;
92
93         g_uri = g_new (SoupUri,1);
94
95         /* Find protocol: initial substring until "://" */
96         colon = strchr (uri_string, ':');
97         if (colon && !strncmp (colon, "://", 3)) {
98                 g_uri->protocol = g_strndup (uri_string, colon - uri_string);
99                 uri_string = colon + 3;
100         } else
101                 g_uri->protocol = NULL;
102
103         /* If there is an @ sign, look for user, authmech, and
104          * password before it.
105          */
106         at = strchr (uri_string, '@');
107         if (at) {
108                 colon = strchr (uri_string, ':');
109                 if (colon && colon < at)
110                         g_uri->passwd = g_strndup (colon + 1, at - colon - 1);
111                 else {
112                         g_uri->passwd = NULL;
113                         colon = at;
114                 }
115
116                 semi = strchr(uri_string, ';');
117                 if (semi && semi < colon && !strncasecmp (semi, ";auth=", 6))
118                         g_uri->authmech = g_strndup (semi + 6, colon - semi - 6);
119                 else {
120                         g_uri->authmech = NULL;
121                         semi = colon;
122                 }
123
124                 g_uri->user = g_strndup (uri_string, semi - uri_string);
125                 uri_string = at + 1;
126         } else
127                 g_uri->user = g_uri->passwd = g_uri->authmech = NULL;
128
129         /* Find host (required) and port. */
130         slash = strchr (uri_string, '/');
131         colon = strchr (uri_string, ':');
132         if (slash && colon > slash)
133                 colon = 0;
134
135         if (colon) {
136                 g_uri->host = g_strndup (uri_string, colon - uri_string);
137                 if (slash)
138                         g_uri->port = atoi(colon + 1);
139                 else
140                         g_uri->port = atoi(colon + 1);
141         } else if (slash) {
142                 g_uri->host = g_strndup (uri_string, slash - uri_string);
143                 g_uri->port = soup_uri_get_default_port (g_uri->protocol);
144         } else {
145                 g_uri->host = g_strdup (uri_string);
146                 g_uri->port = soup_uri_get_default_port (g_uri->protocol);
147         }
148
149         /* setup a fallback, if relative, then empty string, else
150            it will be from root */
151         if (slash == NULL) {
152                 slash = "/";
153         }
154         if (slash && *slash && g_uri->protocol == NULL)
155                 slash++;
156
157         split = g_strsplit(slash, " ", 0);
158         path = g_strjoinv("%20", split);
159         g_strfreev(split);
160
161         query = strchr (path, '?');
162
163         if (query) {
164                 g_uri->path = g_strndup (path, query - path);
165                 g_uri->querystring = g_strdup (++query);
166                 g_free (path);
167         } else {
168                 g_uri->path = path;
169                 g_uri->querystring = NULL;
170         }
171
172         return g_uri;
173 }
174
175 /* Need to handle mailto which apparantly doesn't use the "//" after the : */
176 gchar *
177 soup_uri_to_string (const SoupUri *uri, gboolean show_passwd)
178 {
179         if (uri->port != -1 && 
180             uri->port != soup_uri_get_default_port(uri->protocol))
181                 return g_strdup_printf(
182                         "%s%s%s%s%s%s%s%s%s:%d%s",
183                         uri->protocol ? uri->protocol : "",
184                         uri->protocol ? "://" : "",
185                         uri->user ? uri->user : "",
186                         uri->authmech ? ";auth=" : "",
187                         uri->authmech ? uri->authmech : "",
188                         uri->passwd && show_passwd ? ":" : "",
189                         uri->passwd && show_passwd ? uri->passwd : "",
190                         uri->user ? "@" : "",
191                         uri->host,
192                         uri->port,
193                         uri->path ? uri->path : "");
194         else
195                 return g_strdup_printf(
196                         "%s%s%s%s%s%s%s%s%s%s",
197                         uri->protocol ? uri->protocol : "",
198                         uri->protocol ? "://" : "",
199                         uri->user ? uri->user : "",
200                         uri->authmech ? ";auth=" : "",
201                         uri->authmech ? uri->authmech : "",
202                         uri->passwd && show_passwd ? ":" : "",
203                         uri->passwd && show_passwd ? uri->passwd : "",
204                         uri->user ? "@" : "",
205                         uri->host,
206                         uri->path ? uri->path : "");
207 }
208
209 void
210 soup_uri_free (SoupUri *uri)
211 {
212         g_assert (uri);
213
214         g_free (uri->protocol);
215         g_free (uri->user);
216         g_free (uri->authmech);
217         g_free (uri->passwd);
218         g_free (uri->host);
219         g_free (uri->path);
220
221         g_free (uri);
222 }