1# Copyright (c) 2012-2013 Mitch Garnaat http://garnaat.org/
2# Copyright 2012-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"). You
5# may not use this file except in compliance with the License. A copy of
6# the License is located at
7#
8# http://aws.amazon.com/apache2.0/
9#
10# or in the "license" file accompanying this file. This file is
11# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12# ANY KIND, either express or implied. See the License for the specific
13# language governing permissions and limitations under the License.
14
15import logging
16from io import IOBase
17
18from urllib3.exceptions import ProtocolError as URLLib3ProtocolError
19from urllib3.exceptions import ReadTimeoutError as URLLib3ReadTimeoutError
20
21from botocore import (
22 ScalarTypes, # noqa: F401
23 parsers,
24)
25from botocore.compat import (
26 XMLParseError, # noqa: F401
27 set_socket_timeout,
28)
29from botocore.exceptions import (
30 IncompleteReadError,
31 ReadTimeoutError,
32 ResponseStreamingError,
33)
34from botocore.hooks import first_non_none_response # noqa
35
36logger = logging.getLogger(__name__)
37
38
39class StreamingBody(IOBase):
40 """Wrapper class for an http response body.
41
42 This provides a few additional conveniences that do not exist
43 in the urllib3 model:
44
45 * Set the timeout on the socket (i.e read() timeouts)
46 * Auto validation of content length, if the amount of bytes
47 we read does not match the content length, an exception
48 is raised.
49
50 """
51
52 _DEFAULT_CHUNK_SIZE = 1024
53
54 def __init__(self, raw_stream, content_length):
55 self._raw_stream = raw_stream
56 self._content_length = content_length
57 self._amount_read = 0
58
59 def __del__(self):
60 # Extending destructor in order to preserve the underlying raw_stream.
61 # The ability to add custom cleanup logic introduced in Python3.4+.
62 # https://www.python.org/dev/peps/pep-0442/
63 pass
64
65 def set_socket_timeout(self, timeout):
66 """Set the timeout seconds on the socket."""
67 # The problem we're trying to solve is to prevent .read() calls from
68 # hanging. This can happen in rare cases. What we'd like to ideally
69 # do is set a timeout on the .read() call so that callers can retry
70 # the request.
71 # Unfortunately, this isn't currently possible in requests.
72 # See: https://github.com/kennethreitz/requests/issues/1803
73 # So what we're going to do is reach into the guts of the stream and
74 # grab the socket object, which we can set the timeout on. We're
75 # putting in a check here so in case this interface goes away, we'll
76 # know.
77 try:
78 set_socket_timeout(self._raw_stream, timeout)
79 except AttributeError:
80 logger.error(
81 "Cannot access the socket object of "
82 "a streaming response. It's possible "
83 "the interface has changed.",
84 exc_info=True,
85 )
86 raise
87
88 def readable(self):
89 try:
90 return self._raw_stream.readable()
91 except AttributeError:
92 return False
93
94 def read(self, amt=None):
95 """Read at most amt bytes from the stream.
96
97 If the amt argument is omitted, read all data.
98 """
99 try:
100 chunk = self._raw_stream.read(amt)
101 except URLLib3ReadTimeoutError as e:
102 # TODO: the url will be None as urllib3 isn't setting it yet
103 raise ReadTimeoutError(endpoint_url=e.url, error=e)
104 except URLLib3ProtocolError as e:
105 raise ResponseStreamingError(error=e)
106 self._amount_read += len(chunk)
107 if amt is None or (not chunk and amt > 0):
108 # If the server sends empty contents or
109 # we ask to read all of the contents, then we know
110 # we need to verify the content length.
111 self._verify_content_length()
112 return chunk
113
114 def readinto(self, b):
115 """Read bytes into a pre-allocated, writable bytes-like object b, and return the number of bytes read."""
116 try:
117 amount_read = self._raw_stream.readinto(b)
118 except URLLib3ReadTimeoutError as e:
119 # TODO: the url will be None as urllib3 isn't setting it yet
120 raise ReadTimeoutError(endpoint_url=e.url, error=e)
121 except URLLib3ProtocolError as e:
122 raise ResponseStreamingError(error=e)
123 self._amount_read += amount_read
124 if amount_read == 0 and len(b) > 0:
125 # If the server sends empty contents then we know we need to verify
126 # the content length.
127 self._verify_content_length()
128 return amount_read
129
130 def readlines(self):
131 return self._raw_stream.readlines()
132
133 def __iter__(self):
134 """Return an iterator to yield 1k chunks from the raw stream."""
135 return self.iter_chunks(self._DEFAULT_CHUNK_SIZE)
136
137 def __next__(self):
138 """Return the next 1k chunk from the raw stream."""
139 current_chunk = self.read(self._DEFAULT_CHUNK_SIZE)
140 if current_chunk:
141 return current_chunk
142 raise StopIteration()
143
144 def __enter__(self):
145 return self._raw_stream
146
147 def __exit__(self, type, value, traceback):
148 self._raw_stream.close()
149
150 next = __next__
151
152 def iter_lines(self, chunk_size=_DEFAULT_CHUNK_SIZE, keepends=False):
153 """Return an iterator to yield lines from the raw stream.
154
155 This is achieved by reading chunk of bytes (of size chunk_size) at a
156 time from the raw stream, and then yielding lines from there.
157 """
158 pending = b''
159 for chunk in self.iter_chunks(chunk_size):
160 lines = (pending + chunk).splitlines(True)
161 for line in lines[:-1]:
162 yield line.splitlines(keepends)[0]
163 pending = lines[-1]
164 if pending:
165 yield pending.splitlines(keepends)[0]
166
167 def iter_chunks(self, chunk_size=_DEFAULT_CHUNK_SIZE):
168 """Return an iterator to yield chunks of chunk_size bytes from the raw
169 stream.
170 """
171 while True:
172 current_chunk = self.read(chunk_size)
173 if current_chunk == b"":
174 break
175 yield current_chunk
176
177 def _verify_content_length(self):
178 # See: https://github.com/kennethreitz/requests/issues/1855
179 # Basically, our http library doesn't do this for us, so we have
180 # to do this ourself.
181 if self._content_length is not None and self._amount_read != int(
182 self._content_length
183 ):
184 raise IncompleteReadError(
185 actual_bytes=self._amount_read,
186 expected_bytes=int(self._content_length),
187 )
188
189 def tell(self):
190 return self._raw_stream.tell()
191
192 def close(self):
193 """Close the underlying http response stream."""
194 self._raw_stream.close()
195
196
197def get_response(operation_model, http_response):
198 protocol = operation_model.service_model.resolved_protocol
199 response_dict = {
200 'headers': http_response.headers,
201 'status_code': http_response.status_code,
202 }
203 # TODO: Unfortunately, we have to have error logic here.
204 # If it looks like an error, in the streaming response case we
205 # need to actually grab the contents.
206 if response_dict['status_code'] >= 300:
207 response_dict['body'] = http_response.content
208 elif operation_model.has_streaming_output:
209 response_dict['body'] = StreamingBody(
210 http_response.raw, response_dict['headers'].get('content-length')
211 )
212 else:
213 response_dict['body'] = http_response.content
214
215 parser = parsers.create_parser(protocol)
216 return http_response, parser.parse(
217 response_dict, operation_model.output_shape
218 )