#!/usr/bin/env python """client.py - HTTP client APIs.""" __license__ = """ Copyright (c) 2001-2004 Mark Nottingham Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ __version__ = "0.11" # TODO: # Dict # - trap socket errors (e.g., no address, timeout, connect, etc.) # - phase out httplib # - HTTP persistent connections / connection management # - caching # - authentication; userinfo as property dict, raise error # - redirection handling # - content negotiation; prefs as properties # - better HTTP header handling # - delta encoding # - stricter HTTP conformance, corner cases # - SSL / TLS # - define and use URI object # - allow access to body as a file-like object # - allow dispatch to other URI schemes? # - WebDAV methods: .copy() .move() .lock() PROPFIND? MKCOL? # - HTTP Extension framework support? # - set user-agent w/ property # - cookies w/ property dict # - consider other dictionary methods (see UserDict.UserDict) # - allow instantiation to specify DNS forwarder from httplib import HTTP from urlparse import urlparse from message import Request, Response import status class Dict: """Dictionary of Representations.""" def __getitem__(self, key): "GET" request = Request() request.uri = key request.method = "GET" response = dereference(request) return response.representation def __setitem__(self, key, item): "PUT" request = Request(representation=item) request.uri = key request.method = "PUT" response = dereference(request) def __delitem__(self, key): "DELETE" request = Request() request.uri = key request.method = "DELETE" response = dereference(request) def __call__(self, uri, representation): "POST" request = Request(representation=representation) request.uri = uri request.method = "POST" response = dereference(request) return response.representation def dereference(request): """Given a Request instance, dereference the .uri and return a Response instance.""" response = Response() h = HTTP(urlparse(request.uri)[1]) ### split out port, userinfo h.putrequest(request.method, request.uri) if request.representation.body != None: for field_name, field_value in request.representation.headers.items(): if field_name.lower() == "content-length": continue h.putheader(field_name, field_value) h.putheader('content-length', str(len(request.representation.body))) h.endheaders() if request.representation.body != None: h.send(request.representation.body) status_code, status_phrase, headers = h.getreply() response = status.lookup.get(status_code, None) if response is None: response = status.Status() response.status_code = status_code response.status_phrase = status_phrase else: response = response() response.representation.headers = headers.dict ### split entity and message hdrs if isinstance(response, (status.ClientError, status.ServerError)): raise response ### more fine-grained response.representation.body = h.getfile().read() return response