Beginnings of test-suite added.
[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 = strlen (proto);
51
52         if (strncasecmp (proto, "https", len) == 0)
53                 return 443;
54         else if (strncasecmp (proto, "http", len) == 0)
55                 return 80;
56         else if (strncasecmp (proto, "mailto", len) == 0 || 
57                  strncasecmp (proto, "smtp", len) == 0)
58                 return 25;
59         else if (strncasecmp (proto, "ftp", len) == 0)
60                 return 21;
61         else
62                 return -1;
63 }
64
65 /**
66  * soup_uri_new: create a SoupUri object from a string
67  *
68  * @uri_string: The string containing the URL to scan
69  * 
70  * This routine takes a gchar and parses it as a
71  * URL of the form:
72  *   protocol://user;AUTH=mech:password@host:port/path?querystring
73  * There is no test on the values. For example,
74  * "port" can be a string, not only a number!
75  * The SoupUri structure fields are filled with
76  * the scan results. When a member of the 
77  * general URL can not be found, the corresponding
78  * SoupUri member is NULL.
79  * Fields filled in the SoupUri structure are allocated
80  * and url_string is not modified. 
81  * 
82  * Return value: a SoupUri structure containing the URL items.
83  **/
84 SoupUri *soup_uri_new (const gchar* uri_string)
85 {
86         SoupUri *g_uri;
87         char *semi, *colon, *at, *slash, *path, *query;
88         char **split;
89
90         g_uri = g_new (SoupUri,1);
91
92         /* Find protocol: initial substring until "://" */
93         colon = strchr (uri_string, ':');
94         if (colon && !strncmp (colon, "://", 3)) {
95                 g_uri->protocol = g_strndup (uri_string, colon - uri_string);
96                 uri_string = colon + 3;
97         } else
98                 g_uri->protocol = NULL;
99
100         /* If there is an @ sign, look for user, authmech, and
101          * password before it.
102          */
103         at = strchr (uri_string, '@');
104         if (at) {
105                 colon = strchr (uri_string, ':');
106                 if (colon && colon < at)
107                         g_uri->passwd = g_strndup (colon + 1, at - colon - 1);
108                 else {
109                         g_uri->passwd = NULL;
110                         colon = at;
111                 }
112
113                 semi = strchr(uri_string, ';');
114                 if (semi && semi < colon && !strncasecmp (semi, ";auth=", 6))
115                         g_uri->authmech = g_strndup (semi + 6, colon - semi - 6);
116                 else {
117                         g_uri->authmech = NULL;
118                         semi = colon;
119                 }
120
121                 g_uri->user = g_strndup (uri_string, semi - uri_string);
122                 uri_string = at + 1;
123         } else
124                 g_uri->user = g_uri->passwd = g_uri->authmech = NULL;
125
126         /* Find host (required) and port. */
127         slash = strchr (uri_string, '/');
128         colon = strchr (uri_string, ':');
129         if (slash && colon > slash)
130                 colon = 0;
131
132         if (colon) {
133                 g_uri->host = g_strndup (uri_string, colon - uri_string);
134                 if (slash)
135                         g_uri->port = atoi(colon + 1);
136                 else
137                         g_uri->port = atoi(colon + 1);
138         } else if (slash) {
139                 g_uri->host = g_strndup (uri_string, slash - uri_string);
140                 g_uri->port = soup_uri_get_default_port (g_uri->protocol);
141         } else {
142                 g_uri->host = g_strdup (uri_string);
143                 g_uri->port = soup_uri_get_default_port (g_uri->protocol);
144         }
145
146         /* setup a fallback, if relative, then empty string, else
147            it will be from root */
148         if (slash == NULL) {
149                 slash = "/";
150         }
151         if (slash && *slash && g_uri->protocol == NULL)
152                 slash++;
153
154         split = g_strsplit(slash, " ", 0);
155         path = g_strjoinv("%20", split);
156         g_strfreev(split);
157
158         query = strchr (path, '?');
159
160         if (query) {
161                 g_uri->path = g_strndup (path, query - path);
162                 g_uri->querystring = g_strdup (++query);
163                 g_free (path);
164         }
165
166         return g_uri;
167 }
168
169 gchar *
170 soup_uri_to_string (const SoupUri *uri, gboolean show_passwd)
171 {
172         if (uri->port != -1)
173                 return g_strdup_printf(
174                         "%s%s%s%s%s%s%s%s%s:%d%s",
175                         uri->protocol ? uri->protocol : "",
176                         uri->protocol ? "://" : "",
177                         uri->user ? uri->user : "",
178                         uri->authmech ? ";auth=" : "",
179                         uri->authmech ? uri->authmech : "",
180                         uri->passwd && show_passwd ? ":" : "",
181                         uri->passwd && show_passwd ? uri->passwd : "",
182                         uri->user ? "@" : "",
183                         uri->host,
184                         uri->port,
185                         uri->path ? uri->path : "");
186         else
187                 return g_strdup_printf(
188                         "%s%s%s%s%s%s%s%s%s%s",
189                         uri->protocol ? uri->protocol : "",
190                         uri->protocol ? "://" : "",
191                         uri->user ? uri->user : "",
192                         uri->authmech ? ";auth=" : "",
193                         uri->authmech ? uri->authmech : "",
194                         uri->passwd && show_passwd ? ":" : "",
195                         uri->passwd && show_passwd ? uri->passwd : "",
196                         uri->user ? "@" : "",
197                         uri->host,
198                         uri->path ? uri->path : "");
199 }
200
201 void
202 soup_uri_free (SoupUri *uri)
203 {
204         g_assert (uri);
205
206         g_free (uri->protocol);
207         g_free (uri->user);
208         g_free (uri->authmech);
209         g_free (uri->passwd);
210         g_free (uri->host);
211         g_free (uri->path);
212
213         g_free (uri);
214 }