diff --git a/IkiWiki/CGI.pm b/IkiWiki/CGI.pm index 70e3b7134..0224c2aac 100644 --- a/IkiWiki/CGI.pm +++ b/IkiWiki/CGI.pm @@ -66,7 +66,10 @@ sub cgitemplate ($$$;@) { my $page=""; if (exists $params{page}) { $page=delete $params{page}; - $params{forcebaseurl}=urlabs(urlto($page), $topurl); + $params{forcebaseurl}=urlto($page); + if (! $config{html5}) { + $params{forcebaseurl}=urlabs($params{forcebaseurl}, $topurl); + } } run_hooks(pagetemplate => sub { shift->( @@ -77,12 +80,17 @@ sub cgitemplate ($$$;@) { }); templateactions($template, ""); + my $baseurl = baseurl(); + if (! $config{html5}) { + $baseurl = urlabs($baseurl, $topurl), + } + $template->param( dynamic => 1, title => $title, wikiname => $config{wikiname}, content => $content, - baseurl => urlabs(baseurl(), $topurl), + baseurl => $baseurl, html5 => $config{html5}, %params, ); diff --git a/doc/bugs/trouble_with_base_in_search.mdwn b/doc/bugs/trouble_with_base_in_search.mdwn index ca6a6c5cc..7c3d13387 100644 --- a/doc/bugs/trouble_with_base_in_search.mdwn +++ b/doc/bugs/trouble_with_base_in_search.mdwn @@ -58,3 +58,14 @@ And I'm sure someone else could come up with something better and more general. >> I suppose what I would like would be to not need to use a `` in searching at all. >> --[[KathrynAndersen]] + +>>> `` is *not* required to be absolute in HTML5, so when +>>> `html5: 1` is used, I've changed it to be host-relative in most cases. +>>> I think that at least partially addresses this bug report, +>>> particularly if we [[todo/generate HTML5 by default]] like I've suggested. +>>> +>>> The `` is there so we can avoid having to compute how to +>>> get to (the virtual directory containing) the root of the wiki from +>>> `ikiwiki.cgi`, which might well be somewhere odd like `/cgi-bin/`. +>>> I think there are probably other things that it fixes or simplifies. +>>> --[[smcv]] diff --git a/t/relativity.t b/t/relativity.t index 86b807b5b..300c6e61d 100755 --- a/t/relativity.t +++ b/t/relativity.t @@ -77,6 +77,7 @@ url: "http://example.com/wiki/" cgiurl: "http://example.com/cgi-bin/ikiwiki.cgi" cgi_wrapper: t/tmp/ikiwiki.cgi cgi_wrappermode: 0754 +html5: 0 # make it easier to test previewing add_plugins: - anonok @@ -162,6 +163,106 @@ like($bits{stylehref}, qr{^(?:(?:http:)?//example.com)?/wiki/style.css$}); like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$}); like($bits{cgihref}, qr{^(?:(?:http:)?//example.com)?/cgi-bin/ikiwiki.cgi$}); +# in html5, the is allowed to be relative, and we take full +# advantage of that +writefile("test.setup", "t/tmp", < on static HTML +unlike($content, qr{]+href="/cgi-bin/ikiwiki.cgi\?do=prefs"}); +# cross-links between static pages are relative +like($content, qr{
  • A: a
  • }); +like($content, qr{
  • B: b
  • }); +like($content, qr{
  • E: e
  • }); + +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '80'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'example.com'; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "/wiki/"); +is($bits{stylehref}, "/wiki/style.css"); +is($bits{tophref}, "/wiki/"); +is($bits{cgihref}, "/cgi-bin/ikiwiki.cgi"); + +# when accessed via HTTPS, links are secure - this is easy because under +# html5 they're independent of the URL at which the CGI was accessed +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '443'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'example.com'; + $ENV{HTTPS} = 'on'; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "/wiki/"); +is($bits{stylehref}, "/wiki/style.css"); +is($bits{tophref}, "/wiki/"); +is($bits{cgihref}, "/cgi-bin/ikiwiki.cgi"); + +# when accessed via a different hostname, links stay on that host - +# this is really easy in html5 because we can use relative URLs +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '80'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'staging.example.net'; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "/wiki/"); +is($bits{stylehref}, "/wiki/style.css"); +is($bits{tophref}, "/wiki/"); +is($bits{cgihref}, "/cgi-bin/ikiwiki.cgi"); + +# previewing a page +$in = 'do=edit&page=a/b/c&Preview'; +run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'POST'; + $ENV{SERVER_PORT} = '80'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{HTTP_HOST} = 'example.com'; + $ENV{CONTENT_LENGTH} = length $in; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "/wiki/a/b/c/"); +is($bits{stylehref}, "/wiki/style.css"); +like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$}); +is($bits{cgihref}, "/cgi-bin/ikiwiki.cgi"); + ####################################################################### # site 2: static content and CGI are on different servers @@ -175,6 +276,7 @@ url: "http://static.example.com/" cgiurl: "http://cgi.example.com/ikiwiki.cgi" cgi_wrapper: t/tmp/ikiwiki.cgi cgi_wrappermode: 0754 +html5: 0 # make it easier to test previewing add_plugins: - anonok @@ -246,11 +348,101 @@ run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub { like($bits{basehref}, qr{^http://static.example.com/a/b/c/$}); like($bits{stylehref}, qr{^(?:(?:http:)?//static.example.com|\.\./\.\./\.\.)/style.css$}); like($bits{tophref}, qr{^(?:(?:http:)?//static.example.com|\.\./\.\./\.\.)/$}); +like($bits{cgihref}, qr{^(?:(?:http:)?//(?:staging\.example\.net|cgi\.example\.com))?/ikiwiki.cgi$}); TODO: { local $TODO = "use self-referential CGI URL?"; like($bits{cgihref}, qr{^(?:(?:http:)?//staging.example.net)?/ikiwiki.cgi$}); } +writefile("test.setup", "t/tmp", < on static HTML +unlike($content, qr{]+href="(?:http:)?//cgi.example.com/ikiwiki.cgi\?do=prefs"}); +# cross-links between static pages are still relative +like($content, qr{
  • A: a
  • }); +like($content, qr{
  • B: b
  • }); +like($content, qr{
  • E: e
  • }); + +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '80'; + $ENV{SCRIPT_NAME} = '/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'cgi.example.com'; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "//static.example.com/"); +is($bits{stylehref}, "//static.example.com/style.css"); +is($bits{tophref}, "//static.example.com/"); +is($bits{cgihref}, "//cgi.example.com/ikiwiki.cgi"); + +# when accessed via HTTPS, links are secure - in fact they're exactly the +# same as when accessed via HTTP +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '443'; + $ENV{SCRIPT_NAME} = '/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'cgi.example.com'; + $ENV{HTTPS} = 'on'; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "//static.example.com/"); +is($bits{stylehref}, "//static.example.com/style.css"); +is($bits{tophref}, "//static.example.com/"); +is($bits{cgihref}, "//cgi.example.com/ikiwiki.cgi"); + +# when accessed via a different hostname, links to the CGI (only) should +# stay on that host? +$in = 'do=edit&page=a/b/c&Preview'; +run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'POST'; + $ENV{SERVER_PORT} = '80'; + $ENV{SCRIPT_NAME} = '/ikiwiki.cgi'; + $ENV{HTTP_HOST} = 'staging.example.net'; + $ENV{CONTENT_LENGTH} = length $in; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "//static.example.com/a/b/c/"); +is($bits{stylehref}, "//static.example.com/style.css"); +is($bits{tophref}, "../../../"); +like($bits{cgihref}, qr{//(?:staging\.example\.net|cgi\.example\.com)/ikiwiki\.cgi}); +TODO: { +local $TODO = "use self-referential CGI URL maybe?"; +is($bits{cgihref}, "//staging.example.net/ikiwiki.cgi"); +} + ####################################################################### # site 3: we specifically want everything to be secure @@ -264,6 +456,7 @@ url: "https://example.com/wiki/" cgiurl: "https://example.com/cgi-bin/ikiwiki.cgi" cgi_wrapper: t/tmp/ikiwiki.cgi cgi_wrappermode: 0754 +html5: 0 # make it easier to test previewing add_plugins: - anonok @@ -356,6 +549,8 @@ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$}); like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$}); like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$}); +# not testing html5: 0 here because that ends up identical to site 1 + ####################################################################### # site 4 (NetBSD wiki): CGI is secure, static content doesn't have to be @@ -369,6 +564,7 @@ url: "http://example.com/wiki/" cgiurl: "https://example.com/cgi-bin/ikiwiki.cgi" cgi_wrapper: t/tmp/ikiwiki.cgi cgi_wrappermode: 0754 +html5: 0 # make it easier to test previewing add_plugins: - anonok @@ -441,6 +637,7 @@ run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { like($bits{basehref}, qr{^https://staging.example.net/wiki/$}); like($bits{stylehref}, qr{^(?:(?:https:)?//staging.example.net)?/wiki/style.css$}); like($bits{tophref}, qr{^(?:(?:(?:https:)?//staging.example.net)?/wiki|\.)/$}); +like($bits{cgihref}, qr{^(?:(?:https:)?//(?:staging\.example\.net|example\.com))?/cgi-bin/ikiwiki.cgi$}); TODO: { local $TODO = "this should really point back to itself but currently points to example.com"; like($bits{cgihref}, qr{^(?:(?:https:)?//staging.example.net)?/cgi-bin/ikiwiki.cgi$}); @@ -462,6 +659,111 @@ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$}); like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$}); like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$}); +writefile("test.setup", "t/tmp", < on static HTML +unlike($content, qr{]+href="https://example.com/cgi-bin/ikiwiki.cgi\?do=prefs"}); +# cross-links between static pages are relative +like($content, qr{
  • A: a
  • }); +like($content, qr{
  • B: b
  • }); +like($content, qr{
  • E: e
  • }); + +# when accessed via HTTPS, links are secure (to avoid mixed-content) +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '443'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'example.com'; + $ENV{HTTPS} = 'on'; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "/wiki/"); +is($bits{stylehref}, "/wiki/style.css"); +is($bits{tophref}, "/wiki/"); +like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$}); + +# when not accessed via HTTPS, ??? +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '80'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'example.com'; +}); +%bits = parse_cgi_content($content); +like($bits{basehref}, qr{^(?:https?://example.com)?/wiki/$}); +like($bits{stylehref}, qr{^(?:(?:https?:)?//example.com)?/wiki/style.css$}); +like($bits{tophref}, qr{^(?:(?:https?://example.com)?/wiki|\.)/$}); +like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$}); + +# when accessed via a different hostname, links stay on that host +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{SERVER_PORT} = '443'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; + $ENV{HTTP_HOST} = 'staging.example.net'; + $ENV{HTTPS} = 'on'; +}); +%bits = parse_cgi_content($content); +# because the static and dynamic stuff is on the same server, we assume that +# both are also on the staging server +is($bits{basehref}, "/wiki/"); +is($bits{stylehref}, "/wiki/style.css"); +like($bits{tophref}, qr{^(?:/wiki|\.)/$}); +like($bits{cgihref}, qr{^(?:(?:https:)?//(?:example\.com|staging\.example\.net))?/cgi-bin/ikiwiki.cgi$}); +TODO: { +local $TODO = "this should really point back to itself but currently points to example.com"; +like($bits{cgihref}, qr{^(?:(?:https:)?//staging.example.net)?/cgi-bin/ikiwiki.cgi$}); +} + +# previewing a page +$in = 'do=edit&page=a/b/c&Preview'; +run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'POST'; + $ENV{SERVER_PORT} = '443'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi'; + $ENV{HTTP_HOST} = 'example.com'; + $ENV{CONTENT_LENGTH} = length $in; + $ENV{HTTPS} = 'on'; +}); +%bits = parse_cgi_content($content); +is($bits{basehref}, "/wiki/a/b/c/"); +is($bits{stylehref}, "/wiki/style.css"); +like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$}); +like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$}); + # Deliberately not testing https static content with http cgiurl, # because that makes remarkably little sense. @@ -478,6 +780,57 @@ cgiurl: ikiwiki.cgi w3mmode: 1 cgi_wrapper: t/tmp/ikiwiki.cgi cgi_wrappermode: 0754 +html5: 0 +add_plugins: +- anonok +anonok_pagespec: "*" +ENV: { 'PERL5LIB': '$PERL5LIB' } +EOF +); + +ok(unlink("t/tmp/ikiwiki.cgi")); +ok(! system("./ikiwiki.out --setup t/tmp/test.setup --rebuild --wrappers")); + +# CGI wrapper should be exactly the requested mode +(undef, undef, $mode, undef, undef, + undef, undef, undef, undef, undef, + undef, undef, undef) = stat("t/tmp/ikiwiki.cgi"); +is($mode & 07777, 0754); + +ok(-e "t/tmp/out/a/b/c/index.html"); +$content = readfile("t/tmp/out/a/b/c/index.html"); +# no on static HTML +unlike($content, qr{]+href="(?:file://)?/\$LIB/ikiwiki-w3m.cgi/ikiwiki.cgi\?do=prefs"}); +# cross-links between static pages are still relative +like($content, qr{
  • A: a
  • }); +like($content, qr{
  • B: b
  • }); +like($content, qr{
  • E: e
  • }); + +run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub { + $ENV{REQUEST_METHOD} = 'GET'; + $ENV{PATH_INFO} = '/ikiwiki.cgi'; + $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki-w3m.cgi'; + $ENV{QUERY_STRING} = 'do=prefs'; +}); +%bits = parse_cgi_content($content); +like($bits{tophref}, qr{^(?:\Q$pwd\E/t/tmp/out|\.)/$}); +like($bits{cgihref}, qr{^(?:file://)?/\$LIB/ikiwiki-w3m.cgi/ikiwiki.cgi$}); +like($bits{basehref}, qr{^(?:(?:file:)?//)?\Q$pwd\E/t/tmp/out/$}); +like($bits{stylehref}, qr{^(?:(?:(?:file:)?//)?\Q$pwd\E/t/tmp/out|\.)/style.css$}); + +writefile("test.setup", "t/tmp", <