Files
swift-nio/Sources/NIOHTTP1/HTTP.swift
T
Kevin Clark ed98bffe28 Renames and cleanup
* Massage HTTPHeaders

Simple had it's own version that was the same aside from a couple of
minor tweaks.

* Default initializer for HTTPHeaders

* Rename HTTPRequest -> HTTPRequestPart

* HTTPResponse -> HTTPResponsePart

* Add HTTPVersion(major:,minor:)

* Allow access to HTTPVersion.major/minor

* Comments on HTTP response codes

* Fix tests (needed rename applied)
2017-07-18 10:38:23 -07:00

49 lines
1.9 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 Foundation
import NIO
public final class HTTPResponseEncoder : ChannelOutboundHandler {
public typealias OutboundIn = HTTPResponsePart
public typealias OutboundOut = ByteBuffer
public init() {}
public func write(ctx: ChannelHandlerContext, data: IOData, promise: Promise<Void>?) {
switch self.tryUnwrapOutboundIn(data) {
case .some(.head(let response)):
// TODO: Is 256 really a good value here ?
var buffer = ctx.channel!.allocator.buffer(capacity: 256)
response.version.write(buffer: &buffer)
response.status.write(buffer: &buffer)
response.headers.write(buffer: &buffer)
ctx.write(data: self.wrapOutboundOut(buffer), promise: promise)
case .some(.body(.more(let buffer))):
ctx.write(data: self.wrapOutboundOut(buffer), promise: promise)
case .some(.body(.last(let buffer))):
if let buf = buffer {
ctx.write(data: self.wrapOutboundOut(buf), promise: promise)
} else if promise != nil {
// We only need to pass the promise further if the user is even interested in the result.
// Empty content so just write an empty buffer
ctx.write(data: self.wrapOutboundOut(ctx.channel!.allocator.buffer(capacity: 0)), promise: promise)
}
case .none:
ctx.write(data: data, promise: promise)
}
}
}