2007-10-12 05:29:23 +00:00
|
|
|
|
|
|
|
from cStringIO import StringIO
|
|
|
|
import urlparse, httplib
|
versioning: include an "appname" in the application version string in the versioning protocol, and make that appname be controlled by setup.py
It is currently hardcoded in setup.py to be 'allmydata-tahoe'. Ticket #556 is to make it configurable by a runtime command-line argument to setup.py: "--appname=foo", but I suddenly wondered if we really wanted that and at the same time realized that we don't need that for tahoe-1.3.0 release, so this patch just hardcodes it in setup.py.
setup.py inspects a file named 'src/allmydata/_appname.py' and assert that it contains the string "__appname__ = 'allmydata-tahoe'", and creates it if it isn't already present. src/allmydata/__init__.py import _appname and reads __appname__ from it. The rest of the Python code imports allmydata and inspects "allmydata.__appname__", although actually every use it uses "allmydata.__full_version__" instead, where "allmydata.__full_version__" is created in src/allmydata/__init__.py to be:
__full_version__ = __appname + '-' + str(__version__).
All the code that emits an "application version string" when describing what version of a protocol it supports (introducer server, storage server, upload helper), or when describing itself in general (introducer client), usese allmydata.__full_version__.
This fixes ticket #556 at least well enough for tahoe-1.3.0 release.
2009-02-12 00:18:16 +00:00
|
|
|
import allmydata # for __full_version__
|
2007-10-12 05:29:23 +00:00
|
|
|
|
|
|
|
# copied from twisted/web/client.py
|
|
|
|
def parse_url(url, defaultPort=None):
|
|
|
|
url = url.strip()
|
|
|
|
parsed = urlparse.urlparse(url)
|
|
|
|
scheme = parsed[0]
|
|
|
|
path = urlparse.urlunparse(('','')+parsed[2:])
|
|
|
|
if defaultPort is None:
|
|
|
|
if scheme == 'https':
|
|
|
|
defaultPort = 443
|
|
|
|
else:
|
|
|
|
defaultPort = 80
|
|
|
|
host, port = parsed[1], defaultPort
|
|
|
|
if ':' in host:
|
|
|
|
host, port = host.split(':')
|
|
|
|
port = int(port)
|
|
|
|
if path == "":
|
|
|
|
path = "/"
|
|
|
|
return scheme, host, port, path
|
|
|
|
|
|
|
|
|
|
|
|
def do_http(method, url, body=""):
|
|
|
|
if isinstance(body, str):
|
|
|
|
body = StringIO(body)
|
|
|
|
elif isinstance(body, unicode):
|
2009-02-23 00:31:06 +00:00
|
|
|
raise TypeError("do_http body must be a bytestring, not unicode")
|
2007-10-12 05:29:23 +00:00
|
|
|
else:
|
2008-05-22 00:31:19 +00:00
|
|
|
# We must give a Content-Length header to twisted.web, otherwise it
|
|
|
|
# seems to get a zero-length file. I suspect that "chunked-encoding"
|
|
|
|
# may fix this.
|
2007-10-12 05:29:23 +00:00
|
|
|
assert body.tell
|
|
|
|
assert body.seek
|
|
|
|
assert body.read
|
|
|
|
scheme, host, port, path = parse_url(url)
|
|
|
|
if scheme == "http":
|
|
|
|
c = httplib.HTTPConnection(host, port)
|
|
|
|
elif scheme == "https":
|
|
|
|
c = httplib.HTTPSConnection(host, port)
|
|
|
|
else:
|
|
|
|
raise ValueError("unknown scheme '%s', need http or https" % scheme)
|
|
|
|
c.putrequest(method, path)
|
|
|
|
c.putheader("Hostname", host)
|
2009-02-13 05:37:38 +00:00
|
|
|
c.putheader("User-Agent", allmydata.__full_version__ + " (tahoe-client)")
|
2007-10-12 05:29:23 +00:00
|
|
|
c.putheader("Connection", "close")
|
|
|
|
|
|
|
|
old = body.tell()
|
|
|
|
body.seek(0, 2)
|
|
|
|
length = body.tell()
|
|
|
|
body.seek(old)
|
|
|
|
c.putheader("Content-Length", str(length))
|
|
|
|
c.endheaders()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
data = body.read(8192)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
c.send(data)
|
|
|
|
|
|
|
|
return c.getresponse()
|
2008-05-20 02:28:50 +00:00
|
|
|
|
|
|
|
def check_http_error(resp, stderr):
|
|
|
|
if resp.status < 200 or resp.status >= 300:
|
|
|
|
print >>stderr, "error %d during HTTP request" % resp.status
|
|
|
|
return 1
|