From b955a6579b28fe359ddaee0aaf65b9fc6549fd09 Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Tue, 15 Dec 2015 14:03:11 +0000 Subject: [PATCH] Using the extended log file format for HTTP --- ripe/atlas/tools/renderers/http.py | 44 +++++++++++++++++++++++++++++- tests/__init__.py | 2 ++ tests/renderers/__init__.py | 2 ++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/ripe/atlas/tools/renderers/http.py b/ripe/atlas/tools/renderers/http.py index 92bea39..427dbc1 100644 --- a/ripe/atlas/tools/renderers/http.py +++ b/ripe/atlas/tools/renderers/http.py @@ -13,12 +13,54 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from ..helpers.colours import colourise + from .base import Renderer as BaseRenderer +from .base import Result class Renderer(BaseRenderer): + """ + We're abusing the Extended Log File Format here to render the result, + amending it to include a few things not originally specified in the W3C + Working Draft: http://www.w3.org/TR/WD-logfile.html Namely: + http-version, header-bytes, and body-bytes + """ RENDERS = [BaseRenderer.TYPE_HTTP] + COLOURS = { + "2": "green", + "3": "blue", + "4": "yellow", + "5": "red" + } def on_result(self, result, probes=None): - print("Not ready yet\n") + r = "#Version: 1.0\n#Date: {}\n#Fields: {}\n".format( + result.created.strftime("%Y-%m-%d %H:%M:%S"), + "cs-method cs-uri c-ip s-ip sc-status time-taken http-version " + "header-bytes body-bytes" + ) + for response in result.responses: + r += self._colourise_by_status( + "{} {} {} {} {} {} {} {} {}\n".format( + result.method, + result.uri, + response.source_address, + response.destination_address, + response.code, + response.response_time, + response.version, + response.head_size, + response.body_size + ), + response.code + ) + + return Result(r + "\n", result.probe_id) + + def _colourise_by_status(self, output, status): + try: + return colourise(output, self.COLOURS[str(status)[0]]) + except (IndexError, KeyError): + return colourise(output, "red") diff --git a/tests/__init__.py b/tests/__init__.py index 7f98f57..76bab3e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -23,6 +23,7 @@ from .helpers import TestArgumentTypeHelper from .renderers import ( TestPingRenderer, + TestHttpRenderer, TestSSLConsistency, TestAggregatePing, TestRawRenderer, @@ -37,6 +38,7 @@ TestReportCommand, TestArgumentTypeHelper, TestPingRenderer, + TestHttpRenderer, TestSSLConsistency, TestAggregatePing, TestRawRenderer, diff --git a/tests/renderers/__init__.py b/tests/renderers/__init__.py index 27d49f8..0e63ac1 100644 --- a/tests/renderers/__init__.py +++ b/tests/renderers/__init__.py @@ -14,12 +14,14 @@ # along with this program. If not, see . from .ping import TestPingRenderer +from .http import TestHttpRenderer from .aggregate_ping import TestAggregatePing from .ssl_consistency import TestSSLConsistency from .raw import TestRawRenderer __all__ = [ TestPingRenderer, + TestHttpRenderer, TestAggregatePing, TestSSLConsistency ]