From cce74f261b4b79360bfc70d062b1e4d01f8aef82 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 31 May 2018 14:17:25 -0400 Subject: [PATCH] nitcorn: render the response in a template to avoid a copy&buffer By the way, name the method `render` instead of reusing `to_s` Signed-off-by: Jean Privat --- lib/nitcorn/http_response.nit | 14 ++++++++------ lib/nitcorn/reactor.nit | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/nitcorn/http_response.nit b/lib/nitcorn/http_response.nit index ec83289..b80fb7b 100644 --- a/lib/nitcorn/http_response.nit +++ b/lib/nitcorn/http_response.nit @@ -20,6 +20,7 @@ module http_response import serialization +private import template # A response to send over HTTP class HttpResponse @@ -77,17 +78,18 @@ class HttpResponse end # Get this reponse as a string according to HTTP protocol - redef fun to_s + fun render: Writable do finalize - var buf = new FlatBuffer - buf.append("{http_version} {status_code} {status_message or else ""}\r\n") + var buf = new Template + buf.add("{http_version} {status_code} {status_message or else ""}\r\n") for key, value in header do - buf.append("{key}: {value}\r\n") + buf.add("{key}: {value}\r\n") end - buf.append("\r\n{body}") - return buf.to_s + buf.add("\r\n") + buf.add body + return buf end end diff --git a/lib/nitcorn/reactor.nit b/lib/nitcorn/reactor.nit index afa0d0a..85a2068 100644 --- a/lib/nitcorn/reactor.nit +++ b/lib/nitcorn/reactor.nit @@ -91,7 +91,7 @@ class HttpServer # Send back `response` to the client fun respond(response: HttpResponse) do - write response.to_s + response.render.write_to(self) for path in response.files do write_file path end end -- 1.7.9.5