Revision: 361
Author: bslatkin
Date: Thu Jun  3 16:20:53 2010
Log: hub ignores improper char encodings in etag header values
http://code.google.com/p/pubsubhubbub/source/detail?r=361

Modified:
 /trunk/hub/main.py
 /trunk/hub/main_test.py

=======================================
--- /trunk/hub/main.py  Thu Jun  3 16:00:36 2010
+++ /trunk/hub/main.py  Thu Jun  3 16:20:53 2010
@@ -1275,9 +1275,21 @@
       format: The last parsing format that worked correctly for this feed.
         Should be 'rss' or 'atom'.
     """
-    self.content_type = headers.get('Content-Type', '').lower()
-    self.last_modified = headers.get('Last-Modified')
-    self.etag = headers.get('ETag')
+    try:
+      self.content_type = headers.get('Content-Type', '').lower()
+    except UnicodeDecodeError:
+      logging.exception('Content-Type header had bad encoding')
+
+    try:
+      self.last_modified = headers.get('Last-Modified')
+    except UnicodeDecodeError:
+      logging.exception('Last-Modified header had bad encoding')
+
+    try:
+      self.etag = headers.get('ETag')
+    except UnicodeDecodeError:
+      logging.exception('ETag header had bad encoding')
+
     if header_footer is not None:
       self.header_footer = header_footer
     if format is not None:
=======================================
--- /trunk/hub/main_test.py     Thu Jun  3 16:00:36 2010
+++ /trunk/hub/main_test.py     Thu Jun  3 16:20:53 2010
@@ -2031,6 +2031,33 @@
self.assertEquals(data.replace('\n', ''), event.payload.replace('\n', ''))
     self.assertEquals('application/atom+xml', event.content_type)

+  def testPullWithUnicodeEtag(self):
+    """Tests when the ETag header has a unicode value.
+
+ The ETag value should be ignored because non-ascii ETag values are invalid.
+    """
+ data = ('<?xml version="1.0" encoding="utf-8"?>\n<feed><my header="data"/>'
+            '<entry><id>1</id><updated>123</updated>wooh</entry></feed>')
+    topic = 'http://example.com/my-topic'
+    callback = 'http://example.com/my-subscriber'
+ self.assertTrue(Subscription.insert(callback, topic, 'token', 'secret'))
+    FeedToFetch.insert([topic])
+    urlfetch_test_stub.instance.expect('get', topic, 200, data,
+      response_headers={
+        'ETag': '\xe3\x83\x96\xe3\x83\xad\xe3\x82\xb0\xe8\xa1\x86',
+        'Content-Type': 'application/atom+xml',
+    })
+    self.handle('post', ('topic', topic))
+    feed = FeedToFetch.get_by_key_name(get_hash_key_name(topic))
+    self.assertTrue(feed is None)
+    event = EventToDeliver.all().get()
+ self.assertEquals(data.replace('\n', ''), event.payload.replace('\n', ''))
+    self.assertEquals('application/atom+xml', event.content_type)
+    self.assertEquals(
+        {'Connection': 'cache-control',
+         'Cache-Control': 'no-cache no-store max-age=1'},
+        FeedRecord.all().get().get_request_headers())
+
   def testPullGoodRss(self):
     """Tests when the RSS XML can parse just fine."""
     data = ('<?xml version="1.0" encoding="utf-8"?>\n'

Reply via email to