mirror of
https://github.com/apple/swift-nio
synced 2026-11-27 05:04:22 +00:00
Motivation: HTTP pipelining can be tricky to handle properly on the server side. In particular, it's very easy to write out of order or inconsistently mutate state. Users often need help to handle this appropriately. Modifications: Added a HTTPServerPipelineHandler that only lets one request through at a time. Result: Better servers that are more able to handle HTTP pipelining
509 lines
22 KiB
Swift
509 lines
22 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
|
|
import NIOHTTP1
|
|
|
|
extension String {
|
|
func chopPrefix(_ prefix: String) -> String? {
|
|
if self.unicodeScalars.starts(with: prefix.unicodeScalars) {
|
|
return String(self[self.index(self.startIndex, offsetBy: prefix.count)...])
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func containsDotDot() -> Bool {
|
|
for idx in self.indices {
|
|
if self[idx] == "." && idx < self.index(before: self.endIndex) && self[self.index(after: idx)] == "." {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
private final class HTTPHandler: ChannelInboundHandler {
|
|
private enum FileIOMethod {
|
|
case sendfile
|
|
case nonblockingFileIO
|
|
}
|
|
public typealias InboundIn = HTTPServerRequestPart
|
|
public typealias OutboundOut = HTTPServerResponsePart
|
|
|
|
private enum State {
|
|
case idle
|
|
case waitingForRequestBody
|
|
case sendingResponse
|
|
|
|
mutating func requestReceived() {
|
|
precondition(self == .idle, "Invalid state for request received: \(self)")
|
|
self = .waitingForRequestBody
|
|
}
|
|
|
|
mutating func requestComplete() {
|
|
precondition(self == .waitingForRequestBody, "Invalid state for request complete: \(self)")
|
|
self = .sendingResponse
|
|
}
|
|
|
|
mutating func responseComplete() {
|
|
precondition(self == .sendingResponse, "Invalid state for response complete: \(self)")
|
|
self = .idle
|
|
}
|
|
}
|
|
|
|
private var buffer: ByteBuffer! = nil
|
|
private var keepAlive = false
|
|
private var state = State.idle
|
|
private let htdocsPath: String
|
|
|
|
private var infoSavedRequestHead: HTTPRequestHead?
|
|
private var infoSavedBodyBytes: Int = 0
|
|
|
|
private var continuousCount: Int = 0
|
|
|
|
private var handler: ((ChannelHandlerContext, HTTPServerRequestPart) -> Void)?
|
|
private var handlerFuture: EventLoopFuture<()>?
|
|
private let fileIO: NonBlockingFileIO
|
|
|
|
public init(fileIO: NonBlockingFileIO, htdocsPath: String) {
|
|
self.htdocsPath = htdocsPath
|
|
self.fileIO = fileIO
|
|
}
|
|
|
|
func handleInfo(ctx: ChannelHandlerContext, request: HTTPServerRequestPart) {
|
|
switch request {
|
|
case .head(let request):
|
|
self.infoSavedRequestHead = request
|
|
self.infoSavedBodyBytes = 0
|
|
self.keepAlive = request.isKeepAlive
|
|
self.state.requestReceived()
|
|
case .body(buffer: let buf):
|
|
self.infoSavedBodyBytes += buf.readableBytes
|
|
case .end(_):
|
|
self.state.requestComplete()
|
|
|
|
let response = """
|
|
HTTP method: \(self.infoSavedRequestHead!.method)\r
|
|
URL: \(self.infoSavedRequestHead!.uri)\r
|
|
body length: \(self.infoSavedBodyBytes)\r
|
|
headers: \(self.infoSavedRequestHead!.headers)\r
|
|
client: \(ctx.remoteAddress?.description ?? "zombie")\r
|
|
IO: SwiftNIO Electric Boogaloo™️\r\n
|
|
"""
|
|
self.buffer.clear()
|
|
self.buffer.write(string: response)
|
|
var headers = HTTPHeaders()
|
|
headers.add(name: "Content-Length", value: "\(response.utf8.count)")
|
|
ctx.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: self.infoSavedRequestHead!.version, status: .ok, headers: headers))), promise: nil)
|
|
ctx.write(self.wrapOutboundOut(.body(.byteBuffer(self.buffer))), promise: nil)
|
|
self.completeResponse(ctx, trailers: nil, promise: nil)
|
|
}
|
|
}
|
|
|
|
func handleEcho(ctx: ChannelHandlerContext, request: HTTPServerRequestPart) {
|
|
self.handleEcho(ctx: ctx, request: request, balloonInMemory: false)
|
|
}
|
|
|
|
func handleEcho(ctx: ChannelHandlerContext, request: HTTPServerRequestPart, balloonInMemory: Bool = false) {
|
|
switch request {
|
|
case .head(let request):
|
|
self.keepAlive = request.isKeepAlive
|
|
self.state.requestReceived()
|
|
if balloonInMemory {
|
|
self.buffer.clear()
|
|
} else {
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.head(.init(version: request.version, status: .ok))), promise: nil)
|
|
}
|
|
case .body(buffer: var buf):
|
|
if balloonInMemory {
|
|
self.buffer.write(buffer: &buf)
|
|
} else {
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buf))), promise: nil)
|
|
}
|
|
case .end(_):
|
|
self.state.requestComplete()
|
|
if balloonInMemory {
|
|
var headers = HTTPHeaders()
|
|
headers.add(name: "Content-Length", value: "\(self.buffer.readableBytes)")
|
|
ctx.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: HTTPVersion(major: 1, minor: 0), status: .ok, headers: headers))), promise: nil)
|
|
ctx.write(self.wrapOutboundOut(.body(.byteBuffer(self.buffer))), promise: nil)
|
|
self.completeResponse(ctx, trailers: nil, promise: nil)
|
|
} else {
|
|
self.completeResponse(ctx, trailers: nil, promise: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
func handleJustWrite(ctx: ChannelHandlerContext, request: HTTPServerRequestPart, statusCode: HTTPResponseStatus = .ok, string: String, trailer: (String, String)? = nil, delay: TimeAmount = .nanoseconds(0)) {
|
|
switch request {
|
|
case .head(let request):
|
|
self.keepAlive = request.isKeepAlive
|
|
self.state.requestReceived()
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.head(.init(version: request.version, status: .ok))), promise: nil)
|
|
case .body(buffer: _):
|
|
()
|
|
case .end(_):
|
|
self.state.requestComplete()
|
|
_ = ctx.eventLoop.scheduleTask(in: delay) { () -> Void in
|
|
var buf = ctx.channel.allocator.buffer(capacity: string.utf8.count)
|
|
buf.write(string: string)
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buf))), promise: nil)
|
|
var trailers: HTTPHeaders? = nil
|
|
if let trailer = trailer {
|
|
trailers = HTTPHeaders()
|
|
trailers?.add(name: trailer.0, value: trailer.1)
|
|
}
|
|
|
|
self.completeResponse(ctx, trailers: trailers, promise: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
func handleContinuousWrites(ctx: ChannelHandlerContext, request: HTTPServerRequestPart) {
|
|
switch request {
|
|
case .head(let request):
|
|
self.keepAlive = request.isKeepAlive
|
|
self.continuousCount = 0
|
|
self.state.requestReceived()
|
|
func doNext() {
|
|
self.buffer.clear()
|
|
self.continuousCount += 1
|
|
self.buffer.write(string: "line \(self.continuousCount)\n")
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(self.buffer)))).map {
|
|
_ = ctx.eventLoop.scheduleTask(in: .milliseconds(400), doNext)
|
|
}.whenFailure { (_: Error) in
|
|
self.completeResponse(ctx, trailers: nil, promise: nil)
|
|
}
|
|
}
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.head(HTTPResponseHead(version: request.version, status: .ok))), promise: nil)
|
|
doNext()
|
|
case .end:
|
|
self.state.requestComplete()
|
|
default:
|
|
()
|
|
}
|
|
}
|
|
|
|
func handleMultipleWrites(ctx: ChannelHandlerContext, request: HTTPServerRequestPart, strings: [String], delay: TimeAmount) {
|
|
switch request {
|
|
case .head(let request):
|
|
self.keepAlive = request.isKeepAlive
|
|
self.continuousCount = 0
|
|
self.state.requestReceived()
|
|
func doNext() {
|
|
self.buffer.clear()
|
|
self.buffer.write(string: strings[self.continuousCount])
|
|
self.continuousCount += 1
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(self.buffer)))).whenSuccess {
|
|
if self.continuousCount < strings.count {
|
|
_ = ctx.eventLoop.scheduleTask(in: delay, doNext)
|
|
} else {
|
|
self.completeResponse(ctx, trailers: nil, promise: nil)
|
|
}
|
|
}
|
|
}
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.head(HTTPResponseHead(version: request.version, status: .ok))), promise: nil)
|
|
doNext()
|
|
case .end:
|
|
self.state.requestComplete()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
func dynamicHandler(request reqHead: HTTPRequestHead) -> ((ChannelHandlerContext, HTTPServerRequestPart) -> Void)? {
|
|
switch reqHead.uri {
|
|
case "/dynamic/echo":
|
|
return self.handleEcho
|
|
case "/dynamic/echo_balloon":
|
|
return { self.handleEcho(ctx: $0, request: $1, balloonInMemory: true) }
|
|
case "/dynamic/pid":
|
|
return { ctx, req in self.handleJustWrite(ctx: ctx, request: req, string: "\(getpid())") }
|
|
case "/dynamic/write-delay":
|
|
return { ctx, req in self.handleJustWrite(ctx: ctx, request: req, string: "Hello World\r\n", delay: .milliseconds(100)) }
|
|
case "/dynamic/info":
|
|
return self.handleInfo
|
|
case "/dynamic/trailers":
|
|
return { ctx, req in self.handleJustWrite(ctx: ctx, request: req, string: "\(getpid())\r\n", trailer: ("Trailer-Key", "Trailer-Value")) }
|
|
case "/dynamic/continuous":
|
|
return self.handleContinuousWrites
|
|
case "/dynamic/count-to-ten":
|
|
return { self.handleMultipleWrites(ctx: $0, request: $1, strings: (1...10).map { "\($0)" }, delay: .milliseconds(100)) }
|
|
case "/dynamic/client-ip":
|
|
return { ctx, req in self.handleJustWrite(ctx: ctx, request: req, string: "\(ctx.remoteAddress.debugDescription)") }
|
|
default:
|
|
return { ctx, req in self.handleJustWrite(ctx: ctx, request: req, statusCode: .notFound, string: "not found") }
|
|
}
|
|
}
|
|
|
|
private func handleFile(ctx: ChannelHandlerContext, request: HTTPServerRequestPart, ioMethod: FileIOMethod, path: String) {
|
|
self.buffer.clear()
|
|
|
|
switch request {
|
|
case .head(let request):
|
|
self.keepAlive = request.isKeepAlive
|
|
self.state.requestReceived()
|
|
guard !request.uri.containsDotDot() else {
|
|
let response = HTTPResponseHead(version: request.version, status: .forbidden)
|
|
ctx.write(self.wrapOutboundOut(.head(response)), promise: nil)
|
|
self.completeResponse(ctx, trailers: nil, promise: nil)
|
|
return
|
|
}
|
|
let path = self.htdocsPath + "/" + path
|
|
do {
|
|
let file = try FileHandle(path: path)
|
|
let region = try FileRegion(fileHandle: file)
|
|
var response = HTTPResponseHead(version: request.version, status: .ok)
|
|
|
|
response.headers.add(name: "Content-Length", value: "\(region.endIndex)")
|
|
response.headers.add(name: "Content-Type", value: "text/plain; charset=utf-8")
|
|
|
|
switch ioMethod {
|
|
case .nonblockingFileIO:
|
|
var responseStarted = false
|
|
let f = self.fileIO.readChunked(fileRegion: region,
|
|
chunkSize: 32 * 1024,
|
|
allocator: ctx.channel.allocator,
|
|
eventLoop: ctx.eventLoop) { buffer in
|
|
if !responseStarted {
|
|
responseStarted = true
|
|
ctx.write(self.wrapOutboundOut(.head(response)), promise: nil)
|
|
}
|
|
return ctx.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buffer))))
|
|
}
|
|
f.then { (_: Void) -> EventLoopFuture<Void> in
|
|
let p: EventLoopPromise<Void> = ctx.eventLoop.newPromise()
|
|
self.completeResponse(ctx, trailers: nil, promise: p)
|
|
return p.futureResult
|
|
}.thenIfError { error in
|
|
if !responseStarted {
|
|
let response = HTTPResponseHead(version: request.version, status: .ok)
|
|
ctx.write(self.wrapOutboundOut(.head(response)), promise: nil)
|
|
var buffer = ctx.channel.allocator.buffer(capacity: 100)
|
|
buffer.write(string: "fail: \(error)")
|
|
ctx.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
|
|
self.state.responseComplete()
|
|
return ctx.writeAndFlush(self.wrapOutboundOut(.end(nil)))
|
|
} else {
|
|
return ctx.close()
|
|
}
|
|
}.whenComplete {
|
|
_ = try? file.close()
|
|
}
|
|
case .sendfile:
|
|
ctx.write(self.wrapOutboundOut(.head(response))).then {
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.body(.fileRegion(region))))
|
|
}.then { (_: Void) in
|
|
let p: EventLoopPromise<Void> = ctx.eventLoop.newPromise()
|
|
self.completeResponse(ctx, trailers: nil, promise: p)
|
|
return p.futureResult
|
|
}.thenIfError { (_: Error) in
|
|
ctx.close()
|
|
}.whenComplete {
|
|
_ = try? file.close()
|
|
}
|
|
}
|
|
} catch {
|
|
var body = ctx.channel.allocator.buffer(capacity: 128)
|
|
let response = { () -> HTTPResponseHead in
|
|
switch error {
|
|
case let e as IOError where e.errnoCode == ENOENT:
|
|
body.write(staticString: "IOError (not found)\r\n")
|
|
return HTTPResponseHead(version: request.version, status: .notFound)
|
|
case let e as IOError:
|
|
body.write(staticString: "IOError (other)\r\n")
|
|
body.write(string: e.description)
|
|
body.write(staticString: "\r\n")
|
|
return HTTPResponseHead(version: request.version, status: .notFound)
|
|
default:
|
|
body.write(string: "\(type(of: error)) error\r\n")
|
|
return HTTPResponseHead(version: request.version, status: .internalServerError)
|
|
}
|
|
}()
|
|
body.write(string: "\(error)")
|
|
body.write(staticString: "\r\n")
|
|
ctx.write(self.wrapOutboundOut(.head(response)), promise: nil)
|
|
ctx.write(self.wrapOutboundOut(.body(.byteBuffer(body))), promise: nil)
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
|
|
ctx.channel.close(promise: nil)
|
|
}
|
|
case .end(_):
|
|
self.state.requestComplete()
|
|
default:
|
|
fatalError("oh noes: \(request)")
|
|
}
|
|
}
|
|
|
|
private func completeResponse(_ ctx: ChannelHandlerContext, trailers: HTTPHeaders?, promise: EventLoopPromise<Void>?) {
|
|
self.state.responseComplete()
|
|
|
|
let promise = self.keepAlive ? promise : (promise ?? ctx.eventLoop.newPromise())
|
|
if !self.keepAlive {
|
|
promise!.futureResult.whenComplete { ctx.close(promise: nil) }
|
|
}
|
|
|
|
ctx.writeAndFlush(self.wrapOutboundOut(.end(trailers)), promise: promise)
|
|
}
|
|
|
|
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
|
|
let reqPart = self.unwrapInboundIn(data)
|
|
if let handler = self.handler {
|
|
handler(ctx, reqPart)
|
|
return
|
|
}
|
|
|
|
switch reqPart {
|
|
case .head(let request):
|
|
if request.uri.unicodeScalars.starts(with: "/dynamic".unicodeScalars) {
|
|
self.handler = self.dynamicHandler(request: request)
|
|
self.handler!(ctx, reqPart)
|
|
return
|
|
} else if let path = request.uri.chopPrefix("/sendfile/") {
|
|
self.handler = { self.handleFile(ctx: $0, request: $1, ioMethod: .sendfile, path: path) }
|
|
self.handler!(ctx, reqPart)
|
|
return
|
|
} else if let path = request.uri.chopPrefix("/fileio/") {
|
|
self.handler = { self.handleFile(ctx: $0, request: $1, ioMethod: .nonblockingFileIO, path: path) }
|
|
self.handler!(ctx, reqPart)
|
|
return
|
|
}
|
|
|
|
self.keepAlive = request.isKeepAlive
|
|
self.state.requestReceived()
|
|
|
|
var responseHead = HTTPResponseHead(version: request.version, status: HTTPResponseStatus.ok)
|
|
responseHead.headers.add(name: "content-length", value: "12")
|
|
let response = HTTPServerResponsePart.head(responseHead)
|
|
ctx.write(self.wrapOutboundOut(response), promise: nil)
|
|
case .body:
|
|
break
|
|
case .end:
|
|
self.state.requestComplete()
|
|
let content = HTTPServerResponsePart.body(.byteBuffer(buffer!.slice()))
|
|
ctx.write(self.wrapOutboundOut(content), promise: nil)
|
|
self.completeResponse(ctx, trailers: nil, promise: nil)
|
|
}
|
|
}
|
|
|
|
func channelReadComplete(ctx: ChannelHandlerContext) {
|
|
ctx.flush()
|
|
}
|
|
|
|
func handlerAdded(ctx: ChannelHandlerContext) {
|
|
self.buffer = ctx.channel.allocator.buffer(capacity: 12)
|
|
self.buffer.write(staticString: "Hello World!")
|
|
}
|
|
|
|
func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
|
|
switch event {
|
|
case let evt as ChannelEvent where evt == ChannelEvent.inputClosed:
|
|
// The remote peer half-closed the channel. At this time, any
|
|
// outstanding response will now get the channel closed, and
|
|
// if we are idle or waiting for a request body to finish we
|
|
// will close the channel immediately.
|
|
switch self.state {
|
|
case .idle, .waitingForRequestBody:
|
|
ctx.close(promise: nil)
|
|
case .sendingResponse:
|
|
self.keepAlive = false
|
|
}
|
|
default:
|
|
ctx.fireUserInboundEventTriggered(event)
|
|
}
|
|
}
|
|
}
|
|
|
|
// First argument is the program path
|
|
let arguments = CommandLine.arguments
|
|
let arg1 = arguments.dropFirst().first
|
|
let arg2 = arguments.dropFirst().dropFirst().first
|
|
let arg3 = arguments.dropFirst().dropFirst().dropFirst().first
|
|
|
|
let defaultHost = "::1"
|
|
let defaultPort = 8888
|
|
let defaultHtdocs = "/dev/null/"
|
|
|
|
enum BindTo {
|
|
case ip(host: String, port: Int)
|
|
case unixDomainSocket(path: String)
|
|
}
|
|
|
|
let htdocs: String
|
|
let bindTarget: BindTo
|
|
switch (arg1, arg1.flatMap { Int($0) }, arg2, arg2.flatMap { Int($0) }, arg3) {
|
|
case (.some(let h), _, _, .some(let p), let maybeHtdocs):
|
|
/* second arg an integer --> host port [htdocs] */
|
|
bindTarget = .ip(host: h, port: p)
|
|
htdocs = maybeHtdocs ?? defaultHtdocs
|
|
case (_, .some(let p), let maybeHtdocs, _, _):
|
|
/* first arg an integer --> port [htdocs] */
|
|
bindTarget = .ip(host: defaultHost, port: p)
|
|
htdocs = maybeHtdocs ?? defaultHtdocs
|
|
case (.some(let portString), .none, let maybeHtdocs, .none, .none):
|
|
/* couldn't parse as number --> uds-path [htdocs] */
|
|
bindTarget = .unixDomainSocket(path: portString)
|
|
htdocs = maybeHtdocs ?? defaultHtdocs
|
|
default:
|
|
htdocs = defaultHtdocs
|
|
bindTarget = BindTo.ip(host: defaultHost, port: defaultPort)
|
|
}
|
|
|
|
let group = MultiThreadedEventLoopGroup(numThreads: System.coreCount)
|
|
let threadPool = BlockingIOThreadPool(numberOfThreads: 6)
|
|
threadPool.start()
|
|
|
|
let fileIO = NonBlockingFileIO(threadPool: threadPool)
|
|
let bootstrap = ServerBootstrap(group: group)
|
|
// Specify backlog and enable SO_REUSEADDR for the server itself
|
|
.serverChannelOption(ChannelOptions.backlog, value: 256)
|
|
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
|
|
|
|
// Set the handlers that are applied to the accepted Channels
|
|
.childChannelInitializer { channel in
|
|
channel.pipeline.addHTTPServerHandlers().then {
|
|
channel.pipeline.add(handler: HTTPServerPipelineHandler())
|
|
}.then {
|
|
channel.pipeline.add(handler: HTTPHandler(fileIO: fileIO, htdocsPath: htdocs))
|
|
}
|
|
}
|
|
|
|
// Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels
|
|
.childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
|
|
.childChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
|
|
.childChannelOption(ChannelOptions.maxMessagesPerRead, value: 1)
|
|
.childChannelOption(ChannelOptions.allowRemoteHalfClosure, value: true)
|
|
|
|
defer {
|
|
try! group.syncShutdownGracefully()
|
|
try! threadPool.syncShutdownGracefully()
|
|
}
|
|
|
|
print("htdocs = \(htdocs)")
|
|
|
|
let channel = try { () -> Channel in
|
|
switch bindTarget {
|
|
case .ip(let host, let port):
|
|
return try bootstrap.bind(host: host, port: port).wait()
|
|
case .unixDomainSocket(let path):
|
|
return try bootstrap.bind(unixDomainSocketPath: path).wait()
|
|
}
|
|
}()
|
|
|
|
print("Server started and listening on \(channel.localAddress!), htdocs path \(htdocs)")
|
|
|
|
// This will never unblock as we don't close the ServerChannel
|
|
try channel.closeFuture.wait()
|
|
|
|
print("Server closed")
|