<commit_before>#!/usr/bin/env python
"""This module contains the logic for async forwarding
of requests to the app server."""


import logging

import tornado.httputil
import tornado.httpclient

from trhutil import get_request_body_if_exists


_logger = logging.getLogger("AUTHSERVER.%s" % __name__)


"""Once a request has been authenticated, the request is forwarded
to the app server as defined by this host:port combination."""
app_server = None

"""Once the auth server has verified the sender's identity the request
is forwarded to the app server. The forward to the app server does not
contain the original request's HTTP Authorization header but instead
uses the authorization method described by ```app_server_auth_method```
and the the credential's owner."""
app_server_auth_method = "DAS"


class AsyncAppServerForwarder(object):

    def __init__(self, method, uri, headers, body, owner, identifier):
        object.__init__(self)
        self._method = method
        self._uri = uri
        self._headers = headers
        self._body = body
        self._owner = owner
        self._identifier = identifier

    def forward(self, callback):

        self._callback = callback

        headers = tornado.httputil.HTTPHeaders(self._headers)
        headers["Authorization"] = "%s %s %s" % (
            app_server_auth_method,
            self._owner,
            self._identifier)

        http_request = tornado.httpclient.HTTPRequest(
            url="http://%s%s" % (app_server, self._uri),
            method=self._method,
            body=self._body,
            headers=headers,
            follow_redirects=False)

        http_client = tornado.httpclient.AsyncHTTPClient()
        http_client.fetch(
            http_request,
            self._on_forward_done)

    def _on_forward_done(self, response):
        if response.error:
            self._callback(False)
            return

        body = None
        if 0 <= response.headers.get("Content-Length", 0):
            body = response.body 
        self._callback(
            True,
            response.code,
            response.headers,
            body)<commit_msg>Found a bug in auth server's async_app_server_forwarder module when analyzing unit test results to understand why not 100% coverage<commit_after>#!/usr/bin/env python
"""This module contains the logic for async forwarding
of requests to the app server."""


import logging

import tornado.httputil
import tornado.httpclient

from trhutil import get_request_body_if_exists


_logger = logging.getLogger("AUTHSERVER.%s" % __name__)


"""Once a request has been authenticated, the request is forwarded
to the app server as defined by this host:port combination."""
app_server = None

"""Once the auth server has verified the sender's identity the request
is forwarded to the app server. The forward to the app server does not
contain the original request's HTTP Authorization header but instead
uses the authorization method described by ```app_server_auth_method```
and the the credential's owner."""
app_server_auth_method = "DAS"


class AsyncAppServerForwarder(object):

    def __init__(self, method, uri, headers, body, owner, identifier):
        object.__init__(self)
        self._method = method
        self._uri = uri
        self._headers = headers
        self._body = body
        self._owner = owner
        self._identifier = identifier

    def forward(self, callback):

        self._callback = callback

        headers = tornado.httputil.HTTPHeaders(self._headers)
        headers["Authorization"] = "%s %s %s" % (
            app_server_auth_method,
            self._owner,
            self._identifier)

        http_request = tornado.httpclient.HTTPRequest(
            url="http://%s%s" % (app_server, self._uri),
            method=self._method,
            body=self._body,
            headers=headers,
            follow_redirects=False)

        http_client = tornado.httpclient.AsyncHTTPClient()
        http_client.fetch(
            http_request,
            self._on_forward_done)

    def _on_forward_done(self, response):
        if response.error:
            self._callback(False)
            return

        body = None
        if 0 <= response.headers.get("Content-Length", -1):
            body = response.body 
        self._callback(
            True,
            response.code,
            response.headers,
            body)
{'fraction_non_alphanumeric': 0.08271017448447768, 'fraction_numerical': 0.0015862225243598459, 'mean_word_length': 2.582792207792208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4926644', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1131, 'n_words': 357}