Mercurial Hosting > luan
comparison src/luan/modules/http/Http.luan @ 1607:fa066aaa068c
nginx caching
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 30 Apr 2021 20:23:28 -0600 |
parents | c922446f53aa |
children | a37ffe2d1b14 |
comparison
equal
deleted
inserted
replaced
1606:7c7f28c724e8 | 1607:fa066aaa068c |
---|---|
13 local Io = require "luan:Io.luan" | 13 local Io = require "luan:Io.luan" |
14 local Html = require "luan:Html.luan" | 14 local Html = require "luan:Html.luan" |
15 local Table = require "luan:Table.luan" | 15 local Table = require "luan:Table.luan" |
16 local clear = Table.clear or error() | 16 local clear = Table.clear or error() |
17 local java_to_table_deep = Table.java_to_table_deep or error() | 17 local java_to_table_deep = Table.java_to_table_deep or error() |
18 local case_insensitive = Table.case_insensitive or error() | |
18 local Package = require "luan:Package.luan" | 19 local Package = require "luan:Package.luan" |
19 local String = require "luan:String.luan" | 20 local String = require "luan:String.luan" |
20 local lower = String.lower or error() | 21 local lower = String.lower or error() |
21 local matches = String.matches or error() | 22 local matches = String.matches or error() |
22 local trim = String.trim or error() | 23 local trim = String.trim or error() |
73 local this = {} | 74 local this = {} |
74 Http.request = this | 75 Http.request = this |
75 if java == nil then | 76 if java == nil then |
76 this.method = "GET" | 77 this.method = "GET" |
77 this.scheme = "http" | 78 this.scheme = "http" |
78 this.headers = {} | 79 this.headers = case_insensitive{} |
79 this.parameters = {} | 80 this.parameters = {} |
80 this.cookies = {} | 81 this.cookies = {} |
81 else | 82 else |
82 this.java = java | 83 this.java = java |
83 this.raw_head = java.rawHead or error() | 84 this.raw_head = java.rawHead or error() |
86 this.raw_path = java.rawPath or error() | 87 this.raw_path = java.rawPath or error() |
87 this.original_path = java.originalPath or error() | 88 this.original_path = java.originalPath or error() |
88 this.path = java.path or error() | 89 this.path = java.path or error() |
89 this.protocol = java.protocol or error() | 90 this.protocol = java.protocol or error() |
90 this.scheme = java.scheme or error() | 91 this.scheme = java.scheme or error() |
91 this.headers = java_to_table_deep(java.headers) | 92 this.headers = case_insensitive(java_to_table_deep(java.headers)) |
92 this.parameters = java_to_table_deep(java.parameters,java_to_table_shallow) | 93 this.parameters = java_to_table_deep(java.parameters,java_to_table_shallow) |
93 this.cookies = java_to_table_deep(java.cookies) | 94 this.cookies = java_to_table_deep(java.cookies) |
94 end | 95 end |
95 | 96 |
96 function this.url() | 97 function this.url() |
97 return this.scheme.."://"..this.headers["host"]..this.raw_path | 98 return this.scheme.."://"..this.headers["Host"]..this.raw_path |
98 end | 99 end |
99 | 100 |
100 return this | 101 return this |
101 end | 102 end |
102 | 103 |
113 local this = {} | 114 local this = {} |
114 Http.response = this | 115 Http.response = this |
115 | 116 |
116 function this.reset() | 117 function this.reset() |
117 this.java = Response.new() | 118 this.java = Response.new() |
118 this.headers = {} | 119 this.headers = case_insensitive{} |
119 this.status = STATUS.OK | 120 this.status = STATUS.OK |
120 this.writer = nil | 121 this.writer = nil |
121 end | 122 end |
122 | 123 |
123 this.reset() | 124 this.reset() |
124 | 125 |
125 function this.send_redirect(location) | 126 function this.send_redirect(location) |
126 this.status = STATUS.FOUND | 127 this.status = STATUS.FOUND |
127 this.headers["location"] = location | 128 this.headers["Location"] = location |
128 end | 129 end |
129 | 130 |
130 function this.send_error(status,msg) | 131 function this.send_error(status,msg) |
131 this.reset() | 132 this.reset() |
132 this.status = status | 133 this.status = status |
133 if msg ~= nil then | 134 if msg ~= nil then |
134 this.headers["content-type"] = "text/plain; charset=utf-8" | 135 this.headers["Content-Type"] = "text/plain; charset=utf-8" |
135 local writer = this.text_writer() | 136 local writer = this.text_writer() |
136 writer.write(msg) | 137 writer.write(msg) |
137 end | 138 end |
138 end | 139 end |
139 | 140 |
181 local response = Http.response or error() | 182 local response = Http.response or error() |
182 local java = response.java or error() | 183 local java = response.java or error() |
183 java.status = Status.getStatus(response.status) | 184 java.status = Status.getStatus(response.status) |
184 for name, value in pairs(response.headers) do | 185 for name, value in pairs(response.headers) do |
185 type(name)=="string" or "header name must be string" | 186 type(name)=="string" or "header name must be string" |
186 name = lower(name) | |
187 value = LuanJava.toJava(value) | 187 value = LuanJava.toJava(value) |
188 java.headers.put(name,value) | 188 java.headers.put(name,value) |
189 end | 189 end |
190 response.writer and response.writer.close() | 190 response.writer and response.writer.close() |
191 return java | 191 return java |
207 Http.domain = nil -- set in domain specific cases | 207 Http.domain = nil -- set in domain specific cases |
208 | 208 |
209 Http.is_serving = false | 209 Http.is_serving = false |
210 | 210 |
211 function Http.format_date(date) | 211 function Http.format_date(date) |
212 return time_format(date,"EEE, dd MMM yyyy HH:mm:ss 'GMT'","GMT") | 212 return time_format(date,"EEE, dd MMM yyyy HH:mm:ss z","GMT") |
213 end | 213 end |
214 | 214 |
215 return Http | 215 return Http |