示例#1
0
def input_stream(req):
    """Stream request handler"""
    i = 0

    # chunk must be read with extra method, uwsgi has own
    chunk = uwsgi.chunked_read() if uwsgi else req.read_chunk()
    while chunk:
        log.info("chunk: %s", chunk)
        if chunk != b'%d' % i:
            raise HTTPException(state.HTTP_BAD_REQUEST)

        chunk = uwsgi.chunked_read() if uwsgi else req.read_chunk()
        i += 1
    return EmptyResponse(state.HTTP_OK)
示例#2
0
    def handle(self) -> Union[Response, tuple]:
        """
        Handle a FastRPC request, returns Flask response
        """
        accept_cts = self._get_accepted_content_types()

        if not accept_cts.intersection(self.allowed_content_types):
            logging.warning('No supported content type requested: "%s"', accept_cts)
            return 'Content types in Accept not supported', 400

        if request.headers['Content-Type'] not in self.allowed_content_types:
            logging.warning('Content-Type "%s" is not supported', request.headers['Content-Type'])
            return 'Content-Type not supported', 400

        if fastrpc:
            if FRPC_CONTENT_TYPE == request.headers['Content-Type']:
                # We will be loading binary data, which is sent through chunked transfer encoding - not very friendly.
                # Werkzeug doesn't recognize the header, so the data should be in request.stream BUT
                # since chunked transfer encoding isn't sending Content-Length header, as it does not make sense,
                # werkzeug needs some kind of middleware that handles it. In ideal world, we could use stream
                # because the middleware would set request.environ['wsgi.input_terminated'] - I found none that do that
                # which means for now we'll be supporting just uwsgi until I figure out how to do with with the others
                # like gunicorn etc. big TODO !
                if uwsgi is None:
                    raise NotImplementedError("This application needs to be running on uWSGI, I'm sorry! TODO :) ")
                request_data = uwsgi.chunked_read()
            else:
                request_data = request.data
            args, method_name = fastrpc.loads(request_data)
        else:
            args, method_name = xmlrpc.loads(request.data)

        logging.info('Calling method %s with args: %s', method_name, args)

        return self._create_response(method_name, args, accept_cts)
示例#3
0
文件: flask.py 项目: seznam/fastrpc
    def handle(self) -> Union[Response, tuple]:
        """
        Handle a FastRPC request, returns Flask response
        """
        accept_cts = self._get_accepted_content_types()

        if not accept_cts.intersection(self.allowed_content_types):
            logging.warning('No supported content type requested: "%s"', accept_cts)
            return 'Content types in Accept not supported', 400

        if request.headers['Content-Type'] not in self.allowed_content_types:
            logging.warning('Content-Type "%s" is not supported', request.headers['Content-Type'])
            return 'Content-Type not supported', 400

        if fastrpc:
            if FRPC_CONTENT_TYPE == request.headers['Content-Type']:
                # We will be loading binary data, which is sent through chunked transfer encoding - not very friendly.
                # Werkzeug doesn't recognize the header, so the data should be in request.stream BUT
                # since chunked transfer encoding isn't sending Content-Length header, as it does not make sense,
                # werkzeug needs some kind of middleware that handles it. In ideal world, we could use stream
                # because the middleware would set request.environ['wsgi.input_terminated'] - I found none that do that
                # which means for now we'll be supporting just uwsgi until I figure out how to do with with the others
                # like gunicorn etc. big TODO !
                if uwsgi is None:
                    raise NotImplementedError("This application needs to be running on uWSGI, I'm sorry! TODO :) ")
                request_data = uwsgi.chunked_read()
            else:
                request_data = request.data
            args, method_name = fastrpc.loads(request_data)
        else:
            args, method_name = xmlrpc.loads(request.data)

        logging.info('Calling method %s with args: %s', method_name, args)

        return self._create_response(method_name, args, accept_cts)
