mercurial/httpconnection.py
changeset 28883 032c4c2f802a
parent 28526 47b0cee25e88
child 29227 dffe78d80a6c
equal deleted inserted replaced
28882:800ec7c048b0 28883:032c4c2f802a
    11 from __future__ import absolute_import
    11 from __future__ import absolute_import
    12 
    12 
    13 import logging
    13 import logging
    14 import os
    14 import os
    15 import socket
    15 import socket
    16 import urllib
       
    17 import urllib2
       
    18 
    16 
    19 from .i18n import _
    17 from .i18n import _
    20 from . import (
    18 from . import (
    21     httpclient,
    19     httpclient,
    22     sslutil,
    20     sslutil,
    23     util,
    21     util,
    24 )
    22 )
       
    23 
       
    24 urlerr = util.urlerr
       
    25 urlreq = util.urlreq
    25 
    26 
    26 # moved here from url.py to avoid a cycle
    27 # moved here from url.py to avoid a cycle
    27 class httpsendfile(object):
    28 class httpsendfile(object):
    28     """This is a wrapper around the objects returned by python's "open".
    29     """This is a wrapper around the objects returned by python's "open".
    29 
    30 
   121 _configuredlogging = False
   122 _configuredlogging = False
   122 LOGFMT = '%(levelname)s:%(name)s:%(lineno)d:%(message)s'
   123 LOGFMT = '%(levelname)s:%(name)s:%(lineno)d:%(message)s'
   123 # Subclass BOTH of these because otherwise urllib2 "helpfully"
   124 # Subclass BOTH of these because otherwise urllib2 "helpfully"
   124 # reinserts them since it notices we don't include any subclasses of
   125 # reinserts them since it notices we don't include any subclasses of
   125 # them.
   126 # them.
   126 class http2handler(urllib2.HTTPHandler, urllib2.HTTPSHandler):
   127 class http2handler(urlreq.httphandler, urlreq.httpshandler):
   127     def __init__(self, ui, pwmgr):
   128     def __init__(self, ui, pwmgr):
   128         global _configuredlogging
   129         global _configuredlogging
   129         urllib2.AbstractHTTPHandler.__init__(self)
   130         urlreq.abstracthttphandler.__init__(self)
   130         self.ui = ui
   131         self.ui = ui
   131         self.pwmgr = pwmgr
   132         self.pwmgr = pwmgr
   132         self._connections = {}
   133         self._connections = {}
   133         # developer config: ui.http2debuglevel
   134         # developer config: ui.http2debuglevel
   134         loglevel = ui.config('ui', 'http2debuglevel', default=None)
   135         loglevel = ui.config('ui', 'http2debuglevel', default=None)
   185             proxy = (proxyhost, proxyport)
   186             proxy = (proxyhost, proxyport)
   186         else:
   187         else:
   187             proxy = None
   188             proxy = None
   188 
   189 
   189         if not host:
   190         if not host:
   190             raise urllib2.URLError('no host given')
   191             raise urlerr.urlerror('no host given')
   191 
   192 
   192         connkey = use_ssl, host, proxy
   193         connkey = use_ssl, host, proxy
   193         allconns = self._connections.get(connkey, [])
   194         allconns = self._connections.get(connkey, [])
   194         conns = [c for c in allconns if not c.busy()]
   195         conns = [c for c in allconns if not c.busy()]
   195         if conns:
   196         if conns:
   215             if path[0] != '/':
   216             if path[0] != '/':
   216                 path = '/' + path
   217                 path = '/' + path
   217             h.request(req.get_method(), path, req.data, headers)
   218             h.request(req.get_method(), path, req.data, headers)
   218             r = h.getresponse()
   219             r = h.getresponse()
   219         except socket.error as err: # XXX what error?
   220         except socket.error as err: # XXX what error?
   220             raise urllib2.URLError(err)
   221             raise urlerr.urlerror(err)
   221 
   222 
   222         # Pick apart the HTTPResponse object to get the addinfourl
   223         # Pick apart the HTTPResponse object to get the addinfourl
   223         # object initialized properly.
   224         # object initialized properly.
   224         r.recv = r.read
   225         r.recv = r.read
   225 
   226 
   226         resp = urllib.addinfourl(r, r.headers, req.get_full_url())
   227         resp = urlreq.addinfourl(r, r.headers, req.get_full_url())
   227         resp.code = r.status
   228         resp.code = r.status
   228         resp.msg = r.reason
   229         resp.msg = r.reason
   229         return resp
   230         return resp
   230 
   231 
   231     # httplib always uses the given host/port as the socket connect
   232     # httplib always uses the given host/port as the socket connect