Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
stylesheets: Add protocol-relative stylesheets.
Browse files Browse the repository at this point in the history
Now that we have protocol-relative media objects, there's no need to
maintain HTTP and HTTPS variants of subreddit stylesheets. Instead,
we'll start writing all new stylesheets with protocol-relative image
URLs. Old subreddits will continue to use their protocol-specific
stylesheets for now until new stylesheets are uploaded or existing ones
migrated.
  • Loading branch information
spladug committed Dec 1, 2014
1 parent d24a8eb commit 7040139
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
6 changes: 1 addition & 5 deletions r2/r2/controllers/api.py
Expand Up @@ -2015,11 +2015,7 @@ def POST_subreddit_stylesheet(self, form, jquery,
description = wiki.modactions.get('config/stylesheet')
ModAction.create(c.site, c.user, 'wikirevise', description)

parsed_http, parsed_https = parsed
if c.secure:
jquery.apply_stylesheet(parsed_https)
else:
jquery.apply_stylesheet(parsed_http)
jquery.apply_stylesheet(parsed)

if op == 'preview':
# try to find a link to use, otherwise give up and
Expand Down
9 changes: 7 additions & 2 deletions r2/r2/lib/pages/pages.py
Expand Up @@ -123,6 +123,7 @@
comment_label,
format_number,
get_domain,
make_url_https,
make_url_protocol_relative,
static,
)
Expand Down Expand Up @@ -356,10 +357,14 @@ def __init__(self, space_compress=None, nav_menus=None, loginbox=True,
def get_subreddit_stylesheet_url(sr):
if not g.css_killswitch and c.can_apply_styles and c.allow_styles:
if c.secure:
if sr.stylesheet_url_https:
if sr.stylesheet_url:
return make_url_https(sr.stylesheet_url)
elif sr.stylesheet_url_https:
return sr.stylesheet_url_https
else:
if sr.stylesheet_url_http:
if sr.stylesheet_url:
return sr.stylesheet_url
elif sr.stylesheet_url_http:
return sr.stylesheet_url_http

def wiki_actions_menu(self, moderator=False):
Expand Down
8 changes: 8 additions & 0 deletions r2/r2/lib/template_helpers.py
Expand Up @@ -110,6 +110,14 @@ def make_url_protocol_relative(url):
return urlparse.urlunsplit((None, netloc, path, query, fragment))


def make_url_https(url):
if not url or url.startswith("https://"):
return url

scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
return urlparse.urlunsplit(("https", netloc, path, query, fragment))


def header_url(url):
if url == g.default_header_url:
return static(url)
Expand Down
38 changes: 18 additions & 20 deletions r2/r2/models/subreddit.py
Expand Up @@ -207,6 +207,7 @@ class Subreddit(Thing, Printable, BaseSite):
# attribute, even on a cname. So c.site.static_path should always be
# the same as g.static_path.
_defaults = dict(BaseSite._defaults,
stylesheet_url="",
stylesheet_url_http="",
stylesheet_url_https="",
header_size=None,
Expand Down Expand Up @@ -574,30 +575,23 @@ def parse_css(self, content, verify=True):
return (None, None)

if not content:
return ([], ("", ""))
return ([], "")

# parse in regular old http mode
http_images = ImagesByWikiPage.get_images(self, "config/stylesheet")
parsed_http, errors_http = cssfilter.validate_css(
images = ImagesByWikiPage.get_images(self, "config/stylesheet")
protocol_relative_images = {
name: make_url_protocol_relative(url)
for name, url in images.iteritems()}
parsed, errors = cssfilter.validate_css(
content,
http_images,
protocol_relative_images,
)

# parse and resolve images with https-safe urls
https_images = {name: make_url_protocol_relative(url)
for name, url in http_images.iteritems()}
parsed_https, errors_https = cssfilter.validate_css(
content,
https_images,
)

# the two reports should be identical so we'll just return the http one
return (errors_http, (parsed_http, parsed_https))
return (errors, parsed)

def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False):
from r2.models import ModAction
from r2.lib.media import upload_stylesheet
from r2.lib.template_helpers import make_url_protocol_relative

author = author if author else c.user._id36
if content is None:
Expand All @@ -608,12 +602,12 @@ def change_css(self, content, parsed, prev=None, reason=None, author=None, force
wiki = WikiPage.create(self, 'config/stylesheet')
wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force)

minified_http, minified_https = parsed
if minified_http or minified_https:
self.stylesheet_url_http = upload_stylesheet(minified_http)
self.stylesheet_url_https = make_url_protocol_relative(
upload_stylesheet(minified_https))
if parsed:
self.stylesheet_url = upload_stylesheet(parsed)
self.stylesheet_url_http = ""
self.stylesheet_url_https = ""
else:
self.stylesheet_url = ""
self.stylesheet_url_http = ""
self.stylesheet_url_https = ""
self._commit()
Expand Down Expand Up @@ -1327,6 +1321,10 @@ def header_title(self):
def header_size(self):
return (self._base and self._base.header_size) or None

@property
def stylesheet_url(self):
return self._base.stylesheet_url if self._base else ""

@property
def stylesheet_url_http(self):
return self._base.stylesheet_url_http if self._base else ""
Expand Down

0 comments on commit 7040139

Please sign in to comment.