From 1ba7ac28677ab13abd6616526434e1373d3de55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Abdelkader=20Mart=C3=ADnez=20P=C3=A9rez?= Date: Thu, 4 Apr 2019 08:50:20 +0200 Subject: [PATCH] Content-Type guessing --- .../pdfeditor/{pdfeditor.html => index.html} | 0 kapow.py | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) rename examples/pdfeditor/{pdfeditor.html => index.html} (100%) diff --git a/examples/pdfeditor/pdfeditor.html b/examples/pdfeditor/index.html similarity index 100% rename from examples/pdfeditor/pdfeditor.html rename to examples/pdfeditor/index.html diff --git a/kapow.py b/kapow.py index 0435766..f54b793 100644 --- a/kapow.py +++ b/kapow.py @@ -291,10 +291,20 @@ async def response_from_context(context): await context["stream"].write_eof() return context["stream"] else: - return web.Response( - body=context["response_body"].getbuffer(), - status=context["response_status"], - headers=context["response_headers"]) + body = context["response_body"].getvalue() + status = context["response_status"] + headers = context["response_headers"] + + # Content-Type guessing (for demo only) + if "Content-Type" not in headers: + try: + body = body.decode("utf-8") + except UnicodeDecodeError: + pass + else: + headers["Content-Type"] = "text/plain" + + return web.Response(body=body, status=status, headers=headers) def generate_endpoint(code):