Method: CGI#header

Defined in:
lib/cgi.rb

#header(options = "text/html") ⇒ Object

Create an HTTP header block as a string.

Includes the empty line that ends the header block.

options can be a string specifying the Content-Type (defaults to text/html), or a hash of header key/value pairs. The following header keys are recognized:

type

the Content-Type header. Defaults to “text/html”

charset

the charset of the body, appended to the Content-Type header.

nph

a boolean value. If true, prepend protocol string and status code, and date; and sets default values for “server” and “connection” if not explicitly set.

status

the HTTP status code, returned as the Status header. See the list of available status codes below.

server

the server software, returned as the Server header.

connection

the connection type, returned as the Connection header (for instance, “close”.

length

the length of the content that will be sent, returned as the Content-Length header.

language

the language of the content, returned as the Content-Language header.

expires

the time on which the current content expires, as a Time object, returned as the Expires header.

cookie

a cookie or cookies, returned as one or more Set-Cookie headers. The value can be the literal string of the cookie; a CGI::Cookie object; an Array of literal cookie strings or Cookie objects; or a hash all of whose values are literal cookie strings or Cookie objects. These cookies are in addition to the cookies held in the @output_cookies field.

Other header lines can also be set; they are appended as key: value.

header
  # Content-Type: text/html

header("text/plain")
  # Content-Type: text/plain

header("nph"        => true,
       "status"     => "OK",  # == "200 OK"
         # "status"     => "200 GOOD",
       "server"     => ENV['SERVER_SOFTWARE'],
       "connection" => "close",
       "type"       => "text/html",
       "charset"    => "iso-2022-jp",
         # Content-Type: text/html; charset=iso-2022-jp
       "length"     => 103,
       "language"   => "ja",
       "expires"    => Time.now + 30,
       "cookie"     => [cookie1, cookie2],
       "my_header1" => "my_value"
       "my_header2" => "my_value")

The status codes are:

"OK"                  --> "200 OK"
"PARTIAL_CONTENT"     --> "206 Partial Content"
"MULTIPLE_CHOICES"    --> "300 Multiple Choices"
"MOVED"               --> "301 Moved Permanently"
"REDIRECT"            --> "302 Found"
"NOT_MODIFIED"        --> "304 Not Modified"
"BAD_REQUEST"         --> "400 Bad Request"
"AUTH_REQUIRED"       --> "401 Authorization Required"
"FORBIDDEN"           --> "403 Forbidden"
"NOT_FOUND"           --> "404 Not Found"
"METHOD_NOT_ALLOWED"  --> "405 Method Not Allowed"
"NOT_ACCEPTABLE"      --> "406 Not Acceptable"
"LENGTH_REQUIRED"     --> "411 Length Required"
"PRECONDITION_FAILED" --> "412 Precondition Failed"
"SERVER_ERROR"        --> "500 Internal Server Error"
"NOT_IMPLEMENTED"     --> "501 Method Not Implemented"
"BAD_GATEWAY"         --> "502 Bad Gateway"
"VARIANT_ALSO_VARIES" --> "506 Variant Also Negotiates"

This method does not perform charset conversion.



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/cgi.rb', line 539

def header(options = "text/html")

  buf = ""

  case options
  when String
    options = { "type" => options }
  when Hash
    options = options.dup
  end

  unless options.has_key?("type")
    options["type"] = "text/html"
  end

  if options.has_key?("charset")
    options["type"] += "; charset=" + options.delete("charset")
  end

  options.delete("nph") if defined?(MOD_RUBY)
  if options.delete("nph") or
      (/IIS\/(\d+)/n.match(env_table['SERVER_SOFTWARE']) and $1.to_i < 5)
    buf += (env_table["SERVER_PROTOCOL"] or "HTTP/1.0")  + " " +
           (HTTP_STATUS[options["status"]] or options["status"] or "200 OK") +
           EOL +
           "Date: " + CGI::rfc1123_date(Time.now) + EOL

    unless options.has_key?("server")
      options["server"] = (env_table['SERVER_SOFTWARE'] or "")
    end

    unless options.has_key?("connection")
      options["connection"] = "close"
    end

    options.delete("status")
  end

  if options.has_key?("status")
    buf += "Status: " +
           (HTTP_STATUS[options["status"]] or options["status"]) + EOL
    options.delete("status")
  end

  if options.has_key?("server")
    buf += "Server: " + options.delete("server") + EOL
  end

  if options.has_key?("connection")
    buf += "Connection: " + options.delete("connection") + EOL
  end

  buf += "Content-Type: " + options.delete("type") + EOL

  if options.has_key?("length")
    buf += "Content-Length: " + options.delete("length").to_s + EOL
  end

  if options.has_key?("language")
    buf += "Content-Language: " + options.delete("language") + EOL
  end

  if options.has_key?("expires")
    buf += "Expires: " + CGI::rfc1123_date( options.delete("expires") ) + EOL
  end

  if options.has_key?("cookie")
    if options["cookie"].kind_of?(String) or
         options["cookie"].kind_of?(Cookie)
      buf += "Set-Cookie: " + options.delete("cookie").to_s + EOL
    elsif options["cookie"].kind_of?(Array)
      options.delete("cookie").each{|cookie|
        buf += "Set-Cookie: " + cookie.to_s + EOL
      }
    elsif options["cookie"].kind_of?(Hash)
      options.delete("cookie").each_value{|cookie|
        buf += "Set-Cookie: " + cookie.to_s + EOL
      }
    end
  end
  if @output_cookies
    for cookie in @output_cookies
      buf += "Set-Cookie: " + cookie.to_s + EOL
    end
  end

  options.each{|key, value|
    buf += key + ": " + value.to_s + EOL
  }

  if defined?(MOD_RUBY)
    table = Apache::request.headers_out
    buf.scan(/([^:]+): (.+)#{EOL}/n){ |name, value|
      warn sprintf("name:%s value:%s\n", name, value) if $DEBUG
      case name
      when 'Set-Cookie'
        table.add(name, value)
      when /^status$/ni
        Apache::request.status_line = value
        Apache::request.status = value.to_i
      when /^content-type$/ni
        Apache::request.content_type = value
      when /^content-encoding$/ni
        Apache::request.content_encoding = value
      when /^location$/ni
 if Apache::request.status == 200
   Apache::request.status = 302
 end
        Apache::request.headers_out[name] = value
      else
        Apache::request.headers_out[name] = value
      end
    }
    Apache::request.send_http_header
    ''
  else
    buf + EOL
  end

end