From 0a897305bc5c005779c534794a3d94deb120f5a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sat, 19 Sep 2015 09:03:56 -0400 Subject: [PATCH] lib/nitcorn: intro `HttpResponse::files` to append files MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/nitcorn/http_response.nit | 26 +++++++++++++++++++++++++- lib/nitcorn/reactor.nit | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/nitcorn/http_response.nit b/lib/nitcorn/http_response.nit index 523c051..1690864 100644 --- a/lib/nitcorn/http_response.nit +++ b/lib/nitcorn/http_response.nit @@ -37,12 +37,36 @@ class HttpResponse # Body of this response var body = "" is writable + # Files appended after `body` + var files = new Array[String] + # Finalize this response before sending it over HTTP fun finalize do # Set the content length if not already set if not header.keys.has("Content-Length") then - header["Content-Length"] = body.bytelen.to_s + # Size of the body + var len = body.bytelen + + # Size of included files + for path in files do + # TODO handle these error cases elsewhere, an error here will result in an invalid response + if not path.file_exists then + print_error "File does not exists at '{path}'" + continue + end + + var stat = path.file_stat + if stat == null then + print_error "Failed to stat file at '{path}'" + continue + end + + len += stat.size + end + + # Set header + header["Content-Length"] = len.to_s end # Set server ID diff --git a/lib/nitcorn/reactor.nit b/lib/nitcorn/reactor.nit index 8124f97..4b13677 100644 --- a/lib/nitcorn/reactor.nit +++ b/lib/nitcorn/reactor.nit @@ -85,6 +85,8 @@ class HttpServer # Send back a response write response.to_s + for path in response.files do write_file path + close end end -- 1.7.9.5