Module: Rackr::Action

Included in:
Router::DevHtml::Dump, Router::DevHtml::Errors
Defined in:
lib/rackr/action.rb,
lib/rackr/action/callbacks.rb

Overview

This module provides the action functions available inside the routes context or specific action class that included the Rackr::Action.

Defined Under Namespace

Modules: Callbacks

Constant Summary collapse

MIME_TYPES =
{
  text: 'text/plain',
  html: 'text/html',
  json: 'application/json',
  manifest: 'text/cache-manifest',
  atom: 'application/atom+xml',
  avi: 'video/x-msvideo',
  bmp: 'image/bmp',
  bz: 'application/x-bzip',
  bz2: 'application/x-bzip2',
  chm: 'application/vnd.ms-htmlhelp',
  css: 'text/css',
  csv: 'text/csv',
  flv: 'video/x-flv',
  gif: 'image/gif',
  gz: 'application/x-gzip',
  h264: 'video/h264',
  ico: 'image/vnd.microsoft.icon',
  ics: 'text/calendar',
  jpg: 'image/jpeg',
  js: 'application/javascript',
  mp4: 'video/mp4',
  mov: 'video/quicktime',
  mp3: 'audio/mpeg',
  mp4a: 'audio/mp4',
  mpg: 'video/mpeg',
  oga: 'audio/ogg',
  ogg: 'application/ogg',
  ogv: 'video/ogg',
  pdf: 'application/pdf',
  pgp: 'application/pgp-encrypted',
  png: 'image/png',
  psd: 'image/vnd.adobe.photoshop',
  rss: 'application/rss+xml',
  rtf: 'application/rtf',
  sh: 'application/x-sh',
  svg: 'image/svg+xml',
  swf: 'application/x-shockwave-flash',
  tar: 'application/x-tar',
  torrent: 'application/x-bittorrent',
  tsv: 'text/tab-separated-values',
  uri: 'text/uri-list',
  vcs: 'text/x-vcalendar',
  wav: 'audio/x-wav',
  webm: 'video/webm',
  wmv: 'video/x-ms-wmv',
  woff: 'application/font-woff',
  woff2: 'application/font-woff2',
  wsdl: 'application/wsdl+xml',
  xhtml: 'application/xhtml+xml',
  xml: 'application/xml',
  xslt: 'application/xslt+xml',
  yml: 'text/yaml',
  zip: 'application/zip'
}.freeze
DEFAULT_CSP_HEADERS =
{
  base_uri: "'self'",
  child_src: "'self'",
  connect_src: "'self'",
  default_src: "'none'",
  font_src: "'self'",
  form_action: "'self'",
  frame_ancestors: "'self'",
  frame_src: "'self'",
  img_src: "'self' https: data:",
  media_src: "'self'",
  object_src: "'none'",
  script_src: "'self'",
  style_src: "'self' 'unsafe-inline' https:"
}.freeze
DEFAULT_HEADERS =

These are constant (not methods) for better performance

(proc do |content_type, headers, content|
  {
    'content-type' => content_type,
    'content-length' => content.bytesize.to_s
  }.merge(headers)
end).freeze
RENDER =
{
  json: proc do |content, status, headers, charset|
    content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
    [status || 200, DEFAULT_HEADERS.call("application/json; charset=#{charset}", headers, content), [content]]
  end,
  html: proc do |content, status, headers, charset, content_security_policy|
    headers['content-security-policy'] = content_security_policy
    [status || 200, DEFAULT_HEADERS.call("text/html; charset=#{charset}", headers, content), [content]]
  end,
  res: proc do |content, status, _headers, charset|
    content.status = status if status
    if charset
      content.content_type =
        (content.content_type || 'charset=utf-8').sub(/charset=\S+/, "charset=#{charset}")
    end
    content.headers['content-length'] ||= content.body.join.bytesize.to_s
    content.finish
  end,
  response: proc do |content, status, _headers, charset|
    content.status = status if status
    if charset
      content.content_type =
        (content.content_type || 'charset=utf-8').sub(/charset=\S+/, "charset=#{charset}")
    end
    content.headers['content-length'] ||= content.body.join.bytesize.to_s
    content.finish
  end
}.freeze
BUILD_RESPONSE =
{
  json: proc do |content, status, headers, charset|
    content = Oj.dump(content, mode: :compat) unless content.is_a?(String)
    Rack::Response.new(content, status,
                       DEFAULT_HEADERS.call("application/json; charset=#{charset}", headers, content))
  end,
  html: proc do |content, status, headers, charset, content_security_policy|
    headers['content-security-policy'] = content_security_policy
    Rack::Response.new(content, status, DEFAULT_HEADERS.call("text/html; charset=#{charset}", headers, content))
  end,
  head: proc do |status, _empty, headers|
    Rack::Response.new(nil, status, headers)
  end,
  redirect_to: proc do |content, _status, headers|
    Rack::Response.new(
      nil,
      302,
      { 'location' => content }.merge(headers)
    )
  end
}.freeze

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/rackr/action.rb', line 145

