comparison 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
comparison
equal deleted inserted replaced
1170:3a0f58d09ee7 1171:794ddcfbee20
1 return require "luan:http/impl/Http.luan" 1 java()
2 local Luan = require "luan:Luan.luan"
3 local error = Luan.error
4 local ipairs = Luan.ipairs or error()
5 local pairs = Luan.pairs or error()
6 local type = Luan.type or error()
7 local Io = require "luan:Io.luan"
8 local Html = require "luan:Html.luan"
9 local url_encode = Html.url_encode or error()
10 local Table = require "luan:Table.luan"
11 local clear = Table.clear or error()
12 local Package = require "luan:Package.luan"
13 local String = require "luan:String.luan"
14 local lower = String.lower or error()
15 local matches = String.matches or error()
16 local HttpServicer = require "java:luan.modules.http.HttpServicer"
17 local IoLuan = require "java:luan.modules.IoLuan"
18 local JavaLuan = require "java:luan.Luan"
19 local Response = require "java:luan.webserver.Response"
20 local ResponseOutputStream = require "java:luan.webserver.ResponseOutputStream"
21 local Status = require "java:luan.webserver.Status"
22 local OutputStreamWriter = require "java:java.io.OutputStreamWriter"
23 local HashMap = require "java:java.util.HashMap"
24
25
26 local Http = {}
27
28 Http.version = "luan"
29
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()
44 this.headers = JavaLuan.toLuan(java.headers)
45 this.parameters = JavaLuan.toLuan(java.parameters)
46 this.cookies = JavaLuan.toLuan(java.cookies)
47 end
48 this.scheme = "http"
49
50 function this.full_path() -- compatible with jetty
51 return this.raw_path or this.path
52 end
53
54 function this.url()
55 return this.scheme.."://"..this.headers["host"]..this.raw_path
56 end
57
58 return this
59 end
60
61 local STATUS = {
62 OK = 200
63 MOVED_PERMANENTLY = 301
64 FOUND = 302
65 -- add more as needed
66 }
67 Http.STATUS = STATUS
68
69 function Http.new_response()
70 local this = {}
71 Http.response = this
72
73 function this.reset()
74 this.java = Response.new()
75 this.headers = {}
76 this.status = STATUS.OK
77 this.writer = nil
78 end
79
80 this.reset()
81
82 function this.send_redirect(location)
83 this.reset()
84 this.status = STATUS.FOUND
85 this.headers["location"] = location
86 end
87
88 function this.send_error(status,msg)
89 this.reset()
90 this.status = status
91 if msg ~= nil then
92 this.headers["content-type"] = "text/plain; charset=utf-8"
93 local writer = this.text_writer()
94 writer.write(msg)
95 end
96 end
97
98 function this.set_cookie(name,value,attributes)
99 attributes = attributes or {}
100 local attrMap = HashMap.new()
101 for attr_name, attr_value in pairs(attributes) do
102 type(attr_name)=="string" or "cookie attribute name must be string"
103 type(attr_value)=="string" or "cookie attribute value must be string"
104 attrMap.put(attr_name,attr_value)
105 end
106 this.java.setCookie(name,value,attrMap)
107 end
108
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
120
121 function this.text_writer()
122 this.writer and "writer already set"
123 this.writer = ResponseOutputStream.new(this.java)
124 this.writer = OutputStreamWriter.new(this.writer)
125 return IoLuan.textWriter(this.writer)
126 end
127
128 function this.binary_writer()
129 this.writer and "writer already set"
130 this.writer = ResponseOutputStream.new(this.java)
131 return IoLuan.binaryWriter(this.writer)
132 end
133
134 return this
135 end
136
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
142 type(name)=="string" or "header name must be string"
143 name = lower(name)
144 value = JavaLuan.toJava(value)
145 java.headers.put(name,value)
146 end
147 response.writer and response.writer.close()
148 return java
149 end
150
151
152 function Http.uncache_site()
153 for k in pairs(Table.copy(Package.loaded)) do
154 if matches(k,"^site:") then
155 Package.loaded[k] = nil
156 end
157 end
158 end
159
160 return Http