Sha256: 0951d35496362bc20c80eb592357d4f5113d42149cd255a328cf5f065b29adf5

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

require 'thread'
require 'time'
require 'uri'
# require 'addressable/uri'
require 'rack'
require 'rack/builder'

module Crystal
  # The request object. See Rack::Request for more info:
  # http://rack.rubyforge.org/doc/classes/Rack/Request.html
  class Request < Rack::Request
    # Returns an array of acceptable media types for the response
    def accept
      @env['HTTP_ACCEPT'].to_s.split(',').map { |a| a.split(';')[0].strip }
    end

    def secure?
      (@env['HTTP_X_FORWARDED_PROTO'] || @env['rack.url_scheme']) == 'https'
    end

    # Override Rack < 1.1's Request#params implementation (see lh #72 for
    # more info) and add a Request#user_agent method.
    # XXX remove when we require rack > 1.1
    if Rack.release < '1.1'
      def params
        self.GET.update(self.POST)
      rescue EOFError, Errno::ESPIPE
        self.GET
      end

      def user_agent
        @env['HTTP_USER_AGENT']
      end
    end
  end
  
  # The response object. See Rack::Response and Rack::ResponseHelpers for
  # more info:
  # http://rack.rubyforge.org/doc/classes/Rack/Response.html
  # http://rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html
  class Response < Rack::Response
    def finish
      @body = block if block_given?
      if [204, 304].include?(status.to_i)
        header.delete "Content-Type"
        [status.to_i, header.to_hash, []]
      else
        body = @body || []
        body = [body] if body.respond_to? :to_str
        if body.respond_to?(:to_ary)
          header["Content-Length"] = body.to_ary.
            inject(0) { |len, part| len + Rack::Utils.bytesize(part) }.to_s
        end
        [status.to_i, header.to_hash, body]
      end
    end
  end
  
  class NotFound < NameError #:nodoc:
    def code ; 404 ; end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
crystal-0.0.8 crystal/lib/crystal/rack/support.rb
crystal-0.0.7 crystal/lib/crystal/rack/support.rb
crystal-0.0.6 crystal/lib/crystal/rack/support.rb
crystal-0.0.5 lib/crystal/rack/support.rb
crystal-0.0.4 lib/crystal/rack/support.rb
crystal-0.0.3 lib/crystal/rack/support.rb
crystal-0.0.2 lib/crystal/rack/support.rb