Mercurial Hosting > luan
comparison src/luan/modules/http/Http.luan @ 1088:bae2d0c2576c
change module naming convention
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 26 Dec 2016 22:29:36 -0700 |
parents | ece3567d4df5 |
children | b1c3baf3aa46 |
comparison
equal
deleted
inserted
replaced
1087:4aab4dd3ac9c | 1088:bae2d0c2576c |
---|---|
13 local String = require "luan:String.luan" | 13 local String = require "luan:String.luan" |
14 local matches = String.matches or error() | 14 local matches = String.matches or error() |
15 local HttpServicer = require "java:luan.modules.http.HttpServicer" | 15 local HttpServicer = require "java:luan.modules.http.HttpServicer" |
16 local IoLuan = require "java:luan.modules.IoLuan" | 16 local IoLuan = require "java:luan.modules.IoLuan" |
17 | 17 |
18 local M = {} | 18 local Http = {} |
19 | 19 |
20 local singular_metatable = {} | 20 local singular_metatable = {} |
21 | 21 |
22 function singular_metatable.__index(table,key) | 22 function singular_metatable.__index(table,key) |
23 local list = table.__plural[key] | 23 local list = table.__plural[key] |
40 error "headers are not accessible after you start writing content" | 40 error "headers are not accessible after you start writing content" |
41 end | 41 end |
42 | 42 |
43 local sent_error_metatable = { __index=sent_error, __new_index=sent_error } | 43 local sent_error_metatable = { __index=sent_error, __new_index=sent_error } |
44 | 44 |
45 function M.sent_headers(headers) | 45 function Http.sent_headers(headers) |
46 clear(headers) | 46 clear(headers) |
47 set_metatable(headers,sent_error_metatable) | 47 set_metatable(headers,sent_error_metatable) |
48 end | 48 end |
49 | 49 |
50 | 50 |
55 set_metatable(this.header,singular_metatable) | 55 set_metatable(this.header,singular_metatable) |
56 return this | 56 return this |
57 end | 57 end |
58 | 58 |
59 | 59 |
60 function M.new_request(this) | 60 function Http.new_request(this) |
61 this = new_common(this) | 61 this = new_common(this) |
62 this.method = "GET" -- default | 62 this.method = "GET" -- default |
63 -- this.path | 63 -- this.path |
64 -- this.protocol | 64 -- this.protocol |
65 this.scheme = "http" -- default | 65 this.scheme = "http" -- default |
100 | 100 |
101 local STATUS = { | 101 local STATUS = { |
102 OK = 200; | 102 OK = 200; |
103 -- add more as needed | 103 -- add more as needed |
104 } | 104 } |
105 M.STATUS = STATUS | 105 Http.STATUS = STATUS |
106 | 106 |
107 function M.new_response(this) | 107 function Http.new_response(this) |
108 this = new_common(this) | 108 this = new_common(this) |
109 this.status = STATUS.OK | 109 this.status = STATUS.OK |
110 if this.java ~= nil then | 110 if this.java ~= nil then |
111 this.send_redirect = this.java.sendRedirect | 111 this.send_redirect = this.java.sendRedirect |
112 this.send_error = this.java.sendError | 112 this.send_error = this.java.sendError |
113 | 113 |
114 function this.set_cookie(name,value,is_persistent,domain) | 114 function this.set_cookie(name,value,is_persistent,domain) |
115 HttpServicer.setCookie(M.request.java,this.java,name,value,is_persistent,domain) | 115 HttpServicer.setCookie(Http.request.java,this.java,name,value,is_persistent,domain) |
116 end | 116 end |
117 | 117 |
118 function this.remove_cookie(name,domain) | 118 function this.remove_cookie(name,domain) |
119 HttpServicer.removeCookie(M.request.java,this.java,name,domain) | 119 HttpServicer.removeCookie(Http.request.java,this.java,name,domain) |
120 end | 120 end |
121 | 121 |
122 function this.set() | 122 function this.set() |
123 HttpServicer.setResponse(this,this.java) | 123 HttpServicer.setResponse(this,this.java) |
124 M.sent_headers(this.headers) | 124 Http.sent_headers(this.headers) |
125 end | 125 end |
126 | 126 |
127 function this.text_writer() | 127 function this.text_writer() |
128 this.java.setCharacterEncoding "UTF-8" | 128 this.java.setCharacterEncoding "UTF-8" |
129 this.java.setContentType "text/html; charset=UTF-8" | 129 this.java.setContentType "text/html; charset=UTF-8" |
146 | 146 |
147 -- request = new_request{} -- filled in by HttpServicer | 147 -- request = new_request{} -- filled in by HttpServicer |
148 -- response = new_response{} -- filled in by HttpServicer | 148 -- response = new_response{} -- filled in by HttpServicer |
149 | 149 |
150 | 150 |
151 M.per_session_pages = {} | 151 Http.per_session_pages = {} |
152 | 152 |
153 function M.per_session(page) | 153 function Http.per_session(page) |
154 M.per_session_pages[page] = true | 154 Http.per_session_pages[page] = true |
155 end | 155 end |
156 | 156 |
157 function M.clear_session() | 157 function Http.clear_session() |
158 M.request.java.getSession().removeAttribute("luan") | 158 Http.request.java.getSession().removeAttribute("luan") |
159 end | 159 end |
160 | 160 |
161 | 161 |
162 function M.uncache_site() | 162 function Http.uncache_site() |
163 for k in pairs(Table.copy(Package.loaded)) do | 163 for k in pairs(Table.copy(Package.loaded)) do |
164 if matches(k,"^site:") then | 164 if matches(k,"^site:") then |
165 Package.loaded[k] = nil | 165 Package.loaded[k] = nil |
166 end | 166 end |
167 end | 167 end |
168 end | 168 end |
169 | 169 |
170 M.run_later = HttpServicer.run_later | 170 Http.run_later = HttpServicer.run_later |
171 | 171 |
172 return M | 172 return Http |