Mercurial Hosting > luan
annotate src/luan/modules/http/Http.luan @ 1213:d5d5d29d7592
fix cookie bug
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 14 Mar 2018 18:50:18 -0600 |
parents | f9136432847e |
children | b6aa2cd51b02 |
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 |
1171 | 28 Http.version = "luan" |
1170 | 29 |
1160 | 30 function Http.new_request(java) |
31 local this = {} | |
32 Http.request = this | |
33 if java == nil then | |
34 this.method = "GET" | |
35 this.headers = {} | |
36 this.parameters = {} | |
37 this.cookies = {} | |
38 else | |
39 this.java = java | |
1179
f9136432847e
add request.raw_head
Franklin Schmidt <fschmidt@gmail.com>
parents:
1171
diff
changeset
|
40 this.raw_head = java.rawHead or error() |
1160 | 41 this.method = java.method or error() |
42 this.raw_path = java.rawPath or error() | |
43 this.path = java.path or error() | |
44 this.protocol = java.protocol or error() | |
1164 | 45 this.headers = JavaLuan.toLuan(java.headers) |
46 this.parameters = JavaLuan.toLuan(java.parameters) | |
47 this.cookies = JavaLuan.toLuan(java.cookies) | |
1160 | 48 end |
49 this.scheme = "http" | |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
50 |
1160 | 51 function this.full_path() -- compatible with jetty |
52 return this.raw_path or this.path | |
499 | 53 end |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
54 |
499 | 55 function this.url() |
1160 | 56 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
|
57 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
58 |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
59 return this |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
60 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
61 |
503 | 62 local STATUS = { |
1109 | 63 OK = 200 |
64 MOVED_PERMANENTLY = 301 | |
1160 | 65 FOUND = 302 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
66 -- add more as needed |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
67 } |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
68 Http.STATUS = STATUS |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
69 |
1163 | 70 function Http.new_response() |
1160 | 71 local this = {} |
72 Http.response = this | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
73 |
1163 | 74 function this.reset() |
75 this.java = Response.new() | |
76 this.headers = {} | |
77 this.status = STATUS.OK | |
78 this.writer = nil | |
79 end | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
80 |
1163 | 81 this.reset() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
82 |
1160 | 83 function this.send_redirect(location) |
1163 | 84 this.reset() |
1160 | 85 this.status = STATUS.FOUND |
86 this.headers["location"] = location | |
87 end | |
88 | |
1161 | 89 function this.send_error(status,msg) |
1163 | 90 this.reset() |
1161 | 91 this.status = status |
92 if msg ~= nil then | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
93 this.headers["content-type"] = "text/plain; charset=utf-8" |
1161 | 94 local writer = this.text_writer() |
95 writer.write(msg) | |
96 end | |
97 end | |
98 | |
1160 | 99 function this.set_cookie(name,value,attributes) |
1163 | 100 attributes = attributes or {} |
1213 | 101 attributes["Path"] = attributes["Path"] or "/" |
1163 | 102 local attrMap = HashMap.new() |
103 for attr_name, attr_value in pairs(attributes) do | |
1164 | 104 type(attr_name)=="string" or "cookie attribute name must be string" |
105 type(attr_value)=="string" or "cookie attribute value must be string" | |
1163 | 106 attrMap.put(attr_name,attr_value) |
107 end | |
108 this.java.setCookie(name,value,attrMap) | |
1160 | 109 end |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
110 |
1160 | 111 function this.set_persistent_cookie(name,value,attributes) |
112 attributes = attributes or {} | |
113 attributes["Max-Age"] = "10000000" | |
114 this.set_cookie(name,value,attributes) | |
115 end | |
116 | |
117 function this.remove_cookie(name,attributes) | |
118 attributes = attributes or {} | |
119 attributes["Max-Age"] = "0" | |
120 this.set_cookie(name,"delete",attributes) | |
121 end | |
568
aa17ad66a370
fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
509
diff
changeset
|
122 |
1160 | 123 function this.text_writer() |
1163 | 124 this.writer and "writer already set" |
1160 | 125 this.writer = ResponseOutputStream.new(this.java) |
126 this.writer = OutputStreamWriter.new(this.writer) | |
127 return IoLuan.textWriter(this.writer) | |
128 end | |
500
ab9c2afefb47
add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents:
499
diff
changeset
|
129 |
1160 | 130 function this.binary_writer() |
1163 | 131 this.writer and "writer already set" |
1160 | 132 this.writer = ResponseOutputStream.new(this.java) |
133 return IoLuan.binaryWriter(this.writer) | |
134 end | |
580 | 135 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
136 return this |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
137 end |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
138 |
1163 | 139 function Http.finish() -- called only from java |
140 local response = Http.response or error() | |
141 local java = response.java or error() | |
142 java.status = Status.getStatus(response.status) | |
143 for name, value in pairs(response.headers) do | |
1164 | 144 type(name)=="string" or "header name must be string" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
145 name = lower(name) |
1164 | 146 value = JavaLuan.toJava(value) |
1163 | 147 java.headers.put(name,value) |
148 end | |
149 response.writer and response.writer.close() | |
150 return java | |
151 end | |
152 | |
504
dbdf4b8193a8
add Http.per_session()
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
153 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
154 function Http.uncache_site() |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
155 for k in pairs(Table.copy(Package.loaded)) do |
629
35dde32c02ab
change String.matches()
Franklin Schmidt <fschmidt@gmail.com>
parents:
626
diff
changeset
|
156 if matches(k,"^site:") then |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
157 Package.loaded[k] = nil |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
158 end |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
159 end |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
160 end |
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
161 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
162 return Http |