reopening, reverting part of this for now
[ikiwiki] / doc / todo / want_to_avoid_ikiwiki_using_http_or_https_in_urls_to_allow_serving_both.mdwn
1 ## current status
2
3 99% of the way there, but:
4
5 * CGI pages, with the exception of edit pages, set `<base>` to
6   `$config{url}`
7 * When doing a Logout or Cancel, or when saving an edited page,
8   the user is returned to something derived from `$config{url}`.
9 * RecentChanges page may link to things derived from `$config{url}`.
10
11 All of the above are because I had to revert `baseurl(undef)` to always
12 returning an absolute url, since relative urls caused breakage as seen in
13 [[preview_base_url_should_be_absolute]]. --[[Joey]] 
14
15 Ideally, baseurl would return an absolute url derived from the url
16 being used to access the cgi, but that needs access to the CGI object,
17 which it does not have. --[[Joey]] 
18
19 ## old attempt
20
21 It looks like all links in websites are absolute paths, this has some limitations:
22
23 * If connecting to website via https://... all links will take you back to http://
24 * Makes it harder to mirror website via HTML version, as all links have to be updated.
25
26 It would be good if relative paths could be used instead, so the transport method isn't changed unless specifically requested.
27
28 -- Brian May
29
30 > Er, which absolute links are you talking about? If you view the source
31 > to this page, you'll find links such as "../favicon.ico", "../style.css",
32 > "../../", and "../". The only absolute links are to CGIs and the w3c DTD.
33 > --[[Joey]]
34
35 >> The problem is within the CGI script. The links within the HTML page are all
36 >> absolute, including links to the css file. Having a http links within a HTML
37 >> page retrieved using https upset most browsers (I think). Also if I push cancel
38 >> on the edit page in https, I end up at at http page. -- Brian May
39
40 >>> Ikiwiki does not hardcode http links anywhere. If you don't want
41 >>> it to use such links, change your configuration to use https
42 >>> consistently. --[[Joey]]
43
44 Errr... That is not a solution, that is a work around. ikiwiki does not hard
45 code the absolute paths, but absolute paths are hard coded in the configuration
46 file. If you want to serve your website so that the majority of users can see
47 it as http, including in rss feeds (this allows proxy caches to cache the
48 contents and has reduced load requirements), but editing is done via https for
49 increased security, it is not possible. I have some ideas how this can be
50 implemented (as ikiwiki has the absolute path to the CGI script and the
51 absolute path to the destination, it should be possible to generate a relative
52 path from one to the other), although some minor issues still need to be
53 resolved. -- Brian May
54
55 I noticed the links to the images on <http://ikiwiki.info/recentchanges/> are
56 also absolute, that is <http://ikiwiki.info/wikiicons/diff.png>; this seems
57 surprising, as the change.tmpl file uses &lt;TMPL_VAR BASEURL&gt; which seems
58 to do the right thing in page.tmpl, but not for change.tmpl. Where is BASEURL
59 set? -- Brian May
60
61 > The use of an absolute baseurl in change.tmpl is a special case. --[[Joey]]
62
63 So I'm facing this same issue. I have a wiki which needs to be accessed on
64 three different URLs(!) and the hard coding of the URL from the setup file is
65 becoming a problem for me. Is there anything I can do here? --[[Perry]]
66
67 > I remain puzzled by the problem that Brian is discussing. I don't see
68 > why you can't just set the cgiurl and url to a https url, and serve
69 > the site using both http and https.
70
71 > Just for example, <https://kitenet.net/> is an ikiwiki, and it is accessible
72 > via https or http, and if you use https, links will remain on https (except
73 > for links using the cgi, which I could fix by changing the cgiurl to https).
74
75 > I think it's possible ikiwiki used to have some
76 > absolute urls that have been fixed since Brian filed the bug. --[[Joey]]  
77
78 [[wishlist]]
79
80 ----
81
82 [[!toggle id="smcv-https" text="Some discussion of a rejected implementation, smcv/https."]]
83 [[!toggleable id="smcv-https" text="""
84
85 [[!template id=gitbranch branch=smcv/https author="[[smcv]]"]]
86
87 For a while I've been using a configuration where each wiki has a HTTP and
88 a HTTPS mirror, and updating one automatically updates the other, but
89 that seems unnecessarily complicated. My `https` branch adds `https_url`
90 and `https_cgiurl` config options which can be used to provide a HTTPS
91 variant of an existing site; the CGI script automatically detects whether
92 it was accessed over HTTPS and switches to the other one.
93
94 This required some refactoring, which might be worth merging even if
95 you don't like my approach:
96
97 * change `IkiWiki::cgiurl` to return the equivalent of `$config{cgiurl}` if
98   called with no parameters, and change all plugins to indirect through it
99   (then I only need to change that one function for the HTTPS hack)
100
101 * `IkiWiki::baseurl` already has similar behaviour, so change nearly all
102   references to the `$config{url}` to call `baseurl` (a couple of references
103   specifically wanted the top-level public URL for Google or Blogspam rather
104   than a URL for the user's browser, so I left those alone)
105
106 --[[smcv]]
107
108 > The justification for your patch seems to be wanting to use a different
109 > domain, like secure.foo.com, for https? Can you really not just configure
110 > both url and cgiurl to use `https://secure.foo.com/...` and rely on
111 > relative links to keep users of `http://insecure.foo.com/` on http until
112 > they need to use the cgi? 
113
114 >> My problem with that is that uses of the CGI aren't all equal (and that
115 >> the CA model is broken). You could put CGI uses in two classes:
116 >>
117 >> - websetup and other "serious" things (for the sites I'm running, which
118 >>   aren't very wiki-like, editing pages is also in this class).
119 >>   I'd like to be able to let privileged users log in over
120 >>   https with httpauth (or possibly even a client certificate), and I don't
121 >>   mind teaching these few people how to do the necessary contortions to
122 >>   enable something like CACert.
123 >>
124 >> - Random users making limited use of the CGI: do=goto, do=404, and
125 >>   commenting with an OpenID. I don't think it's realistic to expect
126 >>   users to jump through all the CA hoops to get CACert installed for that,
127 >>   which leaves their browsers being actively obstructive, unless I either
128 >>   pay the CA tax (per subdomain) to get "real" certificates, or use plain
129 >>   http.
130 >>
131 >> On a more wiki-like wiki, the second group would include normal page edits.
132 >>
133 >>> I see your use case. It still seems to me that for the more common
134 >>> case where CA tax has been paid (getting a cert that is valid for
135 >>> multiple subdomains should be doable?), having anything going through the
136 >>> cgiurl upgrade to https would be ok. In that case, http is just an
137 >>> optimisation for low-value, high-aggregate-bandwidth type uses, so a
138 >>> little extra https on the side is not a big deal. --[[Joey]]
139 >>
140 >> Perhaps I'm doing this backwards, and instead of having the master
141 >> `url`/`cgiurl` be the HTTP version and providing tweakables to override
142 >> these with HTTPS, I should be overriding particular uses to plain HTTP...
143 >>
144 >> --[[smcv]]
145 >>> 
146 >>> Maybe, or I wonder if you could just use RewriteEngine for such selective
147 >>> up/downgrading. Match on `do=(edit|create|prefs)`. --[[Joey]]
148
149 > I'm unconvinced.
150
151 > `Ikiwiki::baseurl()."foo"` just seems to be asking for trouble,
152 > ie being accidentially written as `IkiWiki::baseurl("foo")`,
153 > which will fail when foo is not a page, but some file.
154
155 >> That's a good point. --s
156
157 > I see multiple places (inline.pm, meta.pm, poll.pm, recentchanges.pm)
158 > where it will now put the https url into a static page if the build
159 > happens to be done by the cgi accessed via https, but not otherwise.
160 > I would rather not have to audit for such problems going forward.
161
162 >> Yes, that's a problem with this approach (either way round). Perhaps
163 >> making it easier to run two mostly-synched copies like I was previously
164 >> doing is the only solution... --s
165
166 """]]
167
168 ----
169
170 [[!template id=gitbranch branch=smcv/ready/localurl author="[[smcv]]"]]
171 [[!tag patch]]
172
173 OK, here's an alternative approach, closer in spirit to what was initially
174 requested. I included a regression test for `urlto`, `baseurl` and `cgiurl`,
175 now that they have slightly more complex behaviour.
176
177 The idea is that in the common case, the CGI and the pages will reside on the
178 same server, so they can use "semi-absolute" URLs (`/ikiwiki.cgi`, `/style.css`,
179 `/bugs/done`) to refer to each other. Most redirects, form actions, links etc.
180 can safely use this form rather than the fully-absolute URL.
181
182 The initial version of the branch had config options `local_url` and
183 `local_cgiurl`, but they're now automatically computed by checking
184 whether `url` and `cgiurl` are on the same server with the the same URL
185 scheme. In theory you could use things like `//static.example.com/wiki/`
186 and `//dynamic.example.com/ikiwiki.cgi` to preserve choice of http/https
187 while switching server, but I don't know how consistently browsers
188 support that.
189
190 "local" here is short for "locally valid", because these URLs are neither
191 fully relative nor fully absolute, and there doesn't seem to be a good name
192 for them...
193
194 I've tested this on a demo website with the CGI enabled, and it seemed to
195 work nicely (there might be bugs in some plugins, I didn't try all of them).
196 The branch at [[todo/use secure cookies for SSL logins]] goes well with
197 this one.
198
199 The `$config{url}` and `$config{cgiurl}` are both HTTP, but if I enable
200 `httpauth`, set `cgiauthurl` to a HTTPS version of the same site and log
201 in via that, links all end up in the HTTPS version.
202
203 New API added by this branch:
204
205 * `urlto(x, y, 'local')` uses `$local_url` instead of `$config{url}`
206
207   > Yikes. I see why you wanted to keep it to 3 parameters (4 is too many,
208   > and po overrides it), but I dislike overloading the third parameter
209   > like that.
210   > 
211   > There are fairly few calls to `urlto($foo, $bar)`, so why not
212   > make that always return the semi-local url form, and leave the third
213   > parameter for the cases that need a true fully-qualified url.
214   > The new form for local urls will typically be only a little bit longer,
215   > except in the unusual case where the cgiurl is elsewhere. --[[Joey]]
216
217   >> So, have urlto(x, y) use `$local_url`? There are few calls, but IMO
218   >> they're for the most important things - wikilinks, img, map and
219   >> other ordinary hyperlinks. Using `$local_url` would be fine for
220   >> webserver-based use, but it does stop you browsing your wiki's
221   >> HTML over `file:///` (unless you set that as the base URL, but
222   >> then you can't move it around), and stops you moving simple
223   >> outputs (like the docwiki!) around.
224   >>
225   >> I personally think breaking the docwiki is enough to block that.
226   >>
227   >>> Well, the docwiki doesn't have an url configured at all, so I assumed
228   >>> it would need to fall back to current behavior in that case. I had
229   >>> not thought about browsing wiki's html files though, good point.
230   >>
231   >> How about this?
232   >>
233   >> * `urlto($link, $page)` with `$page` defined: relative
234   >> * `urlto($link, undef)`: local, starts with `/`
235   >> * `urlto($link)`: also local, as a side-effect
236   >> * `urlto($link, $anything, 1)` (but idiomatically, `$anything` is
237   >>   normally undef): absolute, starts with `http[s]://`
238   >>
239   >> --[[smcv]]
240   >> 
241   >>> That makes a great deal of sense, bravo for actually removing
242   >>> parameters in the common case while maintaining backwards
243   >>> compatability! --[[Joey]]
244   >>>
245   >>>> Done in my `localurl` branch; not tested in a whole-wiki way
246   >>>> yet, but I did add a regression test. I've used
247   >>>> `urlto(x, undef)` rather than `urlto(x)` so far, but I could
248   >>>> go back through the codebase using the short form if you'd
249   >>>> prefer. --[[smcv]]
250   >>> 
251   >>> It does highlight that it would be better to have a
252   >>> `absolute_urlto($link)` (or maybe `absolute(urlto($link))` )
253   >>> rather than the 3 parameter form. --[[Joey]]
254   >>>
255   >>> Possibly. I haven't added this.
256
257 * `IkiWiki::baseurl` has a new second argument which works like the
258   third argument of `urlto`
259
260   > I assume you have no objection to this --[[smcv]]
261
262   >> It's so little used that I don't really care if it's a bit ugly.
263   >> (But I assume changes to `urlto` will follow through here anyway.)
264   >> --[[Joey]] 
265
266   >>> I had to use it a bit more, as a replacement for `$config{url}`
267   >>> when doing things like referencing stylesheets or redirecting to
268   >>> the top of the wiki.
269   >>>
270   >>> I ended up redoing this without the extra parameter. Previously,
271   >>> `baseurl(undef)` was the absolute URL; now, `baseurl(undef)` is
272   >>> the local path. I know you objected to me using `baseurl()` in
273   >>> an earlier branch, because `baseurl().$x` looks confusingly
274   >>> similar to `baseurl($x)` but has totally different semantics;
275   >>> I've generally written it `baseurl(undef)` now, to be more
276   >>> explicit. --[[smcv]]
277
278 * `IkiWiki::cgiurl` uses `$local_cgiurl` if passed `local_cgiurl => 1`
279
280   > Now changed to always use the `$local_cgiurl`. --[[smcv]]
281
282 * `IkiWiki::cgiurl` omits the trailing `?` if given no named parameters
283   except `cgiurl` and/or `local_cgiurl`
284
285   > I assume you have no objection to this --[[smcv]]
286   > 
287   >> Nod, although I don't know of a use case. --[[Joey]]
288
289   >>> The use-case is that I can replace `$config{cgiurl}` with
290   >>> `IkiWiki::cgiurl()` for things like the action attribute of
291   >>> forms. --[[smcv]]
292
293 Fixed bugs:
294
295 * I don't think anything except `openid` calls `cgiurl` without also
296   passing in `local_cgiurl => 1`, so perhaps that should be the default;
297   `openid` uses the `cgiurl` named parameter anyway, so there doesn't even
298   necessarily need to be a way to force absolute URLs? Any other module
299   that really needs an absolute URL could use
300   `cgiurl(cgiurl => $config{cgiurl}, ...)`,
301   although that does look a bit strange
302
303   > I agree that makes sense. --[[Joey]]
304
305   >> I'm not completely sure whether you're agreeing with "perhaps do this"
306   >> or "that looks too strange", so please disambiguate:
307   >> would you accept a patch that makes `cgiurl` default to a local
308   >> (starts-with-`/`) result? If you would, that'd reduce the diff. --[[smcv]]
309
310   >>> Yes, I absolutely think it should default to local. (Note that
311   >>> if `absolute()` were implemented as suggested above, it could also
312   >>> be used with cgiurl if necessary.) --[[Joey]]
313
314   >>>> Done (minus `absolute()`). --[[smcv]]
315
316 Potential future things:
317
318 * It occurs to me that `IkiWiki::cgiurl` could probably benefit from being
319   exported? Perhaps also `IkiWiki::baseurl`?
320
321   > Possibly, see [[firm_up_plugin_interface]]. --[[Joey]]
322
323   >> Not really part of this branch, though, so wontfix (unless you ask me
324   >> to do so). --[[smcv]]
325
326 * Or, to reduce use of the unexported `baseurl` function, it might make
327   sense to give `urlto` a special case that references the root of the wiki,
328   with a trailing slash ready to append stuff: perhaps `urlto('/')`,
329   with usage like this?
330
331         do_something(baseurl => urlto('/', undef, local)`);
332         do_something_else(urlto('/').'style.css');
333         IkiWiki::redirect(urlto('/', undef, 1));
334
335   > AFACIS, `baseurl` is only called in 3 places so I don't think that's
336   > needed. --[[Joey]] 
337
338   >> OK, wontfix. For what it's worth, my branch has 6 uses in IkiWiki
339   >> core code (IkiWiki, CGI, Render and the pseudo-core part of editpage)
340   >> and 5 in plugins, since I used it for things like redirection back
341   >> to the top of the wiki --[[smcv]]
342
343 merged|done --[[Joey]] (But reopened, see above.)
344
345 ----
346
347 Update: I had to revert part of 296e5cb2fd3690e998b3824d54d317933c595873,
348 since it broke openid logins. The openid object requires a complete,
349 not a relative cgiurl. I'm not sure if my changing that back to using
350 `$config{cgiurl}` will force users back to eg, the non-https version of a
351 site when logging in via openid.
352
353 > Ok, changed it to use `CGI->url` to get the current absolute cgi url. --[[Joey]]