struct HTTP::Headers

Overview

A Hash-like object that holds HTTP headers.

Two headers are considered the same if their downcase representation is the same (in which _ is the downcase version of -).

NOTE To use Headers, you must explicitly import it with require "http/headers"

Defined in:

http/headers.cr

Constructors

Macro Summary

Instance Method Summary

Instance methods inherited from struct Struct

==(other) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io : IO) : Nil to_s

Instance methods inherited from struct Value

==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup

Instance methods inherited from class Object

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash
hash
, in?(collection : Object) : Bool
in?(*values : Object) : Bool
in?
, inspect(io : IO) : Nil
inspect : String
inspect
, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil!
not_nil!
, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String
to_json
, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil
to_pretty_json
, to_s(io : IO) : Nil
to_s : String
to_s
, to_yaml(io : IO) : Nil
to_yaml : String
to_yaml
, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

Class methods inherited from class Object

from_json(string_or_io, root : String)
from_json(string_or_io)
from_json
, from_yaml(string_or_io : String | IO) from_yaml

Macros inherited from class Object

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

Constructor Detail

def self.new #

[View source]

Macro Detail

macro method_missing(call) #

[View source]

Instance Method Detail

def ==(other : self) #

Equality operator.

Returns true if other is equal to self.

Keys are matched case-insensitive. String values are treated equal to an array values with the same string as single element.

HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"Foo" => "bar"}   # => true
HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"foo" => "bar"}   # => true
HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"Foo" => ["bar"]} # => true
HTTP::Headers{"Foo" => "bar"} == HTTP::Headers{"Foo" => "baz"}   # => false

[View source]
def [](key) : String #

[View source]
def []=(key, value : String) #

[View source]
def []=(key, value : Array(String)) #

[View source]
def []?(key) : String | Nil #

[View source]
def add(key, value : String) : self #

Adds a header with key and value to the header set. If a header with key already exists in the set, value is appended to the existing header.

require "http/headers"

headers = HTTP::Headers.new
headers.add("Connection", "keep-alive")
headers["Connection"] # => "keep-alive"
headers.add("Connection", "Upgrade")
headers["Connection"] # => "keep-alive,Upgrade"

[View source]
def add(key, value : Array(String)) : self #

[View source]
def add?(key, value : String) : Bool #

[View source]
def add?(key, value : Array(String)) : Bool #

[View source]
def clone #

[View source]
def delete(key) : String | Nil #

[View source]
def dup #
Description copied from struct Value

Returns a shallow copy of this object.

Because Value is a value type, this method returns self, which already involves a shallow copy of this object because value types are passed by value.


[View source]
def each(&) #
Description copied from module Enumerable({String, Array(String)})

Must yield this collection's elements to the block.


[View source]
def empty? : Bool #
Description copied from module Enumerable({String, Array(String)})

Returns true if self does not contain any element.

([] of Int32).empty? # => true
([1]).empty?         # => false
[nil, false].empty?  # => false
  • #present? returns the inverse.

[View source]
def fetch(key, default) : String | Nil #

[View source]
def fetch(key, &) #

[View source]
def get(key) : Array(String) #

[View source]
def get?(key) : Array(String) | Nil #

[View source]
def has_key?(key) : Bool #

[View source]
def hash(hasher) #

[View source]
def includes_word?(key, word) : Bool #

Returns if among the headers for key there is some that contains word as a value. The word is expected to match between word boundaries (i.e. non-alphanumeric chars).

require "http/headers"

headers = HTTP::Headers{"Connection" => "keep-alive, Upgrade"}
headers.includes_word?("Connection", "Upgrade") # => true

[View source]
def inspect(io : IO) : Nil #
Description copied from struct Struct

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"

[View source]
def merge!(other) : self #

[View source]
def pretty_print(pp) #

[View source]
def same?(other : HTTP::Headers) : Bool #

[View source]
def serialize(io : IO) : Nil #

Serializes headers according to the HTTP protocol.

Prints a list of HTTP header fields in the format described in RFC 7230 §3.2, with each field terminated by a CRLF sequence ("\r\n").

The serialization does not include a double CRLF sequence at the end.

headers = HTTP::Headers{"foo" => "bar", "baz" => %w[qux qox]}
headers.serialize # => "foo: bar\r\nbaz: qux\r\nbaz: qox\r\n"

[View source]
def serialize : String #

Serializes headers according to the HTTP protocol.

Prints a list of HTTP header fields in the format described in RFC 7230 §3.2, with each field terminated by a CRLF sequence ("\r\n").

The serialization does not include a double CRLF sequence at the end.

headers = HTTP::Headers{"foo" => "bar", "baz" => %w[qux qox]}
headers.serialize # => "foo: bar\r\nbaz: qux\r\nbaz: qox\r\n"

[View source]
def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


[View source]
def valid_value?(value) : Bool #

[View source]