chiark / gitweb /
make sure disorder-normalize gets the right config file
[disorder] / lib / url.c
CommitLineData
36bde473 1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2007 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
31#include "mem.h"
32#include "log.h"
33#include "printf.h"
34#include "url.h"
35#include "kvp.h"
36
37/** @brief Infer the for the web interface
38 * @return Inferred URL
39 *
40 * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>.
41 */
42char *infer_url(void) {
43 const char *scheme = "http", *server, *script, *e;
44 char *url;
45 int port;
46
47 /* Figure out the server. 'MUST' be set and we don't cope if it
48 * is not. */
49 if(!(server = getenv("SERVER_NAME")))
50 fatal(0, "SERVER_NAME is not set");
51 server = xstrdup(server);
52
53 /* Figure out the port. 'MUST' be set but we cope if it is not. */
54 if((e = getenv("SERVER_PORT")))
55 port = atoi(e);
56 else
57 port = 80;
58
59 /* Figure out path to ourselves */
60 if(!(script = getenv("SCRIPT_NAME")))
61 fatal(0, "SCRIPT_NAME is not set");
62 if(script[0] != '/')
63 fatal(0, "SCRIPT_NAME does not start with a '/'");
64 script = xstrdup(script);
65
66 if(port == 80)
67 byte_xasprintf(&url, "%s://%s%s",
68 scheme, server, script);
69 else
70 byte_xasprintf(&url, "%s://%s:%d%s",
71 scheme, server, port, script);
72 return url;
73}
74
75/** @brief Parse a URL
76 * @param url URL to parsed
77 * @param parsed Where to store parsed URL data
78 * @return 0 on success, non-0 on error
79 *
80 * NB that URLs with usernames and passwords are NOT currently supported.
81 */
82int parse_url(const char *url, struct url *parsed) {
83 const char *s;
84 long n;
85
86 /* The scheme */
87 for(s = url; *s && *s != '/' && *s != ':'; ++s)
88 ;
89 if(*s == ':') {
90 parsed->scheme = xstrndup(url, s - url);
91 url = s + 1;
92 } else
93 parsed->scheme = 0;
94
95 /* The host and port */
96 if(*url == '/' && url[1] == '/') {
97 /* //user:password@host:port, but we don't support the
98 * user:password@ part. */
99 url += 2;
100 for(s = url; *s && *s != '/' && *s != ':'; ++s)
101 ;
102 parsed->host = xstrndup(url, s - url);
103 if(*s == ':') {
104 /* We have host:port[/...] */
105 ++s;
106 errno = 0;
107 n = strtol(s, (char **)&url, 10);
108 if(errno)
109 return -1;
110 if(n < 0 || n > 65535)
111 return -1;
112 parsed->port = n;
113 } else {
114 /* We just have host[/...] */
115 url = s;
116 parsed->port = -1;
117 }
118 }
119
120 /* The path */
121 for(s = url; *s && *s != '?'; ++s)
122 ;
123 if(!(parsed->path = urldecodestring(url, s - url)))
124 return -1;
125 url = s;
126
127 /* The query */
128 if(*url == '?')
129 parsed->query = xstrdup(url + 1);
130 else
131 parsed->query = 0;
132
133 return 0;
134}
135
136/*
137Local Variables:
138c-basic-offset:2
139comment-column:40
140fill-column:79
141indent-tabs-mode:nil
142End:
143*/