httpconnection.py
132 lines
| 4.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / httpconnection.py
|
r14244 | # httpconnection.py - urllib2 handler for new http support | ||
# | ||||
# Copyright 2005, 2006, 2007, 2008 Matt Mackall <mpm@selenic.com> | ||||
# Copyright 2006, 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br> | ||||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
# Copyright 2011 Google, Inc. | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
|
r27521 | |||
from __future__ import absolute_import | ||||
import os | ||||
|
r14244 | |||
|
r27521 | from .i18n import _ | ||
|
r43355 | from .pycompat import open | ||
|
r27521 | from . import ( | ||
|
r36669 | pycompat, | ||
|
r27521 | util, | ||
) | ||||
|
r14244 | |||
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
|
r14244 | # moved here from url.py to avoid a cycle | ||
class httpsendfile(object): | ||||
"""This is a wrapper around the objects returned by python's "open". | ||||
|
r15152 | Its purpose is to send file-like objects via HTTP. | ||
It do however not define a __len__ attribute because the length | ||||
might be more than Py_ssize_t can handle. | ||||
|
r14244 | """ | ||
def __init__(self, ui, *args, **kwargs): | ||||
self.ui = ui | ||||
self._data = open(*args, **kwargs) | ||||
self.seek = self._data.seek | ||||
self.close = self._data.close | ||||
self.write = self._data.write | ||||
|
r15152 | self.length = os.fstat(self._data.fileno()).st_size | ||
|
r14244 | self._pos = 0 | ||
# We pass double the max for total because we currently have | ||||
# to send the bundle twice in the case of a server that | ||||
# requires authentication. Since we can't know until we try | ||||
# once whether authentication will be required, just lie to | ||||
# the user and maybe the push succeeds suddenly at 50%. | ||||
|
r43345 | self._progress = ui.makeprogress( | ||
|
r43347 | _(b'sending'), unit=_(b'kb'), total=(self.length // 1024 * 2) | ||
|
r43345 | ) | ||
|
r38412 | |||
def read(self, *args, **kwargs): | ||||
ret = self._data.read(*args, **kwargs) | ||||
if not ret: | ||||
self._progress.complete() | ||||
return ret | ||||
self._pos += len(ret) | ||||
self._progress.update(self._pos // 1024) | ||||
|
r14244 | return ret | ||
|
r30142 | def __enter__(self): | ||
return self | ||||
def __exit__(self, exc_type, exc_val, exc_tb): | ||||
self.close() | ||||
|
r43345 | |||
|
r14244 | # moved here from url.py to avoid a cycle | ||
|
r15025 | def readauthforuri(ui, uri, user): | ||
|
r36669 | uri = pycompat.bytesurl(uri) | ||
|
r14244 | # Read configuration | ||
|
r31300 | groups = {} | ||
|
r43347 | for key, val in ui.configitems(b'auth'): | ||
if key in (b'cookiefile',): | ||||
|
r31935 | continue | ||
|
r43347 | if b'.' not in key: | ||
ui.warn(_(b"ignoring invalid [auth] key '%s'\n") % key) | ||||
|
r14244 | continue | ||
|
r43347 | group, setting = key.rsplit(b'.', 1) | ||
|
r31300 | gdict = groups.setdefault(group, {}) | ||
|
r43347 | if setting in (b'username', b'cert', b'key'): | ||
|
r14244 | val = util.expandpath(val) | ||
gdict[setting] = val | ||||
# Find the best match | ||||
|
r43347 | scheme, hostpath = uri.split(b'://', 1) | ||
|
r15005 | bestuser = None | ||
|
r14244 | bestlen = 0 | ||
bestauth = None | ||||
|
r31300 | for group, auth in groups.iteritems(): | ||
|
r43347 | if user and user != auth.get(b'username', user): | ||
|
r15005 | # If a username was set in the URI, the entry username | ||
# must either match it or be unset | ||||
continue | ||||
|
r43347 | prefix = auth.get(b'prefix') | ||
|
r14244 | if not prefix: | ||
continue | ||||
|
r40699 | |||
prefixurl = util.url(prefix) | ||||
if prefixurl.user and prefixurl.user != user: | ||||
# If a username was set in the prefix, it must match the username in | ||||
# the URI. | ||||
continue | ||||
# The URI passed in has been stripped of credentials, so erase the user | ||||
# here to allow simpler matching. | ||||
prefixurl.user = None | ||||
prefix = bytes(prefixurl) | ||||
|
r43347 | p = prefix.split(b'://', 1) | ||
|
r14244 | if len(p) > 1: | ||
schemes, prefix = [p[0]], p[1] | ||||
else: | ||||
|
r43347 | schemes = (auth.get(b'schemes') or b'https').split() | ||
|
r43345 | if ( | ||
|
r43347 | (prefix == b'*' or hostpath.startswith(prefix)) | ||
|
r43345 | and ( | ||
len(prefix) > bestlen | ||||
or ( | ||||
len(prefix) == bestlen | ||||
and not bestuser | ||||
|
r43347 | and b'username' in auth | ||
|
r43345 | ) | ||
) | ||||
and scheme in schemes | ||||
): | ||||
|
r14244 | bestlen = len(prefix) | ||
bestauth = group, auth | ||||
|
r43347 | bestuser = auth.get(b'username') | ||
|
r15005 | if user and not bestuser: | ||
|
r43347 | auth[b'username'] = user | ||
|
r14244 | return bestauth | ||