Module: Syntropy::ResponseMethods
- Included in:
- Request
- Defined in:
- lib/syntropy/request/response.rb
Overview
Response methods.
Constant Summary collapse
- WEBSOCKET_GUID =
'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
Instance Method Summary collapse
- #file_full_path(path, opts) ⇒ Object
- #json_pretty_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_html(html, **headers) ⇒ Object
- #respond_json(obj, **headers) ⇒ Object
-
#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(k, v) ⇒ 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
73 74 75 76 77 78 79 |
# File 'lib/syntropy/request/response.rb', line 73 def file_full_path(path, opts) if (base_path = opts[:base_path]) File.join(opts[:base_path], path) else path end end |
#json_pretty_response(obj, **headers) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/syntropy/request/response.rb', line 188 def json_pretty_response(obj, **headers) respond( JSON.pretty_generate(obj), 'Content-Type' => 'application/json; charset=utf-8', **headers ) end |
#redirect(url, status = HTTP::FOUND) ⇒ Object
25 26 27 28 |
# File 'lib/syntropy/request/response.rb', line 25 def redirect(url, status = HTTP::FOUND) url = File.(File.join(path, url)) if url =~ /^\./ respond(nil, ':status' => status, 'Location' => url) end |
#redirect_to_host(new_host, status = HTTP::FOUND) ⇒ Object
35 36 37 38 |
# File 'lib/syntropy/request/response.rb', line 35 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
30 31 32 33 |
# File 'lib/syntropy/request/response.rb', line 30 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.
129 130 131 132 133 134 135 136 |
# File 'lib/syntropy/request/response.rb', line 129 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_html(html, **headers) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/syntropy/request/response.rb', line 172 def respond_html(html, **headers) respond( html, 'Content-Type' => 'text/html; charset=utf-8', **headers ) end |
#respond_json(obj, **headers) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/syntropy/request/response.rb', line 180 def respond_json(obj, **headers) respond( JSON.dump(obj), 'Content-Type' => 'application/json; charset=utf-8', **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.
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/syntropy/request/response.rb', line 144 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.
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/syntropy/request/response.rb', line 161 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
85 86 87 88 89 90 91 92 |
# File 'lib/syntropy/request/response.rb', line 85 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/syntropy/request/response.rb', line 40 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
81 82 83 |
# File 'lib/syntropy/request/response.rb', line 81 def serve_io(io, opts) respond(io.read, opts[:headers] || {}) end |
#set_cookie(k, v) ⇒ Object
98 99 100 |
# File 'lib/syntropy/request/response.rb', line 98 def (k, v) adapter.(k, v) end |
#set_response_headers(headers) ⇒ Object
94 95 96 |
# File 'lib/syntropy/request/response.rb', line 94 def set_response_headers(headers) adapter.set_response_headers(headers) end |
#upgrade(protocol, custom_headers = nil, &block) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/syntropy/request/response.rb', line 102 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
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/syntropy/request/response.rb', line 14 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
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/syntropy/request/response.rb', line 62 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 |