def self.included(base)
  base.class_eval do
    if self == Rackr
      include HtmlSlice if Object.const_defined?('HtmlSlice')
      include Stimulux if Object.const_defined?('Stimulux')
    else
      attr_reader :routes, :config, :deps, :db, :log, :cache

      include Callbacks unless include?(Rackr::Action::Callbacks)
      include HtmlSlice if Object.const_defined?('HtmlSlice')
      include Stimulux if Object.const_defined?('Stimulux')
    end

    def initialize(routes: nil, config: nil)
      # self.class.include(HtmlSlice) if Object.const_defined?('HtmlSlice')
      # self.class.include(Stimulux) if Object.const_defined?('Stimulux')

      @routes = routes
      @config = config
      @deps = config&.dig(:deps)
      @db = config&.dig(:deps, :db)
      @log = config&.dig(:deps, :log)
      @cache = config&.dig(:deps, :cache)
    end

    def url_for(method, name)
      "#{config&.dig(:host)}#{path_for(method, name)}"
    end

    def path_for(method, name)
      routes.send(method)&.dig(name)
    end

    def render(content = nil, **opts)
      if content
        return RENDER[:html].call(
          content.to_s,
          opts[:status],
          opts[:headers] || {},
          opts[:charset] || 'utf-8',
          content_security_policy
        )
      end

      type = opts.keys.first
      content = opts[type]

      if (renderer = RENDER[type])
        return renderer.call(
          content,
          opts[:status],
          opts[:headers] || {},
          opts[:charset] || 'utf-8',
          content_security_policy
        )
      end

      if (mime = MIME_TYPES[type])
        return [
          opts[:status] || 200,
          DEFAULT_HEADERS.call(
            "#{mime}; charset=#{opts[:charset] || 'utf-8'}",
            opts[:headers] || {},
            content
          ),
          [content]
        ]
      end

      _render_view(
        content,
        status: opts[:status] || 200,
        headers: opts[:headers] || {},
        layout_path: opts[:layout_path] || 'layout',
        view: opts[:view],
        response_instance: opts[:response_instance] || false,
        charset: opts[:charset] || 'utf-8'
      )
    end

    def build_response(content = nil, **opts)
      if content
        return BUILD_RESPONSE[:html].call(
          content.to_s,
          opts[:status] || 200,
          opts[:headers] || {},
          opts[:charset] || 'utf-8',
          content_security_policy
        )
      end

      type = opts.keys.first
      content = opts[type]

      if (builder = BUILD_RESPONSE[type])
        return builder.call(
          content,
          opts[:status] || 200,
          opts[:headers] || {},
          opts[:charset] || 'utf-8',
          content_security_policy
        )
      end

      if (mime = MIME_TYPES[type])
        return Rack::Response.new(
          content,
          opts[:status] || 200,
          DEFAULT_HEADERS.call(
            "#{mime}; charset=#{opts[:charset] || 'utf-8'}",
            opts[:headers] || {},
            content
          )
        )
      end

      _render_view(
        content,
        status: opts[:status] || 200,
        headers: opts[:headers] || {},
        layout_path: opts[:layout_path] || 'layout',
        view: opts[:view],
        response_instance: true,
        charset: opts[:charset] || 'utf-8'
      )
    end

    def _render_view(
      paths,
      status:,
      headers:,
      layout_path:,
      response_instance:,
      view:,
      charset:
    )
      base_path = config.dig(:views, :path) || 'views'
      headers['content-security-policy'] ||= content_security_policy

      file_or_nil = proc do |path|
        ::File.read(path)
      rescue Errno::ENOENT
        nil
      end

      file_content = if paths.is_a?(Array)
                       paths.map { |path| ::File.read("#{base_path}/#{path}.html.erb") }.join
                     else
                       ::File.read("#{base_path}/#{paths}.html.erb")
                     end

      layout_content = file_or_nil.call("#{base_path}/#{layout_path}.html.erb")

      parsed_erb =
        if layout_content
          load_erb(layout_content) do
            load_erb(file_content, binding_context: binding)
          end
        else
          load_erb(file_content, binding_context: binding)
        end

      if response_instance
        return Rack::Response.new(
          parsed_erb,
          status,
          DEFAULT_HEADERS.call("text/html; charset=#{charset}", headers, parsed_erb)
        )
      end

      [status, DEFAULT_HEADERS.call("text/html; charset=#{charset}", headers, parsed_erb), [parsed_erb]]
    end

    def d(content)
      raise Rackr::Dump, content
    end

    def not_found!
      raise Rackr::NotFound
    end

    # rubocop:disable Security/Eval
    def load_erb(content, binding_context: nil)
      eval(Erubi::Engine.new(content).src, binding_context)
    end
    # rubocop:enable Security/Eval

    def head(status, headers: {})
      [status, headers, []]
    end

    def redirect_to(url, headers: {})
      [302, { 'location' => url }.merge(headers), []]
    end

    def response(body = nil, status = 200, headers = {})
      Rack::Response.new(body, status, headers)
    end

    def content_security_policy
      @content_security_policy ||=
        DEFAULT_CSP_HEADERS
        .merge(config&.dig(:csp_headers) || {})
        .compact
        .map { |k, v| "#{k.to_s.tr('_', '-')} #{v}" }
        .join('; ')
    end
  end
end