comparison src/luan/modules/http/Http.luan @ 1158:267fdf5e9fbd

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