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