1 // This source file is part of the Swift.org Server APIs open source project
3 // Copyright (c) 2017 Swift Server API project authors
4 // Licensed under Apache License v2.0 with Runtime Library Exception
6 // See http://swift.org/LICENSE.txt for license information
12 /// A structure representing the headers from a HTTP request, without the body of the request.
13 public struct HTTPRequest {
14 /// HTTP request method.
15 public var method: HTTPMethod
16 /// HTTP request URI, eg. "/foo/bar?buz=qux"
17 public var target: String
18 /// HTTP request version
19 public var httpVersion: HTTPVersion
20 /// HTTP request headers
21 public var headers: HTTPHeaders
23 public init(method: HTTPMethod, target: String, httpVersion: HTTPVersion, headers: HTTPHeaders) {
26 self.httpVersion = httpVersion
27 self.headers = headers
31 public extension HTTPRequest {
35 /// Method that takes a chunk of request body and is expected to write to the `HTTPResponseWriter`
36 /// - Parameter HTTPBodyChunk: `HTTPBodyChunk` representing some or all of the incoming request body
37 /// - Parameter Bool: A boolean flag that can be set to true in order to prevent further processing
38 public typealias HTTPBodyHandler = (HTTPBodyChunk, inout Bool) -> Void
40 /// Indicates whether the body is going to be processed or ignored
41 public enum HTTPBodyProcessing {
42 /// Used to discard the body data associated with the incoming HTTP request
44 /// Used to process the body data associated with the incoming HTTP request using a `HTTPBodyHandler`
45 case processBody(handler: HTTPBodyHandler)
48 /// Part or all of the incoming request body
49 public enum HTTPBodyChunk {
50 /// A new chunk of the incoming HTTP reqest body data has arrived. `finishedProcessing()` must be called when
51 /// that chunk has been processed.
52 case chunk(data: DispatchData, finishedProcessing: () -> Void)
53 /// An error has occurred whilst streaming the incoming HTTP request data, eg. the connection closed
54 case failed(error: Error)
55 /// A trailer header has arrived during the processing of the incoming HTTP request data.
56 /// This is currently unimplemented.
57 case trailer(key: String, value: String)
58 /// The stream of incoming HTTP request data has completed.