Mercurial Hosting > luan
annotate src/luan/modules/http/impl/Http.luan @ 1166:7ef40e1923b7
add back Thread.global
allow metatables to have metatables
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 08 Feb 2018 02:22:51 -0700 |
parents | 668f29bc52ea |
children | 3a0f58d09ee7 |
rev | line source |
---|---|
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
1 java() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
2 local Luan = require "luan:Luan.luan" |
508 | 3 local error = Luan.error |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
568
diff
changeset
|
4 local ipairs = Luan.ipairs or error() |
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
568
diff
changeset
|
5 local pairs = Luan.pairs or error() |
1152
21d157b153fe
change http parameters interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1150
diff
changeset
|
6 local type = Luan.type or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
7 local Io = require "luan:Io.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
8 local Html = require "luan:Html.luan" |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
568
diff
changeset
|
9 local url_encode = Html.url_encode or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
10 local Table = require "luan:Table.luan" |
580 | 11 local clear = Table.clear or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
12 local Package = require "luan:Package.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
13 local String = require "luan:String.luan" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
14 local lower = String.lower or error() |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
15 local matches = String.matches or error() |
1160 | 16 local HttpServicer = require "java:luan.modules.http.impl.HttpServicer" |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
17 local IoLuan = require "java:luan.modules.IoLuan" |
1164 | 18 local JavaLuan = require "java:luan.Luan" |
1163 | 19 local Response = require "java:luan.webserver.Response" |
1160 | 20 local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream" |
1163 | 21 local Status = require "java:luan.webserver.Status" |
1160 | 22 local OutputStreamWriter = require "java:java.io.OutputStreamWriter" |
1163 | 23 local HashMap = require "java:java.util.HashMap" |
320
fed1893821bf
remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
300
diff
changeset
|
24 |
1152
21d157b153fe
change http parameters interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1150
diff
changeset
|
25 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
26 local Http = {} |
320
fed1893821bf
remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
300
diff
changeset
|
27 |
1160 | 28 function Http.new_request(java) |
29 local this = {} | |
30 Http.request = this | |
31 if java == nil then | |
32 this.method = "GET" | |
33 this.headers = {} | |
34 this.parameters = {} | |
35 this.cookies = {} | |
36 else | |
37 this.java = java | |
38 this.method = java.method or error() | |
39 this.raw_path = java.rawPath or error() | |
40 this.path = java.path or error() | |
41 this.protocol = java.protocol or error() | |
1164 | 42 this.headers = JavaLuan.toLuan(java.headers) |
43 this.parameters = JavaLuan.toLuan(java.parameters) | |
44 this.cookies = JavaLuan.toLuan(java.cookies) | |
1160 | 45 end |
46 this.scheme = "http" | |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
47 |
1160 | 48 function this.full_path() -- compatible with jetty |
49 return this.raw_path or this.path | |
499 | 50 end |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
51 |
499 | 52 function this.url() |
1160 | 53 return this.scheme.."://"..this.headers["host"]..this.raw_path |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
54 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
55 |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
56 return this |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
57 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
58 |
503 | 59 local STATUS = { |
1109 | 60 OK = 200 |
61 MOVED_PERMANENTLY = 301 | |
1160 | 62 FOUND = 302 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
63 -- add more as needed |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
64 } |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
65 Http.STATUS = STATUS |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
66 |
1163 | 67 function Http.new_response() |
1160 | 68 local this = {} |
69 Http.response = this | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
70 |
1163 | 71 function this.reset() |
72 this.java = Response.new() | |
73 this.headers = {} | |
74 this.status = STATUS.OK | |
75 this.writer = nil | |
76 end | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
77 |
1163 | 78 this.reset() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
79 |
1160 | 80 function this.send_redirect(location) |
1163 | 81 this.reset() |
1160 | 82 this.status = STATUS.FOUND |
83 this.headers["location"] = location | |
84 end | |
85 | |
1161 | 86 function this.send_error(status,msg) |
1163 | 87 this.reset() |
1161 | 88 this.status = status |
89 if msg ~= nil then | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
90 this.headers["content-type"] = "text/plain; charset=utf-8" |
1161 | 91 local writer = this.text_writer() |
92 writer.write(msg) | |
93 end | |
94 end | |
95 | |
1160 | 96 function this.set_cookie(name,value,attributes) |
1163 | 97 attributes = attributes or {} |
98 local attrMap = HashMap.new() | |
99 for attr_name, attr_value in pairs(attributes) do | |
1164 | 100 type(attr_name)=="string" or "cookie attribute name must be string" |
101 type(attr_value)=="string" or "cookie attribute value must be string" | |
1163 | 102 attrMap.put(attr_name,attr_value) |
103 end | |
104 this.java.setCookie(name,value,attrMap) | |
1160 | 105 end |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
106 |
1160 | 107 function this.set_persistent_cookie(name,value,attributes) |
108 attributes = attributes or {} | |
109 attributes["Max-Age"] = "10000000" | |
110 this.set_cookie(name,value,attributes) | |
111 end | |
112 | |
113 function this.remove_cookie(name,attributes) | |
114 attributes = attributes or {} | |
115 attributes["Max-Age"] = "0" | |
116 this.set_cookie(name,"delete",attributes) | |
117 end | |
568
aa17ad66a370
fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
509
diff
changeset
|
118 |
1160 | 119 function this.text_writer() |
1163 | 120 this.writer and "writer already set" |
1160 | 121 this.writer = ResponseOutputStream.new(this.java) |
122 this.writer = OutputStreamWriter.new(this.writer) | |
123 return IoLuan.textWriter(this.writer) | |
124 end | |
500
ab9c2afefb47
add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents:
499
diff
changeset
|
125 |
1160 | 126 function this.binary_writer() |
1163 | 127 this.writer and "writer already set" |
1160 | 128 this.writer = ResponseOutputStream.new(this.java) |
129 return IoLuan.binaryWriter(this.writer) | |
130 end | |
580 | 131 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
132 return this |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
133 end |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
134 |
1163 | 135 function Http.finish() -- called only from java |
136 local response = Http.response or error() | |
137 local java = response.java or error() | |
138 java.status = Status.getStatus(response.status) | |
139 for name, value in pairs(response.headers) do | |
1164 | 140 type(name)=="string" or "header name must be string" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
141 name = lower(name) |
1164 | 142 value = JavaLuan.toJava(value) |
1163 | 143 java.headers.put(name,value) |
144 end | |
145 response.writer and response.writer.close() | |
146 return java | |
147 end | |
148 | |
504
dbdf4b8193a8
add Http.per_session()
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
149 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
150 function Http.uncache_site() |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
151 for k in pairs(Table.copy(Package.loaded)) do |
629
35dde32c02ab
change String.matches()
Franklin Schmidt <fschmidt@gmail.com>
parents:
626
diff
changeset
|
152 if matches(k,"^site:") then |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
153 Package.loaded[k] = nil |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
154 end |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
155 end |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
156 end |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
157 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
158 return Http |