Mercurial Hosting > luan
annotate src/luan/modules/http/Http.luan @ 1256:c147e2e877e3
allow subclassing of HttpServicer
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 19 Sep 2018 20:15:16 -0600 |
parents | beebd0f5f2ba |
children | e38f5869e9df |
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) |
1163 | 77 this.reset() |
1160 | 78 this.status = STATUS.FOUND |
79 this.headers["location"] = location | |
80 end | |
81 | |
1161 | 82 function this.send_error(status,msg) |
1163 | 83 this.reset() |
1161 | 84 this.status = status |
85 if msg ~= nil then | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
86 this.headers["content-type"] = "text/plain; charset=utf-8" |
1161 | 87 local writer = this.text_writer() |
88 writer.write(msg) | |
89 end | |
90 end | |
91 | |
1160 | 92 function this.set_cookie(name,value,attributes) |
1163 | 93 attributes = attributes or {} |
1213 | 94 attributes["Path"] = attributes["Path"] or "/" |
1163 | 95 local attrMap = HashMap.new() |
96 for attr_name, attr_value in pairs(attributes) do | |
1164 | 97 type(attr_name)=="string" or "cookie attribute name must be string" |
98 type(attr_value)=="string" or "cookie attribute value must be string" | |
1163 | 99 attrMap.put(attr_name,attr_value) |
100 end | |
101 this.java.setCookie(name,value,attrMap) | |
1160 | 102 end |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
103 |
1160 | 104 function this.set_persistent_cookie(name,value,attributes) |
105 attributes = attributes or {} | |
106 attributes["Max-Age"] = "10000000" | |
107 this.set_cookie(name,value,attributes) | |
108 end | |
109 | |
110 function this.remove_cookie(name,attributes) | |
111 attributes = attributes or {} | |
112 attributes["Max-Age"] = "0" | |
113 this.set_cookie(name,"delete",attributes) | |
114 end | |
568
aa17ad66a370
fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
509
diff
changeset
|
115 |
1160 | 116 function this.text_writer() |
1163 | 117 this.writer and "writer already set" |
1160 | 118 this.writer = ResponseOutputStream.new(this.java) |
119 this.writer = OutputStreamWriter.new(this.writer) | |
120 return IoLuan.textWriter(this.writer) | |
121 end | |
500
ab9c2afefb47
add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents:
499
diff
changeset
|
122 |
1160 | 123 function this.binary_writer() |
1163 | 124 this.writer and "writer already set" |
1160 | 125 this.writer = ResponseOutputStream.new(this.java) |
126 return IoLuan.binaryWriter(this.writer) | |
127 end | |
580 | 128 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
129 return this |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
130 end |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
131 |
1163 | 132 function Http.finish() -- called only from java |
133 local response = Http.response or error() | |
134 local java = response.java or error() | |
135 java.status = Status.getStatus(response.status) | |
136 for name, value in pairs(response.headers) do | |
1164 | 137 type(name)=="string" or "header name must be string" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
138 name = lower(name) |
1164 | 139 value = JavaLuan.toJava(value) |
1163 | 140 java.headers.put(name,value) |
141 end | |
142 response.writer and response.writer.close() | |
143 return java | |
144 end | |
145 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
146 return Http |