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