author | Eric Wong <normalperson@yhbt.net> | 2008-09-01 06:30:33 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-09-01 23:26:37 (UTC) |
commit | 9c931b1e6e68f8dc891a5653035c3d70038ae3c0 (patch) (unidiff) | |
tree | a5cd6d953eb66ad77eb4e5b088e2022973fc4f61 | |
parent | 687fb6d833d25144c86d1841fe0eca611f1eaeb1 (diff) | |
download | cgit-9c931b1e6e68f8dc891a5653035c3d70038ae3c0.zip cgit-9c931b1e6e68f8dc891a5653035c3d70038ae3c0.tar.gz cgit-9c931b1e6e68f8dc891a5653035c3d70038ae3c0.tar.bz2 |
use Host: header to generate cgit_hosturl
I run an instance of lighttpd for cgit behind nginx (nginx
doesn't execute CGI). So the port (SERVER_PORT=33333) that
lighttpd runs on sends to cgit is different from the standard
port 80 that public clients connect to (via nginx).
This was causing the Atom feed URL to show the private port
number that lighttpd was running on.
Since the HTTP/1.1 "Host" header includes the port number if
running on a non-standard port, it allows non-client-facing HTTP
servers to transparently generate public URLs that clients can
see.
So use the "Host" header if it is available and fall back to
SERVER_NAME/SERVER_PORT for some clients that don't set
HTTP_HOST.
Signed-off-by: Eric Wong <normalperson@yhbt.net>
-rw-r--r-- | ui-shared.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/ui-shared.c b/ui-shared.c index 4818e70..c23bc75 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -1,96 +1,101 @@ | |||
1 | /* ui-shared.c: common web output functions | 1 | /* ui-shared.c: common web output functions |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cmd.h" | 10 | #include "cmd.h" |
11 | #include "html.h" | 11 | #include "html.h" |
12 | 12 | ||
13 | const char cgit_doctype[] = | 13 | const char cgit_doctype[] = |
14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
16 | 16 | ||
17 | static char *http_date(time_t t) | 17 | static char *http_date(time_t t) |
18 | { | 18 | { |
19 | static char day[][4] = | 19 | static char day[][4] = |
20 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 20 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
21 | static char month[][4] = | 21 | static char month[][4] = |
22 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 22 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
23 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 23 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
24 | struct tm *tm = gmtime(&t); | 24 | struct tm *tm = gmtime(&t); |
25 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | 25 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
26 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | 26 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
27 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 27 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
28 | } | 28 | } |
29 | 29 | ||
30 | void cgit_print_error(char *msg) | 30 | void cgit_print_error(char *msg) |
31 | { | 31 | { |
32 | html("<div class='error'>"); | 32 | html("<div class='error'>"); |
33 | html_txt(msg); | 33 | html_txt(msg); |
34 | html("</div>\n"); | 34 | html("</div>\n"); |
35 | } | 35 | } |
36 | 36 | ||
37 | char *cgit_hosturl() | 37 | char *cgit_hosturl() |
38 | { | 38 | { |
39 | char *host, *port; | 39 | char *host, *port; |
40 | 40 | ||
41 | host = getenv("SERVER_NAME"); | 41 | host = getenv("HTTP_HOST"); |
42 | if (!host) | 42 | if (host) { |
43 | return NULL; | ||
44 | port = getenv("SERVER_PORT"); | ||
45 | if (port && atoi(port) != 80) | ||
46 | host = xstrdup(fmt("%s:%d", host, atoi(port))); | ||
47 | else | ||
48 | host = xstrdup(host); | 43 | host = xstrdup(host); |
44 | } else { | ||
45 | host = getenv("SERVER_NAME"); | ||
46 | if (!host) | ||
47 | return NULL; | ||
48 | port = getenv("SERVER_PORT"); | ||
49 | if (port && atoi(port) != 80) | ||
50 | host = xstrdup(fmt("%s:%d", host, atoi(port))); | ||
51 | else | ||
52 | host = xstrdup(host); | ||
53 | } | ||
49 | return host; | 54 | return host; |
50 | } | 55 | } |
51 | 56 | ||
52 | char *cgit_rooturl() | 57 | char *cgit_rooturl() |
53 | { | 58 | { |
54 | if (ctx.cfg.virtual_root) | 59 | if (ctx.cfg.virtual_root) |
55 | return fmt("%s/", ctx.cfg.virtual_root); | 60 | return fmt("%s/", ctx.cfg.virtual_root); |
56 | else | 61 | else |
57 | return ctx.cfg.script_name; | 62 | return ctx.cfg.script_name; |
58 | } | 63 | } |
59 | 64 | ||
60 | char *cgit_repourl(const char *reponame) | 65 | char *cgit_repourl(const char *reponame) |
61 | { | 66 | { |
62 | if (ctx.cfg.virtual_root) { | 67 | if (ctx.cfg.virtual_root) { |
63 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); | 68 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); |
64 | } else { | 69 | } else { |
65 | return fmt("?r=%s", reponame); | 70 | return fmt("?r=%s", reponame); |
66 | } | 71 | } |
67 | } | 72 | } |
68 | 73 | ||
69 | char *cgit_fileurl(const char *reponame, const char *pagename, | 74 | char *cgit_fileurl(const char *reponame, const char *pagename, |
70 | const char *filename, const char *query) | 75 | const char *filename, const char *query) |
71 | { | 76 | { |
72 | char *tmp; | 77 | char *tmp; |
73 | char *delim; | 78 | char *delim; |
74 | 79 | ||
75 | if (ctx.cfg.virtual_root) { | 80 | if (ctx.cfg.virtual_root) { |
76 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, | 81 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, |
77 | pagename, (filename ? filename:"")); | 82 | pagename, (filename ? filename:"")); |
78 | delim = "?"; | 83 | delim = "?"; |
79 | } else { | 84 | } else { |
80 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, | 85 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, |
81 | (filename ? filename : "")); | 86 | (filename ? filename : "")); |
82 | delim = "&"; | 87 | delim = "&"; |
83 | } | 88 | } |
84 | if (query) | 89 | if (query) |
85 | tmp = fmt("%s%s%s", tmp, delim, query); | 90 | tmp = fmt("%s%s%s", tmp, delim, query); |
86 | return tmp; | 91 | return tmp; |
87 | } | 92 | } |
88 | 93 | ||
89 | char *cgit_pageurl(const char *reponame, const char *pagename, | 94 | char *cgit_pageurl(const char *reponame, const char *pagename, |
90 | const char *query) | 95 | const char *query) |
91 | { | 96 | { |
92 | return cgit_fileurl(reponame,pagename,0,query); | 97 | return cgit_fileurl(reponame,pagename,0,query); |
93 | } | 98 | } |
94 | 99 | ||
95 | const char *cgit_repobasename(const char *reponame) | 100 | const char *cgit_repobasename(const char *reponame) |
96 | { | 101 | { |