Sha256: 0a02b6b50943f5749b72a0e4accde38911b09a6db3030053eb0881b58e9325f5

Contents?: true

Size: 1.02 KB

Versions: 32

Compression:

Stored size: 1.02 KB

Contents

require 'csshttprequest'

module Rack

  # A Rack middleware for providing CSSHTTPRequest responses.
  class CSSHTTPRequest

    def initialize(app)
      @app = app
    end

    # Proxies the request to the application then encodes the response with
    # the CSSHTTPRequest encoder
    def call(env)
      status, headers, response = @app.call(env)
      if chr_request?(env)
        response = encode(response)
        modify_headers!(headers, response)
      end
      [status, headers, response]
    end

    def chr_request?(env)
      env['csshttprequest.chr'] ||=
        !(/\.chr$/.match(env['PATH_INFO'])).nil? || Rack::Request.new(env).params['_format'] == 'chr'
    end

    def encode(response, assembled_body="")
      response.each { |s| assembled_body << s.to_s } # call down the stack
      return ::CSSHTTPRequest.encode(assembled_body)
    end

    def modify_headers!(headers, encoded_response)
      headers['Content-Length'] = encoded_response.length.to_s
      headers['Content-Type'] = 'text/css'
      nil
    end
  end
end

Version data entries

32 entries across 32 versions & 7 rubygems

Version Path
devver-rack-contrib-0.9.7 lib/rack/contrib/csshttprequest.rb
devver-rack-contrib-0.9.6 lib/rack/contrib/csshttprequest.rb
devver-rack-contrib-0.9.5 lib/rack/contrib/csshttprequest.rb
devver-rack-contrib-0.9.4 lib/rack/contrib/csshttprequest.rb
devver-rack-contrib-0.9.3 lib/rack/contrib/csshttprequest.rb
rack-contrib_stringbot-0.9.3 lib/rack/contrib/csshttprequest.rb
tricycle-rack-contrib-0.9.7 lib/rack/contrib/csshttprequest.rb
tricycle-rack-contrib-0.9.6 lib/rack/contrib/csshttprequest.rb
tricycle-rack-contrib-0.9.5 lib/rack/contrib/csshttprequest.rb
tricycle-rack-contrib-0.9.0 lib/rack/contrib/csshttprequest.rb
rack-contrib-with-working-jsonp-0.9.2.1 lib/rack/contrib/csshttprequest.rb
rack-contrib-0.9.2 lib/rack/contrib/csshttprequest.rb