Bug 476993 - should send Content-Access-Control headers for pushlog (json, feed, and maybe html). r=ted
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Fri, 30 Oct 2009 07:00:45 -0400
changeset 60 4e6c87c96231040d296ec4063d4ba4cb3b468db7
parent 59 c88bf6c80d4ed162330371daee3f7419126d3367
child 61 1e92acd6cb576a635f5a4ae2e070235af2dd1466
push id34
push usertmielczarek@mozilla.com
push dateFri, 30 Oct 2009 11:00:51 +0000
reviewersted
bugs476993
Bug 476993 - should send Content-Access-Control headers for pushlog (json, feed, and maybe html). r=ted
pushlog-feed.py
runtests.py
--- a/pushlog-feed.py
+++ b/pushlog-feed.py
@@ -30,16 +30,17 @@ def addcommand(f, name):
     setattr(hgwebprotocol, name, f)
     hgwebprotocol.__all__.append(name)
 
 def addwebcommand(f, name):
     setattr(hgwebcommands, name, f)
     hgwebcommands.__all__.append(name)
 
 ATOM_MIMETYPE = 'application/atom+xml'
+ACL_HEADER = 'Access-Control-Allow-Origin', '*'
 
 # just an enum
 class QueryType:
     DATE, CHANGESET, PUSHID, COUNT = range(4)
 
 class PushlogQuery:
     page = 1
     dates = []
@@ -313,16 +314,17 @@ def pushlogFeed(web, req, tmpl):
             'node': node,
             'date': isotime(date),
             'user': xmlescape(user),
             'urlbase': query.urlbase,
             'url': req.url,
             'files': [{'name': fn} for fn in ctx.files()],
         })
 
+    req.header([ACL_HEADER])
     req.respond(HTTP_OK, ATOM_MIMETYPE)
     return tmpl('pushlog', **data)
 
 def pushlogHTML(web, req, tmpl):
     """WebCommand for producing the HTML view of the pushlog."""
     query = pushlogSetup(web.repo, req)
 
     # these three functions are in webutil in newer hg, but not in hg 1.0
@@ -414,16 +416,17 @@ def pushlogHTML(web, req, tmpl):
         if limit > 0:
             l = l[:limit]
 
         for e in l:
             yield e
 
     parity = paritygen(web.stripecount)
 
+    req.header([ACL_HEADER])
     return tmpl('pushlog',
                 changenav=changenav(),
                 rev=0,
                 entries=lambda **x: changelist(limit=0,**x),
                 latestentry=lambda **x: changelist(limit=1,**x),
                 startdate='startdate' in req.form and req.form['startdate'][0] or '1 week ago',
                 enddate='enddate' in req.form and req.form['enddate'][0] or 'now',
                 querydescription=query.description(),
@@ -442,16 +445,17 @@ def pushes_worker(query):
                           'date': date,
                           'changesets': [node]
                           }
     return pushes
 
 def pushes(web, req, tmpl):
     """WebCommand to return a data structure containing pushes."""
     query = pushlogSetup(web.repo, req)
+    req.header([ACL_HEADER])
     return tmpl('pushes', data=pushes_worker(query))
 
 def printpushlog(ui, repo, *args):
     """HG Command to print the pushlog data in JSON format."""
     from hgwebjson import HGJSONEncoder
     e = HGJSONEncoder()
     startID = len(args) and args[0] or 0
     endID = len(args) > 1 and args[1] or None
--- a/runtests.py
+++ b/runtests.py
@@ -144,16 +144,34 @@ class TestPushlog(unittest.TestCase):
         self.assertEqual(testjson, expectedjson, "json-pushes did not yield expected json data!")
 
     def testprintpushlog(self):
         """Get all json data via 'hg printpushlog'."""
         testjson = simplejson.loads(Popen(["hg", "-R", self.repodir, "printpushlog"], stdout=PIPE).communicate()[0])
         expectedjson = loadjsonfile("testdata/test-repo-data.json")
         self.assertEqual(testjson, expectedjson, "printpushlog did not yield expected json data!")
 
+    def testaccesscontrolatom(self):
+        """Ensure that /pushlog feed sends Access-Control-Allow-Origin headers."""
+        conn = HTTPConnection("localhost", 8000)
+        conn.request("GET", "/pushlog")
+        r = conn.getresponse()
+        conn.close()
+        h = r.getheader("Access-Control-Allow-Origin", None)
+        self.assertEqual(h, "*", "/pushlog should send Access-Control-Allow-Origin")
+
+    def testaccesscontroljson(self):
+        """Ensure that /json-pushes sends Access-Control-Allow-Origin headers."""
+        conn = HTTPConnection("localhost", 8000)
+        conn.request("GET", "/json-pushes")
+        r = conn.getresponse()
+        conn.close()
+        h = r.getheader("Access-Control-Allow-Origin", None)
+        self.assertEqual(h, "*", "/json-pushes should send Access-Control-Allow-Origin")
+
     def assertEqualFeeds(self, a, b):
         self.assertEqual(a.feed.title, b.feed.title, "not the same title, %s != %s" % (a.feed.title, b.feed.title))
         self.assertEqual(a.feed.updated, b.feed.updated, "not the same updated time, %s != %s" % (a.feed.updated, b.feed.updated))
         self.assertEqual(len(a.entries), len(b.entries), "not the same number of entries, %d != %d" % (len(a.entries), len(b.entries)))
         for ae, be in zip(a.entries, b.entries):
             self.assertEqual(ae.updated, be.updated, "not the same updated time, %s != %s" % (ae.updated, be.updated))
             self.assertEqual(ae.title, be.title, "not the same title, %s != %s" % (ae.title, be.title))
             self.assertEqual(ae.id, be.id, "not the same id, %s != %s" % (ae.id, be.id))