Add option to produce protocol-relative URLs with S3BotoStorage

Issue #105 resolved
idangazit created an issue

Currently, S3BotoStorage returns either HTTP or HTTPS URLs from the url() method [1], depending on the value of secure_urls.

If you're using S3Boto and want to return protocol-relative URLs [2], there's currently no way to ask for this behavior. Protocol-relative URL's omit the protocol and use whatever the browser is currently using so as to avoid mixed-content warnings.

This sort of thing would be especially useful in conjunction with something like django-staticfiles, where I'd like my site's media to work regardless of whether the browser is accessing via HTTP or HTTPS.

My ghetto subclass hack[3] to force this is ugly. More settings are always suspect, but in this case I think they make sense.

Two possible approaches:

=== Approach 1 - More Backwards Compatible ===

Add another setting, AWS_S3_PROTOCOL_RELATIVE_URLS. If it's true, short-circuit the existing AWS_S3_SECURE_URLS setting and always return protocol-relative URLs.

=== Approach 2: Cleaner ===

Get rid of AWS_S3_SECURE_URLS and instead use something like AWS_S3_URL_PROTOCOL. It can be a string like 'http' or 'https'. A blank string would result in a protocol-relative URL.

Side benefit: this would be future-compatible with SPDY :)

Comments (4)

  1. Gordon NA

    I think this is a good idea. +1

    I think Approach #2 is the right way to go. It can be made backwards compatible by setting the default AWS_S3_URL_PROTOCOL based off of AWS_S3_SECURE_URLS and raise a depreciation warning if AWS_S3_SECURE_URLS is used.

    This is a simple untested example of concept:

    import warnings
    AWS_S3_URL_PROTOCOL = getattr(settings, 'AWS_S3_URL_PROTOCOL', None)
    if AWS_S3_URL_PROTOCOL is None:
    	secure = getattr(settings, 'AWS_S3_SECURE_URLS', None)
    	if secure is None:
    		AWS_S3_URL_PROTOCOL = ""
    		warnings.warn("AWS_S3_SECURE_URLS is depreciated.  Use AWS_S3_URL_PROTOCOL instead.", DeprecationWarning)
    		if secure:
    			AWS_S3_URL_PROTOCOL = "https"
    			AWS_S3_URL_PROTOCOL = "http"

    Then support for AWS_S3_SECURE_URLS can be removed at some point.

  2. Log in to comment