Mercurial Hosting > luan
annotate src/luan/modules/http/Http.luan @ 1257:e38f5869e9df
don't reset in send_redirect and other improvements
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 20 Sep 2018 21:04:41 -0600 |
parents | c147e2e877e3 |
children | e8020216dee7 |
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" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
9 local Table = require "luan:Table.luan" |
580 | 10 local clear = Table.clear or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
11 local Package = require "luan:Package.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
12 local String = require "luan:String.luan" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
13 local lower = String.lower or error() |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
14 local matches = String.matches or error() |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
15 local IoLuan = require "java:luan.modules.IoLuan" |
1164 | 16 local JavaLuan = require "java:luan.Luan" |
1163 | 17 local Response = require "java:luan.webserver.Response" |
1160 | 18 local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream" |
1163 | 19 local Status = require "java:luan.webserver.Status" |
1160 | 20 local OutputStreamWriter = require "java:java.io.OutputStreamWriter" |
1163 | 21 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
|
22 |
1152
21d157b153fe
change http parameters interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1150
diff
changeset
|
23 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
24 local Http = {} |
320
fed1893821bf
remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
300
diff
changeset
|
25 |
1160 | 26 function Http.new_request(java) |
27 local this = {} | |
28 Http.request = this | |
29 if java == nil then | |
30 this.method = "GET" | |
1237 | 31 this.scheme = "http" |
1160 | 32 this.headers = {} |
33 this.parameters = {} | |
34 this.cookies = {} | |
35 else | |
36 this.java = java | |
1179
f9136432847e
add request.raw_head
Franklin Schmidt <fschmidt@gmail.com>
parents:
1171
diff
changeset
|
37 this.raw_head = java.rawHead or error() |
1160 | 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() | |
1237 | 42 this.scheme = java.scheme or error() |
1164 | 43 this.headers = JavaLuan.toLuan(java.headers) |
44 this.parameters = JavaLuan.toLuan(java.parameters) | |
45 this.cookies = JavaLuan.toLuan(java.cookies) | |
1160 | 46 end |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
47 |
499 | 48 function this.url() |
1160 | 49 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
|
50 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
51 |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
52 return this |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
53 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
54 |
503 | 55 local STATUS = { |
1109 | 56 OK = 200 |
57 MOVED_PERMANENTLY = 301 | |
1160 | 58 FOUND = 302 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
59 -- add more as needed |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
60 } |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
61 Http.STATUS = STATUS |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
62 |
1163 | 63 function Http.new_response() |
1160 | 64 local this = {} |
65 Http.response = this | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
66 |
1163 | 67 function this.reset() |
68 this.java = Response.new() | |
69 this.headers = {} | |
70 this.status = STATUS.OK | |
71 this.writer = nil | |
72 end | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
73 |
1163 | 74 this.reset() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
75 |
1160 | 76 function this.send_redirect(location) |
77 this.status = STATUS.FOUND | |
78 this.headers["location"] = location | |
79 end | |
80 | |
1161 | 81 function this.send_error(status,msg) |
1163 | 82 this.reset() |
1161 | 83 this.status = status |
84 if msg ~= nil then | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
85 this.headers["content-type"] = "text/plain; charset=utf-8" |
1161 | 86 local writer = this.text_writer() |
87 writer.write(msg) | |
88 end | |
89 end | |
90 | |
1160 | 91 function this.set_cookie(name,value,attributes) |
1163 | 92 attributes = attributes or {} |
1213 | 93 attributes["Path"] = attributes["Path"] or "/" |
1163 | 94 local attrMap = HashMap.new() |
95 for attr_name, attr_value in pairs(attributes) do | |
1164 | 96 type(attr_name)=="string" or "cookie attribute name must be string" |
97 type(attr_value)=="string" or "cookie attribute value must be string" | |
1163 | 98 attrMap.put(attr_name,attr_value) |
99 end | |
100 this.java.setCookie(name,value,attrMap) | |
1160 | 101 end |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
102 |
1160 | 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 | |
568
aa17ad66a370
fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
509
diff
changeset
|
114 |
1160 | 115 function this.text_writer() |
1163 | 116 this.writer and "writer already set" |
1160 | 117 this.writer = ResponseOutputStream.new(this.java) |
118 this.writer = OutputStreamWriter.new(this.writer) | |
119 return IoLuan.textWriter(this.writer) | |
120 end | |
500
ab9c2afefb47
add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents:
499
diff
changeset
|
121 |
1160 | 122 function this.binary_writer() |
1163 | 123 this.writer and "writer already set" |
1160 | 124 this.writer = ResponseOutputStream.new(this.java) |
125 return IoLuan.binaryWriter(this.writer) | |
126 end | |
580 | 127 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
128 return this |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
129 end |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
130 |
1163 | 131 function Http.finish() -- called only from java |
132 local response = Http.response or error() | |
133 local java = response.java or error() | |
134 java.status = Status.getStatus(response.status) | |
135 for name, value in pairs(response.headers) do | |
1164 | 136 type(name)=="string" or "header name must be string" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
137 name = lower(name) |
1164 | 138 value = JavaLuan.toJava(value) |
1163 | 139 java.headers.put(name,value) |
140 end | |
141 response.writer and response.writer.close() | |
142 return java | |
143 end | |
144 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
145 return Http |