Пример #1
0
        def _handler(tornado_request):
            try:
                headers = HttpHeaders()
                for k, v in tornado_request.headers.get_all():
                    headers.add(k, v)

                body_file = io.BytesIO(tornado_request.body)
                request = HttpRequest(proto=tornado_request.version,
                                      host=tornado_request.host,
                                      method=tornado_request.method,
                                      uri=tornado_request.uri,
                                      args=tornado_request.arguments,
                                      remote_ip=tornado_request.remote_ip,
                                      headers=headers,
                                      body=tornado_request.body,
                                      body_file=body_file)
                request._tornado_request = tornado_request

                value = yield launch(self.handle_request, request)
                code, headers, content = extract_response(value)
            except:
                code, headers, content = 500, {}, "500 Internal Server Error"
            tornado_request.write("HTTP/1.1 %s %s\r\n" %
                                  (code, responses.get(code, 'Unknown')))
            headers.setdefault('Server', 'monocle/%s' % VERSION)
            headers.setdefault('Content-Length', str(len(content)))
            for name, value in headers.iteritems():
                tornado_request.write("%s: %s\r\n" % (name, value))
            tornado_request.write("\r\n")
            tornado_request.write(content)
            tornado_request.finish()
Пример #2
0
        def _handler(tornado_request):
            try:
                headers = HttpHeaders()
                for k, v in tornado_request.headers.get_all():
                    headers.add(k, v)

                body_file = io.BytesIO(tornado_request.body)
                request = HttpRequest(proto=tornado_request.version,
                                      host=tornado_request.host,
                                      method=tornado_request.method,
                                      uri=tornado_request.uri,
                                      args=tornado_request.arguments,
                                      remote_ip=tornado_request.remote_ip,
                                      headers=headers,
                                      body=tornado_request.body,
                                      body_file=body_file)
                request._tornado_request = tornado_request

                value = yield launch(self.handle_request, request)
                code, headers, content = extract_response(value)
            except Exception:
                code, headers, content = 500, {}, "500 Internal Server Error"
            tornado_request.write("HTTP/1.1 %s %s\r\n" %
                                  (code, responses.get(code, 'Unknown')))
            headers.setdefault('Server', 'monocle/%s' % VERSION)
            headers.setdefault('Content-Length', str(len(content)))
            for name, value in headers.iteritems():
                if isinstance(name, unicode):
                    name = name.encode('ascii')
                if isinstance(value, unicode):
                    value = value.encode('ascii')
                tornado_request.write("%s: %s\r\n" % (name, value))
            tornado_request.write("\r\n")
            tornado_request.write(content)
            tornado_request.finish()
Пример #3
0
        def _handler(twisted_request):
            try:
                headers = HttpHeaders()
                for k, vs in twisted_request.requestHeaders.getAllRawHeaders():
                    for v in vs:
                        headers.add(k, v)

                # decode UTF-8 keys, matching tornado arg parsing behavior
                args = dict(
                    [(k.decode('utf-8'), v)
                     for k, v
                     in twisted_request.args.iteritems()])

                body = None
                if hasattr(twisted_request.content, "getvalue"):
                    body = twisted_request.content.getvalue()
                else:
                    twisted_request.content.seek(0, os.SEEK_END)
                    if twisted_request.content.tell() <= self.max_body_str_len:
                        twisted_request.content.seek(0, os.SEEK_SET)
                        body = twisted_request.content.read()
                    twisted_request.content.seek(0, os.SEEK_SET)

                request = HttpRequest(
                    proto=twisted_request.clientproto,
                    host=twisted_request.getRequestHostname(),
                    method=twisted_request.method,
                    uri=twisted_request.uri,
                    args=args,
                    remote_ip=twisted_request.getClientIP(),
                    headers=headers,
                    body=body,
                    body_file=twisted_request.content)
                request._twisted_request = twisted_request

                value = yield self.handler(request)
                code, headers, content = extract_response(value)
            except Exception:
                log_exception()
                code, headers, content = 500, {}, "500 Internal Server Error"
            try:
                if twisted_request._disconnected:
                    return

                twisted_request.setResponseCode(code)
                headers.setdefault('Server', 'monocle/%s' % VERSION)
                grouped_headers = {}
                for name, value in headers.iteritems():
                    if name in grouped_headers:
                        grouped_headers[name].append(value)
                    else:
                        grouped_headers[name] = [value]
                for name, value in grouped_headers.iteritems():
                    twisted_request.responseHeaders.setRawHeaders(name, value)
                twisted_request.write(content)

                # close connections with a 'close' header
                if headers.get('Connection', '').lower() == 'close':
                    twisted_request.channel.persistent = False

                twisted_request.finish()
            except Exception:
                log_exception()
                raise
Пример #4
0
        def _handler(twisted_request):
            try:
                headers = HttpHeaders()
                for k, vs in twisted_request.requestHeaders.getAllRawHeaders():
                    for v in vs:
                        headers.add(k, v)

                # decode UTF-8 keys, matching tornado arg parsing behavior
                args = dict([(k.decode('utf-8'), v)
                             for k, v in twisted_request.args.iteritems()])

                body = None
                if hasattr(twisted_request.content, "getvalue"):
                    body = twisted_request.content.getvalue()
                else:
                    twisted_request.content.seek(0, os.SEEK_END)
                    if twisted_request.content.tell() <= self.max_body_str_len:
                        twisted_request.content.seek(0, os.SEEK_SET)
                        body = twisted_request.content.read()
                    twisted_request.content.seek(0, os.SEEK_SET)

                request = HttpRequest(
                    proto=twisted_request.clientproto,
                    host=twisted_request.getRequestHostname(),
                    method=twisted_request.method,
                    uri=twisted_request.uri,
                    args=args,
                    remote_ip=twisted_request.getClientIP(),
                    headers=headers,
                    body=body,
                    body_file=twisted_request.content)
                request._twisted_request = twisted_request

                value = yield self.handler(request)
                code, headers, content = extract_response(value)
            except Exception:
                log_exception()
                code, headers, content = 500, {}, "500 Internal Server Error"
            try:
                if twisted_request._disconnected:
                    return

                twisted_request.setResponseCode(code)
                headers.setdefault('Server', 'monocle/%s' % VERSION)
                grouped_headers = {}
                for name, value in headers.iteritems():
                    if name in grouped_headers:
                        grouped_headers[name].append(value)
                    else:
                        grouped_headers[name] = [value]
                for name, value in grouped_headers.iteritems():
                    if isinstance(name, unicode):
                        name = name.encode('ascii')
                    if isinstance(value, unicode):
                        value = value.encode('ascii')
                    twisted_request.responseHeaders.setRawHeaders(name, value)
                twisted_request.write(content)

                # close connections with a 'close' header
                if headers.get('Connection', '').lower() == 'close':
                    twisted_request.channel.persistent = False

                twisted_request.finish()
            except Exception:
                log_exception()
                raise