Patchwork [05,of,11] py3: raw stringify various JSON and HTTP headers in the LFS blobstore module

login
register
mail settings
Submitter Matt Harbison
Date Jan. 28, 2019, 5:20 a.m.
Message ID <67b051e66fdb70a92eba.1548652851@Envy>
Download mbox | patch
Permalink /patch/38131/
State Accepted
Headers show

Comments

Matt Harbison - Jan. 28, 2019, 5:20 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1548621775 18000
#      Sun Jan 27 15:42:55 2019 -0500
# Node ID 67b051e66fdb70a92eba77c2110789ef976bde60
# Parent  d639ccd9f7139991165d04b4601b06c3341a79cd
py3: raw stringify various JSON and HTTP headers in the LFS blobstore module

This is (almost?) entirely from Augie's work.  I'm a bit surprised that the JSON
data is being encoded with ASCII via `pycompat.bytesurl()`- I would have thought
UTF-8.

Patch

diff --git a/hgext/lfs/blobstore.py b/hgext/lfs/blobstore.py
--- a/hgext/lfs/blobstore.py
+++ b/hgext/lfs/blobstore.py
@@ -291,15 +291,16 @@  class _gitlfsremote(object):
         Return decoded JSON object like {'objects': [{'oid': '', 'size': 1}]}
         See https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md
         """
-        objects = [{'oid': p.oid(), 'size': p.size()} for p in pointers]
-        requestdata = json.dumps({
-            'objects': objects,
-            'operation': action,
-        })
+        objects = [{r'oid': pycompat.strurl(p.oid()),
+                    r'size': p.size()} for p in pointers]
+        requestdata = pycompat.bytesurl(json.dumps({
+            r'objects': objects,
+            r'operation': pycompat.strurl(action),
+        }))
         url = b'%s/objects/batch' % self.baseurl
         batchreq = util.urlreq.request(pycompat.strurl(url), data=requestdata)
-        batchreq.add_header('Accept', 'application/vnd.git-lfs+json')
-        batchreq.add_header('Content-Type', 'application/vnd.git-lfs+json')
+        batchreq.add_header(r'Accept', r'application/vnd.git-lfs+json')
+        batchreq.add_header(r'Content-Type', r'application/vnd.git-lfs+json')
         try:
             with contextlib.closing(self.urlopener.open(batchreq)) as rsp:
                 rawjson = rsp.read()
@@ -332,12 +333,14 @@  class _gitlfsremote(object):
             self.ui.debug(b'%s\n'
                           % b'\n'.join(sorted(headers.splitlines())))
 
-            if 'objects' in response:
-                response['objects'] = sorted(response['objects'],
-                                             key=lambda p: p['oid'])
-            self.ui.debug('%s\n'
-                          % json.dumps(response, indent=2,
-                                       separators=('', ': '), sort_keys=True))
+            if r'objects' in response:
+                response[r'objects'] = sorted(response[r'objects'],
+                                              key=lambda p: p[r'oid'])
+            self.ui.debug(b'%s\n'
+                          % pycompat.bytesurl(
+                              json.dumps(response, indent=2,
+                                         separators=(r'', r': '),
+                                         sort_keys=True)))
 
         return response
 
@@ -419,8 +422,8 @@  class _gitlfsremote(object):
                 raise error.Abort(_(b'detected corrupt lfs object: %s') % oid,
                                   hint=_(b'run hg verify'))
             request.data = filewithprogress(localstore.open(oid), None)
-            request.get_method = lambda: 'PUT'
-            request.add_header('Content-Type', 'application/octet-stream')
+            request.get_method = lambda: r'PUT'
+            request.add_header(r'Content-Type', r'application/octet-stream')
 
         for k, v in headers:
             request.add_header(k, v)