Mercurial Hosting > luan
diff src/luan/modules/http/impl/Http.luan @ 1160:4beabb087be6
add http/impl
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 05 Feb 2018 22:33:59 -0700 |
parents | src/luan/modules/http/jetty/Http.luan@3ef883468fd0 |
children | 6baccd0c85a7 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/http/impl/Http.luan Mon Feb 05 22:33:59 2018 -0700 @@ -0,0 +1,121 @@ +java() +local Luan = require "luan:Luan.luan" +local error = Luan.error +local ipairs = Luan.ipairs or error() +local pairs = Luan.pairs or error() +local type = Luan.type or error() +local Io = require "luan:Io.luan" +local Html = require "luan:Html.luan" +local url_encode = Html.url_encode or error() +local Table = require "luan:Table.luan" +local clear = Table.clear or error() +local Package = require "luan:Package.luan" +local String = require "luan:String.luan" +local matches = String.matches or error() +local HttpServicer = require "java:luan.modules.http.impl.HttpServicer" +local IoLuan = require "java:luan.modules.IoLuan" +local LuanJava = require "java:luan.Luan" +local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream" +local OutputStreamWriter = require "java:java.io.OutputStreamWriter" + + +local Http = {} + +function Http.new_request(java) + local this = {} + Http.request = this + if java == nil then + this.port = 80 + this.method = "GET" + this.headers = {} + this.parameters = {} + this.cookies = {} + else + this.java = java + this.port = java.port or error() + this.method = java.method or error() + this.raw_path = java.rawPath or error() + this.path = java.path or error() + this.protocol = java.protocol or error() + this.headers = LuanJava.toLuan(java.headers) + this.parameters = LuanJava.toLuan(java.parameters) + this.cookies = LuanJava.toLuan(java.cookies) + end + this.scheme = "http" + + function this.full_path() -- compatible with jetty + return this.raw_path or this.path + end + + function this.url() + return this.scheme.."://"..this.headers["host"]..this.raw_path + end + + return this +end + +local STATUS = { + OK = 200 + MOVED_PERMANENTLY = 301 + FOUND = 302 + -- add more as needed +} +Http.STATUS = STATUS + +function Http.new_response(java) + java or error() + local this = {} + Http.response = this + this.java = java + + this.headers = {} + + this.status = STATUS.OK + + function this.send_redirect(location) + this.status = STATUS.FOUND + this.headers["location"] = location + end + + function this.set_cookie(name,value,attributes) + HttpServicer.setCookie(this.java,name,value,attributes) + end + + function this.set_persistent_cookie(name,value,attributes) + attributes = attributes or {} + attributes["Max-Age"] = "10000000" + this.set_cookie(name,value,attributes) + end + + function this.remove_cookie(name,attributes) + attributes = attributes or {} + attributes["Max-Age"] = "0" + this.set_cookie(name,"delete",attributes) + end + + function this.text_writer() + this.writer and error "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 error "writer already set" + this.writer = ResponseOutputStream.new(this.java) + return IoLuan.binaryWriter(this.writer) + end + + return this +end + + +function Http.uncache_site() + for k in pairs(Table.copy(Package.loaded)) do + if matches(k,"^site:") then + Package.loaded[k] = nil + end + end +end + +return Http