Mercurial Hosting > luan
diff src/luan/modules/http/impl/Http.luan @ 1163:fef8f0742da9
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 06 Feb 2018 22:04:47 -0700 |
parents | e2d2354807f3 |
children | 1f9d34a6f308 |
line wrap: on
line diff
--- a/src/luan/modules/http/impl/Http.luan Tue Feb 06 21:25:55 2018 -0700 +++ b/src/luan/modules/http/impl/Http.luan Tue Feb 06 22:04:47 2018 -0700 @@ -1,6 +1,7 @@ java() local Luan = require "luan:Luan.luan" local error = Luan.error +local assert_string = Luan.assert_string or error() local ipairs = Luan.ipairs or error() local pairs = Luan.pairs or error() local type = Luan.type or error() @@ -15,8 +16,11 @@ local HttpServicer = require "java:luan.modules.http.impl.HttpServicer" local IoLuan = require "java:luan.modules.IoLuan" local LuanJava = require "java:luan.Luan" +local Response = require "java:luan.webserver.Response" local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream" +local Status = require "java:luan.webserver.Status" local OutputStreamWriter = require "java:java.io.OutputStreamWriter" +local HashMap = require "java:java.util.HashMap" local Http = {} @@ -60,22 +64,27 @@ } Http.STATUS = STATUS -function Http.new_response(java) - java or error() +function Http.new_response() local this = {} Http.response = this - this.java = java - this.headers = {} + function this.reset() + this.java = Response.new() + this.headers = {} + this.status = STATUS.OK + this.writer = nil + end - this.status = STATUS.OK + this.reset() function this.send_redirect(location) + this.reset() this.status = STATUS.FOUND this.headers["location"] = location end function this.send_error(status,msg) + this.reset() this.status = status if msg ~= nil then this.headers["content-type"] = "text/plain" @@ -85,7 +94,14 @@ end function this.set_cookie(name,value,attributes) - HttpServicer.setCookie(this.java,name,value,attributes) + attributes = attributes or {} + local attrMap = HashMap.new() + for attr_name, attr_value in pairs(attributes) do + assert_string(attr_name) + assert_string(attr_value) + attrMap.put(attr_name,attr_value) + end + this.java.setCookie(name,value,attrMap) end function this.set_persistent_cookie(name,value,attributes) @@ -101,12 +117,14 @@ end function this.text_writer() + this.writer and "writer already set" this.writer = ResponseOutputStream.new(this.java) this.writer = OutputStreamWriter.new(this.writer) return IoLuan.textWriter(this.writer) end function this.binary_writer() + this.writer and "writer already set" this.writer = ResponseOutputStream.new(this.java) return IoLuan.binaryWriter(this.writer) end @@ -114,6 +132,19 @@ return this end +function Http.finish() -- called only from java + local response = Http.response or error() + local java = response.java or error() + java.status = Status.getStatus(response.status) + for name, value in pairs(response.headers) do + assert_string(name) + value = LuanJava.toJava(value) + java.headers.put(name,value) + end + response.writer and response.writer.close() + return java +end + function Http.uncache_site() for k in pairs(Table.copy(Package.loaded)) do