source file: /home/buildslave/tahoe/edgy/build/src/allmydata/scripts/common_http.py
file stats: 53 lines, 45 executed: 84.9% covered
coverage versus previous test: 0 lines added, 0 lines removed
    1. 
    2. from cStringIO import StringIO
    3. import urlparse, httplib
    4. import allmydata # for __full_version__
    5. 
    6. # copied from twisted/web/client.py
    7. def parse_url(url, defaultPort=None):
    8.     url = url.strip()
    9.     parsed = urlparse.urlparse(url)
   10.     scheme = parsed[0]
   11.     path = urlparse.urlunparse(('','')+parsed[2:])
   12.     if defaultPort is None:
   13.         if scheme == 'https':
   14.             defaultPort = 443
   15.         else:
   16.             defaultPort = 80
   17.     host, port = parsed[1], defaultPort
   18.     if ':' in host:
   19.         host, port = host.split(':')
   20.         port = int(port)
   21.     if path == "":
   22.         path = "/"
   23.     return scheme, host, port, path
   24. 
   25. 
   26. def do_http(method, url, body=""):
   27.     if isinstance(body, str):
   28.         body = StringIO(body)
   29.     elif isinstance(body, unicode):
   30.         raise TypeError("do_http body must be a bytestring, not unicode")
   31.     else:
   32.         # We must give a Content-Length header to twisted.web, otherwise it
   33.         # seems to get a zero-length file. I suspect that "chunked-encoding"
   34.         # may fix this.
   35.         assert body.tell
   36.         assert body.seek
   37.         assert body.read
   38.     scheme, host, port, path = parse_url(url)
   39.     if scheme == "http":
   40.         c = httplib.HTTPConnection(host, port)
   41.     elif scheme == "https":
   42.         c = httplib.HTTPSConnection(host, port)
   43.     else:
   44.         raise ValueError("unknown scheme '%s', need http or https" % scheme)
   45.     c.putrequest(method, path)
   46.     c.putheader("Hostname", host)
   47.     c.putheader("User-Agent", allmydata.__full_version__ + " (tahoe-client)")
   48.     c.putheader("Connection", "close")
   49. 
   50.     old = body.tell()
   51.     body.seek(0, 2)
   52.     length = body.tell()
   53.     body.seek(old)
   54.     c.putheader("Content-Length", str(length))
   55.     c.endheaders()
   56. 
   57.     while True:
   58.         data = body.read(8192)
   59.         if not data:
   60.             break
   61.         c.send(data)
   62. 
   63.     return c.getresponse()
   64. 
   65. def check_http_error(resp, stderr):
   66.     if resp.status < 200 or resp.status >= 300:
   67.         print >>stderr, "error %d during HTTP request" % resp.status
   68.         return 1