Files
swift-nio-http2/Sources/NIOHPACK/StringRing.swift
T
Jim Dovey 6f8f523684 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.
2018-08-16 09:51:13 +01:00

349 lines
13 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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)
}
}