Browse Source

Fix Python3 client web support.

Jelmer Vernooij 9 years ago
parent
commit
ff8a613cd1
3 changed files with 32 additions and 15 deletions
  1. 2 0
      NEWS
  2. 21 9
      dulwich/client.py
  3. 9 6
      dulwich/tests/compat/test_client.py

+ 2 - 0
NEWS

@@ -15,6 +15,8 @@
 
   * Drop support for Python 2.6.
 
+  * Fix python3 client web support. (Jelmer Vernooij)
+
  BUG FIXES
 
   * Fix hang on Gzip decompression. (Jonas Haag)

+ 21 - 9
dulwich/client.py

@@ -980,16 +980,21 @@ class HttpGitClient(GitClient):
         url = urlparse.urljoin(url, "info/refs")
         headers = {}
         if self.dumb is not False:
-            url += "?service=%s" % service
-            headers["Content-Type"] = "application/x-%s-request" % service
+            url += "?service=%s" % service.decode('ascii')
+            headers["Content-Type"] = "application/x-%s-request" % (
+                service.decode('ascii'))
         resp = self._http_request(url, headers)
         try:
-            self.dumb = (not resp.info().gettype().startswith("application/x-git-"))
+            content_type = resp.info().gettype()
+        except AttributeError:
+            content_type = resp.info().get_content_type()
+        try:
+            self.dumb = (not content_type.startswith("application/x-git-"))
             if not self.dumb:
                 proto = Protocol(resp.read, None)
                 # The first line should mention the service
                 pkts = list(proto.read_pkt_seq())
-                if pkts != [('# service=%s\n' % service)]:
+                if pkts != [b'# service=' + service + b'\n']:
                     raise GitProtocolError(
                         "unexpected first line %r from smart server" % pkts)
                 return read_pkt_refs(proto)
@@ -1001,11 +1006,18 @@ class HttpGitClient(GitClient):
     def _smart_request(self, service, url, data):
         assert url[-1] == "/"
         url = urlparse.urljoin(url, service)
-        headers = {"Content-Type": "application/x-%s-request" % service}
+        headers = {
+            "Content-Type": "application/x-%s-request" % service
+        }
         resp = self._http_request(url, headers, data)
-        if resp.info().gettype() != ("application/x-%s-result" % service):
+        try:
+            content_type = resp.info().gettype()
+        except AttributeError:
+            content_type = resp.info().get_content_type()
+        if content_type != (
+                "application/x-%s-result" % service):
             raise GitProtocolError("Invalid content-type from server: %s"
-                % resp.info().gettype())
+                % content_type)
         return resp
 
     def send_pack(self, path, determine_wants, generate_pack_contents,
@@ -1045,7 +1057,7 @@ class HttpGitClient(GitClient):
         objects = generate_pack_contents(have, want)
         if len(objects) > 0:
             write_pack(req_proto.write_file(), objects)
-        resp = self._smart_request(b"git-receive-pack", url,
+        resp = self._smart_request("git-receive-pack", url,
                                    data=req_data.getvalue())
         try:
             resp_proto = Protocol(resp.read, None)
@@ -1083,7 +1095,7 @@ class HttpGitClient(GitClient):
             req_proto, negotiated_capabilities, graph_walker, wants,
             lambda: False)
         resp = self._smart_request(
-            b"git-upload-pack", url, data=req_data.getvalue())
+            "git-upload-pack", url, data=req_data.getvalue())
         try:
             resp_proto = Protocol(resp.read, None)
             self._handle_upload_pack_tail(resp_proto, negotiated_capabilities,

+ 9 - 6
dulwich/tests/compat/test_client.py

@@ -62,9 +62,6 @@ from dulwich.tests import (
     SkipTest,
     expectedFailure,
     )
-from dulwich.tests.utils import (
-    skipIfPY3,
-    )
 from dulwich.tests.compat.utils import (
     CompatTestCase,
     check_for_daemon,
@@ -463,7 +460,11 @@ class GitHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
                   'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
             env.setdefault(k, "")
 
-        self.send_response(200, "Script output follows")
+        self.wfile.write(b"HTTP/1.1 200 Script output follows\r\n")
+        self.wfile.write(
+            ("Server: %s\r\n" % self.server.server_name).encode('ascii'))
+        self.wfile.write(
+            ("Date: %s\r\n" % self.date_time_string()).encode('ascii'))
 
         decoded_query = query.replace('+', ' ')
 
@@ -499,7 +500,6 @@ class HTTPGitServer(BaseHTTPServer.HTTPServer):
         return 'http://%s:%s/' % (self.server_name, self.server_port)
 
 
-@skipIfPY3
 class DulwichHttpClientTest(CompatTestCase, DulwichClientTestBase):
 
     min_git_version = (1, 7, 0, 2)
@@ -525,7 +525,10 @@ class DulwichHttpClientTest(CompatTestCase, DulwichClientTestBase):
         return client.HttpGitClient(self._httpd.get_url())
 
     def _build_path(self, path):
-        return path
+        if sys.version_info[0] == 3:
+            return path.decode('ascii')
+        else:
+            return path
 
     def test_archive(self):
         raise SkipTest("exporting archives not supported over http")