From d61e084d3cedacc745558d41ca4b6d38750a1ee1 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 30 Nov 2017 16:07:41 +0000 Subject: [PATCH] Doc comments for NIOHTTP1 --- Sources/NIOHTTP1/HTTPDecoder.swift | 28 +++++ Sources/NIOHTTP1/HTTPEncoder.swift | 8 ++ Sources/NIOHTTP1/HTTPTypes.swift | 138 +++++++++++++++++++++- Sources/NIOHTTP1/HTTPUpgradeHandler.swift | 12 +- 4 files changed, 184 insertions(+), 2 deletions(-) diff --git a/Sources/NIOHTTP1/HTTPDecoder.swift b/Sources/NIOHTTP1/HTTPDecoder.swift index defe5ff9e..b72dc84f8 100644 --- a/Sources/NIOHTTP1/HTTPDecoder.swift +++ b/Sources/NIOHTTP1/HTTPDecoder.swift @@ -15,7 +15,12 @@ import NIO import CNIOHTTPParser +/// A `ChannelInboundHandler` used to decode HTTP requests. See the documentation +/// on `HTTPDecoder` for more. public typealias HTTPRequestDecoder = HTTPDecoder + +/// A `ChannelInboundHandler` used to decode HTTP responses. See the documentation +/// on `HTTPDecoder` for more. public typealias HTTPResponseDecoder = HTTPDecoder private struct HTTPParserState { @@ -124,6 +129,13 @@ extension HTTPDecoder where HTTPMessageT == HTTPServerRequestPart { } } +/// A `ChannelInboundHandler` that parses HTTP/1-style messages, converting them from +/// unstructured bytes to a sequence of HTTP messages. +/// +/// The `HTTPDecoder` is a generic channel handler which can produce messages in +/// either the form of `HTTPClientResponsePart` or `HTTPServerRequestPart`: that is, +/// it produces messages that correspond to the semantic units of HTTP produced by +/// the remote peer. public final class HTTPDecoder: ByteToMessageDecoder, AnyHTTPDecoder { public typealias InboundIn = ByteBuffer public typealias InboundOut = HTTPMessageT @@ -371,6 +383,11 @@ private func evacuateHTTPDecoder(_ opaqueContext: UnsafeMutablePointer HTTPParserError? { switch fromCHTTPParserErrno { case HPE_INVALID_EOF_STATE: @@ -422,6 +439,11 @@ extension HTTPParserError { } extension HTTPMethod { + /// Create a `HTTPMethod` from a given `http_method` produced by + /// `http_parser`. + /// + /// - Parameter httpParserMethod: The method returned by `http_parser`. + /// - Returns: The corresponding `HTTPMethod`. static func from(httpParserMethod: http_method) -> HTTPMethod { switch httpParserMethod { case HTTP_DELETE: @@ -497,6 +519,12 @@ extension HTTPMethod { } extension HTTPResponseStatus { + /// Create a `HTTPResponseStatus` from a given status and reason produced by + /// `http_parser`. + /// + /// - Parameter statusCode: The integer value of the HTTP response status code + /// - Parameter reasonPhrase: The textual reason phrase from the response. + /// - Returns: The corresponding `HTTPResponseStatus`. static func from(_ statusCode: UInt32, _ reasonPhrase: String) -> HTTPResponseStatus { switch statusCode { case 100: diff --git a/Sources/NIOHTTP1/HTTPEncoder.swift b/Sources/NIOHTTP1/HTTPEncoder.swift index 194ef5329..bdf1be10e 100644 --- a/Sources/NIOHTTP1/HTTPEncoder.swift +++ b/Sources/NIOHTTP1/HTTPEncoder.swift @@ -105,6 +105,10 @@ private func sanitizeTransportHeaders(mayHaveBody: Bool, headers: inout HTTPHead } } +/// A `ChannelOutboundHandler` that can serialize HTTP requests. +/// +/// This channel handler is used to translate messages from a series of +/// `HTTPClientRequestPart` into the HTTP/1.1 wire format. public final class HTTPRequestEncoder : ChannelOutboundHandler { public typealias OutboundIn = HTTPClientRequestPart public typealias OutboundOut = IOData @@ -138,6 +142,10 @@ public final class HTTPRequestEncoder : ChannelOutboundHandler { } } +/// A `ChannelOutboundHandler` that can serialize HTTP responses. +/// +/// This channel handler is used to translate messages from a series of +/// `HTTPServerResponsePart` into the HTTP/1.1 wire format. public final class HTTPResponseEncoder : ChannelOutboundHandler { public typealias OutboundIn = HTTPServerResponsePart public typealias OutboundOut = IOData diff --git a/Sources/NIOHTTP1/HTTPTypes.swift b/Sources/NIOHTTP1/HTTPTypes.swift index 805a77187..86b68f5c2 100644 --- a/Sources/NIOHTTP1/HTTPTypes.swift +++ b/Sources/NIOHTTP1/HTTPTypes.swift @@ -19,12 +19,25 @@ let headerSeparator: StaticString = ": " let http1_1: StaticString = "HTTP/1.1" let status200: StaticString = "200 OK" +/// A representation of the request line and header fields of a HTTP request. public struct HTTPRequestHead: Equatable { + /// The HTTP method for this request. public let method: HTTPMethod + + /// The URI used on this request. public let uri: String + + /// The version for this HTTP request. public let version: HTTPVersion + + /// The header fields for this HTTP request. public var headers: HTTPHeaders + /// Create a `HTTPRequestHead` + /// + /// - Parameter version: The version for this HTTP request. + /// - Parameter method: The HTTP method for this request. + /// - Parameter uri: The URI used on this request. public init(version: HTTPVersion, method: HTTPMethod, uri: String) { self.version = version self.method = method @@ -32,6 +45,12 @@ public struct HTTPRequestHead: Equatable { self.headers = HTTPHeaders() } + /// Create a `HTTPRequestHead` + /// + /// - Parameter version: The version for this HTTP request. + /// - Parameter method: The HTTP method for this request. + /// - Parameter uri: The URI used on this request. + /// - Parameter headers: The headers for this HTTP request. init(version: HTTPVersion, method: HTTPMethod, uri: String, headers: HTTPHeaders) { self.version = version self.method = method @@ -44,6 +63,12 @@ public struct HTTPRequestHead: Equatable { } } +/// The parts of a complete HTTP message, either request or response. +/// +/// A HTTP message is made up of a request or status line with several headers, +/// encoded by `.head`, zero or more body parts, and optionally some trailers. To +/// indicate that a complete HTTP message has been sent or received, we use `.end`, +/// which may also contain any trailers that make up the mssage. public enum HTTPPart { case head(HeadT) case body(BodyT) @@ -65,13 +90,21 @@ extension HTTPPart : Equatable { } } +/// The components of a HTTP request from the view of a HTTP client. public typealias HTTPClientRequestPart = HTTPPart + +/// The components of a HTTP request from the view of a HTTP server. public typealias HTTPServerRequestPart = HTTPPart +/// The components of a HTTP response from the view of a HTTP client. public typealias HTTPClientResponsePart = HTTPPart + +/// The components of a HTTP response from the view of a HTTP server. public typealias HTTPServerResponsePart = HTTPPart extension HTTPRequestHead { + /// Whether this HTTP request is a keep-alive request: that is, whether the + /// connection should remain open after the request is complete. public var isKeepAlive: Bool { guard let connection = headers["connection"].first?.lowercased() else { // HTTP 1.1 use keep-alive by default if not otherwise told. @@ -85,11 +118,22 @@ extension HTTPRequestHead { } } +/// A representation of the status line and header fields of a HTTP response. public struct HTTPResponseHead : Equatable { + /// The HTTP response status. public let status: HTTPResponseStatus + + /// The HTTP version that corresponds to this response. public let version: HTTPVersion + + /// The HTTP headers on this response. public var headers: HTTPHeaders + /// Create a `HTTPResponseHead` + /// + /// - Parameter version: The version for this HTTP response. + /// - Parameter status: The status for this HTTP response. + /// - Parameter headers: The headers for this HTTP response. public init(version: HTTPVersion, status: HTTPResponseStatus, headers: HTTPHeaders = HTTPHeaders()) { self.version = version self.status = status @@ -104,6 +148,10 @@ public struct HTTPResponseHead : Equatable { fileprivate typealias HTTPHeadersStorage = [String:[(String, String)]] // [lowerCasedName: [(originalCaseName, value)] +/// 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. struct HTTPHeadersIterator : IteratorProtocol { fileprivate var storageIterator: HTTPHeadersStorage.Iterator fileprivate var valuesIterator: Array<(String, String)>.Iterator? @@ -130,6 +178,18 @@ struct HTTPHeadersIterator : IteratorProtocol { } } +/// A representation of a block of HTTP header fields. +/// +/// HTTP header fields are a complex data structure. The most natural representation +/// for these is a sequence of two-tuples of field name and field value, both as +/// strings. This structure preserves that representation, but provides a number of +/// convenience features in addition to it. +/// +/// For example, this structure enables access to header fields based on the +/// case-insensitive form of the field name, but preserves the original case of the +/// field when needed. It also supports recomposing headers to a maximally joined +/// or split representation, such that header fields that are able to be repeated +/// can be represented appropriately. public struct HTTPHeaders : Sequence, CustomStringConvertible { // [lowerCasedName: [(originalCaseName, value)] @@ -145,26 +205,69 @@ public struct HTTPHeaders : Sequence, CustomStringConvertible { return storage.count } + /// Construct a `HTTPHeaders` structure. + /// + /// - Parameter headers: An initial set of headers to use to populate the + /// header block. public init(_ headers: [(String, String)] = []) { for (key, value) in headers { add(name: key, value: value) } } + /// 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 future-proofing with HTTP/2 lowercase header names are strongly + // recommended. + /// - Parameter value: The header field value to add for the given name. public mutating func add(name: String, value: String) { let keyLower = name.lowercased() storage[keyLower] = (storage[keyLower] ?? []) + [(name, value)] } + /// Add a header name/value pair to the block, replacing any previous values for the + /// same header name that are already in the block. + /// + /// This is a supplemental method to `add` that essentially combines `remove` and `add` + /// in a single function. It can be used to ensure that a header block is in a + /// well-defined form without having to check whether the value was previously there. + /// Like `add`, this method performs case-insensitive comparisons of the header field + /// names. + /// + /// - Parameter name: The header field name. For maximum compatibility this should be an + /// ASCII string. For future-proofing with HTTP/2 lowercase header names are strongly + // recommended. + /// - Parameter value: The header field value to add for the given name. public mutating func replaceOrAdd(name: String, value: String) { let keyLower = name.lowercased() storage[keyLower] = [(name, value)] } + /// Remove all values for a given header name from the block. + /// + /// This method uses case-insensitive comparisons for the header field name. + /// + /// - Parameter name: The name of the header field to remove from the block. public mutating func remove(name: String) { self.storage[name.lowercased()] = nil } + /// Retrieve all of the values for a give header field name from the block. + /// + /// This method uses case-insensitive comparisons for the header field name. It + /// does not return a maximally-decomposed list of the header fields, but instead + /// returns them in their original representation: that means that a comma-separated + /// header field list may contain more than one entry, some of which contain commas + /// and some do not. If you want a representation of the header fields suitable for + /// performing computation on, consider `getCanonicalForm`. + /// + /// - 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] { if let result = storage[name.lowercased()] { return result.map { tuple in tuple.1 } @@ -172,6 +275,10 @@ public struct HTTPHeaders : Sequence, CustomStringConvertible { return [] } + /// Serializes this HTTP header block to bytes suitable for writing to the wire. + /// + /// - Parameter buffer: A buffer to write the serialized bytes into. Will increment + /// the writer index of this buffer. func write(buffer: inout ByteBuffer) { for (key, values) in storage { if key != "set-cookie" { @@ -217,6 +324,9 @@ public struct HTTPHeaders : Sequence, CustomStringConvertible { /// 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 func getCanonicalForm(_ name: String) -> [String] { // It's not safe to split Set-Cookie on comma. let queryName = name.lowercased() @@ -357,7 +467,8 @@ public enum HTTPMethod: Equatable { case MKACTIVITY case UNSUBSCRIBE case RAW(value: String) - + + /// Whether requests with this verb may have a request body. public var mayHaveRequestBody: Bool { // TODO: Add more switch self { @@ -370,6 +481,10 @@ public enum HTTPMethod: Equatable { } extension HTTPMethod { + /// Serializes this HTTP method bytes suitable for writing to the wire. + /// + /// - Parameter buffer: A buffer to write the serialized bytes into. Will increment + /// the writer index of this buffer. func write(buffer: inout ByteBuffer) { switch self { case .GET: @@ -444,21 +559,33 @@ extension HTTPMethod { } } +/// A structure representing a HTTP version. public struct HTTPVersion: Equatable { public static func ==(lhs: HTTPVersion, rhs: HTTPVersion) -> Bool { return lhs.major == rhs.major && lhs.minor == rhs.minor } + /// Create a HTTP version. + /// + /// - Parameter major: The major version number. + /// - Parameter minor: The minor version number. public init(major: UInt16, minor: UInt16) { self.major = major self.minor = minor } + /// The major version number. public let major: UInt16 + + /// The minor version number. public let minor: UInt16 } extension HTTPVersion { + /// Serializes this HTTP version to bytes suitable for writing to the wire. + /// + /// - Parameter buffer: A buffer to write the serialized bytes into. Will increment + /// the writer index of this buffer. func write(buffer: inout ByteBuffer) { if major == 1 && minor == 1 { // Optimize for HTTP/1.1 @@ -527,6 +654,7 @@ extension HTTPParserError: CustomDebugStringConvertible { } } +/// Errors that can be raised while parsing HTTP/1.1. public enum HTTPParserError: Error { case invalidCharactersUsed case trailingGarbage @@ -556,6 +684,7 @@ public enum HTTPParserError: Error { } extension HTTPResponseStatus { + /// The numerical status code for a given HTTP response status. public var code: UInt { get { switch self { @@ -683,6 +812,7 @@ extension HTTPResponseStatus { } } + /// The string reason phrase for a given HTTP response status. public var reasonPhrase: String { get { switch self { @@ -810,6 +940,10 @@ extension HTTPResponseStatus { } } + /// Serializes this response status to bytes suitable for writing to the wire. + /// + /// - Parameter buffer: A buffer to write the serialized bytes into. Will increment + /// the writer index of this buffer. func write(buffer: inout ByteBuffer) { switch self { case .ok: @@ -823,6 +957,7 @@ extension HTTPResponseStatus { } } +/// A HTTP response status code. public enum HTTPResponseStatus { /* use custom if you want to use a non-standard response code or have it available in a (UInt, String) pair from a higher-level web framework. */ @@ -899,6 +1034,7 @@ public enum HTTPResponseStatus { case notExtended case networkAuthenticationRequired + /// Whether responses with this status code may have a response body. public var mayHaveResponseBody: Bool { switch self { case .`continue`, diff --git a/Sources/NIOHTTP1/HTTPUpgradeHandler.swift b/Sources/NIOHTTP1/HTTPUpgradeHandler.swift index 7ac78dd75..197bd58c2 100644 --- a/Sources/NIOHTTP1/HTTPUpgradeHandler.swift +++ b/Sources/NIOHTTP1/HTTPUpgradeHandler.swift @@ -14,16 +14,21 @@ import NIO +/// Errors that may be raised by the `HTTPProtocolUpgrader`. public enum HTTPUpgradeErrors: Error { case invalidHTTPOrdering } +/// User events that may be fired by the `HTTPProtocolUpgrader`. public enum HTTPUpgradeEvents { + /// Fired when HTTP upgrade has completed and the + /// `HTTPProtocolUpgrader` is about to remove itself from the + /// `ChannelPipeline`. case upgradeComplete(toProtocol: String, upgradeRequest: HTTPRequestHead) } -/// An object that implements protocol upgrader knows how to handle HTTP upgrade to +/// An object that implements `ProtocolUpgrader` knows how to handle HTTP upgrade to /// a protocol. public protocol HTTPProtocolUpgrader { /// The protocol this upgrader knows how to support. @@ -64,6 +69,11 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler { /// Whether we've already seen the first request. private var seenFirstRequest = false + /// Create a `HTTPServerUpgradeHandler`. + /// + /// - Parameter upgraders: All `HTTPProtocolUpgrader` objects that this pipeline will be able + /// to use to handle HTTP upgrade. + /// - Parameter upgradeCompletionHandler: A block that will be fired when HTTP upgrade is complete. public init(upgraders: [HTTPProtocolUpgrader], upgradeCompletionHandler: @escaping (ChannelHandlerContext) -> Void) { var upgraderMap = [String: HTTPProtocolUpgrader]() for upgrader in upgraders {