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

Instance Method Details

#file_full_path(path, opts) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/syntropy/request/response.rb', line 72

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



187
188
189
190
191
192
193
# File 'lib/syntropy/request/response.rb', line 187

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
# File 'lib/syntropy/request/response.rb', line 25

def redirect(url, status = HTTP::FOUND)
  respond(nil, ':status' => status, 'Location' => url)
end

#redirect_to_host(new_host, status = HTTP::FOUND) ⇒ Object



34
35
36
37
# File 'lib/syntropy/request/response.rb', line 34

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



29
30
31
32
# File 'lib/syntropy/request/response.rb', line 29

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.

Parameters:

  • map (Hash)

    hash mapping HTTP methods to responses



128
129
130
131
132
133
134
135
# File 'lib/syntropy/request/response.rb', line 128

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



171
172
173
174
175
176
177
# File 'lib/syntropy/request/response.rb', line 171

def respond_html(html, **headers)
  respond(
    html,
    'Content-Type' => 'text/html; charset=utf-8',
    **headers
  )
end

#respond_json(obj, **headers) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/syntropy/request/response.rb', line 179

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.

Parameters:

  • body (String, nil)

    response body

  • headers (Hash) (defaults to: {})

    response headers



143
144
145
146
147
148
149
150
151
152
# File 'lib/syntropy/request/response.rb', line 143

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.

Parameters:

  • body (String, nil)

    response body

  • headers (Hash) (defaults to: {})

    response headers



160
161
162
163
164
165
166
167
168
169
# File 'lib/syntropy/request/response.rb', line 160

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



84
85
86
87
88
89
90
91
# File 'lib/syntropy/request/response.rb', line 84

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/syntropy/request/response.rb', line 39

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



80
81
82
# File 'lib/syntropy/request/response.rb', line 80

def serve_io(io, opts)
  respond(io.read, opts[:headers] || {})
end


97
98
99
# File 'lib/syntropy/request/response.rb', line 97

def set_cookie(k, v)
  adapter.set_cookie(k, v)
end

#set_response_headers(headers) ⇒ Object



93
94
95
# File 'lib/syntropy/request/response.rb', line 93

def set_response_headers(headers)
  adapter.set_response_headers(headers)
end

#upgrade(protocol, custom_headers = nil, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/syntropy/request/response.rb', line 101

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



61
62
63
64
65
66
67
68
69
70
# File 'lib/syntropy/request/response.rb', line 61

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