2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
21 * @brief URL support functions
38 /** @brief Infer the for the web interface
39 * @return Inferred URL
41 * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>.
43 char *infer_url(void) {
44 const char *scheme
= "http", *server
, *script
, *e
, *request_uri
;
48 /* Figure out the server. 'MUST' be set and we don't cope if it
50 if(!(server
= getenv("SERVER_NAME")))
51 fatal(0, "SERVER_NAME is not set");
52 server
= xstrdup(server
);
54 /* Figure out the port. 'MUST' be set but we cope if it is not. */
55 if((e
= getenv("SERVER_PORT")))
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
);
67 script
= xstrdup(request_uri
);
70 if(!(script
= getenv("SCRIPT_NAME")))
71 fatal(0, "SCRIPT_NAME is not set");
72 /* SCRIPT_NAME may be "" */
75 /* SCRIPT_NAME is not URL-encoded */
76 script
= urlencodestring(script
);
79 fatal(0, "SCRIPT_NAME does not start with a '/'");
80 script
= xstrdup(script
);
83 byte_xasprintf(&url
, "%s://%s%s",
84 scheme
, server
, script
);
86 byte_xasprintf(&url
, "%s://%s:%d%s",
87 scheme
, server
, port
, script
);
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
96 * NB that URLs with usernames and passwords are NOT currently supported.
98 int parse_url(const char *url
, struct url
*parsed
) {
103 for(s
= url
; *s
&& *s
!= '/' && *s
!= ':'; ++s
)
106 parsed
->scheme
= xstrndup(url
, s
- url
);
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. */
116 for(s
= url
; *s
&& *s
!= '/' && *s
!= ':'; ++s
)
118 parsed
->host
= xstrndup(url
, s
- url
);
120 /* We have host:port[/...] */
123 n
= strtol(s
, (char **)&url
, 10);
126 if(n
< 0 || n
> 65535)
130 /* We just have host[/...] */
137 for(s
= url
; *s
&& *s
!= '?'; ++s
)
139 if(!(parsed
->path
= urldecodestring(url
, s
- url
)))
145 parsed
->query
= xstrdup(url
+ 1);