Method: Rack::Lint#check_headers

Defined in:
lib/rack/lint.rb

#check_headers(header) ⇒ Object

The Headers



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/rack/lint.rb', line 402

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 be Strings,
    assert("a header value must be a String, but the value of " +
      "'#{key}' is a #{value.class}") { value.kind_of? String }
    ## consisting of lines (for multiple header values, e.g. multiple
    ## <tt>Set-Cookie</tt> values) seperated by "\n".
    value.split("\n").each { |item|
      ## The lines must not contain characters below 037.
      assert("invalid header value #{key}: #{item.inspect}") {
        item !~ /[\000-\037]/
      }
    }
  }
end