示例#4
0
 def __call__(self, environ, start_response):
     if environ.get('HTTP_TRANSFER_ENCODING', '0') == 'chunked':
         del environ['HTTP_TRANSFER_ENCODING']
         uwsgi_way = True
         try:
             import uwsgi
         except:
             uwsgi_way = False
         if uwsgi_way:
             body = uwsgi.chunked_read()
             if body:
                 content_length = len(body)
         else:
             input = environ.get('wsgi.input')
             if input:
                 body = ''
                 size = int(input.readline(), 16)
                 while size > 0:
                     body += input.read(size + 2)
                     size = int(input.readline(), 16)
                 content_length = len(body) - 2 if body else 0
         if body:
             environ['body_copy'] = body
             environ["HTTP_CONTENT_LENGTH"] = content_length
             environ["CONTENT_LENGTH"] = content_length
             environ['wsgi.input'] = StringIO(body)
     app_iter = self.application(environ,
                                 self._sr_callback(start_response))
     return app_iter
示例#5
0
def test_json(req):
    """Test GET / POST json"""
    # numbers are complete list
    data = req.json
    if req.is_chunked_request:
        raw = b''
        # chunk must be read with extra method, uwsgi has own
        chunk = uwsgi.chunked_read() if uwsgi else req.read_chunk()
        while chunk:
            raw += chunk
            chunk = uwsgi.chunked_read() if uwsgi else req.read_chunk()
        data = parse_json_request(raw, req.charset)

    return JSONResponse(status_code=418,
                        message="I'm teapot :-)",
                        numbers=list(range(5)),
                        request=data)
示例#6
0
 def read_chunked(fp):
     data = []
     while True:
         chunk = uwsgi.chunked_read()
         if chunk == '':
             break
         data.append(chunk)
     return ''.join(data)
示例#7
0
 def read_chunked(fp):
     data = []
     while True:
         chunk = uwsgi.chunked_read()
         if chunk == '':
             break
         data.append(chunk)
     return ''.join(data)
示例#8
0
文件: proxy.py 项目: Kayle009/sentry
        def readinto(self, buf):
            if not self._internal_buffer:
                self._internal_buffer = uwsgi.chunked_read()

            n = min(len(buf), len(self._internal_buffer))
            if n > 0:
                buf[:n] = self._internal_buffer[:n]
                self._internal_buffer = self._internal_buffer[n:]

            return n
示例#9
0
        def readinto(self, buf):
            if not self._internal_buffer:
                self._internal_buffer = uwsgi.chunked_read()

            n = min(len(buf), len(self._internal_buffer))
            if n > 0:
                buf[:n] = self._internal_buffer[:n]
                self._internal_buffer = self._internal_buffer[n:]

            return n
示例#10
0
def try_uwsgi_body_gen():
    try:
        import uwsgi
    except Exception:
        return
    while True:
        chunk = uwsgi.chunked_read()
        _dbg("chunked_read -> {!r}".format(chunk))
        if chunk is None or chunk == b'' or chunk == '':
            return
        yield chunk
示例#11
0
 def _write_get_lines():
     encoding = pecan.request.headers.get('Transfer-Encoding', "").lower()
     if encoding == "chunked":
         if uwsgi is None:
             api.abort(
                 501, {
                     "cause": "Not implemented error",
                     "reason": "This server is not running with uwsgi"
                 })
         return encoding, uwsgi.chunked_read()
     return None, pecan.request.body
示例#12
0
 def _write_get_lines():
     encoding = pecan.request.headers.get('Transfer-Encoding', "").lower()
     if encoding == "chunked":
         # TODO(sileht): Support reading chunk without uwsgi when
         # pecan.request.environ['wsgi.input_terminated'] is set.
         # https://github.com/unbit/uwsgi/issues/1428
         if uwsgi is None:
             api.abort(
                 501, {"cause": "Not implemented error",
                       "reason": "This server is not running with uwsgi"})
         return encoding, uwsgi.chunked_read()
     return None, pecan.request.body
示例#13
0
 def _write_get_lines():
     encoding = pecan.request.headers.get('Transfer-Encoding', "").lower()
     if encoding == "chunked":
         # TODO(sileht): Support reading chunk without uwsgi when
         # pecan.request.environ['wsgi.input_terminated'] is set.
         # https://github.com/unbit/uwsgi/issues/1428
         if uwsgi is None:
             api.abort(
                 501, {
                     "cause": "Not implemented error",
                     "reason": "This server is not running with uwsgi"
                 })
         return encoding, uwsgi.chunked_read()
     return None, pecan.request.body
