Implementation of HPACK encoding and header tables (#10)

Motivation:

This is the beginning of removing the dependency on `nghttp2`.

Modifications:

I've added a new library target, `NIOHPACK`, which contains all the necessary pieces to implement HPACK encoding as specified in RFC 7541. There are unit tests exercising all the code and some additional edge cases around integer encoding, along with some performance tests for the Huffman encode/decode process. The latter currently takes longer to decode than to encode, which is worrying, but this is only really noticeable when encoding > 16KB strings as a rule; therefore I've decided not to pull out the optimization hammer just yet. Included in the unit tests are the inputs and outputs from all the examples in RFC 7541 and RFC 7540.

I've tried to match the coding style from the repo, but some of my own idioms may have crept in here & there. Please let me know if you see anything wrong in that regard.

Currently the static header table and Huffman encoding tables are implemented as Swift `let`s. For the static header table that's ok (only 62 entries), but the Huffman table is huge. I'm debating whether it's worth having that in a plain C file somewhere, but I'm not sure what the tradeoffs are vs. performance. Right now it seems to only affect compilation time, but I may be wrong.

Result:

In general, nothing will change: nothing in NIOHTTP2 is using this library yet. That part is what I'm working on now.
This commit is contained in:
Jim Dovey authored and Cory Benfield committed 2018-08-16 09:51:13 +01:00
1 parent b7085b6c0d
commit 6f8f523684
34 files changed
+9857 -3

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
[submodule "hpack-test-case"]
path = hpack-test-case
url = git@github.com:http2jp/hpack-test-case
+2
View File
@@ -1,3 +1,5 @@
Johannes Weiß <johannesweiss@apple.com>
<cbenfield@apple.com> <lukasaoz@gmail.com>
<cbenfield@apple.com> <lukasa@apple.com>
<jimdovey@mac.com> <jdovey@linkedin.com>
<tomer@apple.com> <tomer.doron@gmail.com>
+3
View File
@@ -12,7 +12,10 @@ needs to be listed here.
### Contributors
- Cory Benfield <cbenfield@apple.com>
- Jim Dovey <jimdovey@mac.com>
- Johannes Weiß <johannesweiss@apple.com>
- Tim <0xTim@users.noreply.github.com>
- tomer doron <tomer@apple.com>
**Updating this list**
+6 -2
View File
@@ -19,10 +19,10 @@ let package = Package(
name: "swift-nio-http2",
products: [
.executable(name: "NIOHTTP2Server", targets: ["NIOHTTP2Server"]),
.library(name: "NIOHTTP2", targets: ["NIOHTTP2"])
.library(name: "NIOHTTP2", targets: ["NIOHTTP2"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "1.7.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "1.9.0"),
.package(url: "https://github.com/apple/swift-nio-nghttp2-support.git", from: "1.0.0"),
],
targets: [
@@ -31,7 +31,11 @@ let package = Package(
dependencies: ["NIOHTTP2"]),
.target(name: "NIOHTTP2",
dependencies: ["NIO", "NIOHTTP1", "NIOTLS", "CNIONghttp2"]),
.target(name: "NIOHPACK",
dependencies: ["NIO", "NIOConcurrencyHelpers", "NIOHTTP1"]),
.testTarget(name: "NIOHTTP2Tests",
dependencies: ["NIO", "NIOHTTP1", "NIOHTTP2"]),
.testTarget(name: "NIOHPACKTests",
dependencies: ["NIOHPACK"])
]
)
+173
View File
@@ -0,0 +1,173 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/// Implements the dynamic part of the HPACK header table, as defined in
/// [RFC 7541 § 2.3](https://httpwg.org/specs/rfc7541.html#dynamic.table).
struct DynamicHeaderTable {
public static let defaultSize = 4096
/// The actual table, with items looked up by index.
private var storage: HeaderTableStorage
/// The length of the contents of the table.
var length: Int {
return self.storage.length
}
/// The size to which the dynamic table may currently grow. Represents
/// the current maximum length signaled by the peer via a table-resize
/// value at the start of an encoded header block.
///
/// - note: This value cannot exceed `self.maximumTableLength`.
var allowedLength: Int {
get {
return self.storage.maxSize
}
set {
self.storage.setTableSize(to: newValue)
}
}
/// The maximum permitted size of the dynamic header table as set
/// through a SETTINGS_HEADER_TABLE_SIZE value in a SETTINGS frame.
var maximumTableLength: Int {
didSet {
if self.allowedLength > maximumTableLength {
self.allowedLength = maximumTableLength
}
}
}
/// The number of items in the table.
var count: Int {
return self.storage.count
}
init(allocator: ByteBufferAllocator, maximumLength: Int = DynamicHeaderTable.defaultSize) {
self.storage = HeaderTableStorage(allocator: allocator, maxSize: maximumLength)
self.maximumTableLength = maximumLength
self.allowedLength = maximumLength // until we're told otherwise, this is what we assume the other side expects.
}
/// Subscripts into the dynamic table alone, using a zero-based index.
subscript(i: Int) -> HeaderTableEntry {
return self.storage[i]
}
func view(of index: HPACKHeaderIndex) -> ByteBufferView {
return self.storage.view(of: index)
}
// internal for testing
func dumpHeaders() -> String {
return self.storage.dumpHeaders(offsetBy: StaticHeaderTable.count)
}
// internal for testing -- clears the dynamic table
mutating func clear() {
self.storage.purge(toRelease: self.storage.length)
}
/// Searches the table for a matching header, optionally with a particular value. If
/// a match is found, returns the index of the item and an indication whether it contained
/// the matching value as well.
///
/// Invariants: If `value` is `nil`, result `containsValue` is `false`.
///
/// - Parameters:
/// - name: The name of the header for which to search.
/// - value: Optional value for the header to find. Default is `nil`.
/// - Returns: A tuple containing the matching index and, if a value was specified as a
/// parameter, an indication whether that value was also found. Returns `nil`
/// if no matching header name could be located.
func findExistingHeader<Name: Collection, Value: Collection>(named name: Name, value: Value?) -> (index: Int, containsValue: Bool)? where Name.Element == UInt8, Value.Element == UInt8 {
// looking for both name and value, but can settle for just name if no value
// has been provided. Return the first matching name (lowest index) in that case.
guard let value = value else {
// no `first` on AnySequence, just `first(where:)`
return self.storage.firstIndex(matching: name).map { ($0, false) }
}
// If we have a value, locate the index of the lowest header which contains that
// value, but if no value matches, return the index of the lowest header with a
// matching name alone.
var firstNameMatch: Int? = nil
for index in self.storage.indices(matching: name) {
if firstNameMatch == nil {
// record the first (most recent) index with a matching header name,
// in case there's no value match.
firstNameMatch = index
}
if self.storage.view(of: self.storage[index].value).matches(value) {
// this entry has both the name and the value we're seeking
return (index, true)
}
}
// no value matches -- but did we find a name?
if let index = firstNameMatch {
return (index, false)
} else {
// no matches at all
return nil
}
}
/// Appends a header to the table. Note that if this succeeds, the new item's index
/// is always zero.
///
/// This call may result in an empty table, as per RFC 7541 § 4.4:
/// > "It is not an error to attempt to add an entry that is larger than the maximum size;
/// > an attempt to add an entry larger than the maximum size causes the table to be
/// > emptied of all existing entries and results in an empty table."
///
/// - Parameters:
/// - name: A collection of UTF-8 code points comprising the name of the header to insert.
/// - value: A collection of UTF-8 code points comprising the value of the header to insert.
/// - Returns: `true` if the header was added to the table, `false` if not.
mutating func addHeader<Name: Collection, Value: Collection>(named name: Name, value: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
do {
try self.storage.add(name: name, value: value)
} catch let error as RingBufferError.BufferOverrun {
// ping the error up the stack, with more information
throw NIOHPACKErrors.FailedToAddIndexedHeader(bytesNeeded: self.storage.length + error.amount,
name: name, value: value)
}
}
/// Appends a header to the table. Note that if this succeeds, the new item's index
/// is always zero.
///
/// This call may result in an empty table, as per RFC 7541 § 4.4:
/// > "It is not an error to attempt to add an entry that is larger than the maximum size;
/// > an attempt to add an entry larger than the maximum size causes the table to be
/// > emptied of all existing entries and results in an empty table."
///
/// - Parameters:
/// - name: A contiguous collection of UTF-8 bytes comprising the name of the header to insert.
/// - value: A contiguous collection of UTF-8 bytes comprising the value of the header to insert.
/// - Returns: `true` if the header was added to the table, `false` if not.
mutating func addHeader<Name: ContiguousCollection, Value: ContiguousCollection>(nameBytes: Name, valueBytes: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
do {
try self.storage.add(nameBytes: nameBytes, valueBytes: valueBytes)
} catch let error as RingBufferError.BufferOverrun {
// convert the error to something more useful/meaningful to client code.
throw NIOHPACKErrors.FailedToAddIndexedHeader(bytesNeeded: self.storage.length + error.amount,
name: nameBytes, value: valueBytes)
}
}
}
+233
View File
@@ -0,0 +1,233 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/// An `HPACKDecoder` maintains its own dynamic header table and uses that to
/// decode indexed HTTP headers, along with Huffman-encoded strings and
/// HPACK-encoded integers.
///
/// There are two pieces of shared state: the dynamic header table and an
/// internal `ByteBuffer` used for decode operations. Each decode operation
/// will update the header table as described in RFC 7541, appending and
/// evicting items as described there.
///
/// - note: This type is not thread-safe. It is designed to be owned and operated
/// by a single HTTP/2 stream, operating on a single NIO `EventLoop`.
public struct HPACKDecoder {
public static var maxDynamicTableSize: Int {
return DynamicHeaderTable.defaultSize
}
// private but tests
var headerTable: IndexedHeaderTable
private var decodeBuffer: ByteBuffer
var dynamicTableLength: Int {
return headerTable.dynamicTableLength
}
var allowedDynamicTableLength: Int {
get { return self.headerTable.dynamicTableAllowedLength }
set { self.headerTable.dynamicTableAllowedLength = newValue }
}
/// A string value discovered in a HPACK buffer. The value can either indicate an entry
/// in the header table index or the start of an inline literal string.
enum HPACKString {
case indexed(index: Int)
case literal
init(fromEncodedInteger value: Int) {
switch value {
case 0:
self = .literal
default:
self = .indexed(index: value)
}
}
}
/// The maximum size of the dynamic table. This is defined in RFC 7541
/// to be the sum of [name-octet-count] + [value-octet-count] + 32 for
/// each header it contains.
public internal(set) var maxDynamicTableLength: Int {
get { return headerTable.dynamicTableAllowedLength }
/* private but tests */
set { headerTable.dynamicTableAllowedLength = newValue }
}
/// Creates a new decoder
///
/// - Parameter maxDynamicTableSize: Maximum allowed size of the dynamic header table.
public init(allocator: ByteBufferAllocator, maxDynamicTableSize: Int = HPACKDecoder.maxDynamicTableSize) {
self.headerTable = IndexedHeaderTable(allocator: allocator, maxDynamicTableSize: maxDynamicTableSize)
// start the decode buffer at the requested size of the dynamic header list table, or the default
// size, whichever is larger.
self.decodeBuffer = allocator.buffer(capacity: max(maxDynamicTableSize, DynamicHeaderTable.defaultSize))
}
/// Reads HPACK encoded header data from a `ByteBuffer`.
///
/// It is expected that the buffer cover only the bytes of the header list, i.e.
/// that this is in fact a slice containing only the payload bytes of a
/// `HEADERS` or `CONTINUATION` frame.
///
/// - Parameter buffer: A byte buffer containing the encoded header bytes.
/// - Returns: An array of (name, value) pairs.
/// - Throws: HpackDecoder.Error in the event of a decode failure.
public mutating func decodeHeaders(from buffer: inout ByteBuffer) throws -> HPACKHeaders {
// take a local copy to mutate
var bufCopy = buffer
self.decodeBuffer.clear()
var headers: [HPACKHeader] = []
while bufCopy.readableBytes > 0 {
// this will update our own `decodeBuffer` property.
if let header = try self.decodeHeader(from: &bufCopy) {
headers.append(header)
}
}
buffer = bufCopy
return HPACKHeaders(buffer: self.decodeBuffer, headers: headers)
}
private mutating func decodeHeader(from buffer: inout ByteBuffer) throws -> HPACKHeader? {
// peek at the first byte to decide how many significant bits of that byte we
// will need to include in our decoded integer. Some values use a one-bit prefix,
// some use two, or four.
// This one-byte read is guaranteed safe because `decodeHeaders(from:)` only calls into
// this function if at least one byte is available to read.
let initial: UInt8 = buffer.getInteger(at: buffer.readerIndex)!
switch initial {
case let x where x & 0b1000_0000 == 0b1000_0000:
// 0b1xxxxxxx -- one-bit prefix, seven bits of value
// purely-indexed header field/value
let hidx = try buffer.readEncodedInteger(withPrefix: 7)
return try self.decodeIndexedHeader(from: Int(hidx))
case let x where x & 0b1100_0000 == 0b0100_0000:
// 0b01xxxxxx -- two-bit prefix, six bits of value
// literal header with possibly-indexed name
let hidx = try buffer.readEncodedInteger(withPrefix: 6)
return try self.decodeLiteralHeader(from: &buffer, headerName: HPACKString(fromEncodedInteger: hidx), addToIndex: true)
case let x where x & 0b1111_0000 == 0b0000_0000:
// 0b0000xxxx -- four-bit prefix, four bits of value
// literal header with possibly-indexed name, not added to dynamic table
let hidx = try buffer.readEncodedInteger(withPrefix: 4)
var header = try self.decodeLiteralHeader(from: &buffer, headerName: HPACKString(fromEncodedInteger: hidx), addToIndex: false)
header.indexing = .nonIndexable
return header
case let x where x & 0b1111_0000 == 0b0001_0000:
// 0b0001xxxx -- four-bit prefix, four bits of value
// literal header with possibly-indexed name, never added to dynamic table nor
// rewritten by proxies
let hidx = try buffer.readEncodedInteger(withPrefix: 4)
var header = try self.decodeLiteralHeader(from: &buffer, headerName: HPACKString(fromEncodedInteger: hidx), addToIndex: false)
header.indexing = .neverIndexed
return header
case let x where x & 0b1110_0000 == 0b0010_0000:
// 0b001xxxxx -- three-bit prefix, five bits of value
// dynamic header table size update
let newMaxLength = try Int(buffer.readEncodedInteger(withPrefix: 5))
// RFC 7541 § 4.2 <https://httpwg.org/specs/rfc7541.html#maximum.table.size> gives us two conditions:
//
// 1. "the chosen size MUST stay lower than or equal to the maximum set by the protocol."
guard newMaxLength <= self.headerTable.maxDynamicTableLength else {
throw NIOHPACKErrors.InvalidDynamicTableSize(requestedSize: newMaxLength, allowedSize: self.headerTable.maxDynamicTableLength)
}
// 2. "This dynamic table size update MUST occur at the beginning of the first header block
// following the change to the dynamic table size."
guard self.decodeBuffer.readableBytes == 0 else {
// If our decode buffer has any data in it, then this is out of place.
// Treat it as an invalid input
throw NIOHPACKErrors.IllegalDynamicTableSizeChange()
}
self.headerTable.dynamicTableAllowedLength = newMaxLength
return nil
default:
throw NIOHPACKErrors.InvalidHeaderStartByte(byte: initial)
}
}
private mutating func decodeIndexedHeader(from hidx: Int) throws -> HPACKHeader {
let (h, v) = try self.headerTable.headerViews(at: hidx)
let start = self.decodeBuffer.writerIndex
let nlen = self.decodeBuffer.write(bytes: h)
let vlen = self.decodeBuffer.write(bytes: v)
return HPACKHeader(start: start, nameLength: nlen, valueLength: vlen)
}
private mutating func decodeLiteralHeader(from buffer: inout ByteBuffer, headerName: HPACKString,
addToIndex: Bool) throws -> HPACKHeader {
let nameStart = self.decodeBuffer.writerIndex
let nameLen: Int
let valueLen: Int
switch headerName {
case .indexed(let idx):
let (name, _) = try self.headerTable.headerViews(at: idx)
nameLen = self.decodeBuffer.write(bytes: name)
case .literal:
nameLen = try self.readEncodedString(from: &buffer)
}
valueLen = try self.readEncodedString(from: &buffer)
if addToIndex {
let nameView = self.decodeBuffer.viewBytes(at: nameStart, length: nameLen)
let valueView = self.decodeBuffer.viewBytes(at: nameStart + nameLen, length: valueLen)
try headerTable.add(headerNameBytes: nameView, valueBytes: valueView)
}
return HPACKHeader(start: nameStart, nameLength: nameLen, valueLength: valueLen)
}
private mutating func readEncodedString(from buffer: inout ByteBuffer) throws -> Int {
// peek to read the encoding bit
guard let initialByte: UInt8 = buffer.getInteger(at: buffer.readerIndex) else {
throw NIOHPACKErrors.InsufficientInput()
}
let huffmanEncoded = initialByte & 0x80 == 0x80
// read the length; there's a seven-bit prefix here (one-bit encoding flag)
let len = try Int(buffer.readEncodedInteger(withPrefix: 7))
guard len <= buffer.readableBytes else {
throw NIOHPACKErrors.StringLengthBeyondPayloadSize(length: len, available: buffer.readableBytes)
}
let outputLength: Int
if huffmanEncoded {
outputLength = try buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: len, into: &self.decodeBuffer)
} else {
outputLength = self.decodeBuffer.write(bytes: buffer.viewBytes(at: buffer.readerIndex, length: len))
}
buffer.moveReaderIndex(forwardBy: len)
return outputLength
}
}
+297
View File
@@ -0,0 +1,297 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/// An `HPACKEncoder` maintains its own dynamic header table and uses that to
/// encode HTTP headers to an internal byte buffer.
///
/// This encoder functions as an accumulator: each encode operation will append
/// bytes to a buffer maintained by the encoder, which must be cleared using
/// `reset()` before the encode can be re-used. It maintains a header table for
/// outbound header indexing, and will update the header table as described in
/// RFC 7541, appending and evicting items as described there.
public struct HPACKEncoder {
/// The default size of the encoder's dynamic header table.
public static var defaultDynamicTableSize: Int { return DynamicHeaderTable.defaultSize }
private static let defaultDataBufferSize = 128
public struct HeaderDefinition {
var name: String
var value: String
var indexing: HPACKIndexing
}
private enum EncoderState {
case idle
case resized(smallestMaxTableSize: Int?)
case encoding
}
// private but tests
var headerIndexTable: IndexedHeaderTable
private var state: EncoderState
private var buffer: ByteBuffer!
/// Whether to use Huffman encoding.
public let useHuffmanEncoding: Bool
/// The current size of the dynamic table.
///
/// This is defined as the sum of [name] + [value] + 32 for each header.
public var dynamicTableSize: Int {
return self.headerIndexTable.dynamicTableLength
}
/// The current maximum size to which the dynamic header table may grow.
public private(set) var allowedDynamicTableSize: Int {
get { return self.headerIndexTable.dynamicTableAllowedLength }
set { self.headerIndexTable.dynamicTableAllowedLength = newValue }
}
/// The hard maximum size of the dynamic header table, set via an HTTP/2
/// SETTINGS frame.
public var maximumDynamicTableSize: Int {
get { return self.headerIndexTable.maxDynamicTableLength }
set { self.headerIndexTable.maxDynamicTableLength = newValue }
}
/// Sets the maximum size for the dynamic table and encodes the new value
/// at the start of the current packed header block to send to the peer.
///
/// - Parameter size: The new maximum size for the dynamic header table.
/// - Throws: If the encoder is currently in use, or if the requested size
/// exceeds the maximum value negotiated with the peer.
public mutating func setDynamicTableSize(_ size: Int) throws {
guard size <= self.maximumDynamicTableSize else {
throw NIOHPACKErrors.InvalidDynamicTableSize(requestedSize: size, allowedSize: self.maximumDynamicTableSize)
}
guard size != self.allowedDynamicTableSize else {
// no need to change anything
return
}
switch self.state {
case .idle:
self.state = .resized(smallestMaxTableSize: nil)
case .resized(nil) where size > self.allowedDynamicTableSize:
self.state = .resized(smallestMaxTableSize: self.allowedDynamicTableSize)
case .resized(let smallest?) where size < smallest:
self.state = .resized(smallestMaxTableSize: nil)
case .encoding:
throw NIOHPACKErrors.EncoderAlreadyActive()
default:
// we don't need to change smallest recorded resize value
break
}
// set the new size on our dynamic table
self.allowedDynamicTableSize = size
}
/// Initializer and returns a new HPACK encoder.
///
/// - Parameters:
/// - allocator: An allocator for `ByteBuffer`s.
/// - maxDynamicTableSize: An initial maximum size for the encoder's dynamic header table.
public init(allocator: ByteBufferAllocator, useHuffmanEncoding: Bool = true, maxDynamicTableSize: Int = HPACKEncoder.defaultDynamicTableSize) {
self.headerIndexTable = IndexedHeaderTable(allocator: allocator, maxDynamicTableSize: maxDynamicTableSize)
self.useHuffmanEncoding = useHuffmanEncoding
self.state = .idle
}
/// Sets up the encoder to begin encoding a new header block.
///
/// - Parameter allocator: Used to allocate the `ByteBuffer` that will contain the encoded
/// bytes, obtained from `endEncoding()`.
public mutating func beginEncoding(allocator: ByteBufferAllocator) throws {
if case .encoding = self.state {
throw NIOHPACKErrors.EncoderAlreadyActive()
}
// create a buffer
self.buffer = allocator.buffer(capacity: 128) // arbitrary size
switch self.state {
case .idle:
self.state = .encoding
case .resized(nil):
// one resize
self.buffer.write(encodedInteger: UInt(self.allowedDynamicTableSize), prefix: 5, prefixBits: 0x20)
self.state = .encoding
case let .resized(smallestSize?):
// two resizes, one smaller than the other
self.buffer.write(encodedInteger: UInt(smallestSize), prefix: 5, prefixBits: 0x20)
self.buffer.write(encodedInteger: UInt(self.allowedDynamicTableSize), prefix: 5, prefixBits: 0x20)
self.state = .encoding
default:
break
}
}
/// Finishes encoding the current header block and returns the resulting buffer.
public mutating func endEncoding() throws -> ByteBuffer {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
self.state = .idle
defer { self.buffer = nil }
return self.buffer
}
/// Appends() headers in the default fashion: indexed if possible, literal+indexable if not.
///
/// - Parameter headers: A sequence of key/value pairs representing HTTP headers.
public mutating func append<S: Sequence>(headers: S) throws where S.Element == (name: String, value: String) {
try self.append(headers: headers.lazy.map { HeaderDefinition(name: $0.0, value: $0.1, indexing: .indexable) })
}
/// Appends headers with their specified indexability.
///
/// - Parameter headers: A sequence of key/value/indexability tuples representing HTTP headers.
public mutating func append<S : Sequence>(headers: S) throws where S.Element == HeaderDefinition {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
for header in headers {
switch header.indexing {
case .indexable:
try self._appendIndexed(header: header.name.utf8, value: header.value.utf8)
case .nonIndexable:
self._appendNonIndexed(header: header.name.utf8, value: header.value.utf8)
case .neverIndexed:
self._appendNeverIndexed(header: header.name.utf8, value: header.value.utf8)
}
}
}
/// Appends a set of headers with their associated indexability.
///
/// - Parameter headers: A `HPACKHeaders` structure containing a set of HTTP/2 header values.
public mutating func append(headers: HPACKHeaders) throws {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
for header in headers.headerIndices {
let nameView = headers.view(of: header.name)
let valueView = headers.view(of: header.value)
switch header.indexing {
case .indexable:
try self._appendIndexed(header: nameView, value: valueView)
case .nonIndexable:
self._appendNonIndexed(header: nameView, value: valueView)
case .neverIndexed:
self._appendNeverIndexed(header: nameView, value: valueView)
}
}
}
/// Appends a header/value pair, using indexed names/values if possible. If no indexed pair is available,
/// it will use an indexed header and literal value, or a literal header and value. The name/value pair
/// will be indexed for future use.
public mutating func append(header name: String, value: String) throws {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
try self._appendIndexed(header: name.utf8, value: value.utf8)
}
/// Returns `true` if the item needs to be added to the header table
private mutating func _appendIndexed<Name: Collection, Value: Collection>(header name: Name, value: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
if let (index, hasValue) = self.headerIndexTable.firstHeaderMatch(for: name, value: value) {
if hasValue {
// purely indexed. Nice & simple.
self.buffer.write(encodedInteger: UInt(index), prefix: 7, prefixBits: 0x80)
// everything is indexed-- nothing more to do!
return
} else {
// no value, so append the index to represent the name, followed by the value's
// length
self.buffer.write(encodedInteger: UInt(index), prefix: 6, prefixBits: 0x40)
// now encode and append the value string
self.appendEncodedString(value)
}
} else {
// no indexed name or value. Have to add them both, with a zero index.
self.buffer.write(integer: UInt8(0x40))
self.appendEncodedString(name)
self.appendEncodedString(value)
}
// add to the header table
try self.headerIndexTable.add(headerNamed: name, value: value)
}
private mutating func appendEncodedString<C: Collection>(_ utf8: C) where C.Element == UInt8 {
// encode the value
if self.useHuffmanEncoding {
// problem: we need to encode the length before the encoded bytes, so we can't just receive the length
// after encoding to the target buffer itself. So we have to determine the length first.
self.buffer.write(encodedInteger: UInt(ByteBuffer.huffmanEncodedLength(of: utf8)), prefix: 7, prefixBits: 0x80)
self.buffer.writeHuffmanEncoded(bytes: utf8)
} else {
self.buffer.write(encodedInteger: UInt(utf8.count), prefix: 7, prefixBits: 0)
self.buffer.write(bytes: utf8)
}
}
/// Appends a header that is *not* to be entered into the dynamic header table, but allows that
/// stipulation to be overriden by a proxy server/rewriter.
public mutating func appendNonIndexed(header: String, value: String) throws {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
self._appendNonIndexed(header: header.utf8, value: value.utf8)
}
private mutating func _appendNonIndexed<Name: Collection, Value: Collection>(header: Name, value: Value) where Name.Element == UInt8, Value.Element == UInt8 {
if let (index, _) = self.headerIndexTable.firstHeaderMatch(for: header, value: value) {
// we actually don't care if it has a value in this instance; we're only indexing the
// name.
self.buffer.write(encodedInteger: UInt(index), prefix: 4, prefixBits: 0)
// now append the value
self.appendEncodedString(value)
} else {
self.buffer.write(integer: UInt8(0)) // top 4 bits are zero, and index is zero (no index)
self.appendEncodedString(header)
self.appendEncodedString(value)
}
}
/// Appends a header that is *never* indexed, preventing even rewriting proxies from doing so.
public mutating func appendNeverIndexed(header: String, value: String) throws {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
self._appendNeverIndexed(header: header.utf8, value: value.utf8)
}
private mutating func _appendNeverIndexed<Name: Collection, Value: Collection>(header: Name, value: Value) where Name.Element == UInt8, Value.Element == UInt8 {
// compiler doesn't like us passing `nil` here as it can't infer the types, apparently.
if let (index, _) = self.headerIndexTable.firstHeaderMatch(for: header, value: Optional<Value>.none) {
// we only use the index in this instance
self.buffer.write(encodedInteger: UInt(index), prefix: 4, prefixBits: 0x10)
// now append the value
self.appendEncodedString(value)
} else {
self.buffer.write(integer: UInt8(0x10)) // prefix bits + zero index
self.appendEncodedString(header)
self.appendEncodedString(value)
}
}
}
+105
View File
@@ -0,0 +1,105 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
public protocol NIOHPACKError : Error, Equatable { }
/// Errors raised by NIOHPACK while encoding/decoding data.
public enum NIOHPACKErrors {
/// An indexed header referenced an index that doesn't exist in our
/// header tables.
public struct InvalidHeaderIndex : NIOHPACKError {
/// The offending index.
public let suppliedIndex: Int
/// The highest index we have available.
public let availableIndex: Int
}
/// A header block indicated an indexed header with no accompanying
/// value, but the index referenced an entry with no value of its own
/// e.g. one of the many valueless items in the static header table.
public struct IndexedHeaderWithNoValue : NIOHPACKError {
/// The offending index.
public let index: Int
}
/// An encoded string contained an invalid length that extended
/// beyond its frame's payload size.
public struct StringLengthBeyondPayloadSize : NIOHPACKError {
/// The length supplied.
public let length: Int
/// The available number of bytes.
public let available: Int
}
/// Decoded string data could not be parsed as valid UTF-8.
public struct InvalidUTF8Data : NIOHPACKError {
/// The offending bytes.
public let bytes: ByteBuffer
}
/// The start byte of a header did not match any format allowed by
/// the HPACK specification.
public struct InvalidHeaderStartByte : NIOHPACKError {
/// The offending byte.
public let byte: UInt8
}
/// A dynamic table size update specified an invalid size.
public struct InvalidDynamicTableSize : NIOHPACKError {
/// The offending size.
public let requestedSize: Int
/// The actual maximum size that was set by the protocol.
public let allowedSize: Int
}
/// A dynamic table size update was found outside its allowed place.
/// They may only be included at the start of a header block.
public struct IllegalDynamicTableSizeChange : NIOHPACKError {}
/// A new header could not be added to the dynamic table. Usually
/// this means the header itself is larger than the current
/// dynamic table size.
public struct FailedToAddIndexedHeader<Name: Collection, Value: Collection> : NIOHPACKError where Name.Element == UInt8, Value.Element == UInt8 {
/// The table size required to be able to add this header to the table.
public let bytesNeeded: Int
/// The name of the header that could not be written.
public let name: Name
/// The value of the header that could not be written.
public let value: Value
public static func == (lhs: NIOHPACKErrors.FailedToAddIndexedHeader<Name, Value>, rhs: NIOHPACKErrors.FailedToAddIndexedHeader<Name, Value>) -> Bool {
guard lhs.bytesNeeded == rhs.bytesNeeded else {
return false
}
return lhs.name.elementsEqual(rhs.name) && lhs.value.elementsEqual(rhs.value)
}
}
/// Ran out of input bytes while decoding.
public struct InsufficientInput : NIOHPACKError {}
/// HPACK encoder asked to begin a new header block while partway through encoding
/// another block.
public struct EncoderAlreadyActive : NIOHPACKError {}
/// HPACK encoder asked to append a header without first calling `beginEncoding(allocator:)`.
public struct EncoderNotStarted : NIOHPACKError {}
}
+421
View File
@@ -0,0 +1,421 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
import NIOHTTP1
/// Very similar to `NIOHTTP1.HTTPHeaders`, but we have our own version to make some
/// small changes inside.
public struct HPACKHeaders {
final private class _Storage {
var buffer: ByteBuffer
var headers: [HPACKHeader]
init(buffer: ByteBuffer, headers: [HPACKHeader]) {
self.buffer = buffer
self.headers = headers
}
func copy() -> _Storage {
return .init(buffer: self.buffer, headers: self.headers)
}
}
private var _storage: _Storage
fileprivate var buffer: ByteBuffer {
return self._storage.buffer
}
fileprivate var headers: [HPACKHeader] {
return self._storage.headers
}
/// Returns the `String` for the given `HPACKHeaderIndex`.
///
/// - Parameter idx: The index into the underlying storage.
/// - Returns: The value.
private func string(idx: HPACKHeaderIndex) -> String {
return self.buffer.getString(at: idx.start, length: idx.length)!
}
/// Return all names.
private var names: [HPACKHeaderIndex] {
return self.headers.map { $0.name }
}
/// Constructor used to create a set of contiguous headers from an encoded
/// header buffer.
init(buffer: ByteBuffer, headers: [HPACKHeader]) {
self._storage = _Storage(buffer: buffer, headers: headers)
}
/// Constructor that can be used to map between `HTTPHeaders` and `HPACKHeaders` types.
init(_ httpHeaders: HTTPHeaders) {
// TODO(jim): Once we make HTTPHeaders' internals visible to us, we can just copy the buffer
// and map the headers.
self.init(Array(httpHeaders), allocator: ByteBufferAllocator())
}
/// Construct a `HPACKHeaders` structure.
///
/// The indexability of all headers is assumed to be the default, i.e. indexable and
/// rewritable by proxies.
///
/// - parameters
/// - headers: An initial set of headers to use to populate the header block.
init(_ headers: [(String, String)] = []) {
// Note: this initializer exists because of https://bugs.swift.org/browse/SR-7415.
// Otherwise we'd only have the one below with a default argument for `allocator`.
self.init(headers, allocator: ByteBufferAllocator())
}
/// Construct a `HPACKHeaders` structure.
///
/// The indexability of all headers is assumed to be the default, i.e. indexable and
/// rewritable by proxies.
///
/// - parameters
/// - headers: An initial set of headers to use to populate the header block.
/// - allocator: The allocator to use to allocate the underlying storage.
init(_ headers: [(String, String)] = [], allocator: ByteBufferAllocator) {
// Reserve enough space in the array to hold all indices.
var array: [HPACKHeader] = []
array.reserveCapacity(headers.count)
self.init(buffer: allocator.buffer(capacity: 256), headers: array)
for (key, value) in headers {
self.add(name: key, value: value)
}
}
/// Internal initializer to make things easier for unit tests.
init(fullHeaders: [(HPACKIndexing, String, String)]) {
var array: [HPACKHeader] = []
array.reserveCapacity(fullHeaders.count)
self.init(buffer: ByteBufferAllocator().buffer(capacity: 256), headers: array)
for (indexing, key, value) in fullHeaders {
self.add(name: key, value: value, indexing: indexing)
}
}
/// Add a header name/value pair to the block.
///
/// This method is strictly additive: if there are other values for the given header name
/// already in the block, this will add a new entry. `add` performs case-insensitive
/// comparisons on the header field name.
///
/// - Parameter name: The header field name. For maximum compatibility this should be an
/// ASCII string. For HTTP/2 lowercase header names are strongly encouraged.
/// - Parameter value: The header field value to add for the given name.
private mutating func add(name: String, value: String, indexing: HPACKIndexing = .indexable) {
precondition(!name.utf8.contains(where: { !$0.isASCII }), "name must be ASCII")
if !isKnownUniquelyReferenced(&self._storage) {
self._storage = self._storage.copy()
}
let nameStart = self.buffer.writerIndex
let nameLength = self._storage.buffer.write(string: name)!
let valueLength = self._storage.buffer.write(string: value)!
self._storage.headers.append(HPACKHeader(start: nameStart, nameLength: nameLength,
valueLength: valueLength, indexing: indexing))
}
/// Add a name/value pair to the block without going through the String class and the
/// ensuing UTF8 -> UTF16 -> UTF8 conversions.
///
/// This method is strictly additive: if there are other values for the given header name
/// already in the block, this will add a new entry. `add` performs case-insensitive
/// comparisons on the header field name.
///
/// - Parameter name: The header field name. For maximum compatibility this should be an
/// ASCII string. For HTTP/2 lowercase header names are strongly encouraged.
/// - Parameter value: The header field value to add for the given name.
private mutating func add(nameBytes: ByteBufferView, valueBytes: ByteBufferView, indexing: HPACKIndexing = .indexable) {
precondition(!nameBytes.contains(where: { !$0.isASCII }), "name must be ASCII")
if !isKnownUniquelyReferenced(&self._storage) {
self._storage = self._storage.copy()
}
let nameStart = self.buffer.writerIndex
let nameLength = self._storage.buffer.write(bytes: nameBytes)
let valueLength = self._storage.buffer.write(bytes: valueBytes)
self._storage.headers.append(HPACKHeader(start: nameStart, nameLength: nameLength,
valueLength: valueLength, indexing: indexing))
}
/// Retrieve all of the values for a given 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`.
///
/// - Parameter name: The header field name whose values are to be retrieved.
/// - Returns: A list of the values for that header field name.
public subscript(name: String) -> [String] {
guard !self.headers.isEmpty else {
return []
}
let utf8 = name.utf8
var array: [String] = []
for header in self.headers {
if self.buffer.equalCaseInsensitiveASCII(view: utf8, at: header.name) {
array.append(self.string(idx: header.value))
}
}
return array
}
/// Checks if a header is present
///
/// - parameters:
/// - name: The name of the header
/// - returns: `true` if a header with the name (and value) exists, `false` otherwise.
public func contains(name: String) -> Bool {
guard !self.headers.isEmpty else {
return false
}
let utf8 = name.utf8
for header in self.headers {
if self.buffer.equalCaseInsensitiveASCII(view: utf8, at: header.name) {
return true
}
}
return false
}
/// 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.
///
/// - Parameter name: The header field name whose values are to be retrieved.
/// - Returns: A list of the values for that header field name.
public subscript(canonicalForm name: String) -> [String] {
let result = self[name]
guard result.count > 0 else {
return []
}
// It's not safe to split Set-Cookie on comma.
guard name.lowercased() != "set-cookie" else {
return result
}
return result.flatMap { $0.split(separator: ",") }.map { String($0.trimWhitespace()) }
}
/// Special internal function for use by tests.
internal subscript(position: Int) -> (String, String) {
precondition(position < self.headers.endIndex, "Position \(position) is beyond bounds of \(self.headers.endIndex)")
let header = self.headers[position]
let name = self.string(idx: header.name)
let value = self.string(idx: header.value)
return (name, value)
}
/// Enables encoders to directly enumerate the header views themselves without converting via `String`.
internal var headerIndices: [HPACKHeader] {
return self.headers
}
internal func view(of index: HPACKHeaderIndex) -> ByteBufferView {
return self.buffer.viewBytes(at: index.start, length: index.length)
}
}
extension HPACKHeaders: Sequence {
public typealias Element = (name: String, value: String, indexable: HPACKIndexing)
/// 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.
public struct Iterator : IteratorProtocol {
private var headerParts: Array<(String, String, HPACKIndexing)>.Iterator
init(headerParts: Array<(String, String, HPACKIndexing)>.Iterator) {
self.headerParts = headerParts
}
public mutating func next() -> Element? {
return headerParts.next()
}
}
public func makeIterator() -> HPACKHeaders.Iterator {
return Iterator(headerParts: headers.map { (self.string(idx: $0.name), self.string(idx: $0.value), $0.indexing) }.makeIterator())
}
}
extension HPACKHeaders: CustomStringConvertible {
public var description: String {
var headersArray: [(HPACKIndexing, String, String)] = []
headersArray.reserveCapacity(self.headers.count)
for h in self.headers {
headersArray.append((h.indexing, self.string(idx: h.name), self.string(idx: h.value)))
}
return headersArray.description
}
}
extension HPACKHeaders: Equatable {
public static func == (lhs: HPACKHeaders, rhs: HPACKHeaders) -> Bool {
guard lhs.headers.count == rhs.headers.count else {
return false
}
let lhsNames = Set(lhs.names.lazy.map { lhs.string(idx: $0).lowercased() })
let rhsNames = Set(rhs.names.lazy.map { rhs.string(idx: $0).lowercased() })
guard lhsNames == rhsNames else {
return false
}
for name in lhsNames {
guard lhs[name].sorted() == rhs[name].sorted() else {
return false
}
}
return true
}
}
/// Defines the types of indexing and rewriting operations a decoder may take with
/// regard to this header.
public enum HPACKIndexing: CustomStringConvertible {
/// Header may be written into the dynamic index table or may be rewritten by
/// proxy servers.
case indexable
/// Header is not written to the dynamic index table, but proxies may rewrite
/// it en-route, as long as they preserve its non-indexable attribute.
case nonIndexable
/// Header may not be written to the dynamic index table, and proxies must
/// pass it on as-is without rewriting.
case neverIndexed
public var description: String {
switch self {
case .indexable:
return "[]"
case .nonIndexable:
return "[non-indexable]"
case .neverIndexed:
return "[neverIndexed]"
}
}
}
/// Describes the location of a single header name/value string within a buffer.
struct HPACKHeaderIndex {
var start: Int
var length: Int
init(start: Int, length: Int) {
assert(start >= 0, "start must not be negative")
assert(length >= 0, "length must not be negative")
self.start = start
self.length = length
}
mutating func adjust(by delta: Int, wrappingAt max: Int) {
if self.start + delta < 0 {
self.start += max + delta
} else {
self.start += delta
}
assert(self.start >= 0, "start must not be negative")
}
}
extension HPACKHeaderIndex: CustomStringConvertible {
var description: String {
return "\(self.start)..<\(self.start + self.length)"
}
}
struct HPACKHeader {
var indexing: HPACKIndexing
var name: HPACKHeaderIndex
var value: HPACKHeaderIndex
init(start: Int, nameLength: Int, valueLength: Int, indexing: HPACKIndexing = .indexable) {
self.indexing = indexing
self.name = HPACKHeaderIndex(start: start, length: nameLength)
self.value = HPACKHeaderIndex(start: start + nameLength, length: valueLength)
}
}
private extension ByteBuffer {
func equalCaseInsensitiveASCII(view: String.UTF8View, at index: HPACKHeaderIndex) -> Bool {
guard view.count == index.length else {
return false
}
let bufView = self.viewBytes(at: index.start, length: index.length)
for (idx, byte) in view.enumerated() {
let bufIdx = bufView.index(bufView.startIndex, offsetBy: idx)
guard byte.isASCII && bufView[bufIdx] & 0xdf == byte & 0xdf else {
return false
}
}
return true
}
}
internal extension UInt8 {
var isASCII: Bool {
return self <= 127
}
}
/* private but tests */
internal extension Character {
var isASCIIWhitespace: Bool {
return self == " " || self == "\t" || self == "\r" || self == "\n" || self == "\r\n"
}
}
/* private but tests */
internal extension String {
func trimASCIIWhitespace() -> Substring {
return self.dropFirst(0).trimWhitespace()
}
}
private extension Substring {
func trimWhitespace() -> Substring {
var me = self
while me.first?.isASCIIWhitespace == .some(true) {
me = me.dropFirst()
}
while me.last?.isASCIIWhitespace == .some(true) {
me = me.dropLast()
}
return me
}
}
+318
View File
@@ -0,0 +1,318 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/// Stores offsets into a contiguous buffer locating the name and value bytes
/// for a header name and value pair.
struct HeaderTableEntry {
var name: HPACKHeaderIndex
var value: HPACKHeaderIndex
init(name: HPACKHeaderIndex, value: HPACKHeaderIndex) {
self.name = name
self.value = value
}
// RFC 7541 § 4.1:
//
// The size of an entry is the sum of its name's length in octets (as defined in
// Section 5.2), its value's length in octets, and 32.
//
// The size of an entry is calculated using the length of its name and value
// without any Huffman encoding applied.
var length: Int {
return self.name.length + self.value.length + 32
}
fileprivate mutating func adjust(by delta: Int, wrappingAt max: Int) {
name.adjust(by: delta, wrappingAt: max)
value.adjust(by: delta, wrappingAt: max)
}
}
extension HeaderTableEntry : CustomStringConvertible {
var description: String {
return "(name: \(self.name), value: \(self.value))"
}
}
/// Raw bytes storage for the header tables, both static and dynamic. Similar in spirit to
/// `HPACKHeaders` and `NIOHTTP1.HTTPHeaders`, but uses a ring buffer to hold the bytes to
/// avoid allocation churn while evicting and replacing entries.
struct HeaderTableStorage {
static let defaultMaxSize = 4096
private var buffer: StringRing
private var headers: CircularBuffer<HeaderTableEntry>
private(set) var maxSize: Int
private(set) var length: Int = 0
var count: Int {
return self.headers.count
}
init(allocator: ByteBufferAllocator, maxSize: Int = HeaderTableStorage.defaultMaxSize) {
self.maxSize = maxSize
self.buffer = StringRing(allocator: allocator, capacity: self.maxSize)
self.headers = CircularBuffer(initialRingCapacity: self.maxSize / 64) // rough guess: 64 bytes per header
}
init(allocator: ByteBufferAllocator, staticHeaderList: [(String, String)]) {
// calculate likely total byte length we will actually need
// the static table is all ASCII, so character count == byte count here,
// so we don't need to convert to UTF-8 bytes just to get byte length.
let bytesNeeded = staticHeaderList.reduce(0) { $0 + $1.0.count + $1.1.count }
// now allocate & encode all the things
self.buffer = StringRing(allocator: allocator, capacity: bytesNeeded)
self.headers = CircularBuffer(initialRingCapacity: staticHeaderList.count)
var len = 0
for (name, value) in staticHeaderList {
let nameStart = self.buffer.ringTail
let nameLen = try! self.buffer.write(string: name)
let valueStart = self.buffer.ringTail
let valueLen = try! self.buffer.write(string: value)
let entry = HeaderTableEntry(name: HPACKHeaderIndex(start: nameStart, length: nameLen),
value: HPACKHeaderIndex(start: valueStart, length: valueLen))
// the input list is already in order, so we push at the end of our internal list
self.headers.append(entry)
len += entry.length
}
self.length = len
self.maxSize = len
}
subscript(index: Int) -> HeaderTableEntry {
return self.headers[index]
}
subscript(name: String) -> [String] {
return self.findHeaders(matching: name.utf8).map { self.string(idx: $0.value) }
}
typealias LazyMatchingHeadersCollection = LazyFilterCollection<CircularBuffer<HeaderTableEntry>>
typealias LazyMatchingIndicesSequence = LazyMapSequence<LazyFilterSequence<EnumeratedSequence<CircularBuffer<HeaderTableEntry>>>, Int>
func findHeaders<C: Collection>(matching name: C) -> LazyMatchingHeadersCollection where C.Element == UInt8 {
// LazyFilterCollection<CircularBuffer<HeaderTableEntry>>
return self.headers.lazy.filter {
self.buffer.equalCaseInsensitiveASCII(view: name, at: $0.name)
}
}
func indices<C: Collection>(matching name: C) -> LazyMatchingIndicesSequence where C.Element == UInt8 {
return self.headers.enumerated().lazy.filter {
self.buffer.equalCaseInsensitiveASCII(view: name, at: $1.name)
}.map { (idx, header) in idx }
}
func firstIndex<C : Collection>(matching name: C) -> Int? where C.Element == UInt8 {
for (idx, header) in self.headers.enumerated() {
if self.buffer.equalCaseInsensitiveASCII(view: name, at: header.name) {
return idx
}
}
return nil
}
func string(idx: HPACKHeaderIndex) -> String {
return self.buffer.getString(at: idx.start, length: idx.length)!
}
func view(of idx: HPACKHeaderIndex) -> ByteBufferView {
return self.buffer.viewBytes(at: idx.start, length: idx.length)
}
mutating func setTableSize(to newSize: Int) {
precondition(newSize >= 0)
if newSize < self.length {
// need to clear out some things first.
while newSize < self.length {
purgeOne()
}
}
// if this rebases, we will need to alter all our header indices
let startIndex = self.buffer.ringHead
self.buffer.changeCapacity(to: newSize)
self.maxSize = newSize
if startIndex == self.buffer.ringHead {
// no index changes
return
}
// otherwise, compute a delta and apply it to all indices
let delta = self.buffer.ringHead - startIndex // if index decreased, we apply a negative delta
for index in self.headers.indices {
self.headers[index].adjust(by: delta, wrappingAt: self.buffer.capacity)
}
}
mutating func add<Name: Collection, Value: Collection>(name: Name, value: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
var len = 0
do {
let (nameStart, nameLen, valueStart, valueLen) = try self.encode(name: name, value: value)
len = nameLen + valueLen
try prependHeaderEntry(nameStart: nameStart, nameLen: nameLen, valueStart: valueStart, valueLen: valueLen)
} catch {
// NB: moving backwards here
self.buffer.unwrite(byteCount: len)
throw error
}
}
mutating func add<Name: ContiguousCollection, Value: ContiguousCollection>(nameBytes: Name, valueBytes: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
let (nameStart, nameLen, valueStart, valueLen) = try self.encode(name: nameBytes, value: valueBytes)
do {
try prependHeaderEntry(nameStart: nameStart, nameLen: nameLen, valueStart: valueStart, valueLen: valueLen)
} catch {
// remove everything written by the `encode(name:value:)` call above
self.buffer.unwrite(byteCount: nameLen + valueLen)
throw error
}
}
private mutating func ensureSpaceAvailable(_ amount: Int) throws {
if self.buffer.writableBytes >= amount {
// all is good in the world
return
}
let extraNeeded = amount - self.buffer.writableBytes
self.purge(toRelease: extraNeeded)
if self.buffer.writableBytes >= amount {
// still not enough actual room:
throw RingBufferError.BufferOverrun(amount: amount - self.buffer.writableBytes)
}
}
private mutating func encode<Name: Collection, Value: Collection>(name: Name, value: Value) throws -> (nstart: Int, nlen: Int, vstart: Int, vlen: Int) where Name.Element == UInt8, Value.Element == UInt8 {
try ensureSpaceAvailable(name.count + value.count)
let nstart = self.buffer.ringTail
let nlen = try self.buffer.write(bytes: name)
let vstart = self.buffer.ringTail
let vlen = try self.buffer.write(bytes: value)
return (nstart, nlen, vstart, vlen)
}
private mutating func encode<Name: ContiguousCollection, Value: ContiguousCollection>(name: Name, value: Value) throws -> (nstart: Int, nlen: Int, vstart: Int, vlen: Int) where Name.Element == UInt8, Value.Element == UInt8 {
try ensureSpaceAvailable(name.count + value.count)
let nstart = self.buffer.ringTail
let nlen = try self.buffer.write(bytes: name)
let vstart = self.buffer.ringTail
let vlen = try self.buffer.write(bytes: value)
return (nstart, nlen, vstart, vlen)
}
private mutating func prependHeaderEntry(nameStart: Int, nameLen: Int, valueStart: Int, valueLen: Int) throws {
let nameIndex = HPACKHeaderIndex(start: nameStart, length: nameLen)
let valueIndex = HPACKHeaderIndex(start: valueStart, length: valueLen)
let entry = HeaderTableEntry(name: nameIndex, value: valueIndex)
var newLength = self.length + entry.length
if newLength > self.maxSize {
self.purge(toRelease: newLength - maxSize)
newLength = self.length + entry.length
if newLength > self.maxSize {
// Danger, Will Robinson! We can't free up enough space!
throw RingBufferError.BufferOverrun(amount: newLength - self.maxSize)
}
}
self.headers.prepend(entry)
self.length = newLength
}
/// Purges `toRelease` bytes from the table, where 'bytes' refers to the byte-count
/// of a table entry specified in RFC 7541: [name octets] + [value octets] + 32.
///
/// - parameter toRelease: The table entry length of bytes to remove from the table.
mutating func purge(toRelease count: Int) {
guard count < self.length else {
// clear all the things
self.headers.removeAll()
self.buffer.clear()
self.length = 0
return
}
var available = self.maxSize - self.length
let needed = available + count
while available < needed && !self.headers.isEmpty {
available += self.purgeOne()
}
}
@discardableResult
private mutating func purgeOne() -> Int {
precondition(self.headers.isEmpty == false, "should not call purgeOne() unless we have something to purge")
// Remember: we're removing from the *end* of the header list, since we *prepend* new items there, but we're
// removing bytes from the *start* of the storage, because we *append* there.
let entry = self.headers.removeLast()
self.buffer.moveHead(forwardBy: entry.name.length + entry.value.length)
self.length -= entry.length
return entry.length
}
// internal for testing
func dumpHeaders(offsetBy amount: Int = 0) -> String {
return self.headers.enumerated().reduce("") {
$0 + "\($1.0 + amount) - \(self.string(idx: $1.1.name)) : \(self.string(idx: $1.1.value))\n"
}
}
}
extension HeaderTableStorage : CustomStringConvertible {
var description: String {
var array: [(String, String)] = []
for header in self.headers {
array.append((self.string(idx: header.name), self.string(idx: header.value)))
}
return array.description
}
}
private extension StringRing {
func equalCaseInsensitiveASCII<C : Collection>(view: C, at index: HPACKHeaderIndex) -> Bool where C.Element == UInt8 {
guard view.count == index.length else {
return false
}
return withVeryUnsafeBytes { buffer in
// This should never happens as we control when this is called. Adding an assert to ensure this.
for (idx, byte) in view.enumerated() {
// TODO(jim): Not a big fan of the modulo operation here.
guard byte.isASCII && buffer[(index.start + idx) % self.capacity] & 0xdf == byte & 0xdf else {
return false
}
}
return true
}
}
}
+235
View File
@@ -0,0 +1,235 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/// Adds HPACK-conformant Huffman encoding to `ByteBuffer`. Note that the implementation is *not*
/// thread safe. The intended use is to be within a single HTTP2StreamChannel or similar, on a
/// single EventLoop.
extension ByteBuffer {
fileprivate struct _EncoderState {
var offset = 0
var remainingBits = 8
}
/// Returns the number of *bits* required to encode a given string.
fileprivate static func encodedBitLength<C : Collection>(of bytes: C) -> Int where C.Element == UInt8 {
let clen = bytes.reduce(0) { $0 + StaticHuffmanTable[Int($1)].nbits }
// round up to nearest multiple of 8 for EOS prefix
return (clen + 7) & ~7
}
/// Returns the number of bytes required to encode a given string.
static func huffmanEncodedLength<C : Collection>(of bytes: C) -> Int where C.Element == UInt8 {
return self.encodedBitLength(of: bytes) / 8
}
/// Encodes the given string to the buffer, using HPACK Huffman encoding.
///
/// - Parameter string: The string data to encode.
/// - Returns: The number of bytes used while encoding the string.
@discardableResult
mutating func setHuffmanEncoded<C: Collection>(bytes stringBytes: C) -> Int where C.Element == UInt8 {
let clen = ByteBuffer.encodedBitLength(of: stringBytes)
self.ensureBitsAvailable(clen)
return self.withUnsafeMutableWritableBytes { bytes in
var state = _EncoderState()
for ch in stringBytes {
ByteBuffer.appendSym_fast(StaticHuffmanTable[Int(ch)], &state, bytes: bytes)
}
if state.remainingBits > 0 && state.remainingBits < 8 {
// set all remaining bits of the last byte to 1
bytes[state.offset] |= UInt8(1 << state.remainingBits) - 1
state.offset += 1
state.remainingBits = (state.offset == bytes.count ? 0 : 8)
}
return state.offset
}
}
@discardableResult
mutating func writeHuffmanEncoded<C: Collection>(bytes stringBytes: C) -> Int where C.Element == UInt8 {
let written = self.setHuffmanEncoded(bytes: stringBytes)
self.moveWriterIndex(forwardBy: written)
return written
}
fileprivate static func appendSym_fast(_ sym: HuffmanTableEntry, _ state: inout _EncoderState, bytes: UnsafeMutableRawBufferPointer) {
// will it fit as-is?
if sym.nbits == state.remainingBits {
bytes[state.offset] |= UInt8(sym.bits)
state.offset += 1
state.remainingBits = state.offset == bytes.count ? 0 : 8
} else if sym.nbits < state.remainingBits {
let diff = state.remainingBits - sym.nbits
bytes[state.offset] |= UInt8(sym.bits << diff)
state.remainingBits -= sym.nbits
} else {
var (code, nbits) = sym
nbits -= state.remainingBits
bytes[state.offset] |= UInt8(code >> nbits)
state.offset += 1
if nbits & 0x7 != 0 {
// align code to MSB
code <<= 8 - (nbits & 0x7)
}
// we can short-circuit if less than 8 bits are remaining
if nbits < 8 {
bytes[state.offset] = UInt8(truncatingIfNeeded: code)
state.remainingBits = 8 - nbits
return
}
// longer path for larger amounts
switch nbits {
case _ where nbits > 24:
bytes[state.offset] = UInt8(truncatingIfNeeded: code >> 24)
nbits -= 8
state.offset += 1
fallthrough
case _ where nbits > 16:
bytes[state.offset] = UInt8(truncatingIfNeeded: code >> 16)
nbits -= 8
state.offset += 1
fallthrough
case _ where nbits > 8:
bytes[state.offset] = UInt8(truncatingIfNeeded: code >> 8)
nbits -= 8
state.offset += 1
default:
break
}
if nbits == 8 {
bytes[state.offset] = UInt8(truncatingIfNeeded: code)
state.offset += 1
state.remainingBits = state.offset == bytes.count ? 0 : 8
} else {
state.remainingBits = 8 - nbits
bytes[state.offset] = UInt8(truncatingIfNeeded: code)
}
}
}
fileprivate mutating func ensureBitsAvailable(_ bits: Int) {
let bytesNeeded = bits / 8
if bytesNeeded <= self.writableBytes {
// just zero the requested number of bytes before we start OR-ing in our values
self.withUnsafeMutableWritableBytes { ptr in
ptr.copyBytes(from: repeatElement(0, count: bytesNeeded))
}
return
}
let neededToAdd = bytesNeeded - self.writableBytes
let newLength = self.capacity + neededToAdd
// reallocate to ensure we have the room we need
self.reserveCapacity(newLength)
// now zero all writable bytes that we expect to use
self.withUnsafeMutableWritableBytes { ptr in
ptr.copyBytes(from: repeatElement(0, count: bytesNeeded))
}
}
}
/// Errors that may be encountered by the Huffman decoder.
public enum HuffmanDecodeError
{
/// The decoder entered an invalid state. Usually this means invalid input.
public struct InvalidState : NIOHPACKError {
fileprivate init() {}
}
/// The output data could not be validated as UTF-8.
public struct InvalidUTF8 : NIOHPACKError {
fileprivate init() {}
}
}
/// The decoder table. This structure doesn't actually take up any space, I think?
fileprivate let decoderTable = HuffmanDecoderTable()
extension ByteBuffer {
/// Decodes a huffman-encoded string into a provided `ByteBuffer`.
///
/// - Parameter at: The location of the encoded bytes to read.
/// - Parameter length: The number of huffman-encoded octets to read.
/// - Parameter output: A `ByteBuffer` into which decoded UTF-8 octets will be
/// written.
/// - Returns: The number of UTF-8 characters written into the output `ByteBuffer`.
/// - Throws: HuffmanDecodeError if the data could not be decoded.
@discardableResult
func getHuffmanEncodedString(at index: Int, length: Int, into output: inout ByteBuffer) throws -> Int {
precondition(index + length <= self.capacity, "Requested range out of bounds: \(index ..< index+length) vs. \(self.capacity)")
if length == 0 {
return 0
}
var state: UInt8 = 0
var acceptable = false
let start = output.writerIndex
for ch in self.viewBytes(at: index, length: length) {
var t = decoderTable[state: state, nybble: ch >> 4]
if t.flags.contains(.failure) {
throw HuffmanDecodeError.InvalidState()
}
if t.flags.contains(.symbol) {
output.write(integer: t.sym)
}
t = decoderTable[state: t.state, nybble: ch & 0xf]
if t.flags.contains(.failure) {
throw HuffmanDecodeError.InvalidState()
}
if t.flags.contains(.symbol) {
output.write(integer: t.sym)
}
state = t.state
acceptable = t.flags.contains(.accepted)
}
guard acceptable else {
throw HuffmanDecodeError.InvalidState()
}
return output.writerIndex - start
}
/// Reads a huffman-encoded string into a provided `ByteBuffer`, starting at the buffer's
/// current `readerIndex`. Updates the `readerIndex` when it completes.
///
/// - Parameter length: The number of huffman-encoded octets to read.
/// - Parameter output: A `ByteBuffer` into which decoded UTF-8 octets will be
/// written.
/// - Returns: The number of UTF-8 characters written into the output `ByteBuffer`.
/// - Throws: HuffmanDecodeError if the data could not be decoded.
@discardableResult
mutating func readHuffmanEncodedString(of length: Int, into output: inout ByteBuffer) throws -> Int {
let result = try self.getHuffmanEncodedString(at: self.readerIndex, length: length, into: &output)
self.moveReaderIndex(forwardBy: length)
return result
}
}
File diff suppressed because it is too large. Load diff
+177
View File
@@ -0,0 +1,177 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/// The unified header table used by HTTP/2, encompassing both static and dynamic tables.
public struct IndexedHeaderTable {
// private but tests
@_versioned
let staticTable: HeaderTableStorage
@_versioned
var dynamicTable: DynamicHeaderTable
/// Creates a new header table, optionally specifying a maximum size for the dynamic
/// portion of the table.
///
/// - Parameter maxDynamicTableSize: Maximum size of the dynamic table. Default = 4096.
init(allocator: ByteBufferAllocator, maxDynamicTableSize: Int = DynamicHeaderTable.defaultSize) {
self.staticTable = HeaderTableStorage(allocator: allocator, staticHeaderList: StaticHeaderTable)
self.dynamicTable = DynamicHeaderTable(allocator: allocator, maximumLength: maxDynamicTableSize)
}
/// Obtains the header key/value pair at the given index within the table.
///
/// - note: Per RFC 7541, this uses a *1-based* index.
/// - Parameter index: The index to query.
/// - Returns: A tuple containing the name and value of the stored header.
/// - Throws: `NIOHPACKErrors.InvalidHeaderIndex` if the supplied index was invalid.
public func header(at index: Int) throws -> (name: String, value: String) {
let (nameView, valueView) = try self.headerViews(at: index)
return (String(decoding: nameView, as: UTF8.self), String(decoding: valueView, as: UTF8.self))
}
/// Obtains the header key/value pair at the given index within the table as sequences of
/// raw bytes.
///
/// - note: Per RFC 7541, this uses a *1-based* index.
/// - Parameter index: The index to query.
/// - Returns: A tuple containing the name and value of the stored header.
/// - Throws: `NIOHPACKErrors.InvalidHeaderIndex` if the supplied index was invalid.
public func headerViews(at index: Int) throws -> (name: ByteBufferView, value: ByteBufferView) {
let result: (ByteBufferView, ByteBufferView)
if index < self.staticTable.count {
let entry = self.staticTable[index]
result = (self.staticTable.view(of: entry.name), self.staticTable.view(of: entry.value))
} else if index - self.staticTable.count < self.dynamicTable.count {
let entry = self.dynamicTable[index - self.staticTable.count]
result = (self.dynamicTable.view(of: entry.name), self.dynamicTable.view(of: entry.value))
} else {
throw NIOHPACKErrors.InvalidHeaderIndex(suppliedIndex: index, availableIndex: self.staticTable.count + self.dynamicTable.count - 1)
}
return result
}
/// Searches the table to locate an existing header with the given name and value. If
/// no item exists that contains a matching value, it will return the index of the first
/// item with a matching header name instead, to be encoded as index+value.
///
/// - Parameters:
/// - name: The name of the header to locate.
/// - value: The value for which to search.
/// - Returns: A tuple containing the index of any located header, and a boolean indicating
/// whether the item at that index also contains a matching value. Returns `nil`
/// if no match could be found.
public func firstHeaderMatch(for name: String, value: String?) -> (index: Int, matchesValue: Bool)? {
return self.firstHeaderMatch(for: name.utf8, value: value?.utf8)
}
func firstHeaderMatch<Name: Collection, Value: Collection>(for name: Name, value: Value?) -> (index: Int, matchesValue: Bool)? where Name.Element == UInt8, Value.Element == UInt8 {
guard let value = value else {
// AnySequence only has `first(where:)` method, not `first` property.
return self.staticTable.firstIndex(matching: name).map { ($0, false) }
}
var firstHeaderIndex: Int? = nil
for index in self.staticTable.indices(matching: name) {
// we've found a name, at least
if firstHeaderIndex == nil {
firstHeaderIndex = index
}
if self.staticTable.view(of: self.staticTable[index].value).matches(value) {
return (index, true)
}
}
// no complete match: search the dynamic table now
if let result = self.dynamicTable.findExistingHeader(named: name, value: value) {
if let staticIndex = firstHeaderIndex, result.containsValue == false {
// Dynamic table can't match the value, and we have a name match in the static
// table. In this case, we prefer the static table.
return (staticIndex, false)
} else {
// Either no match in the static table, or the dynamic table has a header with
// a matching value. Return that, but update the index appropriately.
return (result.index + StaticHeaderTable.count, result.containsValue)
}
} else if let staticIndex = firstHeaderIndex {
// nothing in the dynamic table, but the static table had a name match
return (staticIndex, false)
} else {
// no match anywhere, you'll have to encode the whole thing
return nil
}
}
/// Appends a header to the table.
///
/// This call may result in an empty table, as per RFC 7541 § 4.4:
/// > "It is not an error to attempt to add an entry that is larger than the maximum size;
/// > an attempt to add an entry larger than the maximum size causes the table to be
/// > emptied of all existing entries and results in an empty table."
///
/// - Parameters:
/// - name: The name of the header to insert.
/// - value: The value of the header to insert.
/// - Returns: `true` if the header was added to the table, `false` if not.
public mutating func add(headerNamed name: String, value: String) throws {
try self.add(headerNamed: name.utf8, value: value.utf8)
}
/// Appends a header to the table.
///
/// This call may result in an empty table, as per RFC 7541 § 4.4:
/// > "It is not an error to attempt to add an entry that is larger than the maximum size;
/// > an attempt to add an entry larger than the maximum size causes the table to be
/// > emptied of all existing entries and results in an empty table."
///
/// - Parameters:
/// - name: A sequence of contiguous bytes containing the name of the header to insert.
/// - value: A sequence of contiguous bytes containing the value of the header to insert.
public mutating func add<Name: ContiguousCollection, Value: ContiguousCollection>(headerNameBytes nameBytes: Name, valueBytes: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
try self.dynamicTable.addHeader(nameBytes: nameBytes, valueBytes: valueBytes)
}
/// An internal variant, where we've already deconstructed the String into its UTF-8 bytes.
internal mutating func add<Name: Collection, Value: Collection>(headerNamed name: Name, value: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
try self.dynamicTable.addHeader(named: name, value: value)
}
/// Internal for test access.
internal func dumpHeaders() -> String {
return "\(staticTable.dumpHeaders())\n\(dynamicTable.dumpHeaders())"
}
/// The length, in bytes, of the dynamic portion of the header table.
public var dynamicTableLength: Int {
return self.dynamicTable.length
}
/// The current allowed length of the dynamic portion of the header table. May be
/// less than the current protocol-assigned maximum supplied by a SETTINGS frame.
public var dynamicTableAllowedLength: Int {
get { return self.dynamicTable.allowedLength }
set { self.dynamicTable.allowedLength = newValue }
}
/// The hard limit on the size to which the dynamic table may grow. Only a SETTINGS
/// frame can change this: it can't grow beyond this size due to changes within
/// header blocks.
public var maxDynamicTableLength: Int {
get { return self.dynamicTable.maximumTableLength }
set { self.dynamicTable.maximumTableLength = newValue }
}
}
+111
View File
@@ -0,0 +1,111 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/* private but tests */
/// Encodes an integer value into a provided memory location.
///
/// - Parameters:
/// - value: The integer value to encode.
/// - buffer: The location at which to begin encoding.
/// - prefix: The number of bits available for use in the first byte at `buffer`.
/// - prefixBits: Existing bits to place in that first byte of `buffer` before encoding `value`.
/// - Returns: Returns the number of bytes used to encode the integer.
@discardableResult
func encodeInteger(_ value: UInt, to buffer: inout ByteBuffer,
prefix: Int, prefixBits: UInt8 = 0) -> Int {
assert(prefix <= 8)
assert(prefix >= 1)
let start = buffer.writerIndex
let k = (1 << prefix) - 1
var initialByte = prefixBits
if value < k {
// it fits already!
initialByte |= UInt8(truncatingIfNeeded: value)
buffer.write(integer: initialByte)
return 1
}
// if it won't fit in this byte altogether, fill in all the remaining bits and move
// to the next byte.
initialByte |= UInt8(truncatingIfNeeded: k)
buffer.write(integer: initialByte)
// deduct the initial [prefix] bits from the value, then encode it seven bits at a time into
// the remaining bytes.
var n = value - UInt(k)
while n >= 128 {
let nextByte = (1 << 7) | UInt8(n & 0x7f)
buffer.write(integer: nextByte)
n >>= 7
}
buffer.write(integer: UInt8(n))
return buffer.writerIndex - start
}
/* private but tests */
func decodeInteger(from bytes: ByteBufferView, prefix: Int) throws -> (UInt, Int) {
assert(prefix <= 8)
assert(prefix >= 1)
let k = (1 << prefix) - 1
var n: UInt = 0
var i = bytes.startIndex
if n == 0 {
// if the available bits aren't all set, the entire value consists of those bits
if bytes[i] & UInt8(k) != k {
return (UInt(bytes[i] & UInt8(k)), 1)
}
n = UInt(k)
i = bytes.index(after: i)
if i == bytes.endIndex {
return (n, bytes.distance(from: bytes.startIndex, to: i))
}
}
// for the remaining bytes, as long as the top bit is set, consume the low seven bits.
var m: UInt = 0
var b: UInt8 = 0
repeat {
if i == bytes.endIndex {
throw NIOHPACKErrors.InsufficientInput()
}
b = bytes[i]
n += UInt(b & 127) * (1 << m)
m += 7
i = bytes.index(after: i)
} while b & 128 == 128
return (n, bytes.distance(from: bytes.startIndex, to: i))
}
extension ByteBuffer {
mutating func readEncodedInteger(withPrefix prefix: Int = 0) throws -> Int {
let (result, nread) = try decodeInteger(from: self.readableBytesView, prefix: prefix)
self.moveReaderIndex(forwardBy: nread)
return Int(result)
}
mutating func write(encodedInteger value: UInt, prefix: Int = 0, prefixBits: UInt8 = 0) {
encodeInteger(value, to: &self, prefix: prefix, prefixBits: prefixBits)
}
}
+49
View File
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
extension ByteBufferView {
@_inlineable
public func matches<C: ContiguousCollection>(_ other: C) -> Bool where C.Element == UInt8 {
return self.withUnsafeBytes { myBytes in
return other.withUnsafeBytes { theirBytes in
guard myBytes.count == theirBytes.count else {
return false
}
return memcmp(myBytes.baseAddress!, theirBytes.baseAddress!, myBytes.count) == 0
}
}
}
@_inlineable
public func matches<S: Sequence>(_ other: S) -> Bool where S.Element == UInt8 {
return self.elementsEqual(other)
}
}
extension StringRing {
func viewBytes(at index: Int, length: Int) -> ByteBufferView {
let endIndex = index + length
if endIndex < self.capacity {
return self._storage.viewBytes(at: index, length: length)
} else {
// need to allocate a copy to get contiguous access :(
// we know this will work, because we're constraining the bounds
var buf = self._storage.getSlice(at: index, length: self._storage.capacity - index)!
buf.write(bytes: self._storage.viewBytes(at: 0, length: length - buf.readableBytes))
return buf.readableBytesView
}
}
}
+78
View File
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
internal let StaticHeaderTable: [(String, String)] = [
("", ""), // real indexing begins at 1
(":authority", ""),
(":method", "GET"),
(":method", "POST"),
(":path", "/"),
(":path", "/index.html"),
(":scheme", "http"),
(":scheme", "https"),
(":status", "200"),
(":status", "204"),
(":status", "206"),
(":status", "304"),
(":status", "400"),
(":status", "404"),
(":status", "500"),
("accept-charset", ""),
("accept-encoding", "gzip, deflate"),
("accept-language", ""),
("accept-ranges", ""),
("accept", ""),
("access-control-allow-origin", ""),
("age", ""),
("allow", ""),
("authorization", ""),
("cache-control", ""),
("content-disposition", ""),
("content-encoding", ""),
("content-language", ""),
("content-length", ""),
("content-location", ""),
("content-range", ""),
("content-type", ""),
("cookie", ""),
("date", ""),
("etag", ""),
("expect", ""),
("expires", ""),
("from", ""),
("host", ""),
("if-match", ""),
("if-modified-since", ""),
("if-none-match", ""),
("if-range", ""),
("if-unmodified-since", ""),
("last-modified", ""),
("link", ""),
("location", ""),
("max-forwards", ""),
("proxy-authenticate", ""),
("proxy-authorization", ""),
("range", ""),
("referer", ""),
("refresh", ""),
("retry-after", ""),
("server", ""),
("set-cookie", ""),
("strict-transport-security", ""),
("transfer-encoding", ""),
("user-agent", ""),
("vary", ""),
("via", ""),
("www-authenticate", "")
]
+348
View File
@@ -0,0 +1,348 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
enum RingBufferError {
/// Error type thrown when a write would overrun the bounds of a
/// CircularByteBuffer.
struct BufferOverrun : Error, Equatable {
/// The amount by which the write would overrun.
let amount: Int
}
}
struct StringRing {
private(set) var _storage: ByteBuffer
private(set) var _ringHead = 0
private(set) var _ringTail = 0
private(set) var _readableBytes = 0
// private but tests
mutating func _reallocateStorageAndRebase(capacity: Int) {
let capacity = max(self._storage.capacity, capacity)
switch (self._ringHead, self._ringTail) {
case (0, _):
// indices don't need to change
self._storage.reserveCapacity(capacity)
case let (r, w) where r == w:
// make a new store and zero indices
self._storage.reserveCapacity(capacity)
self.moveHead(to: 0)
self.moveTail(to: 0)
case let (r, w) where r < w:
// move the middle bit down and reset the indices
var newBytes = self._storage
newBytes.clear()
newBytes.reserveCapacity(capacity)
newBytes.write(bytes: self._storage.viewBytes(at: r, length: w - r))
self._storage = newBytes
self.moveHead(to: 0)
self.moveTail(to: w - r)
case let (r, w):
// two copies are needed
var newBytes = self._storage
newBytes.clear()
newBytes.reserveCapacity(capacity)
newBytes.write(bytes: self._storage.viewBytes(at: r, length: self._storage.capacity - r))
newBytes.write(bytes: self._storage.viewBytes(at: 0, length: w))
self.moveHead(to: 0)
self.moveTail(to: self._storage.capacity - r + w)
self._storage = newBytes
}
}
mutating func moveHead(to newIndex: Int) {
precondition(newIndex >= 0 && newIndex <= self._storage.capacity)
self._ringHead = newIndex
}
mutating func moveHead(forwardBy amount: Int) {
precondition(amount >= 0)
let readerIsBelowWriter = self._ringHead <= self._ringTail
var newIndex = self._ringHead + amount
// reader index should not pass writer index
if readerIsBelowWriter {
precondition(newIndex <= self._ringTail)
} else if newIndex >= self._storage.capacity {
// wrap it around
newIndex -= self._storage.capacity
// now test that it didn't pass writer
precondition(newIndex <= self._ringTail)
}
self.moveHead(to: newIndex)
self._readableBytes -= amount
}
mutating func moveTail(to newIndex: Int) {
precondition(newIndex >= 0 && newIndex <= self._storage.capacity)
self._ringTail = newIndex
}
mutating func moveTail(forwardBy amount: Int) {
precondition(amount >= 0)
let writerIsBelowReader = self._ringTail < self._ringHead
var newIndex = self._ringTail + amount
// writer index should not pass readerIndex
if writerIsBelowReader {
precondition(newIndex <= self._ringHead)
} else if newIndex >= self._storage.capacity {
// wrap it around
newIndex -= self._storage.capacity
// now test it
precondition(newIndex <= self._ringHead)
}
self.moveTail(to: newIndex)
self._readableBytes += amount
}
mutating func unwrite(byteCount: Int) {
precondition(byteCount >= 0)
precondition(byteCount <= self.readableBytes)
if byteCount <= self._ringTail {
self._ringTail -= byteCount
} else {
self._ringTail = self._storage.capacity - byteCount + self._ringTail
}
self._readableBytes -= byteCount
}
mutating func unread(byteCount: Int) {
precondition(byteCount >= 0)
precondition(byteCount <= self.writableBytes)
if byteCount <= self._ringHead {
self._ringHead -= byteCount
} else {
self._ringHead = self._storage.capacity - byteCount + self._ringHead
}
self._readableBytes += byteCount
}
private var _contiguousWriteSpace: Int {
switch (self._ringHead, self._ringTail) {
case let (r, w) where r == w:
if self._readableBytes == self.capacity {
return 0
}
return self.capacity - w
case let (r, w) where r < w:
return self.capacity - w
case let (r, w) where r > w:
return r - w
default:
fatalError("This should be unreachable")
}
}
// MARK: Core API
init(allocator: ByteBufferAllocator, capacity: Int) {
self._storage = allocator.buffer(capacity: capacity)
}
/// The number of bytes available to read.
var readableBytes: Int {
// we maintain readable (used) byte count separately because readIdx == writeIdx could mean either empty or full.
return self._readableBytes
}
/// The number of available bytes for writing.
var writableBytes: Int {
return self.capacity - self.readableBytes
}
/// The total capacity of this `CircularByteBuffer`.
var capacity: Int {
return Int(self._storage.capacity)
}
/// Change the capacity to exactly `to` bytes.
///
/// - parameters:
/// - to: The desired minimum capacity.
/// - precondition: Int cannot shrink beyond the number of bytes
/// currently in the buffer.
mutating func changeCapacity(to newCapacity: Int) {
precondition(newCapacity >= self.readableBytes,
"new capacity \(newCapacity) is too small to contain current buffer contents")
if newCapacity == self._storage.capacity {
return
}
// we know this capacity is ok
self._reallocateStorageAndRebase(capacity: newCapacity)
}
/// This vends a pointer to the storage of the `CircularByteBuffer`. It's marked as _very unsafe_ because it might contain
/// uninitialised memory and it's undefined behaviour to read it. In most cases you should use `withUnsafeReadableBytes`.
///
/// - warning: Do not escape the pointer from the closure for later use.
func withVeryUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
return try self._storage.withVeryUnsafeBytes(body)
}
/// Resets the state of this `StringRing` to the state of a freshly allocated one, if possible without
/// allocations. This is the cheapest way to recycle a `StringRing` for a new use-case.
mutating func clear() {
self._storage.clear()
self.moveTail(to: 0)
self.moveHead(to: 0)
self._readableBytes = 0
}
/// Write `bytes`, a `ContiguousCollection` of `UInt8` into this `StringRing`. Moves the writer index forward by the number of bytes written.
/// This method is likely more efficient than the one operating on plain `Collection` as it will use `memcpy` to copy all the bytes in one go.
///
/// - parameters:
/// - bytes: A `ContiguousCollection` of `UInt8` to be written.
/// - returns: The number of bytes written or `bytes.count`.
@discardableResult
mutating func write<C: ContiguousCollection>(bytes: C) throws -> Int where C.Element == UInt8 {
let totalCount = bytes.count
guard totalCount <= self.writableBytes else {
throw RingBufferError.BufferOverrun(amount: Int(totalCount - self._storage.capacity))
}
let firstPart = self._storage.capacity - self._ringTail
if firstPart >= bytes.count {
_storage.set(bytes: bytes, at: self._ringTail)
} else {
bytes.withUnsafeBytes { ptr in
_storage.set(bytes: ptr[..<(ptr.startIndex + firstPart)], at: self._ringTail)
_storage.set(bytes: ptr[(ptr.startIndex + firstPart)...], at: 0)
}
}
self.moveTail(forwardBy: totalCount)
return totalCount
}
/// Write `bytes`, a `Collection` of `UInt8` into this `StringRing`. Moves the writer index forward by the number of bytes written.
///
/// - parameters:
/// - bytes: A `Collection` of `UInt8` to be written.
/// - returns: The number of bytes written or `bytes.count`.
@discardableResult
mutating func write<C : Collection>(bytes: C) throws -> Int where C.Element == UInt8 {
assert(!([Array<C.Element>.self, StaticString.self, ContiguousArray<C.Element>.self, UnsafeRawBufferPointer.self, UnsafeBufferPointer<UInt8>.self].contains(where: { (t: Any.Type) -> Bool in t == type(of: bytes) })),
"called the slower set<S: Collection> function even though \(C.self) is a ContiguousCollection")
guard bytes.count <= self.writableBytes else {
throw RingBufferError.BufferOverrun(amount: Int(self._storage.capacity) - bytes.underestimatedCount)
}
// is this a contiguous write?
let contiguousWriteSpace = self._contiguousWriteSpace
guard bytes.count <= contiguousWriteSpace else {
// multiple writes -- split and recurse
let firstPart = bytes.prefix(contiguousWriteSpace)
let secondPart = bytes.dropFirst(contiguousWriteSpace)
return try self.write(bytes: firstPart) + self.write(bytes: secondPart)
}
// single write, should be straightforward
self._storage.withVeryUnsafeBytes { ptr in
let mutable = UnsafeMutableRawBufferPointer(mutating: ptr)
let tailPtr = UnsafeMutableRawBufferPointer(rebasing: mutable[(mutable.startIndex + self._ringTail)...])
tailPtr.copyBytes(from: bytes)
let targetAddr = UnsafeMutableRawPointer(mutating: ptr.baseAddress!.advanced(by: self._ringTail))
let target = UnsafeMutableRawBufferPointer(start: targetAddr, count: bytes.count)
target.copyBytes(from: bytes)
}
self.moveTail(forwardBy: bytes.count)
return bytes.count
}
}
extension StringRing {
internal var ringHead: Int {
return self._ringHead
}
internal var ringTail: Int {
return self._ringTail
}
// MARK: StaticString APIs
/// Write the static `string` into this `StringRing` using UTF-8 encoding, moving the writer index forward appropriately.
///
/// - parameters:
/// - string: The string to write.
/// - returns: The number of bytes written.
@discardableResult
mutating func write(staticString string: StaticString) throws -> Int {
return try self.write(bytes: UnsafeRawBufferPointer(start: string.utf8Start, count: string.utf8CodeUnitCount))
}
// MARK: String APIs
/// Write `string` into this `StringRing` using UTF-8 encoding, moving the writer index forward appropriately.
///
/// - parameters:
/// - string: The string to write.
/// - returns: The number of bytes written.
@discardableResult
mutating func write(string: String) throws -> Int {
return try self.write(bytes: string.utf8)
}
/// Get the string at `index` from this `StringRing` decoding using the UTF-8 encoding. Does not move the reader index.
///
/// - parameters:
/// - index: The starting index into `ByteBuffer` containing the string of interest.
/// - length: The number of bytes making up the string.
/// - returns: A `String` value deserialized from this `StringRing` or `nil` if the requested bytes aren't contained in this `ByteBuffer`.
internal func getString(at index: Int, length: Int) -> String? {
precondition(index >= 0, "index must not be negative")
precondition(length >= 0, "length must not be negative")
guard length <= self.capacity else {
return nil
}
return self.withVeryUnsafeBytes { ptr in
if self.capacity - index >= length {
// single read
return String(decoding: ptr[index..<(index+length)], as: UTF8.self)
} else {
// double read-- we will need to provide an intermediate buffer for the String API to read.
let firstPart = self.capacity - index
var bytes = ContiguousArray<UInt8>()
bytes.reserveCapacity(length)
bytes.append(contentsOf: ptr[index..<(index+firstPart)])
bytes.append(contentsOf: ptr[0..<(length-firstPart)])
return String(decoding: bytes, as: UTF8.self)
}
}
}
mutating func readString(length: Int) -> String? {
precondition(length >= 0, "length must not be negative")
guard length <= self.readableBytes else {
return nil
}
defer { self.moveHead(forwardBy: length) }
return self.getString(at: self.ringHead, length: length)
}
}
+1 -1
View File
@@ -81,7 +81,7 @@ default:
bindTarget = BindTo.ip(host: defaultHost, port: defaultPort)
}
let group = MultiThreadedEventLoopGroup(numThreads: System.coreCount)
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
let bootstrap = ServerBootstrap(group: group)
// Specify backlog and enable SO_REUSEADDR for the server itself
.serverChannelOption(ChannelOptions.backlog, value: 256)
+7
View File
@@ -23,13 +23,20 @@ import XCTest
///
#if os(Linux) || os(FreeBSD)
@testable import NIOHPACKTests
@testable import NIOHTTP2Tests
XCTMain([
testCase(BasicTests.allTests),
testCase(HPACKCodingTests.allTests),
testCase(HPACKIntegrationTests.allTests),
testCase(HTTP2StreamMultiplexerTests.allTests),
testCase(HTTP2ToHTTP1CodecTests.allTests),
testCase(HeaderTableTests.allTests),
testCase(HuffmanCodingTests.allTests),
testCase(IntegerCodingTests.allTests),
testCase(ReentrancyTests.allTests),
testCase(RingBufferTests.allTests),
testCase(SimpleClientServerTests.allTests),
])
#endif
@@ -0,0 +1 @@
//8///3v//W//83//P//8n//m//5///c//+r//wf//v//+v///D//8n//m//9L//+z//zf/+l//8P//m//9L//n//83//R//63//1//+3///e6H//z//+p//+j//zf/9H//p//+b//n///V//5v/+j//8n//m//6P//t//+j//9v//6P//5//+z//93//zf/8///r///X//3f//z//+f//7///2///N//z//+9//5v/+j//p//9H//5f//Z//+n//q//5v//q//8X//8P//N//6X//93//m//9L//73//m//5///v///V//9n//9v//o///r//+D//zf/+l///X//5v//S///s//83//pf/+x//6P//w///j//9H//4v//s///V///D//3v//N//6X//V//5v//S///t//83//P//9v//6///2f//i//8///7P//d//83//R//+T//z//93//7f//8///D//4v//P//97//q//8///9v//vf/+b//0v//t//+b//0v//2//+b//m//5///P//97//1v//N//z///J//5v/+f//3P//q//8H//7///r///w///J//5v//S///s//83//pf//D//5v//S//5///N//0f/+t//9f//t///3uh//8///qf//o//83//R//6f//m//5///1f/+b//o///J//5v/+j//7f//o///b//+j//+f//s///d//83//P//6///1//93//8///n//+///9v//zf/8///vf/+b//o//6f//R//+X//2f//p//6v/+b//6v//F///D//zf/+l///d//5v//S//+9//5v/+f//7///1f//Z///b//6P//6///g//83//pf//1//+b//0v//7P//N//6X//sf/+j//8P//4///R//+L//7P//1f//w//97//zf/+l//1f/+b//0v//7f//N//z///b//+v//9n//4v//P//+z//3f//N//0f//k//8///d//+3///P//w//+L//z///e//6v//P///b//73//m//9L//7f//m//9L//9v//m//5v/+f//z///e//9b//zf/8///yf/+b//n//9z//6v//B//+///6///8P//yf/+b//0v//7P//N//6X//w//+b//0v/+f//zf/9H//rf//X//7f//97of//P//6n//6P//N//0f/+n//5v/+f//9X//m//6P//yf/+b//o//+3//6P//2///o///n//7P//3f//N//z//+v//9f//d///P//5///v///b//83//P//73//m//6P/+n//0f//l//9n//6f/+r//m//+r//xf//w//83//pf//3f/+b//0v//vf/+b//n//+///9X//2f//2//+j//+v//4P//N//6X//9f//m//9L//+z//zf/+l//7H//o///D//+P//0f//i//+z//9X//8P//e//83//pf/9X//m//9L//+3//zf/8///2///r///Z//+L//z///s//93//zf/9H//5P//P//3f//t///z//8P//i//8///3v/+r//z///2//+9//5v//S//+3//5v//S///b//5v/+b//n//8///3v//W//83//P//8n//m//5///c//+r//wf//v//+v///D//8n//m//9L//+z//zf/+l//8P//m//9L//n//83//R//63//1//+3///e6H//z//+p//+j//zf/9H//p//+b//n///V//5v/+j//8n//m//6P//t//+j//9v//6P//5//+z//93//zf/8///r///X//3f//z//+f//7///2///N//z//+9//5v/+j//p//9H//5f//Z//+n//q//5v//q//8X//8P//N//6X//93//m//9L//73//m//5///v///V//9n//9v//o///r//+D//zf/+l///X//5v//S///s//83//pf/+x//6P//w///j//9H//4v//s///V///D//3v//N//6X//V//5v//S///t//83//P//9v//6///2f//i//8///7P//d//83//R//+T//z//93//7f//8///D//4v//P//97//q//8///9v//vf/+b//0v//t//+b//0v//2//+b//m//5///P//97//1v//N//z///J//5v/+f//3P//q//8H//7///r///w///J//5v//S///s//83//pf//D//5v//S//5///N//0f/+t//9f//t///3uh//8///qf//o//83//R//6f//m//5///1f/+b//o///J//5v/+j//7f//o///b//+j//+f//s///d//83//P//6///1//93//8///n//+///9v//zf/8///vf/+b//o//6f//R//+X//2f//p//6v/+b//6v//F///D//zf/+l///d//5v//S//+9//5v/+f//7///1f//Z///b//6P//6///g//83//pf//1//+b//0v//7P//N//6X//sf/+j//8P//4///R//+L//7P//1f//w//97//zf/+l//1f/+b//0v//7f//N//z///b//+v//9n//4v//P//+z//3f//N//0f//k//8///d//+3///P//w//+L//z///e//6v//P///b//73//m//9L//7f//m//9L//9v//m//5v/+f//z///e//9b//zf/8///yf/+b//n//9z//6v//B//+///6///8P//yf/+b//0v//7P//N//6X//w//+b//0v/+f//zf/9H//rf//X//7f//97of//P//6n//6P//N//0f/+n//5v/+f//9X//m//6P//yf/+b//o//+3//6P//2///o///n//7P//3f//N//z//+v//9f//d///P//5///v///b//83//P//73//m//6P/+n//0f//l//9n//6f/+r//m//+r//xf//w//83//pf//3f/+b//0v//vf/+b//n//+///9X//2f//2//+j//+v//4P//N//6X//9f//m//9L//+z//zf/+l//7H//o///D//+P//0f//i//+z//9X//8P//e//83//pf/9X//m//9L//+3//zf/8///2///r///Z//+L//z///s//93//zf/9H//5P//P//3f//t///z//8P//i//8///3v/+r//z///2//+9//5v//S//+3//5v//S///b//5v/+b//n//8///3v//W//83//P//8n//m//5///c//+r//wf//v//+v///D//8n//m//9L//+z//zf/+l//8P//m//9L//n//83//R//63//1//+3///e6H//z//+p//+j//zf/9H//p//+b//n///V//5v/+j//8n//m//6P//t//+j//9v//6P//5//+z//93//zf/8///r///X//3f//z//+f//7///2///N//z//+9//5v/+j//p//9H//5f//Z//+n//q//5v//q//8X//8P//N//6X//93//m//9L//73//m//5///v///V//9n//9v//o///r//+D//zf/+l///X//5v//S///s//83//pf/+x//6P//w///j//9H//4v//s///V///D//3v//N//6X//V//5v//S///t//83//P//9v//6///2f//i//8///7P//d//83//R//+T//z//93//7f//8///D//4v//P//97//q//8///9v//vf/+b//0v//t//+b//0v//2//+b//m//5///P//97//1v//N//z///J//5v/+f//3P//q//8H//7///r///w///J//5v//S///s//83//pf//D//5v//S//5///N//0f/+t//9f//t///3uh//8///qf//o//83//R//6f//m//5///1f/+b//o///J//5v/+j//7f//o///b//+j//+f//s///d//83//P//6///1//93//8///n//+///9v//zf/8///vf/+b//o//6f//R//+X//2f//p//6v/+b//6v//F///D//zf/+l///d//5v//S//+9//5v/+f//7///1f//Z///b//6P//6///g//83//pf//1//+b//0v//7P//N//6X//sf/+j//8P//4///R//+L//7P//1f//w//97//zf/+l//1f/+b//0v//7f//N//z///b//+v//9n//4v//P//+z//3f//N//0f//k//8///d//+3///P//w//+L//z///e//6v//P///b//73//m//9L//7f//m//9L//9v//m//5v/+f//z///e//9b//zf/8///yf/+b//n//9z//6v//B//+///6///8P//yf/+b//0v//7P//N//6X//w//+b//0v/+f//zf/9H//rf//X//7f//97of//P//6n//6P//N//0f/+n//5v/+f//9X//m//6P//yf/+b//o//+3//6P//2///o///n//7P//3f//N//z//+v//9f//d///P//5///v///b//83//P//73//m//6P/+n//0f//l//9n//6f/+r//m//+r//xf//w//83//pf//3f/+b//0v//vf/+b//n//+///9X//2f//2//+j//+v//4P//N//6X//9f//m//9L//+z//zf/+l//7H//o///D//+P//0f//i//+z//9X//8P//e//83//pf/9X//m//9L//+3//zf/8///2///r///Z//+L//z///s//93//zf/9H//5P//P//3f//t///z//8P//i//8///3v/+r//z///2//+9//5v//S//+3//5v//S///b//5v/+b//n//8///3v//W//83//P//8n//m//5///c//+r//wf//v//+v///D//8n//m//9L//+z//zf/+l//8P//m//9L//n//83//R//63//1//+3///e6H//z//+p//+j//zf/9H//p//+b//n///V//5v/+j//8n//m//6P//t//+j//9v//6P//5//Line truncated
@@ -0,0 +1 @@
xlooP9KeD2USLqZFB0qDUnKOQtincdFpftT5pOPuVTeWdTeXylC6mRQdKkxzVTKHuUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqP+T/ikg0jUjtn1LYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlxh5WrqZP9UUKJ89KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/MZaKD/Sng9lEi6mRQdKg1JyjkLYp3HRaX7UyKTj7lU3lnU3l8pQupkUHSpMc1Uyh6lJT2KdrTJKoNSg9UyiTUlUjKQdqCqZFPA6kqJOoWpFthVEzjSomcqhaiHkLIpINI1I7ZZS2KVFLIhRJ1IyqF81jqkLIok6iSoSoe1KiZxpUIeQq6mT/VFCibPSiD0lUpKkaxJx2DIktiFEnPcU////9///q////8UPbNOlRJ1JDqGT9KVH/zGWig/0p4PZRIupkUHSoNSco5C2Kdx0Wl+1Mik4+5VN5Z1N5fKULqZFB0qTHNVMoepSU9ina0ySqDUoPVMok1JVIykHagqmRTwOpKiTqFqRbYVRM40qJnKoWoh5CyKSDSNSO2WUtilRSyIUSdSMqhfNY6pCyKJOokqEqHtSomcaVCHkKupk/1RQomz0og9JVKSpGsScdgyJLYhRJz3FP////f//6v////FD2zTpUSdSQ6hk/SlR/8xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/MZaKD/Sng9lEi6mRQdKg1JyjkLYp3HRaX7UyKTj7lU3lnU3l8pQupkUHSpMc1Uyh6lJT2KdrTJKoNSg9UyiTUlUjKQdqCqZFPA6kqJOoWpFthVEzjSomcqhaiHkLIpINI1I7ZZS2KVFLIhRJ1IyqF81jqkLIok6iSoSoe1KiZxpUIeQq6mT/VFCibPSiD0lUpKkaxJx2DIktiFEnPcU////9///q////8UPbNOlRJ1JDqGT9KVH/zGWig/0p4PZRIupkUHSoNSco5C2Kdx0Wl+1Mik4+5VN5Z1N5fKULqZFB0qTHNVMoepSU9ina0ySqDUoPVMok1JVIykHagqmRTwOpKiTqFqRbYVRM40qJnKoWoh5CyKSDSNSO2WUtilRSyIUSdSMqhfNY6pCyKJOokqEqHtSomcaVCHkKupk/1RQomz0og9JVKSpGsScdgyJLYhRJz3FP////f//6v////FD2zTpUSdSQ6hk/SlR/8xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/MZaKD/Sng9lEi6mRQdKg1JyjkLYp3HRaX7UyKTj7lU3lnU3l8pQupkUHSpMc1Uyh6lJT2KdrTJKoNSg9UyiTUlUjKQdqCqZFPA6kqJOoWpFthVEzjSomcqhaiHkLIpINI1I7ZZS2KVFLIhRJ1IyqF81jqkLIok6iSoSoe1KiZxpUIeQq6mT/VFCibPSiD0lUpKkaxJx2DIktiFEnPcU////9///q////8UPbNOlRJ1JDqGT9KVH/zGWig/0p4PZRIupkUHSoNSco5C2Kdx0Wl+1Mik4+5VN5Z1N5fKULqZFB0qTHNVMoepSU9ina0ySqDUoPVMok1JVIykHagqmRTwOpKiTqFqRbYVRM40qJnKoWoh5CyKSDSNSO2WUtilRSyIUSdSMqhfNY6pCyKJOokqEqHtSomcaVCHkKupk/1RQomz0og9JVKSpGsScdgyJLYhRJz3FP////f//6v////FD2zTpUSdSQ6hk/SlR/8xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/MZaKD/Sng9lEi6mRQdKg1JyjkLYp3HRaX7UyKTj7lU3lnU3l8pQupkUHSpMc1Uyh6lJT2KdrTJKoNSg9UyiTUlUjKQdqCqZFPA6kqJOoWpFthVEzjSomcqhaiHkLIpINI1I7ZZS2KVFLIhRJ1IyqF81jqkLIok6iSoSoe1KiZxpUIeQq6mT/VFCibPSiD0lUpKkaxJx2DIktiFEnPcU////9///q////8UPbNOlRJ1JDqGT9KVH/zGWig/0p4PZRIupkUHSoNSco5C2Kdx0Wl+1Mik4+5VN5Z1N5fKULqZFB0qTHNVMoepSU9ina0ySqDUoPVMok1JVIykHagqmRTwOpKiTqFqRbYVRM40qJnKoWoh5CyKSDSNSO2WUtilRSyIUSdSMqhfNY6pCyKJOokqEqHtSomcaVCHkKupk/1RQomz0og9JVKSpGsScdgyJLYhRJz3FP////f//6v////FD2zTpUSdSQ6hk/SlR/8xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/MZaKD/Sng9lEi6mRQdKg1JyjkLYp3HRaX7UyKTj7lU3lnU3l8pQupkUHSpMc1Uyh6lJT2KdrTJKoNSg9UyiTUlUjKQdqCqZFPA6kqJOoWpFthVEzjSomcqhaiHkLIpINI1I7ZZS2KVFLIhRJ1IyqF81jqkLIok6iSoSoe1KiZxpUIeQq6mT/VFCibPSiD0lUpKkaxJx2DIktiFEnPcU////9///q////8UPbNOlRJ1JDqGT9KVH/zGWig/0p4PZRIupkUHSoNSco5C2Kdx0Wl+1Mik4+5VN5Z1N5fKULqZFB0qTHNVMoepSU9ina0ySqDUoPVMok1JVIykHagqmRTwOpKiTqFqRbYVRM40qJnKoWoh5CyKSDSNSO2WUtilRSyIUSdSMqhfNY6pCyKJOokqEqHtSomcaVCHkKupk/1RQomz0og9JVKSpGsScdgyJLYhRJz3FP////f//6v////FD2zTpUSdSQ6hk/SlR/8xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/MZaKD/Sng9lEi6mRQdKg1JyjkLYp3HRaX7UyKTj7lU3lnU3l8pQupkUHSpMc1Uyh6lJT2KdrTJKoNSg9UyiTUlUjKQdqCqZFPA6kqJOoWpFthVEzjSomcqhaiHkLIpINI1I7ZZS2KVFLIhRJ1IyqF81jqkLIok6iSoSoe1KiZxpUIeQq6mT/VFCibPSiD0lUpKkaxJx2DIktiFEnPcU////9///q////8UPbNOlRJ1JDqGT9KVH/zGWig/0p4PZRIupkUHSoNSco5C2Kdx0Wl+1Mik4+5VN5Z1N5fKULqZFB0qTHNVMoepSU9ina0ySqDUoPVMok1JVIykHagqmRTwOpKiTqFqRbYVRM40qJnKoWoh5CyKSDSNSO2WUtilRSyIUSdSMqhfNY6pCyKJOokqEqHtSomcaVCHkKupk/1RQomz0og9JVKSpGsScdgyJLYhRJz3FP////f//6v////FD2zTpUSdSQ6hk/SlR/8xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/MZaKD/Sng9lEi6mRQdKg1JyjkLYp3HRaX7UyKTj7lU3lnU3l8pQupkUHSpMc1Uyh6lJT2KdrTJKoNSg9UyiTUlUjKQdqCqZFPA6kqJOoWpFthVEzjSomcqhaiHkLIpINI1I7ZZS2KVFLIhRJ1IyqF81jqkLIok6iSoSoe1KiZxpUIeQq6mT/VFCibPSiD0lUpKkaxJx2DIktiFEnPcU////9///q////8UPbNOlRJ1JDqGT9KVH/zGWig/0p4PZRIupkUHSoNSco5C2Kdx0Wl+1Mik4+5VN5Z1N5fKULqZFB0qTHNVMoepSU9ina0ySqDUoPVMok1JVIykHagqmRTwOpKiTqFqRbYVRM4Line truncated
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// HPACKCodingTests+XCTest.swift
//
import XCTest
///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///
extension HPACKCodingTests {
static var allTests : [(String, (HPACKCodingTests) -> () throws -> Void)] {
return [
("testRequestHeadersWithoutHuffmanCoding", testRequestHeadersWithoutHuffmanCoding),
("testRequestHeadersWithHuffmanCoding", testRequestHeadersWithHuffmanCoding),
("testResponseHeadersWithoutHuffmanCoding", testResponseHeadersWithoutHuffmanCoding),
("testResponseHeadersWithHuffmanCoding", testResponseHeadersWithHuffmanCoding),
("testNonIndexedRequest", testNonIndexedRequest),
("testInlineDynamicTableResize", testInlineDynamicTableResize),
("testHPACKHeadersDescription", testHPACKHeadersDescription),
("testHPACKHeadersSubscript", testHPACKHeadersSubscript),
]
}
}
+583
View File
@@ -0,0 +1,583 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOHPACK
class HPACKCodingTests: XCTestCase {
let allocator = ByteBufferAllocator()
private func buffer<C : ContiguousCollection>(wrapping bytes: C) -> ByteBuffer where C.Element == UInt8 {
var buffer = allocator.buffer(capacity: bytes.count)
buffer.write(bytes: bytes)
return buffer
}
// HPACK RFC7541 § C.3
// http://httpwg.org/specs/rfc7541.html#request.examples.without.huffman.coding
func testRequestHeadersWithoutHuffmanCoding() throws {
var request1 = buffer(wrapping: [0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d])
var request2 = buffer(wrapping: [0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d, 0x63, 0x61, 0x63, 0x68, 0x65])
var request3 = buffer(wrapping: [0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
])
let headers2 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache")
])
let headers3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html"),
(":authority", "www.example.com"),
("custom-key", "custom-value")
])
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
XCTAssertEqual(decoder.dynamicTableLength, 0)
let decoded1 = try decoder.decodeHeaders(from: &request1)
XCTAssertEqual(decoded1, headers1)
XCTAssertEqual(decoder.dynamicTableLength, 57)
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 62))
let decoded2 = try decoder.decodeHeaders(from: &request2)
XCTAssertEqual(decoded2, headers2)
XCTAssertEqual(decoder.dynamicTableLength, 110)
XCTAssertEqualTuple(headers2[4], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 63))
let decoded3 = try decoder.decodeHeaders(from: &request3)
XCTAssertEqual(decoded3, headers3)
XCTAssertEqual(decoder.dynamicTableLength, 164)
XCTAssertEqualTuple(headers3[4], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers2[4], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 64))
}
// HPACK RFC7541 § C.4
// http://httpwg.org/specs/rfc7541.html#request.examples.with.huffman.coding
func testRequestHeadersWithHuffmanCoding() throws {
var request1 = buffer(wrapping: [0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request2 = buffer(wrapping: [0x82, 0x86, 0x84, 0xbe, 0x58, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf])
var request3 = buffer(wrapping: [0x82, 0x87, 0x85, 0xbf, 0x40, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
])
let headers2 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache")
])
let headers3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html"),
(":authority", "www.example.com"),
("custom-key", "custom-value")
])
var encoder = HPACKEncoder(allocator: allocator)
try encoder.beginEncoding(allocator: allocator)
try encoder.append(headers: headers1)
XCTAssertEqual(try encoder.endEncoding(), request1)
XCTAssertEqual(encoder.headerIndexTable.dynamicTableLength, 57)
XCTAssertEqualTuple(headers1[3], try encoder.headerIndexTable.header(at: 62))
try encoder.beginEncoding(allocator: allocator)
try encoder.append(headers: headers2)
XCTAssertEqual(try encoder.endEncoding(), request2)
XCTAssertEqual(encoder.headerIndexTable.dynamicTableLength, 110)
XCTAssertEqualTuple(headers2[4], try encoder.headerIndexTable.header(at: 62))
XCTAssertEqualTuple(headers1[3], try encoder.headerIndexTable.header(at: 63))
try encoder.beginEncoding(allocator: allocator)
try encoder.append(headers: headers3)
XCTAssertEqual(try encoder.endEncoding(), request3)
XCTAssertEqual(encoder.headerIndexTable.dynamicTableLength, 164)
XCTAssertEqualTuple(headers3[4], try encoder.headerIndexTable.header(at: 62))
XCTAssertEqualTuple(headers2[4], try encoder.headerIndexTable.header(at: 63))
XCTAssertEqualTuple(headers1[3], try encoder.headerIndexTable.header(at: 64))
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
XCTAssertEqual(decoder.dynamicTableLength, 0)
let decoded1 = try decoder.decodeHeaders(from: &request1)
XCTAssertEqual(decoded1, headers1)
XCTAssertEqual(decoder.dynamicTableLength, 57)
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 62))
let decoded2 = try decoder.decodeHeaders(from: &request2)
XCTAssertEqual(decoded2, headers2)
XCTAssertEqual(decoder.dynamicTableLength, 110)
XCTAssertEqualTuple(headers2[4], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 63))
let decoded3 = try decoder.decodeHeaders(from: &request3)
XCTAssertEqual(decoded3, headers3)
XCTAssertEqual(decoder.dynamicTableLength, 164)
XCTAssertEqualTuple(headers3[4], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers2[4], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 64))
}
// HPACK RFC7541 § C.5
// http://httpwg.org/specs/rfc7541.html#response.examples.without.huffman.coding
func testResponseHeadersWithoutHuffmanCoding() throws {
var response1 = buffer(wrapping: [
// :status: 302
0x48, 0x03, 0x33, 0x30, 0x32,
// cache-control: private
0x58, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65,
// date: Mon, 21 Oct 2013 20:13:21 GMT
0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x31, 0x20, 0x47, 0x4d, 0x54,
// location: https://www.example.com
0x6e, 0x17, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d
])
var response2 = buffer(wrapping: [
// :status: 307
0x48, 0x03, 0x33, 0x30, 0x37,
// cache-control: private
0xc1,
// date: Mon, 21 Oct 2013 20:13:21 GMT
0xc0,
// location: https://www.example.com
0xbf
])
var response3 = buffer(wrapping: [
// :status: 200
0x88,
// cache-control: private
0xc1,
// date: Mon, 21 Oct 2013 20:13:22 GMT
0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x32, 0x20, 0x47, 0x4d, 0x54,
// location: https://www.example.com
0xc0,
// content-encoding: gzip
0x5a, 0x04, 0x67, 0x7a, 0x69, 0x70,
// set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1
0x77, 0x38, 0x66, 0x6f, 0x6f, 0x3d, 0x41, 0x53, 0x44, 0x4a, 0x4b, 0x48, 0x51, 0x4b, 0x42, 0x5a, 0x58, 0x4f, 0x51, 0x57, 0x45, 0x4f, 0x50, 0x49, 0x55, 0x41, 0x58, 0x51, 0x57, 0x45, 0x4f, 0x49, 0x55, 0x3b, 0x20, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65, 0x3d, 0x33, 0x36, 0x30, 0x30, 0x3b, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31
])
let headers1 = HPACKHeaders([
(":status", "302"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
])
let headers2 = HPACKHeaders([
(":status", "307"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
])
let headers3 = HPACKHeaders([
(":status", "200"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
("location", "https://www.example.com"),
("content-encoding", "gzip"),
("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1")
])
var decoder = HPACKDecoder(allocator: ByteBufferAllocator(), maxDynamicTableSize: 256)
XCTAssertEqual(decoder.dynamicTableLength, 0)
let decoded1 = try decoder.decodeHeaders(from: &response1)
XCTAssertEqual(decoded1, headers1)
XCTAssertEqual(decoder.dynamicTableLength, 222)
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers1[2], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers1[1], try decoder.headerTable.header(at: 64))
XCTAssertEqualTuple(headers1[0], try decoder.headerTable.header(at: 65))
let decoded2 = try decoder.decodeHeaders(from: &response2)
XCTAssertEqual(decoded2, headers2)
XCTAssertEqual(decoder.dynamicTableLength, 222)
XCTAssertEqualTuple(headers2[0], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers1[2], try decoder.headerTable.header(at: 64))
XCTAssertEqualTuple(headers1[1], try decoder.headerTable.header(at: 65))
let decoded3 = try decoder.decodeHeaders(from: &response3)
XCTAssertEqual(decoded3, headers3)
XCTAssertEqual(decoder.dynamicTableLength, 215)
XCTAssertEqualTuple(headers3[5], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers3[4], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers3[2], try decoder.headerTable.header(at: 64))
}
// HPACK RFC7541 § C.6
// http://httpwg.org/specs/rfc7541.html#response.examples.with.huffman.coding
func testResponseHeadersWithHuffmanCoding() throws {
var response1 = buffer(wrapping: [
0x48, 0x82, 0x64, 0x02, 0x58, 0x85, 0xae, 0xc3, 0x77, 0x1a, 0x4b, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff, 0x6e, 0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3
])
var response2 = buffer(wrapping: [
0x48, 0x83, 0x64, 0x0e, 0xff, 0xc1, 0xc0, 0xbf
])
var response3 = buffer(wrapping: [
0x88, 0xc1, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x84, 0xa6, 0x2d, 0x1b, 0xff, 0xc0, 0x5a, 0x83, 0x9b, 0xd9, 0xab, 0x77, 0xad, 0x94, 0xe7, 0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b, 0x39, 0x60, 0xd5, 0xaf, 0x27, 0x08, 0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27, 0x0f, 0xb5, 0x29, 0x1f, 0x95, 0x87, 0x31, 0x60, 0x65, 0xc0, 0x03, 0xed, 0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07
])
let headers1 = HPACKHeaders([
(":status", "302"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
])
let headers2 = HPACKHeaders([
(":status", "307"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
])
let headers3 = HPACKHeaders([
(":status", "200"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
("location", "https://www.example.com"),
("content-encoding", "gzip"),
("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1")
])
var encoder = HPACKEncoder(allocator: allocator)
try encoder.beginEncoding(allocator: allocator)
try encoder.append(headers: headers1)
XCTAssertEqual(try encoder.endEncoding(), response1)
XCTAssertEqualTuple(headers1[3], try encoder.headerIndexTable.header(at: 62))
XCTAssertEqualTuple(headers1[2], try encoder.headerIndexTable.header(at: 63))
XCTAssertEqualTuple(headers1[1], try encoder.headerIndexTable.header(at: 64))
XCTAssertEqualTuple(headers1[0], try encoder.headerIndexTable.header(at: 65))
try encoder.beginEncoding(allocator: allocator)
try encoder.append(headers: headers2)
XCTAssertEqual(try encoder.endEncoding(), response2)
XCTAssertEqualTuple(headers2[0], try encoder.headerIndexTable.header(at: 62))
XCTAssertEqualTuple(headers1[3], try encoder.headerIndexTable.header(at: 63))
XCTAssertEqualTuple(headers1[2], try encoder.headerIndexTable.header(at: 64))
XCTAssertEqualTuple(headers1[1], try encoder.headerIndexTable.header(at: 65))
try encoder.beginEncoding(allocator: allocator)
try encoder.append(headers: headers3)
XCTAssertEqual(try encoder.endEncoding(), response3)
XCTAssertEqualTuple(headers3[5], try encoder.headerIndexTable.header(at: 62))
XCTAssertEqualTuple(headers3[4], try encoder.headerIndexTable.header(at: 63))
XCTAssertEqualTuple(headers3[2], try encoder.headerIndexTable.header(at: 64))
var decoder = HPACKDecoder(allocator: ByteBufferAllocator(), maxDynamicTableSize: 256)
XCTAssertEqual(decoder.dynamicTableLength, 0)
let decoded1 = try decoder.decodeHeaders(from: &response1)
XCTAssertEqual(decoded1, headers1)
XCTAssertEqual(decoder.dynamicTableLength, 222)
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers1[2], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers1[1], try decoder.headerTable.header(at: 64))
XCTAssertEqualTuple(headers1[0], try decoder.headerTable.header(at: 65))
let decoded2 = try decoder.decodeHeaders(from: &response2)
XCTAssertEqual(decoded2, headers2)
XCTAssertEqual(decoder.dynamicTableLength, 222)
XCTAssertEqualTuple(headers2[0], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers1[2], try decoder.headerTable.header(at: 64))
XCTAssertEqualTuple(headers1[1], try decoder.headerTable.header(at: 65))
let decoded3 = try decoder.decodeHeaders(from: &response3)
XCTAssertEqual(decoded3, headers3)
XCTAssertEqual(decoder.dynamicTableLength, 215)
XCTAssertEqualTuple(headers3[5], try decoder.headerTable.header(at: 62))
XCTAssertEqualTuple(headers3[4], try decoder.headerTable.header(at: 63))
XCTAssertEqualTuple(headers3[2], try decoder.headerTable.header(at: 64))
}
func testNonIndexedRequest() throws {
var request1 = buffer(wrapping: [0x82, 0x86, 0x84, 0x01, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request2 = buffer(wrapping: [0x82, 0x86, 0x84, 0x01, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x0f, 0x09, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf, 0x00, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
var request3 = buffer(wrapping: [0x82, 0x87, 0x85, 0x11, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x10, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/")
])
let h1NoIndex = (name: ":authority", value: "www.example.com")
let h2NoIndex = (name: "cache-control", value: "no-cache")
let headers3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html")
])
let h3NeverIndex = (name: "custom-key", value: "custom-value")
var encoder = HPACKEncoder(allocator: allocator)
try encoder.beginEncoding(allocator: allocator)
XCTAssertNoThrow(try encoder.append(headers: headers1))
try encoder.appendNonIndexed(header: h1NoIndex.name, value: h1NoIndex.value)
XCTAssertEqual(try encoder.endEncoding(), request1)
XCTAssertEqual(encoder.headerIndexTable.dynamicTableLength, 0)
try encoder.beginEncoding(allocator: allocator)
XCTAssertNoThrow(try encoder.append(headers: headers1))
try encoder.appendNonIndexed(header: h1NoIndex.name, value: h1NoIndex.value)
try encoder.appendNonIndexed(header: h2NoIndex.name, value: h2NoIndex.value)
try encoder.appendNonIndexed(header: h3NeverIndex.name, value: h3NeverIndex.value)
XCTAssertEqual(try encoder.endEncoding(), request2)
XCTAssertEqual(encoder.headerIndexTable.dynamicTableLength, 0)
try encoder.beginEncoding(allocator: allocator)
XCTAssertNoThrow(try encoder.append(headers: headers3))
try encoder.appendNeverIndexed(header: h1NoIndex.name, value: h1NoIndex.value)
try encoder.appendNeverIndexed(header: h3NeverIndex.name, value: h3NeverIndex.value)
XCTAssertEqual(try encoder.endEncoding(), request3)
XCTAssertEqual(encoder.headerIndexTable.dynamicTableLength, 0)
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
XCTAssertEqual(decoder.dynamicTableLength, 0)
let fullHeaders1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
])
let fullHeaders2 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache"),
("custom-key", "custom-value")
])
let fullHeaders3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html"),
(":authority", "www.example.com"),
("custom-key", "custom-value")
])
let decoded1 = try decoder.decodeHeaders(from: &request1)
XCTAssertEqual(decoded1, fullHeaders1)
XCTAssertEqual(decoder.dynamicTableLength, 0)
let decoded2 = try decoder.decodeHeaders(from: &request2)
XCTAssertEqual(decoded2, fullHeaders2)
XCTAssertEqual(decoder.dynamicTableLength, 0)
let decoded3 = try decoder.decodeHeaders(from: &request3)
XCTAssertEqual(decoded3, fullHeaders3)
XCTAssertEqual(decoder.dynamicTableLength, 0)
}
func testInlineDynamicTableResize() throws {
var request1 = buffer(wrapping: [0x3f, 0x32, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
let request2 = buffer(wrapping: [0x3f, 0x21, 0x3f, 0x32, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request3 = buffer(wrapping: [0x3f, 0xe1, 0x20, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request4 = buffer(wrapping: [0x82, 0x86, 0x3f, 0x32, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
])
let oddMaxTableSize = 81
var encoder = HPACKEncoder(allocator: allocator)
XCTAssertNotEqual(encoder.allowedDynamicTableSize, oddMaxTableSize)
// adding these all manually to ensure our table size insert happens
try encoder.setDynamicTableSize(oddMaxTableSize)
try encoder.beginEncoding(allocator: allocator)
XCTAssertNoThrow(try encoder.append(header: ":method", value: "GET"))
XCTAssertNoThrow(try encoder.append(header: ":scheme", value: "http"))
XCTAssertNoThrow(try encoder.append(header: ":path", value: "/"))
XCTAssertNoThrow(try encoder.append(header: ":authority", value: "www.example.com"))
XCTAssertEqual(try encoder.endEncoding(), request1)
XCTAssertEqual(encoder.dynamicTableSize, 57)
XCTAssertEqual(encoder.allowedDynamicTableSize, oddMaxTableSize)
XCTAssertEqualTuple(headers1[3], try encoder.headerIndexTable.header(at: 62))
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
decoder.allowedDynamicTableLength = 22 // not enough to store the value we expect it to eventually store
let decoded: HPACKHeaders
do {
decoded = try decoder.decodeHeaders(from: &request1)
}
catch {
XCTFail("Failed to decode header set containing dynamic-table-resize command: \(error)")
return
}
XCTAssertEqual(decoded, headers1)
XCTAssertEqual(decoder.maxDynamicTableLength, oddMaxTableSize)
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 62))
// Now, ensure some special cases.
request1.moveReaderIndex(to: 0) // make the data available again in our sample buffer
// 1 - if we try to change the size mid-buffer, it'll throw an error.
try encoder.setDynamicTableSize(4096)
// consume the table size change
try encoder.beginEncoding(allocator: ByteBufferAllocator())
_ = try encoder.endEncoding()
encoder.headerIndexTable.dynamicTable.clear()
try encoder.beginEncoding(allocator: allocator)
XCTAssertNoThrow(try encoder.append(header: ":method", value: "GET"))
XCTAssertNoThrow(try encoder.append(header: ":scheme", value: "http"))
XCTAssertNoThrow(try encoder.append(header: ":path", value: "/"))
XCTAssertThrowsError(try encoder.setDynamicTableSize(oddMaxTableSize)) // should throw
XCTAssertNoThrow(try encoder.append(header: ":authority", value: "www.example.com"))
// No resize information, but the rest of the block will be there
XCTAssertEqual(try encoder.endEncoding(), request1.getSlice(at: 2, length: request1.readableBytes - 2))
try encoder.setDynamicTableSize(4096)
// consume the table size change
try encoder.beginEncoding(allocator: ByteBufferAllocator())
_ = try encoder.endEncoding()
encoder.headerIndexTable.dynamicTable.clear()
// 2 - We can set multiple sizes, and both the smallest and the latest will be sent.
try encoder.setDynamicTableSize(64)
try encoder.setDynamicTableSize(75)
try encoder.setDynamicTableSize(oddMaxTableSize /* 81 */)
try encoder.beginEncoding(allocator: allocator)
XCTAssertNoThrow(try encoder.append(header: ":method", value: "GET"))
XCTAssertNoThrow(try encoder.append(header: ":scheme", value: "http"))
XCTAssertNoThrow(try encoder.append(header: ":path", value: "/"))
XCTAssertNoThrow(try encoder.append(header: ":authority", value: "www.example.com"))
XCTAssertEqual(try encoder.endEncoding(), request2)
// 3 - Encoder will throw if the requested size exceeds the maximum value.
// NB: current size is 81 bytes.
encoder.headerIndexTable.dynamicTable.clear()
XCTAssertThrowsError(try encoder.setDynamicTableSize(8192)) { error in
guard let err = error as? NIOHPACKErrors.InvalidDynamicTableSize else {
XCTFail()
return
}
XCTAssertEqual(err.requestedSize, 8192)
XCTAssertEqual(err.allowedSize, 4096)
}
do {
_ = try decoder.decodeHeaders(from: &request3)
XCTFail("Decode should have failed with InvalidDynamicTableSize")
} catch _ as NIOHPACKErrors.InvalidDynamicTableSize {
// this is expected
} catch {
XCTFail("Unexpected error: \(error)")
}
// 5 - Decoder will not accept a table size update unless it appears at the start of a header block.
decoder.headerTable.dynamicTable.clear()
do {
_ = try decoder.decodeHeaders(from: &request4)
XCTFail("Decode should have failed with IllegalDynamicTableSizeChange")
} catch _ as NIOHPACKErrors.IllegalDynamicTableSizeChange {
// this is expected
} catch {
XCTFail("Unexpected error: \(error)")
}
}
func testHPACKHeadersDescription() throws {
let headerList1: [(String, String)] = [(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")]
let headerList2: [(String, String)] = [(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache")]
let headerList3: [(HPACKIndexing, String, String)] = [(.indexable, ":method", "POST"),
(.indexable, ":scheme", "https"),
(.indexable, ":path", "/send.html"),
(.indexable, ":authority", "www.example.com"),
(.indexable, "custom-key", "custom-value"),
(.nonIndexable, "content-length", "42")]
let headers1 = HPACKHeaders(headerList1)
let headers2 = HPACKHeaders(headerList2)
let headers3 = HPACKHeaders(fullHeaders: headerList3)
let description1 = headers1.description
let expected1 = headerList1.map { (HPACKIndexing.indexable, $0.0, $0.1) }.description
XCTAssertEqual(description1, expected1)
let description2 = headers2.description
let expected2 = headerList2.map { (HPACKIndexing.indexable, $0.0, $0.1) }.description
XCTAssertEqual(description2, expected2)
let description3 = headers3.description
let expected3 = headerList3.description // already contains indexing where we'd expect
XCTAssertEqual(description3, expected3)
}
func testHPACKHeadersSubscript() throws {
let headers = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache"),
("custom-key", "value-1,value-2"),
("set-cookie", "abcdefg,hijklmn,opqrst"),
("custom-key", "value-3")
])
XCTAssertEqual(headers[":method"], ["GET"])
XCTAssertEqual(headers[":authority"], ["www.example.com"])
XCTAssertTrue(headers.contains(name: "cache-control"))
XCTAssertTrue(headers.contains(name: "Cache-Control"))
XCTAssertFalse(headers.contains(name: "content-length"))
XCTAssertEqual(headers["custom-key"], ["value-1,value-2", "value-3"])
XCTAssertEqual(headers[canonicalForm: "custom-key"], ["value-1", "value-2", "value-3"])
XCTAssertEqual(headers["set-cookie"], ["abcdefg,hijklmn,opqrst"])
XCTAssertEqual(headers[canonicalForm: "set-cookie"], ["abcdefg,hijklmn,opqrst"])
}
}
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// HPACKIntegrationTests+XCTest.swift
//
import XCTest
///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///
extension HPACKIntegrationTests {
static var allTests : [(String, (HPACKIntegrationTests) -> () throws -> Void)] {
return [
("testAAEncoderWithoutHuffmanCoding", testAAEncoderWithoutHuffmanCoding),
("testABEncoderWithHuffmanCoding", testABEncoderWithHuffmanCoding),
("testDecoder", testDecoder),
]
}
}
@@ -0,0 +1,350 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOHPACK
class HPACKIntegrationTests : XCTestCase {
private let hpackTestsURL: URL = {
let myURL = URL(fileURLWithPath: #file, isDirectory: false)
let result: URL
if #available(OSX 10.11, iOS 9.0, *) {
result = URL(fileURLWithPath: "../../hpack-test-case", isDirectory: true, relativeTo: myURL).absoluteURL
} else {
// Fallback on earlier versions
result = URL(string: "../../hpack-test-case", relativeTo: myURL)!.absoluteURL
}
return result
}()
private enum TestType : String {
case encoding = "raw-data"
case swiftNIOPlain = "swift-nio-hpack-plain-text"
case swiftNIOHuffman = "swift-nio-hpack-huffman"
case node = "node-http2-hpack"
case python = "python-hpack"
case go = "go-hpack"
case nghttp2
case nghttp2ChangeTableSize = "nghttp2-change-table-size"
case nghttp2LargeTables = "nghttp2-16384-4096"
case haskellStatic = "haskell-http2-static"
case haskellStaticHuffman = "haskell-http2-static-huffman"
case haskellNaive = "haskell-http2-naive"
case haskellNaiveHuffman = "haskell-http2-naive-huffman"
case haskellLinear = "haskell-http2-linear"
case haskellLinearHuffman = "haskell-http2-linear-huffman"
static let allCases: [TestType] = [
.encoding, .swiftNIOPlain, .swiftNIOHuffman, .node, .python, .go,
.nghttp2, .nghttp2ChangeTableSize, .nghttp2LargeTables,
.haskellStatic, .haskellStaticHuffman, .haskellNaive, .haskellNaiveHuffman,
.haskellLinear, .haskellLinearHuffman
]
}
private func getSourceURL(for type: TestType) -> URL {
return self.hpackTestsURL.appendingPathComponent(type.rawValue, isDirectory: true).absoluteURL
}
private func loadStories(for test: TestType) -> [HPACKStory] {
let url = self.getSourceURL(for: test)
guard let contents = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: []).sorted(by: {$0.lastPathComponent < $1.lastPathComponent}) else {
return []
}
let decoder = JSONDecoder()
do {
var stories = try contents.compactMap { url -> HPACKStory? in
// for some reason using filter({ $0.pathExtension == "json" }) throws the compiler type-checker for a loop
guard url.pathExtension == "json" else { return nil }
let data = try Data(contentsOf: url, options: [.uncached, .mappedIfSafe])
return try decoder.decode(HPACKStory.self, from: data)
}
// ensure there are valid non-zero sequence numbers
// to ensure we modify these value types in-place we apparently need to use collection subscripting for all accesses
// it seems that if there are sequence numbers on any cases they're on all cases; we can optimize for that.
for idx in stories.indices where stories[idx].cases.count > 1 && stories[idx].cases[1].seqno == 0 {
for cidx in stories[idx].cases.indices {
stories[idx].cases[cidx].seqno = cidx
}
}
return stories
} catch {
XCTFail("Failed to decode encoder stories from JSON files")
return []
}
}
private func runEncodingTests(withOutput type: TestType) {
let huffmanEncoded = type == .swiftNIOHuffman
let stories = loadStories(for: .encoding)
guard stories.count > 0 else {
// we don't have the data, so don't go failing any tests
return
}
let outputDir = getSourceURL(for: type)
if FileManager.default.fileExists(atPath: outputDir.path) == false {
try! FileManager.default.createDirectory(at: outputDir, withIntermediateDirectories: true, attributes: nil)
}
var outputs: [HPACKStory] = []
let start = Date()
for (idx, story) in stories.enumerated() {
let encoded = runEncodeStory(story, idx, huffmanEncoded: huffmanEncoded)
outputs.append(encoded)
}
let end = Date()
print("Encoding took \(String(format: "%.02f", end.timeIntervalSince(start))) seconds.")
for (idx, story) in outputs.enumerated() {
writeOutputStory(story, at: idx, to: outputDir)
}
}
// funky names to ensure the encoder tests run before the decoder tests.
func testAAEncoderWithoutHuffmanCoding() {
runEncodingTests(withOutput: .swiftNIOPlain)
}
// funky names to ensure the encoder tests run before the decoder tests.
func testABEncoderWithHuffmanCoding() {
runEncodingTests(withOutput: .swiftNIOHuffman)
}
func testDecoder() {
var testTimings: [TestType : TimeInterval] = [:]
for test in TestType.allCases where test != .encoding {
let duration = _testDecoder(for: test)
testTimings[test] = duration
}
print("Test timing metrics:")
for (test, duration) in testTimings {
let testName = test.rawValue
var firstPart = " - \(testName) "
if firstPart.count < 40 {
firstPart.append(String(repeating: " ", count: 40 - firstPart.count))
}
print("\(firstPart) : \(String(format: "%.02f", duration)) seconds.")
}
}
private func _testDecoder(for test: TestType) -> TimeInterval {
let loadStart = Date()
print("Loading \(test.rawValue)...", terminator: "")
let stories = loadStories(for: test)
let loadTime = Date().timeIntervalSince(loadStart)
guard stories.count > 0 else {
// no input data = no problems
print(" no stories found.")
return 0
}
print(" done. Load time = \(String(format: "%.02f", loadTime)) seconds.")
// description comes from first test case
if let description = stories.first!.description {
print(description)
}
let start = Date()
for (idx, story) in stories.enumerated() {
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
for testCase in story.cases {
do {
if let size = testCase.headerTableSize {
decoder.maxDynamicTableLength = size
}
guard var bytes = testCase.wire else {
XCTFail("Decoder story case \(testCase.seqno) has no wire data to decode!")
return Date().timeIntervalSince(start)
}
let decoded = try decoder.decodeHeaders(from: &bytes)
XCTAssertEqual(testCase.headers, decoded)
} catch {
print(" \(testCase.seqno) - failed.")
XCTFail("Failure in story \(idx), case \(testCase.seqno) - \(error)")
}
}
}
return Date().timeIntervalSince(start)
}
private func writeOutputStory(_ story: HPACKStory, at index: Int, to directory: URL) {
let outURL = directory
.appendingPathComponent(String(format: "story_%02d", index), isDirectory: false)
.appendingPathExtension("json")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
do {
let data = try encoder.encode(story)
try data.write(to: outURL, options: [.atomic])
} catch {
print("Error writing encoded test case: \(error)")
}
}
private func runEncodeStory(_ story: HPACKStory, _ index: Int, huffmanEncoded: Bool = true) -> HPACKStory {
// do we need to care about the context?
var encoder = HPACKEncoder(allocator: ByteBufferAllocator(), useHuffmanEncoding: huffmanEncoded)
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
var result = story
result.cases.removeAll()
if huffmanEncoded {
result.description = "Encoded using the HPACK implementation of swift-nio-http2, with static table, dynamic table, and huffman encoded strings: <https://github.com/apple/swift-nio-http2>"
} else {
result.description = "Encoded using the HPACK implementation of swift-nio-http2, with static table, dynamic table, and plain-text encoded strings: <https://github.com/apple/swift-nio-http2>"
}
for storyCase in story.cases {
do {
if let tableSize = storyCase.headerTableSize {
try encoder.setDynamicTableSize(tableSize)
decoder.maxDynamicTableLength = tableSize
}
try encoder.beginEncoding(allocator: ByteBufferAllocator())
try encoder.append(headers: storyCase.headers)
var outputCase = storyCase
var encoded = try encoder.endEncoding()
outputCase.wire = encoded
result.cases.append(outputCase)
// now try to decode it
let decoded = try decoder.decodeHeaders(from: &encoded)
XCTAssertEqual(storyCase.headers, decoded)
} catch {
print(" \(storyCase.seqno) - failed.")
XCTFail("Failure in story \(index), case \(storyCase.seqno) - \(error)")
}
}
// the output story now has all the wire values filled in
return result
}
}
struct HPACKStory : Codable {
var context: HPACKStoryContext?
var description: String?
var cases: [HPACKStoryCase]
}
enum HPACKStoryContext : String, Codable {
case request
case response
}
struct HPACKStoryCase : Codable {
var seqno: Int
var headerTableSize: Int?
var wire: ByteBuffer?
var headers: HPACKHeaders
private enum Keys : String, CodingKey {
case seqno
case headerTableSize = "header_table_size"
case wire
case headers
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
self.seqno = try container.decodeIfPresent(Int.self, forKey: .seqno) ?? 0
self.headerTableSize = try container.decodeIfPresent(Int.self, forKey: .headerTableSize)
if let wireString = try container.decodeIfPresent(String.self, forKey: .wire) {
self.wire = decodeHexData(from: wireString)
} else {
self.wire = nil
}
let rawHeaders = try container.decode([[String : String]].self, forKey: .headers)
let pairs = rawHeaders.map { ($0.first!.key, $0.first!.value) }
self.headers = HPACKHeaders(pairs)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(self.seqno, forKey: .seqno)
try container.encode(self.headerTableSize, forKey: .headerTableSize)
if let bytes = self.wire {
try container.encode(encodeHex(data: bytes), forKey: .wire)
}
let rawHeaders = self.headers.map { [$0.0 : $0.1] }
try container.encode(rawHeaders, forKey: .headers)
}
}
fileprivate func decodeHexData(from string: String) -> ByteBuffer {
var bytes: [UInt8] = []
var idx = string.startIndex
repeat {
let byteStr = string[idx...string.index(after: idx)]
bytes.append(UInt8(byteStr, radix: 16)!)
idx = string.index(idx, offsetBy: 2)
} while string.distance(from: idx, to: string.endIndex) > 1
var buf = ByteBufferAllocator().buffer(capacity: bytes.count)
buf.write(bytes: bytes)
return buf
}
fileprivate func encodeHex(data: ByteBuffer) -> String {
var result = ""
for byte in data.readableBytesView {
let str = String(byte, radix: 16)
if str.count == 1 {
result.append("0")
}
result.append(str)
}
return result
}
extension ByteBufferView : CustomDebugStringConvertible {
public var debugDescription: String {
var desc = "\(self.count) bytes: ["
for byte in self {
let hexByte = String(byte, radix: 16)
desc += " \(hexByte.count == 1 ? "0" : "")\(hexByte)"
}
desc += " ]"
return desc
}
}
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// HeaderTableTests+XCTest.swift
//
import XCTest
///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///
extension HeaderTableTests {
static var allTests : [(String, (HeaderTableTests) -> () throws -> Void)] {
return [
("testStaticHeaderTable", testStaticHeaderTable),
("testDynamicTableInsertion", testDynamicTableInsertion),
("testHeaderDump", testHeaderDump),
("testHeaderDescription", testHeaderDescription),
]
}
}
+149
View File
@@ -0,0 +1,149 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOHPACK
func XCTAssertEqualTuple<T1: Equatable, T2: Equatable>(_ expression1: @autoclosure () throws -> (T1, T2), _ expression2: @autoclosure () throws -> (T1, T2), _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
let ex1: (T1, T2)
let ex2: (T1, T2)
do {
ex1 = try expression1()
ex2 = try expression2()
}
catch {
XCTFail("Unexpected exception: \(error) \(message())", file: file, line: line)
return
}
XCTAssertEqual(ex1.0, ex2.0, message(), file: file, line: line)
XCTAssertEqual(ex1.1, ex2.1, message(), file: file, line: line)
}
class HeaderTableTests: XCTestCase {
func testStaticHeaderTable() {
let table = IndexedHeaderTable(allocator: ByteBufferAllocator())
// headers with matching values
XCTAssertEqualTuple((2, true), table.firstHeaderMatch(for: ":method", value: "GET")!)
XCTAssertEqualTuple((3, true), table.firstHeaderMatch(for: ":method", value: "POST")!)
XCTAssertEqualTuple((4, true), table.firstHeaderMatch(for: ":path", value: "/")!)
XCTAssertEqualTuple((5, true), table.firstHeaderMatch(for: ":path", value: "/index.html")!)
XCTAssertEqualTuple((8, true), table.firstHeaderMatch(for: ":status", value: "200")!)
XCTAssertEqualTuple((13, true), table.firstHeaderMatch(for: ":status", value: "404")!)
XCTAssertEqualTuple((14, true), table.firstHeaderMatch(for: ":status", value: "500")!)
XCTAssertEqualTuple((16, true), table.firstHeaderMatch(for: "accept-encoding", value: "gzip, deflate")!)
// headers with no values in the table
XCTAssertEqualTuple((15, false), table.firstHeaderMatch(for: "accept-charset", value: "any")!)
XCTAssertEqualTuple((24, false), table.firstHeaderMatch(for: "cache-control", value: "private")!)
// header-only matches for table entries with a different value
XCTAssertEqualTuple((8, false), table.firstHeaderMatch(for: ":status", value: "501")!)
XCTAssertEqualTuple((4, false), table.firstHeaderMatch(for: ":path", value: "/test/path.html")!)
XCTAssertEqualTuple((2, false), table.firstHeaderMatch(for: ":method", value: "CONNECT")!)
// things that aren't in the table at all
XCTAssertNil(table.firstHeaderMatch(for: "non-existent-key", value: "non-existent-value"))
}
func testDynamicTableInsertion() {
// NB: I'm using the overall table class to verify the expected indices of dynamic table items.
var table = IndexedHeaderTable(allocator: ByteBufferAllocator(), maxDynamicTableSize: 1024)
XCTAssertEqual(table.dynamicTableLength, 0)
XCTAssertNoThrow(try table.add(headerNamed: ":authority", value: "www.example.com"))
XCTAssertEqual(table.dynamicTableLength, 57)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.something-else.com")!)
XCTAssertNoThrow(try table.add(headerNamed: "cache-control", value: "no-cache"))
XCTAssertEqual(table.dynamicTableLength, 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
// custom key not yet in the table, should return nil
XCTAssertNil(table.firstHeaderMatch(for: "custom-key", value: "custom-value"))
XCTAssertNoThrow(try table.add(headerNamed: "custom-key", value: "custom-value"))
XCTAssertEqual(table.dynamicTableLength, 164)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((64, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
// should evict the first-inserted value (:authority = www.example.com)
table.dynamicTableAllowedLength = 128
XCTAssertEqual(table.dynamicTableLength, 164 - 57)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!) // will find the header name in static table
table.dynamicTableAllowedLength = 64
XCTAssertEqual(table.dynamicTableLength, 164 - 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((24, false), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!) // will find the header name in static table
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!) // will find the header name in static table
table.dynamicTableAllowedLength = 164 - 110 // should cause no evictions
XCTAssertEqual(table.dynamicTableLength, 164 - 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
// get code coverage for dynamic table search with no value to match
// Note that the resulting index is an index into the dynamic table only, so we
// have to modify it to check that it's what we expect.
if let found = table.dynamicTable.findExistingHeader(named: "custom-key".utf8, value: Optional<[UInt8]>.none) {
XCTAssertEqualTuple((62 - StaticHeaderTable.count, false), found)
}
// evict final entry
table.dynamicTableAllowedLength = table.dynamicTableLength - 1
XCTAssertEqual(table.dynamicTableLength, 0)
XCTAssertNil(table.firstHeaderMatch(for: "custom-key", value: "custom-value"))
}
func testHeaderDump() throws {
var table = IndexedHeaderTable(allocator: ByteBufferAllocator())
let description = table.dumpHeaders()
// construct what we expect
var expected = StaticHeaderTable.enumerated().reduce("") {
$0 + "\($1.0) - \($1.1.0) : \($1.1.1)\n"
}
// there will be a newline at the end, followed by no data for the dynamic table
expected += "\n"
XCTAssertEqual(description, expected)
// add an item to the dynamic table
try table.add(headerNamed: "custom-key", value: "custom-value")
expected += "\(StaticHeaderTable.count) - custom-key : custom-value\n"
let description2 = table.dumpHeaders()
XCTAssertEqual(description2, expected)
}
func testHeaderDescription() throws {
let table = IndexedHeaderTable(allocator: ByteBufferAllocator())
let staticDescription = table.staticTable.description
let staticExpected = StaticHeaderTable.description
XCTAssertEqual(staticDescription, staticExpected)
}
}
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// HuffmanCodingTests+XCTest.swift
//
import XCTest
///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///
extension HuffmanCodingTests {
static var allTests : [(String, (HuffmanCodingTests) -> () throws -> Void)] {
return [
("testBasicCoding", testBasicCoding),
("testComplexCoding", testComplexCoding),
("testBasicEncodingPerformance", testBasicEncodingPerformance),
("testBasicDecodingPerformance", testBasicDecodingPerformance),
("testComplexEncodingPerformance", testComplexEncodingPerformance),
("testComplexDecodingPerformance", testComplexDecodingPerformance),
]
}
}
@@ -0,0 +1,189 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
import Foundation
@testable import NIOHPACK
class HuffmanCodingTests: XCTestCase {
var scratchBuffer: ByteBuffer = ByteBufferAllocator().buffer(capacity: 4096)
let fixtureDirURL = URL(fileURLWithPath: #file).deletingLastPathComponent().appendingPathComponent("Fixtures").absoluteURL
// MARK: - Helper Methods
func assertEqualContents(_ buffer: ByteBuffer, _ array: [UInt8], file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(buffer.readableBytes, array.count, "Buffer and array are different sizes", file: file, line: line)
buffer.withUnsafeReadableBytes { bufPtr in
array.withUnsafeBytes { arrayPtr in
XCTAssertEqual(memcmp(bufPtr.baseAddress!, arrayPtr.baseAddress!, arrayPtr.count), 0, "Buffer contents don't match", file: file, line: line)
}
}
}
func verifyHuffmanCoding(_ string: String, _ bytes: [UInt8], file: StaticString = #file, line: UInt = #line) throws {
self.scratchBuffer.clear()
let utf8 = string.utf8
let numBytes = self.scratchBuffer.writeHuffmanEncoded(bytes: utf8)
XCTAssertEqual(numBytes, bytes.count, "Wrong length encoding '\(string)'", file: file, line: line)
assertEqualContents(self.scratchBuffer, bytes, file: file, line: line)
var buf = ByteBufferAllocator().buffer(capacity: utf8.count)
let numDecoded = try scratchBuffer.getHuffmanEncodedString(at: self.scratchBuffer.readerIndex, length: self.scratchBuffer.readableBytes, into: &buf)
XCTAssertEqual(numDecoded, utf8.count)
let decoded = buf.readString(length: buf.readableBytes)
XCTAssertEqual(decoded, string, "Failed to decode '\(string)'", file: file, line: line)
}
func testBasicCoding() throws {
// all these values come from http://httpwg.org/specs/rfc7541.html#request.examples.with.huffman.coding
try verifyHuffmanCoding("www.example.com", [0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
try verifyHuffmanCoding("no-cache", [0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf])
try verifyHuffmanCoding("custom-key", [0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f])
try verifyHuffmanCoding("custom-value", [0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
// these come from http://httpwg.org/specs/rfc7541.html#response.examples.with.huffman.coding
try verifyHuffmanCoding("302", [0x64, 0x02])
try verifyHuffmanCoding("private", [0xae, 0xc3, 0x77, 0x1a, 0x4b])
try verifyHuffmanCoding("Mon, 21 Oct 2013 20:13:21 GMT", [0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff])
try verifyHuffmanCoding("https://www.example.com", [0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3])
try verifyHuffmanCoding("307", [0x64, 0x0e, 0xff])
try verifyHuffmanCoding("Mon, 21 Oct 2013 20:13:22 GMT", [0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x84, 0xa6, 0x2d, 0x1b, 0xff])
try verifyHuffmanCoding("gzip", [0x9b, 0xd9, 0xab])
try verifyHuffmanCoding("foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", [0x94, 0xe7, 0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b, 0x39, 0x60, 0xd5, 0xaf, 0x27, 0x08, 0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27, 0x0f, 0xb5, 0x29, 0x1f, 0x95, 0x87, 0x31, 0x60, 0x65, 0xc0, 0x03, 0xed, 0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07])
}
func testComplexCoding() throws {
try verifyHuffmanCoding("鯖審", [0xff, 0xff, 0xaf, 0xff, 0xff, 0x67, 0xff, 0xfe, 0x2f, 0xff, 0xf3, 0xff, 0xff, 0xec, 0xff, 0xff, 0x77])
let text1 = "Hello, world. I am a header value; I have Teh Texts. I am going on for quite a long time because I want to ensure that the encoded data buffer needs to be expanded to test out that code. I'll try some meta-characters too: \r\t\n ought to do it, no?"
guard let encoded1Data = Data(base64Encoded: "xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/Pw==") else {
XCTFail("Unable to import base-64 result data")
return
}
// oh if only Data could conform to ContiguousCollection...
try verifyHuffmanCoding(text1, Array(encoded1Data))
// The encoder has a 256-byte encoding buffer to start with, and we want to overflow that to make it expand automatically
// We also want to include some > 24-bit codes to test all the conditions.
// So: here's a goodly-sized chunk of UTF-8 text. It won't compress worth a damn.
let text2 = "午セイ谷高ぐふあト食71入ツエヘナ津県を類及オモ曜一購ごきわ致掲ぎぐず敗文輪へけり鯖審ヘ塊米卸呪おぴ。"
guard let encoded2Data = Data(base64Encoded: "//8///3v//W//83//P//8n//m//5///c//+r//wf//v//+v///D//8n//m//9L//+z//zf/+l//8P//m//9L//n//83//R//63//1//+3///e6H//z//+p//+j//zf/9H//p//+b//n///V//5v/+j//8n//m//6P//t//+j//9v//6P//5//+z//93//zf/8///r///X//3f//z//+f//7///2///N//z//+9//5v/+j//p//9H//5f//Z//+n//q//5v//q//8X//8P//N//6X//93//m//9L//73//m//5///v///V//9n//9v//o///r//+D//zf/+l///X//5v//S///s//83//pf/+x//6P//w///j//9H//4v//s///V///D//3v//N//6X//V//5v//S///t//83//P//9v//6///2f//i//8///7P//d//83//R//+T//z//93//7f//8///D//4v//P//97//q//8///9v//vf/+b//0v//t//+b//0v//2//+b//m//5/") else {
XCTFail("Unable to import base-64 result data")
return
}
try verifyHuffmanCoding(text2, Array(encoded2Data))
}
func testBasicEncodingPerformance() {
var text = "Hello, world. I am a header value; I have Teh Texts. I am going on for quite a long time because I want to ensure that the encoded data buffer needs to be expanded to test out that code. I'll try some meta-characters too: \r\t\n ought to do it, no?"
while text.count < 1024 * 128 {
text += text
}
// warm up the encoder
self.scratchBuffer.setHuffmanEncoded(bytes: text.utf8)
self.measureMetrics(HuffmanCodingTests.defaultPerformanceMetrics, automaticallyStartMeasuring: false) {
self.scratchBuffer.clear()
startMeasuring()
self.scratchBuffer.setHuffmanEncoded(bytes: text.utf8)
stopMeasuring()
}
}
func testBasicDecodingPerformance() {
let url = fixtureDirURL.appendingPathComponent("large_huffman_b64.txt")
guard let base64Data = try? Data(contentsOf: url) else {
XCTFail("Couldn't load fixture data")
return
}
guard let data = Data(base64Encoded: base64Data) else {
XCTFail("Couldn't decode base64 fixture data")
return
}
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeWithUnsafeMutableBytes { ptr in
let bytePtr = ptr.bindMemory(to: UInt8.self)
return data.copyBytes(to: bytePtr)
}
var targetBuffer = ByteBufferAllocator().buffer(capacity: data.count)
// warm up the decoder
_ = try! buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes, into: &targetBuffer)
self.measureMetrics(HuffmanCodingTests.defaultPerformanceMetrics, automaticallyStartMeasuring: false) {
targetBuffer.clear()
startMeasuring()
_ = try! buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes, into: &targetBuffer)
}
}
func testComplexEncodingPerformance() {
var text = "午セイ谷高ぐふあト食71入ツエヘナ津県を類及オモ曜一購ごきわ致掲ぎぐず敗文輪へけり鯖審ヘ塊米卸呪おぴ。"
while text.utf8.count < 128 * 1024 {
text += text
}
// warm up the encoder
self.scratchBuffer.setHuffmanEncoded(bytes: text.utf8)
self.measureMetrics(HuffmanCodingTests.defaultPerformanceMetrics, automaticallyStartMeasuring: false) {
self.scratchBuffer.clear()
startMeasuring()
self.scratchBuffer.setHuffmanEncoded(bytes: text.utf8)
stopMeasuring()
}
}
func testComplexDecodingPerformance() {
let url = fixtureDirURL.appendingPathComponent("large_complex_huffman_b64.txt")
guard let base64Data = try? Data(contentsOf: url) else {
XCTFail("Couldn't load fixture data")
return
}
guard let data = Data(base64Encoded: base64Data) else {
XCTFail("Couldn't decode base64 fixture data")
return
}
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeWithUnsafeMutableBytes { ptr in
let bytePtr = ptr.bindMemory(to: UInt8.self)
return data.copyBytes(to: bytePtr)
}
var targetBuffer = ByteBufferAllocator().buffer(capacity: data.count)
// ensure the decoder table has been loaded
_ = try! buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes, into: &targetBuffer)
self.measureMetrics(HuffmanCodingTests.defaultPerformanceMetrics, automaticallyStartMeasuring: false) {
targetBuffer.clear()
startMeasuring()
_ = try! buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes, into: &targetBuffer)
}
}
}
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// IntegerCodingTests+XCTest.swift
//
import XCTest
///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///
extension IntegerCodingTests {
static var allTests : [(String, (IntegerCodingTests) -> () throws -> Void)] {
return [
("testIntegerEncoding", testIntegerEncoding),
("testIntegerDecoding", testIntegerDecoding),
]
}
}
@@ -0,0 +1,165 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOHPACK
class IntegerCodingTests : XCTestCase {
var scratchBuffer = ByteBufferAllocator().buffer(capacity: 11)
// MARK: - Array-based helpers
private func encodeIntegerToArray(_ value: UInt, prefix: Int) -> [UInt8] {
var data = [UInt8]()
scratchBuffer.clear()
let len = NIOHPACK.encodeInteger(value, to: &scratchBuffer, prefix: prefix)
data.append(contentsOf: scratchBuffer.viewBytes(at: 0, length: len))
return data
}
private func decodeInteger(from array: [UInt8], prefix: Int) throws -> UInt {
scratchBuffer.clear()
scratchBuffer.write(bytes: array)
let (r, _) = try NIOHPACK.decodeInteger(from: scratchBuffer.readableBytesView, prefix: prefix)
return r
}
// MARK: - Tests
func testIntegerEncoding() {
// values from the standard: http://httpwg.org/specs/rfc7541.html#integer.representation.examples
var data = encodeIntegerToArray(10, prefix: 5) // 0000 1010
XCTAssertEqual(data.count, 1)
XCTAssertEqual(data[0], 0b00001010)
data = encodeIntegerToArray(1337, prefix: 5) // 0000 0101 0011 1001
XCTAssertEqual(data.count, 3)
// prefix bits = 31 = 0001 1111, 1337 - 31 = 1306 = 0101 0001 1010 -> x0001010 x0011010
XCTAssertEqual(data, [31, 154, 10]) // 00011111 , 10011010 , 00001010
// prefix 8 == use the whole first octet
data = encodeIntegerToArray(42, prefix: 8) // 0010 1010
XCTAssertEqual(data.count, 1)
XCTAssertEqual(data[0], 42)
data = encodeIntegerToArray(256, prefix: 8) // 0001 0000 0000 -> 1111 1111 + 0000 0001
XCTAssertEqual(data.count, 2)
XCTAssertEqual(data, [255, 1])
// very large value, few bits set, 8-bit prefix
data = encodeIntegerToArray(17 << 57, prefix: 8) // 00010001 << 57 or 2449958197289549824
// calculations:
// subtract prefix:
// 2449958197289549824 - 255 = 2449958197289549569 or 0010 0001 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0000 0001
// seven bits at a time, grouping from least significant bit:
// 0100001 1111111 1111111 1111111 1111111 1111111 1111111 1111110 0000001
// swap these around:
// 0000001 1111110 1111111 1111111 1111111 1111111 1111111 1111111 0100001
// set the top bit of all but last part to get our remaining output bytes:
// 10000001 11111110 11111111 11111111 11111111 11111111 11111111 11111111 00100001
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, [255, 129, 254, 255, 255, 255, 255, 255, 255, 33])
// same value, 1-bit prefix:
data = encodeIntegerToArray(17 << 57, prefix: 1)
// calculations:
// subtract prefix:
// 2449958197289549824 - 1 = 2449958197289549823 or 0010 0001 (1111 x14)
// seven bits at a time, grouping from least significant bit:
// 0100001 1111111 1111111 1111111 1111111 1111111 1111111 1111111 1111111
// swap these around:
// 1111111 1111111 1111111 1111111 1111111 1111111 1111111 1111111 0100001
// set the top bit of all but last part to get our remaining output bytes:
// 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00100001
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, [1, 255, 255, 255, 255, 255, 255, 255, 255, 33])
// encoding max 64-bit unsigned integer, 1-bit prefix
data = encodeIntegerToArray(UInt(UInt64.max), prefix: 1)
// calculations:
// subtract prefix:
// 18446744073709551615 - 1 = 18446744073709551614 or (1111 x15) 1110
// seven bits at a time, grouping from least significant bit:
// 0000001 1111111 1111111 1111111 1111111 1111111 1111111 1111111 1111111 1111110
// swap these around:
// 1111110 1111111 1111111 1111111 1111111 1111111 1111111 1111111 1111111 0000001
// set the top bit of all but last part to get our remaining output bytes:
// 11111110 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000001
XCTAssertEqual(data.count, 11)
XCTAssertEqual(data, [1, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1])
// something carefully crafted to produce maximum number of output bytes with minimum number of
// nonzero bits:
data = encodeIntegerToArray(9223372036854775809, prefix: 1)
// calculations:
// subtract prefix:
// 9223372036854775809 - 1 = 9223372036854775808 or 1000 (0000 x15)
// seven bits at a time, grouping from least significant bit:
// (000000)1 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
// swap these around:
// 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000001
// set the top bit of all but the last part to get our remaining output bytes:
// 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 00000001
XCTAssertEqual(data.count, 11)
XCTAssertEqual(data, [1, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1])
// something similar, which uses an 8-bit prefix and still produces lots of zero bits:
// for those interested: this is the previous value + 254; thus only the first byte should differ
data = encodeIntegerToArray(9223372036854776063, prefix: 8)
// calculations:
// subtract prefix:
// 9223372036854776063 - 255 = 9223372036854775808 or 1000 (0000 x15)
// seven bits at a time, grouping from least significant bit:
// (000000)1 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
// swap these around:
// 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000001
// set the top bit of all but the last part to get our remaining output bytes:
// 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 00000001
XCTAssertEqual(data.count, 11)
XCTAssertEqual(data, [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1])
}
func testIntegerDecoding() throws {
// any bits above the prefix amount shouldn't affect the outcome.
XCTAssertEqual(try decodeInteger(from: [0b00001010], prefix: 5), 10)
XCTAssertEqual(try decodeInteger(from: [0b11101010], prefix: 5), 10)
XCTAssertEqual(try decodeInteger(from: [0b00011111, 154, 10], prefix: 5), 1337)
XCTAssertEqual(try decodeInteger(from: [0b11111111, 154, 10], prefix: 5), 1337)
XCTAssertEqual(try decodeInteger(from: [0b00101010], prefix: 8), 42)
// Now some larger numbers:
XCTAssertEqual(try decodeInteger(from: [255, 129, 254, 255, 255, 255, 255, 255, 255, 33], prefix: 8), 2449958197289549824)
XCTAssertEqual(try decodeInteger(from: [1, 255, 255, 255, 255, 255, 255, 255, 255, 33], prefix: 1), 2449958197289549824)
XCTAssertEqual(try decodeInteger(from: [1, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1], prefix: 1), UInt(UInt64.max))
// lots of zeroes: each 128 yields zero
XCTAssertEqual(try decodeInteger(from: [1, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1], prefix: 1), 9223372036854775809)
// almost the same bytes, but a different prefix:
XCTAssertEqual(try decodeInteger(from: [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1], prefix: 8), 9223372036854776063)
// now a silly version which should never have been encoded in so many bytes
XCTAssertEqual(try decodeInteger(from: [255, 129, 128, 128, 128, 128, 128, 128, 128, 0], prefix: 8), 256)
}
}
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// RingBufferTests+XCTest.swift
//
import XCTest
///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///
extension RingBufferTests {
static var allTests : [(String, (RingBufferTests) -> () throws -> Void)] {
return [
("testWriteAndUnwriteBytes", testWriteAndUnwriteBytes),
("testReadAndWriteStrings", testReadAndWriteStrings),
("testWrappingBoundary", testWrappingBoundary),
("testRebasing", testRebasing),
]
}
}
+222
View File
@@ -0,0 +1,222 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOHPACK
class RingBufferTests : XCTestCase {
var ring: StringRing = StringRing(allocator: ByteBufferAllocator(), capacity: 32)
override func setUp() {
ring.clear()
}
private func assertRingState(_ head: Int, _ tail: Int, _ readable: Int, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(ring.ringHead, head, file: file, line: line)
XCTAssertEqual(ring.ringTail, tail, file: file, line: line)
XCTAssertEqual(ring.readableBytes, readable, file: file, line: line)
}
func testWriteAndUnwriteBytes() throws {
assertRingState(0, 0, 0)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
try ring.write(bytes: bytes)
assertRingState(0, 10, 10)
ring.unwrite(byteCount: 5)
assertRingState(0, 5, 5)
}
func testReadAndWriteStrings() throws {
// Plain strings (Sequence path)
let string = "This is a test string"
try ring.write(string: string)
assertRingState(0, 21, 21)
ring.unwrite(byteCount: 7) // " string"
assertRingState(0, 14, 14) // "This is a test"
let peekedString = ring.getString(at: ring.ringHead, length: 7) // "This is"
XCTAssertNotNil(peekedString)
XCTAssertEqual(peekedString, "This is")
assertRingState(0, 14, 14)
let readString = ring.readString(length: 14) // "This is a test"
XCTAssertNotNil(readString)
XCTAssertEqual(readString, "This is a test")
assertRingState(14, 14, 0)
ring.unread(byteCount: 4) // un-read "test"
assertRingState(10, 14, 4)
XCTAssertEqual(ring.readString(length: 4), "test")
assertRingState(14, 14, 0)
ring.unread(byteCount: 14)
ring.unwrite(byteCount: 14)
assertRingState(0, 0, 0)
// Static strings (ContiguousCollection path)
try ring.write(staticString: "This is a test string")
assertRingState(0, 21, 21)
ring.unwrite(byteCount: 7) // " string"
assertRingState(0, 14, 14) // "This is a test"
let peekedStaticString = ring.getString(at: ring.ringHead, length: 7) // "This is"
XCTAssertNotNil(peekedStaticString)
XCTAssertEqual(peekedString, "This is")
assertRingState(0, 14, 14)
let readStaticString = ring.readString(length: 14) // "This is a test"
XCTAssertNotNil(readStaticString)
XCTAssertEqual(readStaticString, "This is a test")
assertRingState(14, 14, 0)
ring.unread(byteCount: 4) // un-read "test"
assertRingState(10, 14, 4)
XCTAssertEqual(ring.readString(length: 4), "test")
assertRingState(14, 14, 0)
}
func testWrappingBoundary() throws {
ring.moveTail(forwardBy: 28)
ring.moveHead(forwardBy: 28)
assertRingState(28, 28, 0)
// write eight bytes -- should split right down the middle
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7]
try ring.write(bytes: bytes)
assertRingState(28, 4, 8)
ring.unwrite(byteCount: 8)
assertRingState(28, 28, 0)
// string data, same as before
let string = "This is a test string"
try ring.write(string: string)
assertRingState(28, 17, 21)
ring.unwrite(byteCount: 7) // " string"
assertRingState(28, 10, 14) // "This is a test"
let peekedString = ring.getString(at: ring.ringHead, length: 7) // "This is"
XCTAssertNotNil(peekedString)
XCTAssertEqual(peekedString, "This is")
assertRingState(28, 10, 14)
let readString = ring.readString(length: 14) // "This is a test"
XCTAssertNotNil(readString)
XCTAssertEqual(readString, "This is a test")
assertRingState(10, 10, 0)
ring.unread(byteCount: 4) // un-read "test"
assertRingState(6, 10, 4)
XCTAssertEqual(ring.readString(length: 4), "test")
assertRingState(10, 10, 0)
ring.unread(byteCount: 14)
assertRingState(28, 10, 14)
ring.unwrite(byteCount: 14)
assertRingState(28, 28, 0)
// Static string API, this time:
try ring.write(staticString: "This is a test string")
assertRingState(28, 17, 21)
ring.unwrite(byteCount: 7) // " string"
assertRingState(28, 10, 14) // "This is a test"
let peekedStaticString = ring.getString(at: ring.ringHead, length: 7) // "This is"
XCTAssertNotNil(peekedStaticString)
XCTAssertEqual(peekedStaticString, "This is")
assertRingState(28, 10, 14)
let readStaticString = ring.readString(length: 14) // "This is a test"
XCTAssertNotNil(readStaticString)
XCTAssertEqual(readStaticString, "This is a test")
assertRingState(10, 10, 0)
ring.unread(byteCount: 4) // un-read "test"
assertRingState(6, 10, 4)
XCTAssertEqual(ring.readString(length: 4), "test")
assertRingState(10, 10, 0)
ring.unread(byteCount: 14)
assertRingState(28, 10, 14)
}
func testRebasing() throws {
// NB: the check for newCapacity == currentCapacity is in `changeCapacity(to:)`,
// so since we're calling the internal function, we can take advantage of the
// fact that it doesn't check that input and just rebase.
// case 1: already contiguous, head at zero.
try ring.write(staticString: "This is a test string")
assertRingState(0, 21, 21)
ring._reallocateStorageAndRebase(capacity: 32)
assertRingState(0, 21, 21)
// case 2: buffer is empty, head not at zero
ring.moveHead(forwardBy: 21)
assertRingState(21, 21, 0)
ring._reallocateStorageAndRebase(capacity: 32)
assertRingState(0, 0, 0)
// case 3: already contiguous, head not at zero
try ring.write(staticString: "This is a test string")
assertRingState(0, 21, 21)
ring.moveHead(forwardBy: 5) // "is a test string"
assertRingState(5, 21, 16)
ring._reallocateStorageAndRebase(capacity: 32)
assertRingState(0, 16, 16)
// case 4: non-contiguous, room in the middle to shuffle
ring.unwrite(byteCount: 16)
assertRingState(0, 0, 0)
try ring.write(staticString: "This is a test string")
assertRingState(0, 21, 21)
ring.moveHead(forwardBy: 21)
assertRingState(21, 21, 0)
try ring.write(staticString: "Wraps Buffer")
assertRingState(21, 1, 12)
ring._reallocateStorageAndRebase(capacity: 32)
assertRingState(0, 12, 12)
// case 5: non-contiguous, not enough room to make two copies
ring.moveHead(forwardBy: 12)
assertRingState(12, 12, 0)
try ring.write(staticString: "This is a test string again")
assertRingState(12, 7, 27)
ring._reallocateStorageAndRebase(capacity: 32)
assertRingState(0, 27, 27)
}
}
+1
Submodule hpack-test-case added at b2a1664b43.