Mercurial Hosting > luan
annotate src/luan/modules/http/Http.luan @ 1226:b6aa2cd51b02
Http cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 01 Apr 2018 19:42:47 -0600 |
parents | d5d5d29d7592 |
children | 275d1b52dbce |
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() |
1171 | 16 local HttpServicer = require "java:luan.modules.http.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 | |
1179
f9136432847e
add request.raw_head
Franklin Schmidt <fschmidt@gmail.com>
parents:
1171
diff
changeset
|
38 this.raw_head = java.rawHead or error() |
1160 | 39 this.method = java.method or error() |
40 this.raw_path = java.rawPath or error() | |
41 this.path = java.path or error() | |
42 this.protocol = java.protocol 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 |
47 this.scheme = "http" | |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
48 |
499 | 49 function this.url() |
1160 | 50 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
|
51 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
52 |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
53 return this |
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 |
503 | 56 local STATUS = { |
1109 | 57 OK = 200 |
58 MOVED_PERMANENTLY = 301 | |
1160 | 59 FOUND = 302 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
60 -- add more as needed |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
61 } |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
62 Http.STATUS = STATUS |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
63 |
1163 | 64 function Http.new_response() |
1160 | 65 local this = {} |
66 Http.response = this | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
67 |
1163 | 68 function this.reset() |
69 this.java = Response.new() | |
70 this.headers = {} | |
71 this.status = STATUS.OK | |
72 this.writer = nil | |
73 end | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
74 |
1163 | 75 this.reset() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
76 |
1160 | 77 function this.send_redirect(location) |
1163 | 78 this.reset() |
1160 | 79 this.status = STATUS.FOUND |
80 this.headers["location"] = location | |
81 end | |
82 | |
1161 | 83 function this.send_error(status,msg) |
1163 | 84 this.reset() |
1161 | 85 this.status = status |
86 if msg ~= nil then | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
87 this.headers["content-type"] = "text/plain; charset=utf-8" |
1161 | 88 local writer = this.text_writer() |
89 writer.write(msg) | |
90 end | |
91 end | |
92 | |
1160 | 93 function this.set_cookie(name,value,attributes) |
1163 | 94 attributes = attributes or {} |
1213 | 95 attributes["Path"] = attributes["Path"] or "/" |
1163 | 96 local attrMap = HashMap.new() |
97 for attr_name, attr_value in pairs(attributes) do | |
1164 | 98 type(attr_name)=="string" or "cookie attribute name must be string" |
99 type(attr_value)=="string" or "cookie attribute value must be string" | |
1163 | 100 attrMap.put(attr_name,attr_value) |
101 end | |
102 this.java.setCookie(name,value,attrMap) | |
1160 | 103 end |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
104 |
1160 | 105 function this.set_persistent_cookie(name,value,attributes) |
106 attributes = attributes or {} | |
107 attributes["Max-Age"] = "10000000" | |
108 this.set_cookie(name,value,attributes) | |
109 end | |
110 | |
111 function this.remove_cookie(name,attributes) | |
112 attributes = attributes or {} | |
113 attributes["Max-Age"] = "0" | |
114 this.set_cookie(name,"delete",attributes) | |
115 end | |
568
aa17ad66a370
fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
509
diff
changeset
|
116 |
1160 | 117 function this.text_writer() |
1163 | 118 this.writer and "writer already set" |
1160 | 119 this.writer = ResponseOutputStream.new(this.java) |
120 this.writer = OutputStreamWriter.new(this.writer) | |
121 return IoLuan.textWriter(this.writer) | |
122 end | |
500
ab9c2afefb47
add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents:
499
diff
changeset
|
123 |
1160 | 124 function this.binary_writer() |
1163 | 125 this.writer and "writer already set" |
1160 | 126 this.writer = ResponseOutputStream.new(this.java) |
127 return IoLuan.binaryWriter(this.writer) | |
128 end | |
580 | 129 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
130 return this |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
131 end |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
132 |
1163 | 133 function Http.finish() -- called only from java |
134 local response = Http.response or error() | |
135 local java = response.java or error() | |
136 java.status = Status.getStatus(response.status) | |
137 for name, value in pairs(response.headers) do | |
1164 | 138 type(name)=="string" or "header name must be string" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
139 name = lower(name) |
1164 | 140 value = JavaLuan.toJava(value) |
1163 | 141 java.headers.put(name,value) |
142 end | |
143 response.writer and response.writer.close() | |
144 return java | |
145 end | |
146 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
147 return Http |