Method: Rack::Lint#check_headers

Defined in:
lib/gems/rack-0.9.1/lib/rack/lint.rb

#check_headers(header) ⇒ Object

The Headers



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/gems/rack-0.9.1/lib/rack/lint.rb', line 320

def check_headers(header)
  ## The header must respond to each, and yield values of key and value.
  assert("headers object should respond to #each, but doesn't (got #{header.class} as headers)") {
     header.respond_to? :each
  }
  header.each { |key, value|
    ## The header keys must be Strings.
    assert("header key must be a string, was #{key.class}") {
      key.instance_of? String
    }
    ## The header must not contain a +Status+ key,
    assert("header must not contain Status") { key.downcase != "status" }
    ## contain keys with <tt>:</tt> or newlines in their name,
    assert("header names must not contain : or \\n") { key !~ /[:\n]/ }
    ## contain keys names that end in <tt>-</tt> or <tt>_</tt>,
    assert("header names must not end in - or _") { key !~ /[-_]\z/ }
    ## but only contain keys that consist of
    ## letters, digits, <tt>_</tt> or <tt>-</tt> and start with a letter.
    assert("invalid header name: #{key}") { key =~ /\A[a-zA-Z][a-zA-Z0-9_-]*\z/ }
    ##
    ## The values of the header must respond to #each.
    assert("header values must respond to #each, but the value of " +
      "'#{key}' doesn't (is #{value.class})") { value.respond_to? :each }
    value.each { |item|
      ## The values passed on #each must be Strings
      assert("header values must consist of Strings, but '#{key}' also contains a #{item.class}") {
        item.instance_of?(String)
      }
      ## and not contain characters below 037.
      assert("invalid header value #{key}: #{item.inspect}") {
        item !~ /[\000-\037]/
      }
    }
  }
end