Files
http/Sources/SwiftServerHttp/HTTPRequest.swift
T
2017-05-30 15:37:23 -05:00

78 lines
2.2 KiB
Swift

//
// HTTPRequest.swift
// SwiftServerHttp
//
// Created by Carl Brown on 4/24/17 based on
// https://lists.swift.org/pipermail/swift-server-dev/Week-of-Mon-20170403/000422.html
//
//
import Foundation
import Dispatch
/// HTTP Request NOT INCLUDING THE BODY. This allows for streaming
public struct HTTPRequest {
public var method : HTTPMethod
public var target : String /* e.g. "/foo/bar?buz=qux" */
public var httpVersion : HTTPVersion
public var headers : HTTPHeaders
}
/// Method that takes a chunk of request body and is expected to write to the ResponseWriter
public typealias HTTPBodyHandler = (HTTPBodyChunk, inout Bool) -> Void /* the Bool can be set to true when we don't want to process anything further */
/// Indicates whether the body is going to be processed or ignored
public enum HTTPBodyProcessing {
case discardBody /* if you're not interested in the body */
case processBody(handler: HTTPBodyHandler)
}
/// Part (or maybe all) of the incoming request body
public enum HTTPBodyChunk {
case chunk(data: DispatchData, finishedProcessing: () -> Void) /* a new bit of the HTTP request body has arrived, finishedProcessing() must be called when done with that chunk */
case failed(error: /*HTTPParser*/ Error) /* error while streaming the HTTP request body, eg. connection closed */
case trailer(key: String, value: String) /* trailer has arrived (this we actually haven't implemented yet) */
case end /* body and trailers finished */
}
/// HTTP Methods handled by http_parser.[ch] supports
public enum HTTPMethod: String {
// case custom(method: String)
case UNKNOWN
/* everything that http_parser.[ch] supports */
case DELETE
case GET
case HEAD
case POST
case PUT
case CONNECT
case OPTIONS
case TRACE
case COPY
case LOCK
case MKCOL
case MOVE
case PROPFIND
case PROPPATCH
case SEARCH
case UNLOCK
case BIND
case REBIND
case UNBIND
case ACL
case REPORT
case MKACTIVITY
case CHECKOUT
case MERGE
case MSEARCH
case NOTIFY
case SUBSCRIBE
case UNSUBSCRIBE
case PATCH
case PURGE
case MKCALENDAR
case LINK
case UNLINK
}