X-Git-Url: https://git.distorted.org.uk/~mdw/disorder/blobdiff_plain/36bde473fdfeb75de65fb7b1920a6fb137f32f3c..bdd2a99650780a1ef4a9e788eaabbc5e810cc39c:/lib/url.c diff --git a/lib/url.c b/lib/url.c index 784ee9f..b777282 100644 --- a/lib/url.c +++ b/lib/url.c @@ -1,6 +1,6 @@ /* * This file is part of DisOrder - * Copyright (C) 2007 Richard Kettlewell + * Copyright (C) 2007, 2008 Richard Kettlewell * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,11 +21,8 @@ * @brief URL support functions */ -#include -#include "types.h" +#include "common.h" -#include -#include #include #include "mem.h" @@ -40,9 +37,13 @@ * See RFC 3875. */ char *infer_url(void) { - const char *scheme = "http", *server, *script, *e; + const char *scheme = "http", *server, *script, *e, *request_uri; char *url; int port; + + /* mod_ssl sets HTTPS=on if the scheme is https */ + if((e = getenv("HTTPS")) && !strcmp(e, "on")) + scheme = "https"; /* Figure out the server. 'MUST' be set and we don't cope if it * is not. */ @@ -56,9 +57,24 @@ char *infer_url(void) { else port = 80; - /* Figure out path to ourselves */ - if(!(script = getenv("SCRIPT_NAME"))) - fatal(0, "SCRIPT_NAME is not set"); + /* Figure out path to ourselves. */ + if((request_uri = getenv("REQUEST_URI"))) { + /* REQUEST_URI is an Apache extexnsion. If it's available it results in + * more accurate self-referencing URLs. */ + if((e = strchr(request_uri, '?'))) + script = xstrndup(request_uri, e - request_uri); + else + script = xstrdup(request_uri); + } else { + /* RFC3875 s4.1.13 */ + if(!(script = getenv("SCRIPT_NAME"))) + fatal(0, "SCRIPT_NAME is not set"); + /* SCRIPT_NAME may be "" */ + if(!*script) + script = "/"; + /* SCRIPT_NAME is not URL-encoded */ + script = urlencodestring(script); + } if(script[0] != '/') fatal(0, "SCRIPT_NAME does not start with a '/'"); script = xstrdup(script);