Sha256: 6c290bd24e46365f0d23289d1bdac08ddf301e51cb7877b93d751fdc29f7e248

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

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

1 entries across 1 versions & 1 rubygems

Version Path
crystal-0.0.10 lib/crystal/rack/support.rb