comparison 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
comparison
equal deleted inserted replaced
1159:3ef883468fd0 1160:4beabb087be6
1 java()
2 local Luan = require "luan:Luan.luan"
3 local error = Luan.error
4 local ipairs = Luan.ipairs or error()
5 local pairs = Luan.pairs or error()
6 local type = Luan.type or error()
7 local Io = require "luan:Io.luan"
8 local Html = require "luan:Html.luan"
9 local url_encode = Html.url_encode or error()
10 local Table = require "luan:Table.luan"
11 local clear = Table.clear or error()
12 local Package = require "luan:Package.luan"
13 local String = require "luan:String.luan"
14 local matches = String.matches or error()
15 local HttpServicer = require "java:luan.modules.http.impl.HttpServicer"
16 local IoLuan = require "java:luan.modules.IoLuan"
17 local LuanJava = require "java:luan.Luan"
18 local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream"
19 local OutputStreamWriter = require "java:java.io.OutputStreamWriter"
20
21
22 local Http = {}
23
24 function Http.new_request(java)
25 local this = {}
26 Http.request = this
27 if java == nil then
28 this.port = 80
29 this.method = "GET"
30 this.headers = {}
31 this.parameters = {}
32 this.cookies = {}
33 else
34 this.java = java
35 this.port = java.port or error()
36 this.method = java.method or error()
37 this.raw_path = java.rawPath or error()
38 this.path = java.path or error()
39 this.protocol = java.protocol or error()
40 this.headers = LuanJava.toLuan(java.headers)
41 this.parameters = LuanJava.toLuan(java.parameters)
42 this.cookies = LuanJava.toLuan(java.cookies)
43 end
44 this.scheme = "http"
45
46 function this.full_path() -- compatible with jetty
47 return this.raw_path or this.path
48 end
49
50 function this.url()
51 return this.scheme.."://"..this.headers["host"]..this.raw_path
52 end
53
54 return this
55 end
56
57 local STATUS = {
58 OK = 200
59 MOVED_PERMANENTLY = 301
60 FOUND = 302
61 -- add more as needed
62 }
63 Http.STATUS = STATUS
64
65 function Http.new_response(java)
66 java or error()
67 local this = {}
68 Http.response = this
69 this.java = java
70
71 this.headers = {}
72
73 this.status = STATUS.OK
74
75 function this.send_redirect(location)
76 this.status = STATUS.FOUND
77 this.headers["location"] = location
78 end
79
80 function this.set_cookie(name,value,attributes)
81 HttpServicer.setCookie(this.java,name,value,attributes)
82 end
83
84 function this.set_persistent_cookie(name,value,attributes)
85 attributes = attributes or {}
86 attributes["Max-Age"] = "10000000"
87 this.set_cookie(name,value,attributes)
88 end
89
90 function this.remove_cookie(name,attributes)
91 attributes = attributes or {}
92 attributes["Max-Age"] = "0"
93 this.set_cookie(name,"delete",attributes)
94 end
95
96 function this.text_writer()
97 this.writer and error "writer already set"
98 this.writer = ResponseOutputStream.new(this.java)
99 this.writer = OutputStreamWriter.new(this.writer)
100 return IoLuan.textWriter(this.writer)
101 end
102
103 function this.binary_writer()
104 this.writer and error "writer already set"
105 this.writer = ResponseOutputStream.new(this.java)
106 return IoLuan.binaryWriter(this.writer)
107 end
108
109 return this
110 end
111
112
113 function Http.uncache_site()
114 for k in pairs(Table.copy(Package.loaded)) do
115 if matches(k,"^site:") then
116 Package.loaded[k] = nil
117 end
118 end
119 end
120
121 return Http