Mercurial Hosting > luan
changeset 1163:fef8f0742da9
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 06 Feb 2018 22:04:47 -0700 |
parents | e2d2354807f3 |
children | 1f9d34a6f308 |
files | src/luan/modules/http/Implementation.luan src/luan/modules/http/impl/Http.luan src/luan/modules/http/impl/HttpServicer.java |
diffstat | 3 files changed, 42 insertions(+), 54 deletions(-) [+] |
line wrap: on
line diff
diff -r e2d2354807f3 -r fef8f0742da9 src/luan/modules/http/Implementation.luan --- a/src/luan/modules/http/Implementation.luan Tue Feb 06 21:25:55 2018 -0700 +++ b/src/luan/modules/http/Implementation.luan Tue Feb 06 22:04:47 2018 -0700 @@ -1,4 +1,4 @@ return { --- luan = "luan:http/jetty/" - luan = "luan:http/impl/" + luan = "luan:http/jetty/" +-- luan = "luan:http/impl/" }
diff -r e2d2354807f3 -r fef8f0742da9 src/luan/modules/http/impl/Http.luan --- 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
diff -r e2d2354807f3 -r fef8f0742da9 src/luan/modules/http/impl/HttpServicer.java --- a/src/luan/modules/http/impl/HttpServicer.java Tue Feb 06 21:25:55 2018 -0700 +++ b/src/luan/modules/http/impl/HttpServicer.java Tue Feb 06 22:04:47 2018 -0700 @@ -1,15 +1,9 @@ package luan.modules.http.impl; -import java.io.Closeable; -import java.io.IOException; -import java.util.Map; -import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import luan.webserver.Request; import luan.webserver.Response; -import luan.webserver.Status; -import luan.Luan; import luan.LuanState; import luan.LuanFunction; import luan.LuanException; @@ -48,51 +42,14 @@ newRequestFn.call( luan, new Object[]{request} ); // response - Response response = new Response(); LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response"); - LuanTable responseTbl = (LuanTable)newResponseFn.call( luan, new Object[]{response} ); + newResponseFn.call(luan); fn.call(luan); - response.status = Status.getStatus( Luan.asInteger(responseTbl.rawGet("status")) ); - LuanTable headersTbl = (LuanTable)responseTbl.rawGet("headers"); - if( !headersTbl.rawIsEmpty() ) { - Map headers = (Map)Luan.toJava(headersTbl); - for( Object obj : headers.entrySet() ) { - Map.Entry entry = (Map.Entry)obj; - String name = (String)entry.getKey(); - Object value = entry.getValue(); - response.headers.put(name,value); - } - } - Closeable writer = (Closeable)responseTbl.rawGet("writer"); - if( writer != null ) { - try { - ((Closeable)writer).close(); - } catch(IOException e) { - throw new RuntimeException(e); - } - } - + LuanFunction finishFn = (LuanFunction)module.rawGet("finish"); + Response response = (Response)finishFn.call(luan); return response; } - public static void setCookie(LuanState luan,Response response,String name,String value,LuanTable attributesTbl) - throws LuanException - { - Map<String,String> attributes = new HashMap<String,String>(); - if( attributesTbl != null ) { - for( Map.Entry entry : attributesTbl.iterable(luan) ) { - String key = (String)entry.getKey(); - if( !(key instanceof String) ) - throw new LuanException("cookie attribute name must be string"); - String val = (String)entry.getValue(); - if( !(val instanceof String) ) - throw new LuanException("cookie attribute value must be string"); - attributes.put(key,val); - } - } - response.setCookie(name,value,attributes); - } - }