End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
CGI#http_header
# File tmp/rubies/ruby-3.1.3/lib/cgi/core.rb, line 160
def http_header(options='text/html')
if options.is_a?(String)
content_type = options
buf = _header_for_string(content_type)
elsif options.is_a?(Hash)
if options.size == 1 && options.has_key?('type')
content_type = options['type']
buf = _header_for_string(content_type)
else
buf = _header_for_hash(options.dup)
end
else
raise ArgumentError.new("expected String or Hash but got #{options.class}")
end
if defined?(MOD_RUBY)
_header_for_modruby(buf)
return ''
else
buf << EOL # empty line of separator
return buf
end
end
Create an HTTP header block as a string.
Includes the empty line that ends the header block.
content_type_string-
If this form is used, this string is the
Content-Type headers_hash-
A
Hashof header values. 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 as a
String, returned as the Status header. The values 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
MethodNot 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
MethodNot Implemented - BAD_GATEWAY
-
502 Bad Gateway
- VARIANT_ALSO_VARIES
-
506 Variant Also Negotiates
- 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
Timeobject, 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::Cookieobject; anArrayof literal cookie strings orCookieobjects; or a hash all of whose values are literal cookie strings orCookieobjects.These cookies are in addition to the cookies held in the @output_cookies field.
Other headers can also be set; they are appended as key: value.
Examples:
http_header # Content-Type: text/html http_header("text/plain") # Content-Type: text/plain http_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")
This method does not perform charset conversion.