Module: Syntropy::ResponseMethods
- Included in:
- Request
- Defined in:
- lib/syntropy/request/response.rb
Constant Summary collapse
- WEBSOCKET_GUID =
'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
Instance Method Summary collapse
- #file_full_path(path, opts) ⇒ Object
- #html_response(html, **headers) ⇒ Object
- #json_pretty_response(obj, **headers) ⇒ Object
- #json_response(obj, **headers) ⇒ Object
- #redirect(url, status = HTTP::FOUND) ⇒ Object
- #redirect_to_host(new_host, status = HTTP::FOUND) ⇒ Object
- #redirect_to_https(status = HTTP::MOVED_PERMANENTLY) ⇒ Object
-
#respond_by_http_method(map) ⇒ void
Responds according to the given map.
-
#respond_on_get(body, headers = {}) ⇒ void
Responds to GET requests with the given body and headers.
-
#respond_on_post(body, headers = {}) ⇒ void
Responds to POST requests with the given body and headers.
- #respond_with_static_file(path, etag, last_modified, opts) ⇒ Object
- #serve_file(path, opts = {}) ⇒ Object
- #serve_io(io, opts) ⇒ Object
- #set_cookie ⇒ Object
- #set_response_headers(headers) ⇒ Object
- #upgrade(protocol, custom_headers = nil, &block) ⇒ Object
- #upgrade_to_websocket(custom_headers = nil) ⇒ Object
- #validate_static_file_cache(etag, last_modified) ⇒ Object
Instance Method Details
#file_full_path(path, opts) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/syntropy/request/response.rb', line 83 def file_full_path(path, opts) if (base_path = opts[:base_path]) File.join(opts[:base_path], path) else path end end |
#html_response(html, **headers) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/syntropy/request/response.rb', line 182 def html_response(html, **headers) respond( html, 'Content-Type' => 'text/html; charset=utf-8', **headers ) end |
#json_pretty_response(obj, **headers) ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/syntropy/request/response.rb', line 198 def json_pretty_response(obj, **headers) respond( JSON.pretty_generate(obj), 'Content-Type' => 'application/json; charset=utf-8', **headers ) end |
#json_response(obj, **headers) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/syntropy/request/response.rb', line 190 def json_response(obj, **headers) respond( JSON.dump(obj), 'Content-Type' => 'application/json; charset=utf-8', **headers ) end |
#redirect(url, status = HTTP::FOUND) ⇒ Object
36 37 38 |
# File 'lib/syntropy/request/response.rb', line 36 def redirect(url, status = HTTP::FOUND) respond(nil, ':status' => status, 'Location' => url) end |
#redirect_to_host(new_host, status = HTTP::FOUND) ⇒ Object
45 46 47 48 |
# File 'lib/syntropy/request/response.rb', line 45 def redirect_to_host(new_host, status = HTTP::FOUND) secure_uri = "//#{new_host}#{uri}" redirect(secure_uri, status) end |
#redirect_to_https(status = HTTP::MOVED_PERMANENTLY) ⇒ Object
40 41 42 43 |
# File 'lib/syntropy/request/response.rb', line 40 def redirect_to_https(status = HTTP::MOVED_PERMANENTLY) secure_uri = "https://#{host}#{uri}" redirect(secure_uri, status) end |
#respond_by_http_method(map) ⇒ void
This method returns an undefined value.
Responds according to the given map. The given map defines the responses for each method. The value for each method is either an array containing the body and header values to use as response, or a proc returning such an array. For example:
req.respond_by_http_method(
'head' => [nil, headers],
'get' => -> { [IO.read(fn), headers] }
)
If the request’s method is not included in the given map, an exception is raised.
139 140 141 142 143 144 145 146 |
# File 'lib/syntropy/request/response.rb', line 139 def respond_by_http_method(map) value = map[self.method] raise Syntropy::Error.method_not_allowed if !value value = value.() if value.is_a?(Proc) (body, headers) = value respond(body, headers) end |
#respond_on_get(body, headers = {}) ⇒ void
This method returns an undefined value.
Responds to GET requests with the given body and headers. Otherwise raises an exception.
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/syntropy/request/response.rb', line 154 def respond_on_get(body, headers = {}) case self.method when 'head' respond(nil, headers) when 'get' respond(body, headers) else raise Syntropy::Error.method_not_allowed end end |
#respond_on_post(body, headers = {}) ⇒ void
This method returns an undefined value.
Responds to POST requests with the given body and headers. Otherwise raises an exception.
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/syntropy/request/response.rb', line 171 def respond_on_post(body, headers = {}) case self.method when 'head' respond(nil, headers) when 'post' respond(body, headers) else raise Syntropy::Error.method_not_allowed end end |
#respond_with_static_file(path, etag, last_modified, opts) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/syntropy/request/response.rb', line 95 def respond_with_static_file(path, etag, last_modified, opts) cache_headers = (etag || last_modified) ? { 'etag' => etag, 'last-modified' => last_modified } : {} adapter.respond_with_static_file(self, path, opts, cache_headers) end |
#serve_file(path, opts = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/syntropy/request/response.rb', line 50 def serve_file(path, opts = {}) full_path = file_full_path(path, opts) stat = File.stat(full_path) etag = StaticFileCaching.file_stat_to_etag(stat) last_modified = StaticFileCaching.file_stat_to_last_modified(stat) if validate_static_file_cache(etag, last_modified) return respond(nil, { ':status' => HTTP::NOT_MODIFIED, 'etag' => etag }) end mime_type = MimeTypes[File.extname(path)] opts[:stat] = stat (opts[:headers] ||= {})['Content-Type'] ||= mime_type if mime_type respond_with_static_file(full_path, etag, last_modified, opts) rescue Errno::ENOENT respond(nil, ':status' => HTTP::NOT_FOUND) end |
#serve_io(io, opts) ⇒ Object
91 92 93 |
# File 'lib/syntropy/request/response.rb', line 91 def serve_io(io, opts) respond(io.read, opts[:headers] || {}) end |
#set_cookie ⇒ Object
108 109 110 |
# File 'lib/syntropy/request/response.rb', line 108 def (*) adapter.(*) end |
#set_response_headers(headers) ⇒ Object
104 105 106 |
# File 'lib/syntropy/request/response.rb', line 104 def set_response_headers(headers) adapter.set_response_headers(headers) end |
#upgrade(protocol, custom_headers = nil, &block) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/syntropy/request/response.rb', line 112 def upgrade(protocol, custom_headers = nil, &block) upgrade_headers = { ':status' => HTTP::SWITCHING_PROTOCOLS, 'Upgrade' => protocol, 'Connection' => 'upgrade' } upgrade_headers.merge!(custom_headers) if custom_headers respond(nil, upgrade_headers) adapter.with_stream(&block) end |
#upgrade_to_websocket(custom_headers = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/syntropy/request/response.rb', line 25 def upgrade_to_websocket(custom_headers = nil) key = "#{headers['sec-websocket-key']}#{WEBSOCKET_GUID}" upgrade_headers = { 'Sec-WebSocket-Accept' => Digest::SHA1.base64digest(key) } upgrade_headers.merge!(custom_headers) if custom_headers upgrade('websocket', upgrade_headers) adapter.websocket_connection(self) end |
#validate_static_file_cache(etag, last_modified) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/syntropy/request/response.rb', line 72 def validate_static_file_cache(etag, last_modified) if (none_match = headers['if-none-match']) return true if none_match == etag end if (modified_since = headers['if-modified-since']) return true if modified_since == last_modified end false end |