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