author | mpm@selenic.com |
Thu, 15 Sep 2005 02:59:16 -0500 | |
changeset 1258 | 1945754e466b |
parent 1251 | 84cf8834efb5 |
child 1359 | 51ac9a79f3e5 |
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 * |
|
1251
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
10 |
from demandload import * |
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
11 |
demandload(globals(), "hg os urllib urllib2 urlparse zlib") |
926 | 12 |
|
13 |
class httprepository(remoterepository): |
|
60 | 14 |
def __init__(self, ui, path): |
765 | 15 |
# fix missing / after hostname |
16 |
s = urlparse.urlsplit(path) |
|
17 |
partial = s[2] |
|
18 |
if not partial: partial = "/" |
|
19 |
self.url = urlparse.urlunsplit((s[0], s[1], partial, '', '')) |
|
60 | 20 |
self.ui = ui |
321 | 21 |
no_list = [ "localhost", "127.0.0.1" ] |
22 |
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
|
23 |
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
|
24 |
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
|
25 |
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
|
26 |
host = host[7:] |
321 | 27 |
user = ui.config("http_proxy", "user") |
28 |
passwd = ui.config("http_proxy", "passwd") |
|
29 |
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
|
30 |
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
|
31 |
no = os.environ.get("no_proxy") |
321 | 32 |
if no: |
33 |
no_list = no_list + no.split(",") |
|
515 | 34 |
|
321 | 35 |
no_proxy = 0 |
36 |
for h in no_list: |
|
37 |
if (path.startswith("http://" + h + "/") or |
|
38 |
path.startswith("http://" + h + ":") or |
|
39 |
path == "http://" + h): |
|
40 |
no_proxy = 1 |
|
41 |
||
42 |
# Note: urllib2 takes proxy values from the environment and those will |
|
43 |
# 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
|
44 |
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
|
45 |
try: |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
46 |
if os.environ.has_key(env): |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
47 |
del os.environ[env] |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
48 |
except OSError: |
6390c377a9e6
Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents:
856
diff
changeset
|
49 |
pass |
321 | 50 |
|
51 |
proxy_handler = urllib2.BaseHandler() |
|
52 |
if host and not no_proxy: |
|
53 |
proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host}) |
|
54 |
||
55 |
authinfo = None |
|
56 |
if user and passwd: |
|
57 |
passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
|
58 |
passmgr.add_password(None, host, user, passwd) |
|
59 |
authinfo = urllib2.ProxyBasicAuthHandler(passmgr) |
|
60 |
||
61 |
opener = urllib2.build_opener(proxy_handler, authinfo) |
|
62 |
urllib2.install_opener(opener) |
|
60 | 63 |
|
634
da5378d39269
Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents:
627
diff
changeset
|
64 |
def dev(self): |
da5378d39269
Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents:
627
diff
changeset
|
65 |
return -1 |
da5378d39269
Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents:
627
diff
changeset
|
66 |
|
60 | 67 |
def do_cmd(self, cmd, **args): |
83 | 68 |
self.ui.debug("sending %s command\n" % cmd) |
60 | 69 |
q = {"cmd": cmd} |
70 |
q.update(args) |
|
71 |
qs = urllib.urlencode(q) |
|
72 |
cu = "%s?%s" % (self.url, qs) |
|
752 | 73 |
resp = urllib2.urlopen(cu) |
753 | 74 |
proto = resp.headers['content-type'] |
752 | 75 |
|
753 | 76 |
# accept old "text/plain" and "application/hg-changegroup" for now |
77 |
if not proto.startswith('application/mercurial') and \ |
|
78 |
not proto.startswith('text/plain') and \ |
|
79 |
not proto.startswith('application/hg-changegroup'): |
|
1251
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
80 |
raise hg.RepoError("'%s' does not appear to be an hg repository" % |
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
81 |
self.url) |
752 | 82 |
|
753 | 83 |
if proto.startswith('application/mercurial'): |
84 |
version = proto[22:] |
|
85 |
if float(version) > 0.1: |
|
1251
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
86 |
raise hg.RepoError("'%s' uses newer protocol %s" % |
84cf8834efb5
Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1089
diff
changeset
|
87 |
(self.url, version)) |
753 | 88 |
|
752 | 89 |
return resp |
60 | 90 |
|
222 | 91 |
def heads(self): |
92 |
d = self.do_cmd("heads").read() |
|
93 |
try: |
|
94 |
return map(bin, d[:-1].split(" ")) |
|
95 |
except: |
|
96 |
self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n") |
|
97 |
raise |
|
98 |
||
60 | 99 |
def branches(self, nodes): |
100 |
n = " ".join(map(hex, nodes)) |
|
752 | 101 |
d = self.do_cmd("branches", nodes=n).read() |
217 | 102 |
try: |
103 |
br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] |
|
104 |
return br |
|
105 |
except: |
|
106 |
self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n") |
|
107 |
raise |
|
60 | 108 |
|
109 |
def between(self, pairs): |
|
110 |
n = "\n".join(["-".join(map(hex, p)) for p in pairs]) |
|
752 | 111 |
d = self.do_cmd("between", pairs=n).read() |
217 | 112 |
try: |
113 |
p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] |
|
114 |
return p |
|
115 |
except: |
|
116 |
self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n") |
|
117 |
raise |
|
60 | 118 |
|
119 |
def changegroup(self, nodes): |
|
120 |
n = " ".join(map(hex, nodes)) |
|
752 | 121 |
f = self.do_cmd("changegroup", roots=n) |
192 | 122 |
bytes = 0 |
635
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
123 |
|
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
124 |
class zread: |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
125 |
def __init__(self, f): |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
126 |
self.zd = zlib.decompressobj() |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
127 |
self.f = f |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
128 |
self.buf = "" |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
129 |
def read(self, l): |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
130 |
while l > len(self.buf): |
751
0b245edec124
When pulling from a non hg repository URL (e.g. http://www.kernel.org/hg)
Muli Ben-Yehuda <mulix@mulix.org>
parents:
741
diff
changeset
|
131 |
r = self.f.read(4096) |
635
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
132 |
if r: |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
133 |
self.buf += self.zd.decompress(r) |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
134 |
else: |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
135 |
self.buf += self.zd.flush() |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
136 |
break |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
137 |
d, self.buf = self.buf[:l], self.buf[l:] |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
138 |
return d |
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
139 |
|
752 | 140 |
return zread(f) |
635
85e2209d401c
Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents:
634
diff
changeset
|
141 |
|
923 | 142 |
class httpsrepository(httprepository): |
143 |
pass |