nitcorn: render the response in a template to avoid a copy&buffer
authorJean Privat <jean@pryen.org>
Thu, 31 May 2018 18:17:25 +0000 (14:17 -0400)
committerJean Privat <jean@pryen.org>
Thu, 31 May 2018 19:52:25 +0000 (15:52 -0400)
By the way, name the method `render` instead of reusing `to_s`

Signed-off-by: Jean Privat <jean@pryen.org>

lib/nitcorn/http_response.nit
lib/nitcorn/reactor.nit

index ec83289..b80fb7b 100644 (file)
@@ -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
 
index afa0d0a..85a2068 100644 (file)
@@ -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