Return a `Deferred` yielding at most *max_bytes* bytes from the
    body of a Twisted Web *response*, or the whole body if *max_bytes*
    is `None`.
973780
def read_body(response, max_bytes=None):
    """"""
    finished = Deferred()
    response.deliverBody(TruncatingReadBodyProtocol(
        response.code, response.phrase, finished, max_bytes))
    return finished
1365176
def render_POST(self, request):
        
        from twisted.internet.defer import maybeDeferred
        
        chain = self.factory.newInstance()
        data = request.content.read()
        
        d = maybeDeferred(chain.processRequest, data, request=request, resource=self)
        d.addCallback(chain.processResponse, request=request, resource=self)
        d.addCallback(self._writeResponse, request)
        d.addErrback(self._writeFault, request)
        
        return NOT_DONE_YET
375096
def _request(self, url, **kwargs):
        '''
        '''
        response = super(Bounces, self)._request(url, **kwargs)
        self._last_response = response
        return response
1022601
def get_response(request)
      begin
        data = @transport.recv(@max_bytes)
        message = Message.decode(data, @mib)
        response = message.pdu
      end until request.request_id == response.request_id
      response
    end
1366465
def _read_body_by_length(self, response, file):
        '''
        '''
        _logger.debug('Reading body by length.')

        file_is_async = hasattr(file, 'drain')

        try:
            body_size = int(response.fields['Content-Length'])

            if body_size < 0:
                raise ValueError('Content length cannot be negative.')

        except ValueError as error:
            _logger.warning(__(
                _('Invalid content length: {error}'), error=error
            ))

            yield from self._read_body_until_close(response, file)
            return

        bytes_left = body_size

        while bytes_left > 0:
            data = yield from self._connection.read(self._read_size)

            if not data:
                break

            bytes_left -= len(data)

            if bytes_left < 0:
                data = data[:bytes_left]

                _logger.warning(_('Content overrun.'))
                self.close()

            self._data_event_dispatcher.notify_read(data)

            content_data = self._decompress_data(data)

            if file:
                file.write(content_data)

                if file_is_async:
                    yield from file.drain()

        if bytes_left > 0:
            raise NetworkError('Connection closed.')

        content_data = self._flush_decompressor()

        if file and content_data:
            file.write(content_data)

            if file_is_async:
                yield from file.drain()
973341
def chunk_data(payload, max_size)
      total = payload.bytesize
      cursor = 0
      while (total - cursor) > max_size
        yield payload.byteslice(cursor, max_size)
        cursor += max_size
      end
      payload.byteslice(cursor, total - cursor)
    end
224146
def _receive_eol_token(self):
        
        while self.EOL_TOKEN is None:
            # use yield to have data sent to us - store in buffer
            self._buffer += yield
            if len(self._buffer) > MAX_REQUEST_LENGTH:
                raise HTTPErrorBadRequest("Max request length exceeded")
            self.EOL_TOKEN = self.determine_newline(self._buffer)
109419
function bodyReader(options) {
    var opts = options || {};
    assert.object(opts, 'opts');

    var maxBodySize = opts.maxBodySize || 0;

    function readBody(req, res, next) {
        // #100 don't read the body again if we've read it once
        if (req._readBody) {
            next();
            return;
        } else {
            req._readBody = true;
        }

        if ((req.getContentLength() === 0 && !req.isChunked()) ||
            req.contentType() === 'multipart/form-data' ||
            req.contentType() === 'application/octet-stream') {
            next();
            return;
        }
        var bodyWriter = createBodyWriter(req);

        var bytesReceived = 0;
        var digest;
        var gz;
        var hash;
        var md5;

        var unsupportedCompression;

        if ((md5 = req.headers['content-md5'])) {
            hash = crypto.createHash('md5');
        }

        function done() {
            bodyWriter.end();

            if (unsupportedCompression) {
                var error = unsupportedCompression + ' not supported';
                next(new UnsupportedMediaTypeError(error));
                return;
            }

            if (maxBodySize && bytesReceived > maxBodySize) {
                var msg = 'Request body size exceeds ' + maxBodySize;
                var err;

                // Between Node 0.12 and 4 http status code messages changed
                // RequestEntityTooLarge was changed to PayloadTooLarge
                // this check is to maintain backwards compatibility
                if (PayloadTooLargeError !== undefined) {
                    err = new PayloadTooLargeError(msg);
                } else {
                    err = new RequestEntityTooLargeError(msg);
                }

                next(err);
                return;
            }

            if (!req.body.length) {
                next();
                return;
            }

            if (hash && md5 !== (digest = hash.digest('base64'))) {
                next(new BadDigestError(MD5_MSG, md5, digest));
                return;
            }

            next();
        }

        if (req.headers['content-encoding'] === undefined) {
            // This handles the original else branch
            req.once('end', done);
        } else if (req.headers['content-encoding'] === 'gzip') {
            gz = zlib.createGunzip();
            gz.on('data', bodyWriter.write);
            gz.once('end', done);
            req.once('end', gz.end.bind(gz));
        } else {
            unsupportedCompression = req.headers['content-encoding'];
            res.setHeader('Accept-Encoding', 'gzip');
            req.once('end', done);
        }

        req.on('data', function onRequestData(chunk) {
            if (maxBodySize) {
                bytesReceived += chunk.length;

                if (bytesReceived > maxBodySize) {
                    return;
                }
            }

            if (hash) {
                hash.update(chunk, 'binary');
            }

            if (gz) {
                gz.write(chunk);
            } else {
                bodyWriter.write(chunk);
            }
        });

        req.once('error', next);
        req.resume();
    }

    return (readBody);
}
1172814
def getJson(cls, url, method='GET', headers={}, data=None, socket=None, timeout=120):
        
        if not 'Content-Type' in headers:
            headers['Content-Type'] = ['application/json']

        body = yield cls().getBody(url, method, headers, data, socket, timeout)

        defer.returnValue(json.loads(body))
