author | Vadim Gelfer <vadim.gelfer@gmail.com> |
Thu, 18 May 2006 22:46:27 -0700 | |
changeset 2317 | 6d0a9de9a8ac |
parent 2294 | ce67fa312f61 |
child 2336 | f77edcffb837 |
permissions | -rw-r--r-- |
1089 | 1 |
# httprepo.py - HTTP repository proxy classes for mercurial |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
2 |
# |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
3 |
# Copyright 2005 Matt Mackall <mpm@selenic.com> |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
4 |
# |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
7 |
|
1089 | 8 |
from node import * |
9 |
from remoterepo import * |
|
1400
cf9a1233738a
i18n first part: make '_' available for files who need it
Benoit Boissinot <benoit.boissinot@ens-lyon.org
parents:
1377
diff
changeset
|
10 |
from i18n import gettext as _ |
1251
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
11 |
from demandload import * |
2015
1a09814a5b1f
Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1870
diff
changeset
|
12 |
demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib") |
926 | 13 |
|
2281
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
14 |
class passwordmgr(urllib2.HTTPPasswordMgr): |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
15 |
def __init__(self, ui): |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
16 |
urllib2.HTTPPasswordMgr.__init__(self) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
17 |
self.ui = ui |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
18 |
|
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
19 |
def find_user_password(self, realm, authuri): |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
20 |
authinfo = urllib2.HTTPPasswordMgr.find_user_password( |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
21 |
self, realm, authuri) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
22 |
if authinfo != (None, None): |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
23 |
return authinfo |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
24 |
|
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
25 |
self.ui.write(_("http authorization required\n")) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
26 |
self.ui.status(_("realm: %s\n") % realm) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
27 |
user = self.ui.prompt(_("user:"), default=None) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
28 |
passwd = self.ui.getpass() |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
29 |
|
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
30 |
self.add_password(realm, authuri, user, passwd) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
31 |
return (user, passwd) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
32 |
|
926 | 33 |
class httprepository(remoterepository): |
60 | 34 |
def __init__(self, ui, path): |
765 | 35 |
# fix missing / after hostname |
36 |
s = urlparse.urlsplit(path) |
|
37 |
partial = s[2] |
|
38 |
if not partial: partial = "/" |
|
39 |
self.url = urlparse.urlunsplit((s[0], s[1], partial, '', '')) |
|
60 | 40 |
self.ui = ui |
321 | 41 |
no_list = [ "localhost", "127.0.0.1" ] |
42 |
host = ui.config("http_proxy", "host") |
|
424
9294dce4b633
Allow override of HTTP_PROXY, http_proxy and no_proxy; make no_proxy work.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
385
diff
changeset
|
43 |
if host is None: |
9294dce4b633
Allow override of HTTP_PROXY, http_proxy and no_proxy; make no_proxy work.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
385
diff
changeset
|
44 |
host = os.environ.get("http_proxy") |
426
8c90ab5644c9
Allow hgrc's proxy host and $http_proxy env var to start with http://
Thomas Arendsen Hein <thomas@intevation.de>
parents:
424
diff
changeset
|
45 |
if host and host.startswith('http://'): |
8c90ab5644c9
Allow hgrc's proxy host and $http_proxy env var to start with http://
Thomas Arendsen Hein <thomas@intevation.de>
parents:
424
diff
changeset
|
46 |
host = host[7:] |
321 | 47 |
user = ui.config("http_proxy", "user") |
48 |
passwd = ui.config("http_proxy", "passwd") |
|
49 |
no = ui.config("http_proxy", "no") |
|
424
9294dce4b633
Allow override of HTTP_PROXY, http_proxy and no_proxy; make no_proxy work.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
385
diff
changeset
|
50 |
if no is None: |
9294dce4b633
Allow override of HTTP_PROXY, http_proxy and no_proxy; make no_proxy work.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
385
diff
changeset
|
51 |
no = os.environ.get("no_proxy") |
321 | 52 |
if no: |
53 |
no_list = no_list + no.split(",") |
|
515 | 54 |
|
321 | 55 |
no_proxy = 0 |
56 |
for h in no_list: |
|
57 |
if (path.startswith("http://" + h + "/") or |
|
58 |
path.startswith("http://" + h + ":") or |
|
59 |
path == "http://" + h): |
|
60 |
no_proxy = 1 |
|
61 |
||
62 |
# Note: urllib2 takes proxy values from the environment and those will |
|
63 |
# take precedence |
|
424
9294dce4b633
Allow override of HTTP_PROXY, http_proxy and no_proxy; make no_proxy work.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
385
diff
changeset
|
64 |
for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]: |
859
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
65 |
try: |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
66 |
if os.environ.has_key(env): |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
67 |
del os.environ[env] |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
68 |
except OSError: |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
69 |
pass |
321 | 70 |
|
71 |
proxy_handler = urllib2.BaseHandler() |
|
72 |
if host and not no_proxy: |
|
73 |
proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host}) |
|
74 |
||
2281
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
75 |
proxyauthinfo = None |
321 | 76 |
if user and passwd: |
77 |
passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
|
78 |
passmgr.add_password(None, host, user, passwd) |
|
2281
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
79 |
proxyauthinfo = urllib2.ProxyBasicAuthHandler(passmgr) |
321 | 80 |
|
2281
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
81 |
if ui.interactive: |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
82 |
passmgr = passwordmgr(ui) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
83 |
opener = urllib2.build_opener( |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
84 |
proxy_handler, proxyauthinfo, |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
85 |
urllib2.HTTPBasicAuthHandler(passmgr), |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
86 |
urllib2.HTTPDigestAuthHandler(passmgr)) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
87 |
else: |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
88 |
opener = urllib2.build_opener(proxy_handler, proxyauthinfo) |
7761597b5da3
prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2015
diff
changeset
|
89 |
|
1359
51ac9a79f3e5
Set the user agent for httprepo communication
Matt Mackall <mpm@selenic.com>
parents:
1251
diff
changeset
|
90 |
# 1.0 here is the _protocol_ version |
51ac9a79f3e5
Set the user agent for httprepo communication
Matt Mackall <mpm@selenic.com>
parents:
1251
diff
changeset
|
91 |
opener.addheaders = [('User-agent', 'mercurial/proto-1.0')] |
321 | 92 |
urllib2.install_opener(opener) |
60 | 93 |
|
634
da5378d39269
Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents:
627
diff
changeset
|
94 |
def dev(self): |
da5378d39269
Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents:
627
diff
changeset
|
95 |
return -1 |
da5378d39269
Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents:
627
diff
changeset
|
96 |
|
1870
8a8ab47cccde
make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1736
diff
changeset
|
97 |
def lock(self): |
8a8ab47cccde
make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1736
diff
changeset
|
98 |
raise util.Abort(_('operation not supported over http')) |
8a8ab47cccde
make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1736
diff
changeset
|
99 |
|
60 | 100 |
def do_cmd(self, cmd, **args): |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
101 |
self.ui.debug(_("sending %s command\n") % cmd) |
60 | 102 |
q = {"cmd": cmd} |
103 |
q.update(args) |
|
104 |
qs = urllib.urlencode(q) |
|
105 |
cu = "%s?%s" % (self.url, qs) |
|
2294
ce67fa312f61
Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2281
diff
changeset
|
106 |
try: |
ce67fa312f61
Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2281
diff
changeset
|
107 |
resp = urllib2.urlopen(cu) |
ce67fa312f61
Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2281
diff
changeset
|
108 |
except httplib.HTTPException, inst: |
ce67fa312f61
Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2281
diff
changeset
|
109 |
raise IOError(None, _('http error while sending %s command') % cmd) |
753 | 110 |
proto = resp.headers['content-type'] |
752 | 111 |
|
753 | 112 |
# accept old "text/plain" and "application/hg-changegroup" for now |
113 |
if not proto.startswith('application/mercurial') and \ |
|
114 |
not proto.startswith('text/plain') and \ |
|
115 |
not proto.startswith('application/hg-changegroup'): |
|
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
116 |
raise hg.RepoError(_("'%s' does not appear to be an hg repository") % |
1251
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
117 |
self.url) |
752 | 118 |
|
753 | 119 |
if proto.startswith('application/mercurial'): |
120 |
version = proto[22:] |
|
121 |
if float(version) > 0.1: |
|
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
122 |
raise hg.RepoError(_("'%s' uses newer protocol %s") % |
1251
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
123 |
(self.url, version)) |
753 | 124 |
|
752 | 125 |
return resp |
60 | 126 |
|
222 | 127 |
def heads(self): |
128 |
d = self.do_cmd("heads").read() |
|
129 |
try: |
|
130 |
return map(bin, d[:-1].split(" ")) |
|
131 |
except: |
|
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
132 |
self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
222 | 133 |
raise |
134 |
||
60 | 135 |
def branches(self, nodes): |
136 |
n = " ".join(map(hex, nodes)) |
|
752 | 137 |
d = self.do_cmd("branches", nodes=n).read() |
217 | 138 |
try: |
139 |
br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] |
|
140 |
return br |
|
141 |
except: |
|
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
142 |
self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
217 | 143 |
raise |
60 | 144 |
|
145 |
def between(self, pairs): |
|
146 |
n = "\n".join(["-".join(map(hex, p)) for p in pairs]) |
|
752 | 147 |
d = self.do_cmd("between", pairs=n).read() |
217 | 148 |
try: |
149 |
p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] |
|
150 |
return p |
|
151 |
except: |
|
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
152 |
self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
217 | 153 |
raise |
60 | 154 |
|
1736
50de0887bbcd
add preoutgoing and outgoing hooks.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1402
diff
changeset
|
155 |
def changegroup(self, nodes, kind): |
60 | 156 |
n = " ".join(map(hex, nodes)) |
752 | 157 |
f = self.do_cmd("changegroup", roots=n) |
192 | 158 |
bytes = 0 |
635
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
159 |
|
1376
524ca4a06f70
Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents:
1359
diff
changeset
|
160 |
def zgenerator(f): |
524ca4a06f70
Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents:
1359
diff
changeset
|
161 |
zd = zlib.decompressobj() |
2015
1a09814a5b1f
Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1870
diff
changeset
|
162 |
try: |
1a09814a5b1f
Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1870
diff
changeset
|
163 |
for chnk in f: |
1a09814a5b1f
Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1870
diff
changeset
|
164 |
yield zd.decompress(chnk) |
1a09814a5b1f
Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1870
diff
changeset
|
165 |
except httplib.HTTPException, inst: |
1a09814a5b1f
Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1870
diff
changeset
|
166 |
raise IOError(None, _('connection ended unexpectedly')) |
1376
524ca4a06f70
Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents:
1359
diff
changeset
|
167 |
yield zd.flush() |
635
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
168 |
|
1376
524ca4a06f70
Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents:
1359
diff
changeset
|
169 |
return util.chunkbuffer(zgenerator(util.filechunkiter(f))) |
635
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
170 |
|
923 | 171 |
class httpsrepository(httprepository): |
172 |
pass |