Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rack::Request#headers for getting access to HTTP headers #1881

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Apr 27, 2022

  1. Add Rack::Request#headers for getting access to HTTP headers

    In addition to being backwards compatible, I think this offers
    a nicer API:
    
    ```ruby
      request.headers['host']           # env['HTTP_HOST']
      request.headers['content-type']   # env['CONTENT_TYPE']
      request.headers['content-length'] # env['CONTENT_LENGTH']
    
      request.headers['allow'] = 'GET'     # env['HTTP_ALLOW'] = 'GET'
      request.headers.add('allow', 'POST') # env['HTTP_ALLOW'] #=> %w'GET POST'
    
      request.headers.has_key?('allow')
      request.headers.fetch('allow')
      request.headers.delete('allow')
    
      request.headers.each{|key, value| }
      request.headers.to_h
    ```
    
    You can compare this to a backwards incompatible proposal to modify
    the related *_header methods:
    
    ```ruby
      # This proposal
      request.headers['host']
    
      # Backwards incompatible alternative
      request.get_header('host')
    ```
    
    This only allocates potentially one Rack::Request::Headers object
    per request, one the first call to Rack::Request#headers.  I don't
    think this will be a performance issue.  Any code desiring maximum
    performance should access env directly.
    jeremyevans committed Apr 27, 2022
    Configuration menu
    Copy the full SHA
    1bd47e3 View commit details
    Browse the repository at this point in the history