Class: Mailpeek::WebApplication

Inherits:
Object
  • Object
show all
Extended by:
WebRouter
Defined in:
lib/mailpeek/web/application.rb

Overview

Public: WebApplication

Constant Summary collapse

CONTENT_LENGTH =
'Content-Length'
CONTENT_TYPE =
'Content-Type'
CSP_HEADER =
[
  "default-src 'self' https: http:",
  "child-src 'self'",
  "connect-src 'self' https: http: wss: ws:",
  "font-src 'self' https: http:",
  "frame-src 'self'",
  "img-src 'self' https: http: data:",
  "manifest-src 'self'",
  "media-src 'self'",
  "object-src 'none'",
  "script-src 'self' https: http: 'unsafe-inline'",
  "style-src 'self' https: http: 'unsafe-inline'",
  "worker-src 'self'",
  "base-uri 'self'"
].join('; ').freeze

Constants included from WebRouter

Mailpeek::WebRouter::DELETE, Mailpeek::WebRouter::GET, Mailpeek::WebRouter::HEAD, Mailpeek::WebRouter::PATCH, Mailpeek::WebRouter::PATH_INFO, Mailpeek::WebRouter::POST, Mailpeek::WebRouter::PUT, Mailpeek::WebRouter::REQUEST_METHOD, Mailpeek::WebRouter::ROUTE_PARAMS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WebRouter

delete, get, match, patch, post, put, route

Constructor Details

#initialize(klass) ⇒ WebApplication

Returns a new instance of WebApplication.



26
27
28
# File 'lib/mailpeek/web/application.rb', line 26

def initialize(klass)
  @klass = klass
end

Class Method Details

.helpers(mod = nil, &block) ⇒ Object

rubocop:enable Metrics/MethodLength



146
147
148
149
150
151
152
# File 'lib/mailpeek/web/application.rb', line 146

def self.helpers(mod = nil, &block)
  if block_given?
    WebAction.class_eval(&block)
  else
    WebAction.send(:include, mod)
  end
end

.set(key, val) ⇒ Object



38
39
40
# File 'lib/mailpeek/web/application.rb', line 38

def self.set(key, val)
  # nothing, backwards compatibility
end

.settingsObject



34
35
36
# File 'lib/mailpeek/web/application.rb', line 34

def self.settings
  Mailpeek::Web.settings
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/MethodLength



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mailpeek/web/application.rb', line 107

def call(env)
  action = self.class.match(env)

  unless action
    return [
      404,
      { 'Content-Type' => 'text/plain', 'X-Cascade' => 'pass' },
      ['Not Found']
    ]
  end

  response = catch(:halt) do
    response = action.instance_exec(env, &action.block)
  end

  response =
    case response
    when Array
      response
    else
      headers = {
        'Content-Type' => 'text/html',
        'Cache-Control' => 'no-cache',
        'Content-Security-Policy' => CSP_HEADER
      }

      [200, headers, [response]]
    end

  response[1] = response[1].dup

  response[1][CONTENT_LENGTH] = response[2].inject(0) do |l, p|
    l + p.bytesize
  end.to_s

  response
end

#settingsObject



30
31
32
# File 'lib/mailpeek/web/application.rb', line 30

def settings
  @klass.settings
end