Changeset 3349

Show
Ignore:
Timestamp:
01/11/10 12:22:44 (10 years ago)
Author:
cemeyer
Message:

httpretrieve: Fix some remaining bugs.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • seattle/trunk/seattlelib/httpretrieve.repy

    r3348 r3349  
    2323 
    2424 
    25 class HttpConnectionError: 
     25class HttpConnectionError(Exception): 
    2626  """ 
    2727  Error indicating that the web server has unexpectedly dropped the 
     
    3232 
    3333 
    34 class HttpBrokenServerError: 
     34class HttpBrokenServerError(Exception): 
    3535  """ 
    3636  Error indicating that the web server has sent us complete garbage instead 
     
    113113  if host is None: 
    114114    raise ValueError("Missing hostname.") 
    115   if parsedurl['query'] is not None and urlparse['query'] != "": 
     115  if parsedurl['query'] is not None and parsedurl['query'] != "": 
    116116    raise ValueError("URL cannot include a query string.") 
    117117 
     
    137137 
    138138  if path == "": 
    139     path = "/" 
     139    raise ValueError("Invalid path -- empty string.") 
    140140  if type(querydata) is dict: 
    141141    querydata = urllib_quote_parameters(querydata) 
     
    146146 
    147147  httpheader = method + ' ' + path + encquerydata + ' HTTP/1.0\r\n' 
    148   if "Host" not in httpheaders: 
    149     httpheader += "Host: " + host + ':' + port + "\r\n" 
    150148  if httpheaders is not None: 
    151149    if type(httpheaders) is not dict: 
    152150      raise TypeError("Expected HTTP headers as a dictionary.") 
    153151    else: 
     152      if "Host" not in httpheaders: 
     153        httpheader += "Host: " + host + ':' + port + "\r\n" 
    154154      for key, val in httpheaders.items(): 
    155155        httpheader += key + ": " + val + '\r\n' 
     
    179179 
    180180  httpheaderlines = headers_str.split("\r\n") 
     181  while httpheaderlines[-1] == "": 
     182    httpheaderlines = httpheaderlines[:-1] 
    181183 
    182184  # get the status code and status message from the HTTP response 
     
    185187  if len(headersplit) < 3: 
    186188    raise HttpBrokenServerError("Server returned garbage for HTTP response.") 
    187   if headersplit[0] != 'HTTP': 
     189  if not headersplit[0].startswith('HTTP'): 
    188190    raise HttpBrokenServerError("Server returned garbage for HTTP response.") 
    189191  statusmsg = headersplit[2] 
     
    350352    httpcontent = '' 
    351353    while True: 
    352       content = self._sock.recv(lefttoread or 4096) 
     354      try: 
     355        content = self._sock.recv(lefttoread or 4096) 
     356      except Exception, e: 
     357        if str(e) == "Socket closed": 
     358          self._totalcontentisreceived = True 
     359          break 
     360        else: 
     361          raise 
     362       
    353363      httpcontent += content 
    354364      self._totalread += len(content) 
     
    384394    """ 
    385395    self._fileobjclosed = True 
    386     self._sock.close() 
     396    try: 
     397      self._sock.close() 
     398    except Exception, e: 
     399      pass # don't care 
    387400 
    388401 
     
    415428            res[lastheader] = [] 
    416429          res[lastheader].append(lastheader_str.strip()) 
    417         lastheader, lastheader_str = headerlines[i].split(":") 
     430        lastheader, lastheader_str = headerlines[i].split(":", 1) 
    418431      i += 1 
    419432      if i >= len(headerlines): 
     433        if lastheader is not None: 
     434          if lastheader not in res: 
     435            res[lastheader] = [] 
     436          res[lastheader].append(lastheader_str.strip()) 
    420437        break 
    421438    return res