Module: WebSocket::HeaderHelpers

Included in:
HttpStaticFileServerHandler
Defined in:
lib/websocket/header_helpers.rb

Overview

The HeaderHelpers module

Constant Summary collapse

PrivateMaxAgeTempalte =
'private, max-age=%<max_age>s'.freeze

Instance Method Summary collapse

Instance Method Details

#cache_control_header(response, cache_control) ⇒ Object



36
37
38
# File 'lib/websocket/header_helpers.rb', line 36

def cache_control_header(response, cache_control)
  response.headers().set(HttpHeaderNames::CACHE_CONTROL, cache_control)
end

#content_type_header(response, content_type) ⇒ Object

rubocop: enable Metrics/AbcSize



57
58
59
# File 'lib/websocket/header_helpers.rb', line 57

def content_type_header(response, content_type)
  response.headers().set(HttpHeaderNames::CONTENT_TYPE, content_type)
end

#date_and_cache_headers(response, path, timestamp = Time.now) ⇒ Object

rubocop: disable Metrics/AbcSize



47
48
49
50
51
52
53
54
# File 'lib/websocket/header_helpers.rb', line 47

def date_and_cache_headers(response, path, timestamp = Time.now)
  maximum_age = options[:http_cache_seconds]
  date_format = options[:http_date_format]
  date_header(response, timestamp.strftime(date_format))
  expires_header(response, Time.at(timestamp.to_i + maximum_age).strftime(date_format))
  cache_control_header(response, format(PrivateMaxAgeTempalte, max_age: maximum_age.to_s))
  last_modified_header(response, File.mtime(path).strftime(date_format))
end

#date_header(response, date) ⇒ Object



28
29
30
# File 'lib/websocket/header_helpers.rb', line 28

def date_header(response, date)
  response.headers().set(HttpHeaderNames::DATE, date)
end

#expires_header(response, expires) ⇒ Object



32
33
34
# File 'lib/websocket/header_helpers.rb', line 32

def expires_header(response, expires)
  response.headers().set(HttpHeaderNames::EXPIRES, expires)
end

#guess_content_type(path, _charset = nil) ⇒ Object



61
62
63
64
65
66
# File 'lib/websocket/header_helpers.rb', line 61

def guess_content_type(path, _charset = nil)
  i = path.rindex(/\./)
  return nil if i == -1
  extension = path[(i + 1)..].downcase.to_sym
  ::Server::MimeTypes.fetch(extension, ::Server::MimeTypes[:txt])
end

#keep_alive_header(response) ⇒ Object



24
25
26
# File 'lib/websocket/header_helpers.rb', line 24

def keep_alive_header(response)
  response.headers().set(HttpHeaderNames::CONNECTION, HttpHeaderValues::KEEP_ALIVE)
end

#last_modified_header(response, last_modified) ⇒ Object



40
41
42
# File 'lib/websocket/header_helpers.rb', line 40

def last_modified_header(response, last_modified)
  response.headers().set(HttpHeaderNames::LAST_MODIFIED, last_modified)
end