Skip to content

Commit

Permalink
Avoid allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Feb 26, 2019
1 parent b09247d commit 18a4fb7
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions Sources/HttpPipeline/NIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ private final class Handler: ChannelInboundHandler {
}
case .end:
guard let req = self.request else {
_ = ctx.channel.write(HTTPServerResponsePart.head(HTTPResponseHead(
version: .init(major: 1, minor: 1),
status: .init(statusCode: 307),
headers: .init([("location", self.baseUrl.absoluteString)])
)))
ctx.channel.write(
HTTPServerResponsePart.head(
HTTPResponseHead(
version: .init(major: 1, minor: 1),
status: .init(statusCode: 307),
headers: .init([("location", self.baseUrl.absoluteString)])
)
),
promise: nil
)
_ = ctx.channel.writeAndFlush(HTTPServerResponsePart.end(nil)).then {
ctx.channel.close()
}
Expand All @@ -95,11 +100,11 @@ private final class Handler: ChannelInboundHandler {
status: .init(statusCode: res.status.rawValue),
headers: .init(res.headers.map { ($0.name, $0.value) })
)
_ = ctx.channel.write(HTTPServerResponsePart.head(head))
ctx.channel.write(HTTPServerResponsePart.head(head), promise: nil)

var buffer = ctx.channel.allocator.buffer(capacity: res.body.count)
buffer.write(bytes: res.body)
_ = ctx.channel.write(HTTPServerResponsePart.body(.byteBuffer(buffer)))
ctx.channel.write(HTTPServerResponsePart.body(.byteBuffer(buffer)), promise: nil)

return ctx.channel.writeAndFlush(HTTPServerResponsePart.end(nil)).then {
ctx.channel.close()
Expand All @@ -109,7 +114,7 @@ private final class Handler: ChannelInboundHandler {
}

func errorCaught(ctx: ChannelHandlerContext, error: Error) {
_ = ctx.close()
ctx.close(promise: nil)
}
}

Expand Down

0 comments on commit 18a4fb7

Please sign in to comment.