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