/atom/http.py

http://radioappz.googlecode.com/ · Python · 318 lines · 263 code · 17 blank · 38 comment · 6 complexity · 729682eb171640f841392b8d0dfbe1cd MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2008 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """HttpClients in this module use httplib to make HTTP requests.
  17. This module make HTTP requests based on httplib, but there are environments
  18. in which an httplib based approach will not work (if running in Google App
  19. Engine for example). In those cases, higher level classes (like AtomService
  20. and GDataService) can swap out the HttpClient to transparently use a
  21. different mechanism for making HTTP requests.
  22. HttpClient: Contains a request method which performs an HTTP call to the
  23. server.
  24. ProxiedHttpClient: Contains a request method which connects to a proxy using
  25. settings stored in operating system environment variables then
  26. performs an HTTP call to the endpoint server.
  27. """
  28. __author__ = 'api.jscudder (Jeff Scudder)'
  29. import types
  30. import os
  31. import httplib
  32. import atom.url
  33. import atom.http_interface
  34. import socket
  35. import base64
  36. import atom.http_core
  37. ssl_imported = False
  38. ssl = None
  39. try:
  40. import ssl
  41. ssl_imported = True
  42. except ImportError:
  43. pass
  44. class ProxyError(atom.http_interface.Error):
  45. pass
  46. class TestConfigurationError(Exception):
  47. pass
  48. DEFAULT_CONTENT_TYPE = 'application/atom+xml'
  49. class HttpClient(atom.http_interface.GenericHttpClient):
  50. # Added to allow old v1 HttpClient objects to use the new
  51. # http_code.HttpClient. Used in unit tests to inject a mock client.
  52. v2_http_client = None
  53. def __init__(self, headers=None):
  54. self.debug = False
  55. self.headers = headers or {}
  56. def request(self, operation, url, data=None, headers=None):
  57. """Performs an HTTP call to the server, supports GET, POST, PUT, and
  58. DELETE.
  59. Usage example, perform and HTTP GET on http://www.google.com/:
  60. import atom.http
  61. client = atom.http.HttpClient()
  62. http_response = client.request('GET', 'http://www.google.com/')
  63. Args:
  64. operation: str The HTTP operation to be performed. This is usually one
  65. of 'GET', 'POST', 'PUT', or 'DELETE'
  66. data: filestream, list of parts, or other object which can be converted
  67. to a string. Should be set to None when performing a GET or DELETE.
  68. If data is a file-like object which can be read, this method will
  69. read a chunk of 100K bytes at a time and send them.
  70. If the data is a list of parts to be sent, each part will be
  71. evaluated and sent.
  72. url: The full URL to which the request should be sent. Can be a string
  73. or atom.url.Url.
  74. headers: dict of strings. HTTP headers which should be sent
  75. in the request.
  76. """
  77. all_headers = self.headers.copy()
  78. if headers:
  79. all_headers.update(headers)
  80. # If the list of headers does not include a Content-Length, attempt to
  81. # calculate it based on the data object.
  82. if data and 'Content-Length' not in all_headers:
  83. if isinstance(data, types.StringTypes):
  84. all_headers['Content-Length'] = str(len(data))
  85. else:
  86. raise atom.http_interface.ContentLengthRequired('Unable to calculate '
  87. 'the length of the data parameter. Specify a value for '
  88. 'Content-Length')
  89. # Set the content type to the default value if none was set.
  90. if 'Content-Type' not in all_headers:
  91. all_headers['Content-Type'] = DEFAULT_CONTENT_TYPE
  92. if self.v2_http_client is not None:
  93. http_request = atom.http_core.HttpRequest(method=operation)
  94. atom.http_core.Uri.parse_uri(str(url)).modify_request(http_request)
  95. http_request.headers = all_headers
  96. if data:
  97. http_request._body_parts.append(data)
  98. return self.v2_http_client.request(http_request=http_request)
  99. if not isinstance(url, atom.url.Url):
  100. if isinstance(url, types.StringTypes):
  101. url = atom.url.parse_url(url)
  102. else:
  103. raise atom.http_interface.UnparsableUrlObject('Unable to parse url '
  104. 'parameter because it was not a string or atom.url.Url')
  105. connection = self._prepare_connection(url, all_headers)
  106. if self.debug:
  107. connection.debuglevel = 1
  108. connection.putrequest(operation, self._get_access_url(url),
  109. skip_host=True)
  110. if url.port is not None:
  111. connection.putheader('Host', '%s:%s' % (url.host, url.port))
  112. else:
  113. connection.putheader('Host', url.host)
  114. # Overcome a bug in Python 2.4 and 2.5
  115. # httplib.HTTPConnection.putrequest adding
  116. # HTTP request header 'Host: www.google.com:443' instead of
  117. # 'Host: www.google.com', and thus resulting the error message
  118. # 'Token invalid - AuthSub token has wrong scope' in the HTTP response.
  119. if (url.protocol == 'https' and int(url.port or 443) == 443 and
  120. hasattr(connection, '_buffer') and
  121. isinstance(connection._buffer, list)):
  122. header_line = 'Host: %s:443' % url.host
  123. replacement_header_line = 'Host: %s' % url.host
  124. try:
  125. connection._buffer[connection._buffer.index(header_line)] = (
  126. replacement_header_line)
  127. except ValueError: # header_line missing from connection._buffer
  128. pass
  129. # Send the HTTP headers.
  130. for header_name in all_headers:
  131. connection.putheader(header_name, all_headers[header_name])
  132. connection.endheaders()
  133. # If there is data, send it in the request.
  134. if data:
  135. if isinstance(data, list):
  136. for data_part in data:
  137. _send_data_part(data_part, connection)
  138. else:
  139. _send_data_part(data, connection)
  140. # Return the HTTP Response from the server.
  141. return connection.getresponse()
  142. def _prepare_connection(self, url, headers):
  143. if not isinstance(url, atom.url.Url):
  144. if isinstance(url, types.StringTypes):
  145. url = atom.url.parse_url(url)
  146. else:
  147. raise atom.http_interface.UnparsableUrlObject('Unable to parse url '
  148. 'parameter because it was not a string or atom.url.Url')
  149. if url.protocol == 'https':
  150. if not url.port:
  151. return httplib.HTTPSConnection(url.host)
  152. return httplib.HTTPSConnection(url.host, int(url.port))
  153. else:
  154. if not url.port:
  155. return httplib.HTTPConnection(url.host)
  156. return httplib.HTTPConnection(url.host, int(url.port))
  157. def _get_access_url(self, url):
  158. return url.to_string()
  159. class ProxiedHttpClient(HttpClient):
  160. """Performs an HTTP request through a proxy.
  161. The proxy settings are obtained from enviroment variables. The URL of the
  162. proxy server is assumed to be stored in the environment variables
  163. 'https_proxy' and 'http_proxy' respectively. If the proxy server requires
  164. a Basic Auth authorization header, the username and password are expected to
  165. be in the 'proxy-username' or 'proxy_username' variable and the
  166. 'proxy-password' or 'proxy_password' variable.
  167. After connecting to the proxy server, the request is completed as in
  168. HttpClient.request.
  169. """
  170. def _prepare_connection(self, url, headers):
  171. proxy_auth = _get_proxy_auth()
  172. if url.protocol == 'https':
  173. # destination is https
  174. proxy = os.environ.get('https_proxy')
  175. if proxy:
  176. # Set any proxy auth headers
  177. if proxy_auth:
  178. proxy_auth = 'Proxy-authorization: %s' % proxy_auth
  179. # Construct the proxy connect command.
  180. port = url.port
  181. if not port:
  182. port = '443'
  183. proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (url.host, port)
  184. # Set the user agent to send to the proxy
  185. if headers and 'User-Agent' in headers:
  186. user_agent = 'User-Agent: %s\r\n' % (headers['User-Agent'])
  187. else:
  188. user_agent = ''
  189. proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, user_agent)
  190. # Find the proxy host and port.
  191. proxy_url = atom.url.parse_url(proxy)
  192. if not proxy_url.port:
  193. proxy_url.port = '80'
  194. # Connect to the proxy server, very simple recv and error checking
  195. p_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  196. p_sock.connect((proxy_url.host, int(proxy_url.port)))
  197. p_sock.sendall(proxy_pieces)
  198. response = ''
  199. # Wait for the full response.
  200. while response.find("\r\n\r\n") == -1:
  201. response += p_sock.recv(8192)
  202. p_status = response.split()[1]
  203. if p_status != str(200):
  204. raise ProxyError('Error status=%s' % str(p_status))
  205. # Trivial setup for ssl socket.
  206. sslobj = None
  207. if ssl_imported:
  208. sslobj = ssl.wrap_socket(p_sock, None, None)
  209. else:
  210. sock_ssl = socket.ssl(p_sock, None, None)
  211. sslobj = httplib.FakeSocket(p_sock, sock_ssl)
  212. # Initalize httplib and replace with the proxy socket.
  213. connection = httplib.HTTPConnection(proxy_url.host)
  214. connection.sock = sslobj
  215. return connection
  216. else:
  217. # The request was HTTPS, but there was no https_proxy set.
  218. return HttpClient._prepare_connection(self, url, headers)
  219. else:
  220. proxy = os.environ.get('http_proxy')
  221. if proxy:
  222. # Find the proxy host and port.
  223. proxy_url = atom.url.parse_url(proxy)
  224. if not proxy_url.port:
  225. proxy_url.port = '80'
  226. if proxy_auth:
  227. headers['Proxy-Authorization'] = proxy_auth.strip()
  228. return httplib.HTTPConnection(proxy_url.host, int(proxy_url.port))
  229. else:
  230. # The request was HTTP, but there was no http_proxy set.
  231. return HttpClient._prepare_connection(self, url, headers)
  232. def _get_access_url(self, url):
  233. return url.to_string()
  234. def _get_proxy_auth():
  235. proxy_username = os.environ.get('proxy-username')
  236. if not proxy_username:
  237. proxy_username = os.environ.get('proxy_username')
  238. proxy_password = os.environ.get('proxy-password')
  239. if not proxy_password:
  240. proxy_password = os.environ.get('proxy_password')
  241. if proxy_username:
  242. user_auth = base64.encodestring('%s:%s' % (proxy_username,
  243. proxy_password))
  244. return 'Basic %s\r\n' % (user_auth.strip())
  245. else:
  246. return ''
  247. def _send_data_part(data, connection):
  248. if isinstance(data, types.StringTypes):
  249. connection.send(data)
  250. return
  251. # Check to see if data is a file-like object that has a read method.
  252. elif hasattr(data, 'read'):
  253. # Read the file and send it a chunk at a time.
  254. while 1:
  255. binarydata = data.read(100000)
  256. if binarydata == '': break
  257. connection.send(binarydata)
  258. return
  259. else:
  260. # The data object was not a file.
  261. # Try to convert to a string and send the data.
  262. connection.send(str(data))
  263. return