示例#14
0
    def _chunked_read(self):
        size = 0
        try:
            chunk = uwsgi.chunked_read()
            old_pos = self._body.pos
            self._body.write(chunk)
            self._body.pos = old_pos
            size = len(chunk)

            if not size:
                self._body.pos = 0

        except IOError as e:
            raise IOError(
                "Error reading chunk, is --http-raw-body enabled? Error: {}".
                format(e))

        return size
示例#15
0
def chunked_reader():
    try:
        # If we're running under uWSGI, use the uwsgi.chunked_read method
        # to read chunked input.
        import uwsgi  # noqa

        while True:
            chunk = uwsgi.chunked_read()
            if len(chunk) > 0:
                yield chunk
            else:
                return
    except ImportError:
        # Otherwise try to read the wsgi input. This works in embedded Apache.
        stream = flask.request.environ["wsgi.input"]
        try:
            while True:
                yield stream.next()
        except:
            return
示例#16
0
def chunked_reader():
    try:
        # If we're running under uWSGI, use the uwsgi.chunked_read method
        # to read chunked input.
        import uwsgi  # noqa

        while True:
            chunk = uwsgi.chunked_read()
            if len(chunk) > 0:
                yield chunk
            else:
                return
    except ImportError:
        # Otherwise try to read the wsgi input. This works in embedded Apache.
        stream = flask.request.environ["wsgi.input"]
        try:
            while True:
                yield stream.next()
        except Exception:
            return
示例#17
0
    def read(self, length=None):
        position = 0
        if length == 0:
            return b""

        if length and length < 0:
            length = None

        response = []
        while True:
            data = uwsgi.chunked_read()
            # Return everything if we reached the end of the file
            if not data:
                break
            response.append(data)
            # Return the data if we've reached the length
            if length is not None:
                position += len(data)
                if position >= length:
                    break
        return b''.join(response)
示例#18
0
    c_origin = ffi.new('char[]', origin)
    c_proto = ffi.new('char[]', proto)
    if lib.uwsgi_websocket_handshake(wsgi_req, c_key, len(key), c_origin, len(origin), c_proto, len(proto)) < 0:
        raise IOError("unable to complete websocket handshake")
uwsgi.websocket_handshake = uwsgi_pypy_websocket_handshake

"""
uwsgi.websocket_send(msg)
"""
def uwsgi_pypy_websocket_send(msg):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    if lib.uwsgi_websocket_send(wsgi_req, ffi.new('char[]', msg), len(msg)) < 0:
        raise IOError("unable to send websocket message")
uwsgi.websocket_send = uwsgi_pypy_websocket_send

"""
uwsgi.chunked_read(timeout=0)
"""
def uwsgi_pypy_chunked_read(timeout=0):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    rlen = ffi.new("size_t*")
    chunk = lib.uwsgi_chunked_read(wsgi_req, rlen, timeout, 0)
    if chunk == ffi.NULL:
        raise IOError("unable to receive chunked part")
    return ffi.string(chunk, rlen[0])
uwsgi.chunked_read = uwsgi_pypy_chunked_read

"""
uwsgi.chunked_read_nb()
"""
def uwsgi_pypy_chunked_read_nb():
示例#19
0
def uwsgi_pypy_websocket_handshake(key, origin=''):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    if lib.uwsgi_websocket_handshake(wsgi_req, ffi.new('char[]', key), len(key), ffi.new('char[]',origin), len(origin)) < 0:
        raise IOError("unable to complete websocket handshake")
uwsgi.websocket_handshake = uwsgi_pypy_websocket_handshake

"""
uwsgi.websocket_send(msg)
"""
def uwsgi_pypy_websocket_send(msg):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    if lib.uwsgi_websocket_send(wsgi_req, ffi.new('char[]', msg), len(msg)) < 0:
        raise IOError("unable to send websocket message")
uwsgi.websocket_send = uwsgi_pypy_websocket_send

"""
uwsgi.chunked_read(timeout=0)
"""
def uwsgi_pypy_chunked_read(timeout=0):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    rlen = ffi.new("size_t*")
    chunk = lib.uwsgi_chunked_read(wsgi_req, rlen, timeout, 0)
    if chunk == ffi.NULL:
        raise IOError("unable to receive chunked part")
    return ffi.string(chunk, rlen[0])
uwsgi.chunked_read = uwsgi_pypy_chunked_read

"""
uwsgi.chunked_read_nb()
"""
def uwsgi_pypy_chunked_read_nb():