comparison src/luan/modules/http/impl/Http.luan @ 1163:fef8f0742da9

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Feb 2018 22:04:47 -0700
parents e2d2354807f3
children 1f9d34a6f308
comparison
equal deleted inserted replaced
1162:e2d2354807f3 1163:fef8f0742da9
1 java() 1 java()
2 local Luan = require "luan:Luan.luan" 2 local Luan = require "luan:Luan.luan"
3 local error = Luan.error 3 local error = Luan.error
4 local assert_string = Luan.assert_string or error()
4 local ipairs = Luan.ipairs or error() 5 local ipairs = Luan.ipairs or error()
5 local pairs = Luan.pairs or error() 6 local pairs = Luan.pairs or error()
6 local type = Luan.type or error() 7 local type = Luan.type or error()
7 local Io = require "luan:Io.luan" 8 local Io = require "luan:Io.luan"
8 local Html = require "luan:Html.luan" 9 local Html = require "luan:Html.luan"
13 local String = require "luan:String.luan" 14 local String = require "luan:String.luan"
14 local matches = String.matches or error() 15 local matches = String.matches or error()
15 local HttpServicer = require "java:luan.modules.http.impl.HttpServicer" 16 local HttpServicer = require "java:luan.modules.http.impl.HttpServicer"
16 local IoLuan = require "java:luan.modules.IoLuan" 17 local IoLuan = require "java:luan.modules.IoLuan"
17 local LuanJava = require "java:luan.Luan" 18 local LuanJava = require "java:luan.Luan"
19 local Response = require "java:luan.webserver.Response"
18 local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream" 20 local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream"
21 local Status = require "java:luan.webserver.Status"
19 local OutputStreamWriter = require "java:java.io.OutputStreamWriter" 22 local OutputStreamWriter = require "java:java.io.OutputStreamWriter"
23 local HashMap = require "java:java.util.HashMap"
20 24
21 25
22 local Http = {} 26 local Http = {}
23 27
24 function Http.new_request(java) 28 function Http.new_request(java)
58 FOUND = 302 62 FOUND = 302
59 -- add more as needed 63 -- add more as needed
60 } 64 }
61 Http.STATUS = STATUS 65 Http.STATUS = STATUS
62 66
63 function Http.new_response(java) 67 function Http.new_response()
64 java or error()
65 local this = {} 68 local this = {}
66 Http.response = this 69 Http.response = this
67 this.java = java
68 70
69 this.headers = {} 71 function this.reset()
72 this.java = Response.new()
73 this.headers = {}
74 this.status = STATUS.OK
75 this.writer = nil
76 end
70 77
71 this.status = STATUS.OK 78 this.reset()
72 79
73 function this.send_redirect(location) 80 function this.send_redirect(location)
81 this.reset()
74 this.status = STATUS.FOUND 82 this.status = STATUS.FOUND
75 this.headers["location"] = location 83 this.headers["location"] = location
76 end 84 end
77 85
78 function this.send_error(status,msg) 86 function this.send_error(status,msg)
87 this.reset()
79 this.status = status 88 this.status = status
80 if msg ~= nil then 89 if msg ~= nil then
81 this.headers["content-type"] = "text/plain" 90 this.headers["content-type"] = "text/plain"
82 local writer = this.text_writer() 91 local writer = this.text_writer()
83 writer.write(msg) 92 writer.write(msg)
84 end 93 end
85 end 94 end
86 95
87 function this.set_cookie(name,value,attributes) 96 function this.set_cookie(name,value,attributes)
88 HttpServicer.setCookie(this.java,name,value,attributes) 97 attributes = attributes or {}
98 local attrMap = HashMap.new()
99 for attr_name, attr_value in pairs(attributes) do
100 assert_string(attr_name)
101 assert_string(attr_value)
102 attrMap.put(attr_name,attr_value)
103 end
104 this.java.setCookie(name,value,attrMap)
89 end 105 end
90 106
91 function this.set_persistent_cookie(name,value,attributes) 107 function this.set_persistent_cookie(name,value,attributes)
92 attributes = attributes or {} 108 attributes = attributes or {}
93 attributes["Max-Age"] = "10000000" 109 attributes["Max-Age"] = "10000000"
99 attributes["Max-Age"] = "0" 115 attributes["Max-Age"] = "0"
100 this.set_cookie(name,"delete",attributes) 116 this.set_cookie(name,"delete",attributes)
101 end 117 end
102 118
103 function this.text_writer() 119 function this.text_writer()
120 this.writer and "writer already set"
104 this.writer = ResponseOutputStream.new(this.java) 121 this.writer = ResponseOutputStream.new(this.java)
105 this.writer = OutputStreamWriter.new(this.writer) 122 this.writer = OutputStreamWriter.new(this.writer)
106 return IoLuan.textWriter(this.writer) 123 return IoLuan.textWriter(this.writer)
107 end 124 end
108 125
109 function this.binary_writer() 126 function this.binary_writer()
127 this.writer and "writer already set"
110 this.writer = ResponseOutputStream.new(this.java) 128 this.writer = ResponseOutputStream.new(this.java)
111 return IoLuan.binaryWriter(this.writer) 129 return IoLuan.binaryWriter(this.writer)
112 end 130 end
113 131
114 return this 132 return this
133 end
134
135 function Http.finish() -- called only from java
136 local response = Http.response or error()
137 local java = response.java or error()
138 java.status = Status.getStatus(response.status)
139 for name, value in pairs(response.headers) do
140 assert_string(name)
141 value = LuanJava.toJava(value)
142 java.headers.put(name,value)
143 end
144 response.writer and response.writer.close()
145 return java
115 end 146 end
116 147
117 148
118 function Http.uncache_site() 149 function Http.uncache_site()
119 for k in pairs(Table.copy(Package.loaded)) do 150 for k in pairs(Table.copy(Package.loaded)) do