comparison src/nabble/utils/luan/Http.luan @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
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 set_metatable = Luan.set_metatable or error()
7 local type = Luan.type or error()
8 local Io = require "luan:Io.luan"
9 local Html = require "luan:Html.luan"
10 local url_encode = Html.url_encode or error()
11 local Table = require "luan:Table.luan"
12 local clear = Table.clear or error()
13 local Package = require "luan:Package.luan"
14 local String = require "luan:String.luan"
15 local matches = String.matches or error()
16 local Boot = require "luan:Boot.luan"
17 local HttpServicer = require "java:nabble.utils.luan.HttpServicer"
18 local IoLuan = require "java:luan.modules.IoLuan"
19
20
21 local Http = {}
22
23 Http.version = "jetty"
24
25 local function sent_error(_,_,_)
26 error "headers are not accessible after you start writing content"
27 end
28
29 local sent_error_metatable = { __index=sent_error, __new_index=sent_error }
30
31 function Http.sent_headers(headers)
32 clear(headers)
33 set_metatable(headers,sent_error_metatable)
34 end
35
36
37 local function new_common(this)
38 this = this or {}
39 this.headers = {}
40 return this
41 end
42
43 local function to_list(input)
44 return type(input) == "table" and input or {input}
45 end
46
47
48 function Http.new_request(this)
49 this = new_common(this)
50 this.method = "GET" -- default
51 -- this.path
52 -- this.protocol
53 this.scheme = "http" -- default
54 this.port = 80 -- default
55 this.parameters = {}
56 this.cookies = {}
57
58 function this.query_string()
59 local string_uri = Io.uri "string:"
60 local out = string_uri.text_writer()
61 local and_char = ""
62 for name, values in pairs(this.parameters) do
63 for _, value in ipairs(to_list(values)) do
64 out.write( and_char, url_encode(name), "=", url_encode(value) )
65 and_char = "&"
66 end
67 end
68 out.close()
69 local s = string_uri.read_text()
70 return s ~= "" and s or nil
71 end
72
73 function this.full_path() -- compatible with impl
74 local path = this.path
75 if this.method ~= "POST" then
76 local query = this.query_string()
77 if query ~= nil then
78 path = path.."?"..query
79 end
80 end
81 return path
82 end
83
84 function this.url()
85 return this.scheme.."://"..this.headers["host"]..this.full_path()
86 end
87
88 return this
89 end
90
91 local STATUS = {
92 OK = 200
93 MOVED_PERMANENTLY = 301
94 -- add more as needed
95 }
96 Http.STATUS = STATUS
97
98 function Http.new_response(this)
99 this = new_common(this)
100 this.status = STATUS.OK
101 if this.java ~= nil then
102 this.send_redirect = this.java.sendRedirect
103 this.send_error = this.java.sendError
104
105 function this.set_cookie(name,value,attributes)
106 attributes = attributes or {}
107 attributes["Path"] = attributes["Path"] or "/"
108 HttpServicer.setCookie(Http.request.java,this.java,name,value,attributes)
109 end
110
111 function this.set_persistent_cookie(name,value,attributes)
112 attributes = attributes or {}
113 attributes["Max-Age"] = "10000000"
114 this.set_cookie(name,value,attributes)
115 end
116
117 function this.remove_cookie(name,attributes)
118 attributes = attributes or {}
119 attributes["Max-Age"] = "0"
120 this.set_cookie(name,"delete",attributes)
121 end
122
123 function this.set()
124 HttpServicer.setResponse(this,this.java)
125 Http.sent_headers(this.headers)
126 end
127
128 function this.text_writer()
129 this.java.setCharacterEncoding "UTF-8"
130 this.java.setContentType "text/html; charset=UTF-8"
131 this.set()
132 return Boot.text_writer(this.java.getWriter())
133 end
134
135 function this.binary_writer()
136 this.set()
137 return IoLuan.binaryWriter(this.java.getOutputStream())
138 end
139
140 function this.reset()
141 this.java.reset()
142 set_metatable(this.headers,nil)
143 end
144 end
145 return this
146 end
147
148 -- request = new_request{} -- filled in by HttpServicer
149 -- response = new_response{} -- filled in by HttpServicer
150
151
152 function Http.uncache_site()
153 for k in pairs(Table.copy(Package.loaded)) do
154 if matches(k,"^site:") then
155 Package.loaded[k] = nil
156 end
157 end
158 end
159
160 return Http