415554
def read(self, size=sys.maxsize):
        
        blob_size = int(self.blob_properties.get('content-length'))
        if self._pointer < blob_size:
            chunk = self._download_chunk_with_retries(
                chunk_offset=self._pointer, chunk_size=size)
            self._pointer += size
            return chunk
864958
def download_chunk(self, url, headers):
        
        response = requests.get(url, headers=headers, stream=True)
        if response.status_code == SWIFT_EXPIRED_STATUS_CODE \
                or response.status_code == S3_EXPIRED_STATUS_CODE:
            raise DownloadInconsistentError(response.text)
        response.raise_for_status()
        self.actual_bytes_read = 0
        self._write_response_to_file(response)
        self._verify_download_complete()
1358747
def from_body(cls, body):
        """"""
        # TODO: Check length
        request = cls()
        request.channel = body[1]
        request.seq = body[2]
        request.cemi = body[4:]
        return request
260815
def _fetch_one(self, request: Request) -> Tuple[bool, float]:
        '''
        '''
        _logger.info(_('Fetching ‘{url}’.'), url=request.url)

        response = None

        try:
            response = yield from self._web_client_session.start()
            self._item_session.response = response

            action = self._result_rule.handle_pre_response(self._item_session)

            if action in (Actions.RETRY, Actions.FINISH):
                raise HookPreResponseBreak()

            self._file_writer_session.process_response(response)

            if not response.body:
                response.body = Body(
                    directory=self._item_session.app_session.root_path,
                    hint='resp_cb'
                )

            yield from \
                self._web_client_session.download(
                    file=response.body,
                    duration_timeout=self._fetch_rule.duration_timeout
                )
        except HookPreResponseBreak:
            _logger.debug('Hook pre-response break.')
            return True, None
        except REMOTE_ERRORS as error:
            self._log_error(request, error)

            self._result_rule.handle_error(self._item_session, error)
            wait_time = self._result_rule.get_wait_time(
                self._item_session, error=error
            )

            if request.body:
                request.body.close()

            if response:
                response.body.close()

            return True, wait_time
        else:
            self._log_response(request, response)
            action = self._handle_response(request, response)
            wait_time = self._result_rule.get_wait_time(self._item_session)

            yield from self._run_coprocessors(request, response)

            response.body.close()

            if request.body:
                request.body.close()

            return action != Actions.NORMAL, wait_time
827182
async def get_data(self, raw: bool=True) -> AnyStr:
        """"""
        if self.implicit_sequence_conversion:
            self.response = self.data_body_class(await self.response.convert_to_sequence())
        result = b'' if raw else ''
        async with self.response as body:  # type: ignore
            async for data in body:
                if raw:
                    result += data
                else:
                    result += data.decode(self.charset)
        return result
577333
def tail(self, bytes):
        
        self.io.seek(max(0, self.size() - bytes))
        return self.io.read()
1295628
def _body(self):
        
        maxread = max(0, self.content_length)
        stream = self.environ['wsgi.input']
        body = BytesIO() if maxread < MEMFILE_MAX else TemporaryFile(mode='w+b')
        while maxread > 0:
            part = stream.read(min(maxread, MEMFILE_MAX))
            if not part: break
            body.write(part)
            maxread -= len(part)
        self.environ['wsgi.input'] = body
        body.seek(0)
        return body
1077654
def _recv(self, max_size):
        
        # wait for read
        if not self._can_read():
            self.close()
            return None
        # recv
        try:
            r_buffer = self.__sock.recv(max_size)
        except socket.error:
            r_buffer = None
        # handle recv error
        if not r_buffer:
            self.__last_error = const.MB_RECV_ERR
            self.__debug_msg('_recv error')
            self.close()
            return None
        return r_buffer