Ticket #13218: 13218.diff

File 13218.diff, 4.5 KB (added by Ben Firshman, 14 years ago)
  • django/contrib/syndication/views.py

    diff --git a/django/contrib/syndication/views.py b/django/contrib/syndication/views.py
    index b99f3f9..b287544 100644
    a b from django.utils import feedgenerator, tzinfo  
    88from django.utils.encoding import force_unicode, iri_to_uri, smart_unicode
    99from django.utils.html import escape
    1010
    11 def add_domain(domain, url):
     11def add_domain(domain, url, secure=False):
    1212    if not (url.startswith('http://')
    1313            or url.startswith('https://')
    1414            or url.startswith('mailto:')):
    1515        # 'url' must already be ASCII and URL-quoted, so no need for encoding
    1616        # conversions here.
    17         url = iri_to_uri(u'http://%s%s' % (domain, url))
     17        if secure:
     18            protocol = 'https'
     19        else:
     20            protocol = 'http'
     21        url = iri_to_uri(u'%s://%s%s' % (protocol, domain, url))
    1822    return url
    1923
    2024class FeedDoesNotExist(ObjectDoesNotExist):
    class Feed(object):  
    97101            current_site = RequestSite(request)
    98102
    99103        link = self.__get_dynamic_attr('link', obj)
    100         link = add_domain(current_site.domain, link)
     104        link = add_domain(current_site.domain, link, request.is_secure())
    101105
    102106        feed = self.feed_type(
    103107            title = self.__get_dynamic_attr('title', obj),
    class Feed(object):  
    105109            link = link,
    106110            description = self.__get_dynamic_attr('description', obj),
    107111            language = settings.LANGUAGE_CODE.decode(),
    108             feed_url = add_domain(current_site.domain,
    109                     self.__get_dynamic_attr('feed_url', obj) or request.path),
     112            feed_url = add_domain(
     113                current_site.domain,
     114                self.__get_dynamic_attr('feed_url', obj) or request.path,
     115                request.is_secure(),
     116            ),
    110117            author_name = self.__get_dynamic_attr('author_name', obj),
    111118            author_link = self.__get_dynamic_attr('author_link', obj),
    112119            author_email = self.__get_dynamic_attr('author_email', obj),
    class Feed(object):  
    140147                description = description_tmp.render(RequestContext(request, {'obj': item, 'site': current_site}))
    141148            else:
    142149                description = self.__get_dynamic_attr('item_description', item)
    143             link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
     150            link = add_domain(
     151                current_site.domain,
     152                self.__get_dynamic_attr('item_link', item),
     153                request.is_secure(),
     154            )
    144155            enc = None
    145156            enc_url = self.__get_dynamic_attr('item_enclosure_url', item)
    146157            if enc_url:
  • tests/regressiontests/syndication/tests.py

    diff --git a/tests/regressiontests/syndication/tests.py b/tests/regressiontests/syndication/tests.py
    index 7911657..9939f70 100644
    a b class SyndicationFeedTest(FeedTestCase):  
    235235        for link in doc.getElementsByTagName('link'):
    236236            if link.getAttribute('rel') == 'self':
    237237                self.assertEqual(link.getAttribute('href'), 'http://example.com/customfeedurl/')
     238   
     239    def test_secure_urls(self):
     240        """
     241        Test URLs are prefixed with https:// when feed is requested over HTTPS.
     242        """
     243        response = self.client.get('/syndication/rss2/', **{
     244            'wsgi.url_scheme': 'https',
     245        })
     246        doc = minidom.parseString(response.content)
     247        chan = doc.getElementsByTagName('channel')[0]
     248        self.assertEqual(
     249            chan.getElementsByTagName('link')[0].firstChild.wholeText[0:5],
     250            'https'
     251        )
     252        atom_link = chan.getElementsByTagName('atom:link')[0]
     253        self.assertEqual(atom_link.getAttribute('href')[0:5], 'https')
     254        for link in doc.getElementsByTagName('link'):
     255            if link.getAttribute('rel') == 'self':
     256                self.assertEqual(link.getAttribute('href')[0:5], 'https')
    238257
    239258    def test_item_link_error(self):
    240259        """
    class SyndicationFeedTest(FeedTestCase):  
    271290            'http://example.com/foo/?arg=value'
    272291        )
    273292        self.assertEqual(
     293            views.add_domain('example.com', '/foo/?arg=value', True),
     294            'https://example.com/foo/?arg=value'
     295        )
     296        self.assertEqual(
    274297            views.add_domain('example.com', 'http://djangoproject.com/doc/'),
    275298            'http://djangoproject.com/doc/'
    276299        )
Back to Top