Class: ProxyPacRb::Rack::ProxyPacCompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_pac_rb/rack/proxy_pac_compressor.rb

Overview

Rack Middleware to compress proxy pac

require 'proxy_pac_rb/rack/proxy_pac_compressor' use ProxyPacRb::Rack::ProxyPacCompressor

require 'proxy_pac_rb/rack/proxy_pac_compressor' use ProxyPacRb::Rack::ProxyPacCompressor

require 'proxy_pac_rb/rack/proxy_pac_compressor' use ProxyPacRb::Rack::ProxyPacCompressor

Examples:

Sinatra's <server>.rb

Rack's config.ru

Middleman's config.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, enabled: true, options: nil) ⇒ ProxyPacCompressor

Returns a new instance of ProxyPacCompressor


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/proxy_pac_rb/rack/proxy_pac_compressor.rb', line 28

def initialize(
  app,
  enabled: true,
  options: nil
)
  @app        = app

  @compressor = if options.blank?
                  ProxyPacRb::ProxyPacCompressor.new
                else
                  ProxyPacRb::ProxyPacCompressor.new(options: options)
                end

  @loader     = ProxyPacRb::ProxyPacLoader.new
  @enabled    = enabled
end

Instance Method Details

#call(env) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/proxy_pac_rb/rack/proxy_pac_compressor.rb', line 45

def call(env)
  status, headers, body = @app.call(env)

  return [status, headers, body] if enabled == false
  return [status, headers, body] unless headers.key?('Content-Type') \
    && %r{application/x-ns-proxy-autoconfig} === headers['Content-Type']

  content = []
  body.each { |part| content << part }
  content = content.join

  begin
    proxy_pac = ProxyPacFile.new(source: content)

    loader.load(proxy_pac)
    compressor.compress(proxy_pac)

    content = proxy_pac.content
  rescue => err
    status  = 500
    content = err.message
  end

  headers['Content-Length'] = content.bytesize.to_s if headers['Content-Length']

  [status, headers, [content]]
ensure
  body.close if body.respond_to? :close
end