Mercurial Hosting > luan
comparison src/luan/modules/http/Http.luan @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | http/src/luan/modules/http/Http.luan@d7371dc8c2e7 |
children | 022ff91bde60 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
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 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.HttpServicer" | |
16 local IoLuan = require "java:luan.modules.IoLuan" | |
17 | |
18 local M = {} | |
19 | |
20 local singular_metatable = {} | |
21 | |
22 function singular_metatable.__index(table,key) | |
23 local list = table.__plural[key] | |
24 return list and list[1] | |
25 end | |
26 | |
27 function singular_metatable.__new_index(table,key,value) | |
28 table.__plural[key] = value and {value} | |
29 end | |
30 | |
31 function singular_metatable.__pairs(table) | |
32 local iter = pairs(table.__plural) | |
33 return function() | |
34 local key, value = iter() | |
35 return key, value and value[1] | |
36 end | |
37 end | |
38 | |
39 local function sent_error() | |
40 error "headers are not accessible after you start writing content" | |
41 end | |
42 | |
43 local sent_error_metatable = { __index=sent_error, __new_index=sent_error } | |
44 | |
45 function M.sent_headers(headers) | |
46 clear(headers) | |
47 set_metatable(headers,sent_error_metatable) | |
48 end | |
49 | |
50 | |
51 local function new_common(this) | |
52 this = this or {} | |
53 this.headers = {} | |
54 this.header = {__plural=this.headers} | |
55 set_metatable(this.header,singular_metatable) | |
56 return this | |
57 end | |
58 | |
59 | |
60 function M.new_request(this) | |
61 this = new_common(this) | |
62 this.method = "GET" -- default | |
63 -- this.path | |
64 -- this.protocol | |
65 this.scheme = "http" -- default | |
66 this.port = 80 -- default | |
67 this.parameters = {} | |
68 this.parameter = {__plural=this.parameters} | |
69 set_metatable(this.parameter,singular_metatable) | |
70 this.cookie = {} | |
71 | |
72 function this.query_string() | |
73 local string_uri = Io.uri "string:" | |
74 local out = string_uri.text_writer() | |
75 local and_char = "" | |
76 for name, values in pairs(this.parameters) do | |
77 for _, value in ipairs(values) do | |
78 out.write( and_char, url_encode(name), "=", url_encode(value) ) | |
79 and_char = "&" | |
80 end | |
81 end | |
82 out.close() | |
83 local s = string_uri.read_text() | |
84 return s ~= "" and s or nil | |
85 end | |
86 | |
87 function this.url() | |
88 local url = this.scheme.."://"..this.header.host..this.path | |
89 if this.method ~= "POST" then | |
90 local query = this.query_string() | |
91 if query ~= nil then | |
92 url = url.."?"..query | |
93 end | |
94 end | |
95 return url | |
96 end | |
97 | |
98 return this | |
99 end | |
100 | |
101 local STATUS = { | |
102 OK = 200; | |
103 -- add more as needed | |
104 } | |
105 M.STATUS = STATUS | |
106 | |
107 function M.new_response(this) | |
108 this = new_common(this) | |
109 this.status = STATUS.OK | |
110 if this.java ~= nil then | |
111 this.send_redirect = this.java.sendRedirect | |
112 this.send_error = this.java.sendError | |
113 | |
114 function this.set_cookie(name,value,is_persistent,domain) | |
115 HttpServicer.setCookie(M.request.java,this.java,name,value,is_persistent,domain) | |
116 end | |
117 | |
118 function this.remove_cookie(name,domain) | |
119 HttpServicer.removeCookie(M.request.java,this.java,name,domain) | |
120 end | |
121 | |
122 function this.set() | |
123 HttpServicer.setResponse(this,this.java) | |
124 M.sent_headers(this.headers) | |
125 end | |
126 | |
127 function this.text_writer() | |
128 this.set() | |
129 return IoLuan.textWriter(this.java.getWriter()) | |
130 end | |
131 | |
132 function this.binary_writer() | |
133 this.set() | |
134 return IoLuan.binaryWriter(this.java.getOutputStream()) | |
135 end | |
136 | |
137 function this.reset() | |
138 this.java.reset() | |
139 set_metatable(this.headers,nil) | |
140 end | |
141 end | |
142 return this | |
143 end | |
144 | |
145 -- request = new_request{} -- filled in by HttpServicer | |
146 -- response = new_response{} -- filled in by HttpServicer | |
147 | |
148 | |
149 M.per_session_pages = {} | |
150 | |
151 function M.per_session(page) | |
152 M.per_session_pages[page] = true | |
153 end | |
154 | |
155 function M.clear_session() | |
156 M.request.java.getSession().removeAttribute("luan") | |
157 end | |
158 | |
159 | |
160 function M.uncache_site() | |
161 for k in pairs(Table.copy(Package.loaded)) do | |
162 if matches(k,"^site:") then | |
163 Package.loaded[k] = nil | |
164 end | |
165 end | |
166 end | |
167 | |
168 M.run_later = HttpServicer.run_later | |
169 | |
170 return M |