1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, self.list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, self.list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
34 from urlparse import urlparse
35 from .pool.http import get_http_connection
36 from . import HTTPConnection, HTTPResponse, HTTPConnectionError
39 from time import sleep
40 from httplib import ResponseNotReady
42 class KamakiHTTPResponse(HTTPResponse):
44 def _get_response(self):
45 print('KamakiHTTPResponse:should I get response?')
47 print('\tKamakiHTTPResponse: no, I have already done that before')
49 print('\tKamakiHTTPResponse: yes, pls')
50 r = self.request.getresponse()
51 self.prefetched = True
53 for k,v in r.getheaders():
55 self.headers = headers
56 self.content = r.read(r.length)
57 self.status_code = r.status
58 self.status = r.reason
59 print('KamakiHTTPResponse: Niiiiice')
72 from json import loads
74 return loads(self._content)
75 except ValueError as err:
76 HTTPConnectionError('Response not formated in JSON', details=unicode(err), status=702)
81 class KamakiHTTPConnection(HTTPConnection):
95 def _load_connection_settings(self, url=None, scheme=None, params=None, headers=None, host=None,
96 port=None, method=None):
97 if params is not None:
99 if headers is not None:
100 self.headers = headers
104 if host is None or scheme is None:
113 for i,(key, val) in enumerate(self.params.items()):
114 param_str = ('?' if i == 0 else '&') + unicode(key)
116 param_str+= '='+unicode(val)
117 url = p.path + param_str
120 port = port if port is not None else self.scheme_ports[scheme]
121 #NOTE: we force host:port as canonical form,
122 # lest we have a cache miss 'host' vs 'host:80'
123 netloc = "%s%s" % (host, port)
126 self.url = url #if url in (None, '') or url[0] != '/' else url[1:]
129 if method is not None:
132 def perform_request(self, url=None, params=None, headers=None, method=None, host=None,
133 port=None, data=None):
134 self._load_connection_settings(url=url, params=params, headers=headers, host=host,
135 port=port, method=method)
136 print('---> %s %s %s %s %s'%(self.method, self.scheme, self.netloc, self.url, self.headers))
137 conn = get_http_connection(netloc=self.netloc, scheme=self.scheme)
139 conn.request(self.method, self.url, headers=self.headers, body=data)
143 return KamakiHTTPResponse(conn)