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