chiark / gitweb /
Bodge version number for test installs
[disorder] / lib / url.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/url.c
21  * @brief URL support functions
22  */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <string.h>
31
32 #include "mem.h"
33 #include "log.h"
34 #include "printf.h"
35 #include "url.h"
36 #include "kvp.h"
37
38 /** @brief Infer the for the web interface
39  * @return Inferred URL
40  *
41  * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>.
42  */
43 char *infer_url(void) {
44   const char *scheme = "http", *server, *script, *e, *request_uri;
45   char *url;
46   int port;
47   
48   /* Figure out the server.  'MUST' be set and we don't cope if it
49    * is not. */
50   if(!(server = getenv("SERVER_NAME")))
51     fatal(0, "SERVER_NAME is not set");
52   server = xstrdup(server);
53   
54   /* Figure out the port.  'MUST' be set but we cope if it is not. */
55   if((e = getenv("SERVER_PORT")))
56     port = atoi(e);
57   else
58     port = 80;
59   
60   /* Figure out path to ourselves. */
61   if((request_uri = getenv("REQUEST_URI"))) {
62     /* REQUEST_URI is an Apache extexnsion.  If it's available it results in
63      * more accurate self-referencing URLs.  */
64     if((e = strchr(request_uri, '?')))
65       script = xstrndup(request_uri, e - request_uri);
66     else
67       script = xstrdup(request_uri);
68   } else {
69     /* RFC3875 s4.1.13 */
70     if(!(script = getenv("SCRIPT_NAME")))
71       fatal(0, "SCRIPT_NAME is not set");
72     /* SCRIPT_NAME may be "" */
73     if(!*script)
74       script = "/";
75     /* SCRIPT_NAME is not URL-encoded */
76     script = urlencodestring(script);
77   }
78   if(script[0] != '/')
79     fatal(0, "SCRIPT_NAME does not start with a '/'");
80   script = xstrdup(script);
81   
82   if(port == 80)
83     byte_xasprintf(&url, "%s://%s%s",
84                    scheme, server, script);
85   else
86     byte_xasprintf(&url, "%s://%s:%d%s",
87                    scheme, server, port, script);
88   return url;
89 }
90
91 /** @brief Parse a URL
92  * @param url URL to parsed
93  * @param parsed Where to store parsed URL data
94  * @return 0 on success, non-0 on error
95  *
96  * NB that URLs with usernames and passwords are NOT currently supported.
97  */
98 int parse_url(const char *url, struct url *parsed) {
99   const char *s;
100   long n;
101
102   /* The scheme */
103   for(s = url; *s && *s != '/' && *s != ':'; ++s)
104     ;
105   if(*s == ':') {
106     parsed->scheme = xstrndup(url, s - url);
107     url = s + 1;
108   } else
109     parsed->scheme = 0;
110
111   /* The host and port */
112   if(*url == '/' && url[1] == '/') {
113     /* //user:password@host:port, but we don't support the
114      * user:password@ part. */
115     url += 2;
116     for(s = url; *s && *s != '/' && *s != ':'; ++s)
117       ;
118     parsed->host = xstrndup(url, s - url);
119     if(*s == ':') {
120       /* We have host:port[/...] */
121       ++s;
122       errno = 0;
123       n = strtol(s, (char **)&url, 10);
124       if(errno)
125         return -1;
126       if(n < 0 || n > 65535)
127         return -1;
128       parsed->port = n;
129     } else {
130       /* We just have host[/...] */
131       url = s;
132       parsed->port = -1;
133     }
134   }
135
136   /* The path */
137   for(s = url; *s && *s != '?'; ++s)
138     ;
139   if(!(parsed->path = urldecodestring(url, s - url)))
140     return -1;
141   url = s;
142
143   /* The query */
144   if(*url == '?')
145     parsed->query = xstrdup(url + 1);
146   else
147     parsed->query = 0;
148
149   return 0;
150 }
151
152 /*
153 Local Variables:
154 c-basic-offset:2
155 comment-column:40
156 fill-column:79
157 indent-tabs-mode:nil
158 End:
159 */