Problem
file_create_url()
currently does not allow protocol-relative (//files/image.jpg
or even ://files/image.jpg
) nor root-relative (/files/image.jpg
) file URLs to be created.
Protocol-relative URLs
Protocol-relative URLs are a necessity to be able to transparently switch between the HTTP and HTTPs version of a site, i.e. without generating different HTML.
What is gained:
- This would allow the Secure Pages module in Drupal 7 to automatically serve all resources on the page through HTTPS when HTTPS is used for the page and through HTTP when HTTP is used for the page, thanks to the power of
hook_file_url_alter()
(contributed by me in #499156: CDN integration: allow file URLs to be rewritten by hook_file_url_alter(), documentation).See http://carloamoroso.it/blog/web-standard/protocol-relative-urls-and-mixe... and http://www.stevesouders.com/blog/2010/02/10/5a-missing-schema-double-dow... for more details.
(That last post also details a problem with IE7 and IE8 when it comes to protocol-relative URLs though: it causes CSS files to be downloaded twice. Likely, this will be fixed in the next version of IE. This won't prevent us from using protocol-relative URLs for resources other than CSS of course.) - This would allow the same cached content to be served on both HTTP and HTTPS pages.
- This would allow Drupal 7 ports of modules such as Parallel to remain protocol-agnostic, CDN integration to remain flexible and Pathologic to continue to offer full flexibility in configuring preferred URL styles.
Root-relative URLs
I personally don't use root-relative URLs, but there's no good reason that I know of to prevent these URLs from being created.
What is gained:
- This would allow Drupal 7 ports of modules such as Pathologic to continue to offer full flexibility in configuring preferred URL styles.
Solution
Patch attached that makes this possible. Comes with updated unit tests, that pass.
Comment | File | Size | Author |
---|---|---|---|
#6 | 777830-6.patch | 10.02 KB | Wim Leers |
file_create_url-root-relative-and-protocol-relative-urls.patch | 9.69 KB | Wim Leers | |
Comments
Comment #2
Wim LeersWell, I can't reproduce the error. At least, not with HEAD. Apparently tests are running against alpha 3. Must be because I had set 7.0-alpha3 as the version.
Now I also installed D7 alpha 3, ran the test that is claimed to be failing, and found that it again fully passed. So there must be something wrong with the testing bot?
Marking as needs review and changed version to 7.x-dev. Hopefully testbot won't fail this time.
Comment #3
Wim Leersfile_create_url-root-relative-and-protocol-relative-urls.patch queued for re-testing.
Comment #4
aaron CreditAttribution: aaron commentedsubscribe
Comment #5
aaron CreditAttribution: aaron commentedchatted briefly w/ Wim in irc: the patch looks good after a review. looks like it would largely handle unmanaged shipped files, and since it's not actually touching streams, I don't think it would introduce anything that would negatively impact any existing functionality. thus, it looks to be beneficial, especially as its tests are thorough.
the only minor bit is that
really only needs to be
from the standpoint of the computer (with maybe a quick comment for human evaluators).
Comment #6
Wim LeersThanks again for your time Aaron! :)
I fully agree with your concerns. Patch rerolled and with hopefully sufficiently clear explanation in the comment.
Comment #7
Wim LeersAlso, a quick note: I cite
://files/image.jpg
as an example of a protocol-relative URI in the first post. While that is true, it's not properly supported in all browsers: at least Webkit-based browsers don't support this, and possibly others. And since//files/image.jpg
is equivalent and most definitely more widespread, only the latter are supported by this core patch. But again: this does not reduce the spectrum of possibilities.Comment #8
aaron CreditAttribution: aaron commentedthis looks excellent, thanks Wim!
Comment #9
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.
Comment #10
Wim LeersTagging for better findability in the future
Comment #12
hass CreditAttribution: hass commented@Wim: Do I need secure pages module to get urls absolute to the root in source code (/drupal7/foo/bar.png) - otherwise they are still kept full qualified?