Beispiel #1
0
    def decorator(*args, **kwargs):
        response = func(*args, **kwargs)
        if not isinstance(response, Response):
            response = Response(response)

        # Check if the response is appropriate for gzipping.
        if ("gzip" not in request.headers.get("Accept-Encoding", "").lower()
                or not 200 <= response.status_code < 300
                or "Content-Encoding" in response.headers):
            return response

        response.direct_passthrough = False
        # GZIP the response.
        gzip_buffer = BytesIO()
        with GzipFile(mode="wb", compresslevel=9,
                      fileobj=gzip_buffer) as gzip_file:
            gzip_file.write(response.get_data())

        response.set_data(gzip_buffer.getvalue())
        response.headers["Content-Encoding"] = "gzip"
        response.direct_passthrough = False

        vary = response.headers.get("Vary")
        if vary:
            if "accept-encoding" not in vary.lower():
                response.headers["Vary"] = "{}, Accept-Encoding".format(vary)
        else:
            response.headers["Vary"] = "Accept-Encoding"
        return response
Beispiel #2
0
def handle_response(response: FlaskResponse) -> FlaskResponse:

    response.headers["_RV"] = str(version)

    PROJECT_VERSION = get_project_configuration("project.version", default="0")
    if PROJECT_VERSION is not None:
        response.headers["Version"] = str(PROJECT_VERSION)

    data_string = get_data_from_request()

    url = obfuscate_query_parameters(request.url)

    if (GZIP_ENABLE and not response.is_streamed
            and "gzip" in request.headers.get("Accept-Encoding", "").lower()):
        response.direct_passthrough = False
        content, headers = ResponseMaker.gzip_response(
            response.data,
            response.status_code,
            response.headers.get("Content-Encoding"),
            response.headers.get("Content-Type"),
        )
        if content:
            response.data = content
            response.headers.update(headers)

    resp = str(response).replace("<Response ", "").replace(">", "")
    ip = BaseAuthentication.get_remote_ip(raise_warnings=False)

    is_healthcheck = (ip == "127.0.0.1" and request.method == "GET"
                      and url == "/api/status")
    if is_healthcheck:
        log.debug(
            "{} {} {}{} -> {} [HEALTHCHECK]",
            ip,
            request.method,
            url,
            data_string,
            resp,
        )
    else:
        log.info(
            "{} {} {}{} -> {}",
            ip,
            request.method,
            url,
            data_string,
            resp,
        )

    return response
Beispiel #3
0
    def handle_api_error(cls, e):
        http_response = Response(json.dumps(HttpResponse().response(
            code=e.code,
            message=e.message,
            description="Exception({})".format(e.exception.__str__()))),
                                 500,
                                 mimetype="application/json")
        http_response.headers[
            HeaderConstants.
            X_REQUEST_ID] = KubectlView.message_dumper.get_header(
                HeaderConstants.X_REQUEST_ID)
        response = KubectlView.fluentd.emit(
            tag="api", msg=KubectlView.message_dumper.dump(http_response))
        app.logger.debug(f"{response}")

        http_response.direct_passthrough = False
        return http_response