def render(self, request):
        # For proxied requests, we have to override getClientIP to return
        # the proper IP address.
        if request.requestHeaders.hasHeader(b'x-forwarded-for'):
            client_ip = request.requestHeaders.getRawHeaders(
                b"x-forwarded-for", [b"-"])[0].split(b",")[0].strip()
            request.getClientIP = lambda: client_ip

        # Add a few properties to the request object that are used to
        # return additional information for the request_buffer debugging
        # tool
        request._zaction = {
            "metrics": [],
            "maps": [],
            "events": []
        }

        result = TwistedResource.render(self, request)

        # log any requests that generated error responses
        # this will only get us the code, but the detailed response bodies
        # will be in the /health logs.
        if request.code >= 400:
            timestamp = datetimeToLogString(reactor.seconds())
            log.error(combinedLogFormatter(timestamp, request))

        if not request.uri.startswith("/health"):
            self.site.request_buffer.store_request(request, result)

        return result
Exemple #2
0
 def taggedLogFormatter(timestamp, request):
     'Extends access log with a tag field to tie in with other (e.g. debug) logging.'
     line = http.combinedLogFormatter(timestamp, request)
     try:
         tag = request.log.tag
     except AttributeError:
         tag = '-'
     return '{} "{}"'.format(line, tag)
Exemple #3
0
    def _xforwardedforTest(self, header):
        """
        Assert that a request with the given value in its I{X-Forwarded-For}
        header is logged by L{proxiedLogFormatter} the same way it would have
        been logged by L{combinedLogFormatter} but with 172.16.1.2 as the
        client address instead of the normal value.

        @param header: An I{X-Forwarded-For} header with left-most address of
            172.16.1.2.
        """
        reactor = Clock()
        reactor.advance(1234567890)

        timestamp = http.datetimeToLogString(reactor.seconds())
        request = DummyRequestForLogTest(http.HTTPFactory())
        expected = http.combinedLogFormatter(timestamp, request).replace(
            u"1.2.3.4", u"172.16.1.2")
        request.requestHeaders.setRawHeaders(b"x-forwarded-for", [header])
        line = http.proxiedLogFormatter(timestamp, request)

        self.assertEqual(expected, line)
Exemple #4
0
    def test_nonASCII(self):
        """
        Bytes in fields of the request which are not part of ASCII are escaped
        in the result.
        """
        reactor = Clock()
        reactor.advance(1234567890)

        timestamp = http.datetimeToLogString(reactor.seconds())
        request = DummyRequestForLogTest(http.HTTPFactory())
        request.client = IPv4Address("TCP", b"evil x-forwarded-for \x80", 12345)
        request.method = b"POS\x81"
        request.protocol = b"HTTP/1.\x82"
        request.headers[b"referer"] = b"evil \x83"
        request.headers[b"user-agent"] = b"evil \x84"

        line = http.combinedLogFormatter(timestamp, request)
        self.assertEqual(
            u'"evil x-forwarded-for \\x80" - - [13/Feb/2009:23:31:30 +0000] '
            u'"POS\\x81 /dummy HTTP/1.0" 123 - "evil \\x83" "evil \\x84"',
            line)
Exemple #5
0
    def _xforwardedforTest(self, header):
        """
        Assert that a request with the given value in its I{X-Forwarded-For}
        header is logged by L{proxiedLogFormatter} the same way it would have
        been logged by L{combinedLogFormatter} but with 172.16.1.2 as the
        client address instead of the normal value.

        @param header: An I{X-Forwarded-For} header with left-most address of
            172.16.1.2.
        """
        reactor = Clock()
        reactor.advance(1234567890)

        timestamp = http.datetimeToLogString(reactor.seconds())
        request = DummyRequestForLogTest(http.HTTPFactory(reactor=reactor))
        expected = http.combinedLogFormatter(timestamp, request).replace(
            u"1.2.3.4", u"172.16.1.2")
        request.requestHeaders.setRawHeaders(b"x-forwarded-for", [header])
        line = http.proxiedLogFormatter(timestamp, request)

        self.assertEqual(expected, line)
Exemple #6
0
    def test_nonASCII(self):
        """
        Bytes in fields of the request which are not part of ASCII are escaped
        in the result.
        """
        reactor = Clock()
        reactor.advance(1234567890)

        timestamp = http.datetimeToLogString(reactor.seconds())
        request = DummyRequestForLogTest(http.HTTPFactory(reactor=reactor))
        request.client = IPv4Address("TCP", b"evil x-forwarded-for \x80", 12345)
        request.method = b"POS\x81"
        request.protocol = b"HTTP/1.\x82"
        request.requestHeaders.addRawHeader(b"referer", b"evil \x83")
        request.requestHeaders.addRawHeader(b"user-agent", b"evil \x84")

        line = http.combinedLogFormatter(timestamp, request)
        self.assertEqual(
            u'"evil x-forwarded-for \\x80" - - [13/Feb/2009:23:31:30 +0000] '
            u'"POS\\x81 /dummy HTTP/1.0" 123 - "evil \\x83" "evil \\x84"',
            line)
Exemple #7
0
 def taggedLogFormatter(timestamp, request):
     'Extends access log with a tag field to tie in with other (e.g. debug) logging.'
     line = http.combinedLogFormatter(timestamp, request)
     try: tag = request.log.tag
     except AttributeError: tag = '-'
     return '{} "{}"'.format(line, tag)