Changeset 3307

Show
Ignore:
Timestamp:
12/17/09 15:21:44 (10 years ago)
Author:
cemeyer
Message:

#783: Move xmlrpc_client to httpretrieve. Tested against a local xmlrpc server.

Files:
1 modified

Legend:

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

    r3260 r3307  
    1616 
    1717include urlparse.repy 
     18include httpretrieve.repy 
    1819include xmlrpc_common.repy 
    1920 
     
    101102    request_xml = xmlrpc_common_call2xml(method_name, params) 
    102103 
    103     try: 
    104       # Assign None to socket so that socket will always be assigned 
    105       # for the finally: block. 
    106       socket = None 
     104    response = httpretrieve_get_string("http://%s:%s%s" % (self.server_host, \ 
     105        self.server_port, self.server_path), http_post=request_xml, \ 
     106        header_timeout=timeout, content_timeout=timeout, http_header={\ 
     107        "User-Agent": self.USER_AGENT, "Content-Type": "text/xml"}) 
    107108 
    108       # Open the connection to the server 
    109       socket = openconn(self.server_host, self.server_port, timeout=timeout) 
    110  
    111       # Timeout if the openconn took too long. 
    112       if getruntime() - starttime > timeout: 
    113         raise xmlrpc_common_Timeout() 
    114  
    115       # Send the xmlrpc request. 
    116       socket.send("POST %s HTTP/1.1\r\n" + \ 
    117           "User-Agent: %s\r\n" + \ 
    118           "Host: %s\r\n" + \ 
    119           "Content-Type: text/xml\r\n" + \ 
    120           "Content-Length: %d\r\n" + \ 
    121           "Connection: close\r\n\r\n%s" % 
    122           (self.server_path, self.USER_AGENT, self.server_host, 
    123             len(request_xml), request_xml)) 
    124  
    125       # Timeout if the request sending took too long. 
    126       if getruntime() - starttime > timeout: 
    127         raise xmlrpc_common_Timeout() 
    128  
    129       # Receive the raw response.  If the server does not respond before 
    130       # the timeout, a socket.error exception will be raised. 
    131       response = "" 
    132       while response.find("\r\n\r\n") < 0: 
    133         try: 
    134           recvdata = socket.recv(4096) 
    135         except Exception, e: 
    136           raise xmlrpc_common_ConnectionError("A connection error occurred.") 
    137  
    138         if len(recvdata) == 0: 
    139           raise xmlrpc_common_ConnectionError( \ 
    140               "Server closed connection before completing response.") 
    141  
    142         response += recvdata 
    143  
    144         # Timeout if recieving the response headers takes too long. 
    145         if getruntime() - starttime > timeout: 
    146           raise xmlrpc_common_Timeout() 
    147  
    148       # Separate the header from the response body. 
    149       raw_header, raw_content = response.split("\r\n\r\n", 1) 
    150       raw_header_lines = raw_header.split("\r\n") 
    151  
    152       # Parse headers for the appropriate server response. 
    153       if raw_header_lines[0] != "HTTP/1.1 200 OK" and \ 
    154         raw_header_lines[0] != "HTTP/1.0 200 OK": 
    155         raise ValueError("Invalid response protocol: %s" % raw_header_lines[0]) 
    156  
    157       # The response content size is initially 0. 
    158       content_size = 0 
    159  
    160       # Extract the response content size from the headers. 
    161       for line in raw_header_lines[1:]: 
    162         header_tag, header_value = line.split(":", 1) 
    163         if header_tag.strip().lower() == "content-length": 
    164           content_size = int(header_value.strip()) 
    165  
    166       # Get the rest of the response body. 
    167       while len(raw_content) < content_size: 
    168         raw_content += socket.recv(4096) 
    169  
    170         # Timeout if recieving the body took too long. 
    171         if getruntime() - starttime > timeout: 
    172           raise xmlrpc_common_Timeout() 
    173  
    174     finally: 
    175       # Close the socket.  Connection: keep-alive is not yet supported. 
    176       if socket is not None:  # socket will be None if openconn() threw an exception. 
    177         socket.close() 
    178  
    179     # Trim the response body and save it as the XML response. 
    180     xml_response = raw_content[0:content_size] 
     109    # Timeout if the POST took too long. 
     110    if getruntime() - starttime > timeout: 
     111      raise xmlrpc_common_Timeout() 
    181112 
    182113    # Parse the XML response body into Python values. 
    183     response_value = xmlrpc_common_response2python(xml_response) 
     114    response_value = xmlrpc_common_response2python(response) 
    184115 
    185116    # If a fault was decoded, raise the exception.