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
37 /** @brief Infer the for the web interface
38 * @return Inferred URL
40 * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>.
42 char *infer_url(void) {
43 const char *scheme
= "http", *server
, *script
, *e
;
47 /* Figure out the server. 'MUST' be set and we don't cope if it
49 if(!(server
= getenv("SERVER_NAME")))
50 fatal(0, "SERVER_NAME is not set");
51 server
= xstrdup(server
);
53 /* Figure out the port. 'MUST' be set but we cope if it is not. */
54 if((e
= getenv("SERVER_PORT")))
59 /* Figure out path to ourselves */
60 if(!(script
= getenv("SCRIPT_NAME")))
61 fatal(0, "SCRIPT_NAME is not set");
63 fatal(0, "SCRIPT_NAME does not start with a '/'");
64 script
= xstrdup(script
);
67 byte_xasprintf(&url
, "%s://%s%s",
68 scheme
, server
, script
);
70 byte_xasprintf(&url
, "%s://%s:%d%s",
71 scheme
, server
, port
, script
);
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
80 * NB that URLs with usernames and passwords are NOT currently supported.
82 int parse_url(const char *url
, struct url
*parsed
) {
87 for(s
= url
; *s
&& *s
!= '/' && *s
!= ':'; ++s
)
90 parsed
->scheme
= xstrndup(url
, s
- url
);
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. */
100 for(s
= url
; *s
&& *s
!= '/' && *s
!= ':'; ++s
)
102 parsed
->host
= xstrndup(url
, s
- url
);
104 /* We have host:port[/...] */
107 n
= strtol(s
, (char **)&url
, 10);
110 if(n
< 0 || n
> 65535)
114 /* We just have host[/...] */
121 for(s
= url
; *s
&& *s
!= '?'; ++s
)
123 if(!(parsed
->path
= urldecodestring(url
, s
- url
)))
129 parsed
->query
= xstrdup(url
+ 1);