Пример #1
0
    def __call__(self, request):
        data = {}
        response = []
        def start_response(status, response_headers, exc_info=None):
            data["status"] = status
            data["headers"] = response_headers
            return response.append
        app_response = self.wsgi_application(
            WSGIContainer.environ(request), start_response)
        response.extend(app_response)
        body = b("").join(response)
        if hasattr(app_response, "close"):
            app_response.close()
        if not data: raise Exception("WSGI app did not call start_response")

        status_code = int(data["status"].split()[0])
        headers = data["headers"]
        header_set = set(k.lower() for (k,v) in headers)
        body = escape.utf8(body)
        if "content-length" not in header_set:
            headers.append(("Content-Length", str(len(body))))
        if "content-type" not in header_set:
            headers.append(("Content-Type", "text/html; charset=UTF-8"))
        if "server" not in header_set:
            headers.append(("Server", "TornadoServer/%s" % tornado.version))

        parts = [escape.utf8("HTTP/1.1 " + data["status"] + "\r\n")]
        for key, value in headers:
            parts.append(escape.utf8(key) + b(": ") + escape.utf8(value) + b("\r\n"))
        parts.append(b("\r\n"))
        parts.append(body)
        request.write(b("").join(parts))
        request.finish()
        self._log(status_code, request)
Пример #2
0
def deferred_result_available(result, key, ret, request):
    logger.info('got result for key {key} of {result}', key=key, result=result)
    ret[key] = result
    ret = resolve_all_functions(ret, request)
    if twserver.NOT_DONE_YET != ret:
        # ok, finally done, let's return it
        logger.info('writing HTTP result of {ret}', ret=ret)
        request.write(json.dumps(ret).encode())
        request.finish()
    return
Пример #3
0
 def _process_request(self, request: RequestInfo,
                      context: ProxyTestContext):
     """ Make a GET request to the URL, optionally using a proxy URL."""
     request.start()
     try:
         result = self._get_result(config=request.config,
                                   timeout=context.timeout)
     except Exception as e:
         request.finish(error=e)
     else:
         request.finish(result=result)
Пример #4
0
    def __call__(self, request):
        data = {}
        def start_response(status, response_headers):
            data["status"] = status
            data["headers"] = HTTPHeaders(response_headers)
        body = "".join(self.wsgi_application(
            WSGIContainer.environ(request), start_response))
        if not data: raise Exception("WSGI app did not call start_response")

        status_code = int(data["status"].split()[0])
        headers = data["headers"]
        body = escape.utf8(body)
        headers["Content-Length"] = str(len(body))
        headers.setdefault("Content-Type", "text/html; charset=UTF-8")
        headers.setdefault("Server", "TornadoServer/0.1")

        parts = ["HTTP/1.1 " + data["status"] + "\r\n"]
        for key, value in headers.items():
            parts.append(escape.utf8(key) + ": " + escape.utf8(value) + "\r\n")
        parts.append("\r\n")
        parts.append(body)
        request.write("".join(parts))
        request.finish()
        self._log(status_code, request)
Пример #5
0
def deferred_result_error(failure, request):
    logger.failure("Logging an uncaught exception", failure=failure)
    request.setResponseCode(400)
    request.write(json.dumps({'error': 'internal error'}).encode())
    request.finish()
    return