def read(self, size=None): """Read bytes from the slice. Compared to other streams, there is one case where we may unexpectedly raise an exception on read: if the underlying stream is exhausted (i.e. returns no bytes on read), and the size of this slice indicates we should still be able to read more bytes, we raise :exc:`IncompleteRead`. :type size: integer or None :param size: If provided, read no more than size bytes from the stream. :rtype: bytes :returns: bytes read from this slice. :raises: :exc:`IncompleteRead` """ if size is not None: read_size = min(size, self._remaining_bytes) else: read_size = self._remaining_bytes data = self._stream.read(read_size) if read_size > 0 and not data: raise http_client.IncompleteRead( self._max_bytes - self._remaining_bytes, self._max_bytes) self._remaining_bytes -= len(data) return data
def testDefaultExceptionHandler(self): """Ensures exception handles swallows (retries)""" mock_http_content = 'content'.encode('utf8') for exception_arg in (http_client.BadStatusLine('line'), http_client.IncompleteRead('partial'), http_client.ResponseNotReady(), socket.error(), socket.gaierror(), httplib2.ServerNotFoundError(), ValueError(), oauth2client.client.HttpAccessTokenRefreshError( status=503), exceptions.RequestError(), exceptions.BadStatusCodeError({'status': 503}, mock_http_content, 'url'), exceptions.RetryAfterError({'status': 429}, mock_http_content, 'url', 0)): retry_args = http_wrapper.ExceptionRetryArgs( http={'connections': {}}, http_request=_MockHttpRequest(), exc=exception_arg, num_retries=0, max_retry_wait=0, total_wait_sec=0) # Disable time.sleep for this handler as it is called with # a minimum value of 1 second. with patch('time.sleep', return_value=None): http_wrapper.HandleExceptionsAndRebuildHttpConnections( retry_args)
def _update_chunk_length(self): # First, we'll figure out length of a chunk and then # we'll try to read it from socket. if self.chunk_left is not None: return line = self._fp.fp.readline() line = line.split(b";", 1)[0] try: self.chunk_left = int(line, 16) except ValueError: # Invalid chunked protocol response, abort. self.close() raise httplib.IncompleteRead(line)
def read_len(self, length): """Read the data stream until `length` characters been received :param length: The number of characters to read from the stream(int) :return: The str of the data read with `length` characters (str in python 2, bytes in python 3) """ while not self._stream.closed: if len(self._buffer) >= length: return self._pop(length) read_len = max(self._chunk_size, length - len(self._buffer)) self._buffer += self._stream.read(read_len) # if stream is closed before enough characters were received raise httplib.IncompleteRead( '%d bytes read, %d more expected' % (len(self._buffer), length - len(self._buffer)))
def read_line(self, sep=six.b('\n')): """Read the data stream until a given separator is found (default \n) :param sep: Separator to read until. Must by of the bytes type (str in python 2, bytes in python 3) :return: The str of the data read until sep (str in python 2, bytes in python 3) """ start = 0 while not self._stream.closed: loc = self._buffer.find(sep, start) if loc >= 0: return self._pop(loc + len(sep)) else: start = len(self._buffer) self._buffer += self._stream.read(self._chunk_size) # if stream is closed before a seperator was received raise httplib.IncompleteRead('"%s" sep was not received' % (str(sep), ))
def request(*args, **kwargs): url = kwargs.get('url') or \ (args[0] if isinstance(args[0], str) else args[2]) params = kwargs.get('params') if kwargs.get('stream'): return StreamResponse('./tests/data/' + 'sample.jpg') for i, bundle in enumerate(MockedRequest.mapping): regex, path = bundle if regex.search(url) is not None: json = JsonResponse('./tests/data/' + path) if (i == 1 and kwargs.get('params') and kwargs.get('params').get('before')): global post_metas_requests post_metas_requests += 1 if post_metas_requests >= 50: # cheating hacks Orz return JsonResponse() elif i == 4: json.comments_case = True json.start = params.get('after', 0) if params else 0 json.load_mockeddata() return json else: if kwargs.get('error') == 'ValueError': raise ValueError elif kwargs.get('error') == 'IncompleteRead': e = httplib.IncompleteRead(partial='some error here') raise e elif kwargs.get('error') == 'RetryError': raise RetryError elif kwargs.get('error') == 'Exception': raise Exception elif kwargs.get('resp_error'): return JsonResponse(error=kwargs.get('resp_error')) else: error_json = JsonResponse() error_json.result = {'error': 'Not found Ya'} error_json.status_code = 404 return error_json