HTTPHeaders

public struct HTTPHeaders : CustomStringConvertible

A representation of a block of HTTP header fields.

HTTP header fields are a complex data structure. The most natural representation for these is a sequence of two-tuples of field name and field value, both as strings. This structure preserves that representation, but provides a number of convenience features in addition to it.

For example, this structure enables access to header fields based on the case-insensitive form of the field name, but preserves the original case of the field when needed. It also supports recomposing headers to a maximally joined or split representation, such that header fields that are able to be repeated can be represented appropriately.

  • Declaration

    Swift

    public var description: String { get }
  • Creates a header block from a pre-filled contiguous string buffer containing a UTF-8 encoded HTTP header block, along with a list of the locations of each name/value pair within the block.

    Note

    This is public to aid in the creation of supplemental HTTP libraries, e.g. NIOHTTP2 and NIOHPACK. It is not intended for general use.

    Declaration

    Swift

    public static func createHeaderBlock(buffer: ByteBuffer, headers: [HTTPHeader]) -> HTTPHeaders

    Parameters

    buffer

    A buffer containing UTF-8 encoded HTTP headers.

    headers

    The locations within buffer of the name and value of each header.

    Return Value

    A new HTTPHeaders using the provided buffer as storage.

  • Provides access to raw UTF-8 storage of the headers in this header block, along with a list of the header strings’ indices.

    Note

    This is public to aid in the creation of supplemental HTTP libraries, e.g. NIOHTTP2 and NIOHPACK. It is not intended for general use.

    Declaration

    Swift

    public func withUnsafeBufferAndIndices<R>(_ block: (_ buf: ByteBuffer, _ locations: [HTTPHeader], _ contiguous: Bool) throws -> R) rethrows -> R

    Parameters

    block

    A block that will be provided UTF-8 header block information.

    buf

    A raw ByteBuffer containing potentially-contiguous sequences of UTF-8 encoded characters.

    locations

    An array of HTTPHeaders, each of which contains information on the location in the buffer of both a header’s name and value.

    contiguous

    A Bool indicating whether the headers are stored contiguously, with no padding or orphaned data within the block. If this is true, then the buffer represents a HTTP/1 header block appropriately encoded for the wire.

  • Construct a HTTPHeaders structure.

    Declaration

    Swift

    public init(_ headers: [(String, String)] = [])
  • Construct a HTTPHeaders structure.

    Declaration

    Swift

    public init(_ headers: [(String, String)] = [], allocator: ByteBufferAllocator)
  • Declaration

    Swift

    public mutating func add(name: String, value: String)

    Parameters

    value

    The header field value to add for the given name.

  • Declaration

    Swift

    public mutating func replaceOrAdd(name: String, value: String)

    Parameters

    value

    The header field value to add for the given name.

  • Remove all values for a given header name from the block.

    This method uses case-insensitive comparisons for the header field name.

    Declaration

    Swift

    public mutating func remove(name: String)

    Parameters

    name

    The name of the header field to remove from the block.

  • Retrieve all of the values for a give header field name from the block.

    This method uses case-insensitive comparisons for the header field name. It does not return a maximally-decomposed list of the header fields, but instead returns them in their original representation: that means that a comma-separated header field list may contain more than one entry, some of which contain commas and some do not. If you want a representation of the header fields suitable for performing computation on, consider getCanonicalForm.

    Declaration

    Swift

    public subscript(name: String) -> [String] { get }

    Parameters

    name

    The header field name whose values are to be retrieved.

    Return Value

    A list of the values for that header field name.

  • Undocumented

    Declaration

    Swift

    public func contains(name: String) -> Bool
  • Undocumented

    Declaration

    Swift

    public func getCanonicalForm(_ name: String) -> [String]
  • Retrieves the header values for the given header field in canonical form: that is, splitting them on commas as extensively as possible such that multiple values received on the one line are returned as separate entries. Also respects the fact that Set-Cookie should not be split in this way.

    Declaration

    Swift

    public subscript(canonicalForm name: String) -> [String] { get }

    Parameters

    name

    The header field name whose values are to be retrieved.

    Return Value

    A list of the values for that header field name.

  • An iterator of HTTP header fields.

    This iterator will return each value for a given header name separately. That means that name is not guaranteed to be unique in a given block of headers.

    See more

    Declaration

    Swift

    public struct Iterator : IteratorProtocol
  • Declaration

    Swift

    public func makeIterator() -> Iterator
  • Declaration

    Swift

    public static func ==(lhs: HTTPHeaders, rhs: HTTPHeaders) -> Bool