Skip to content

Commit

Permalink
don't re-add content-length header for HEAD request responses (vapor#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored and pull[bot] committed Apr 14, 2020
1 parent 97a7dac commit 6f49c2c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 1 addition & 3 deletions Sources/Vapor/HTTP/Server/HTTPServerHandler.swift
Expand Up @@ -19,11 +19,9 @@ final class HTTPServerHandler: ChannelInboundHandler, RemovableChannelHandler {
case .failure(let error):
self.errorCaught(context: context, error: error)
case .success(let response):
let contentLength = response.headers.first(name: .contentLength)
if request.method == .HEAD {
response.body = .init()
response.forHeadRequest = true
}
response.headers.replaceOrAdd(name: .contentLength, value: contentLength ?? "0")
self.serialize(response, for: request, context: context)
}
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/Vapor/HTTP/Server/HTTPServerResponseEncoder.swift
Expand Up @@ -31,8 +31,9 @@ final class HTTPServerResponseEncoder: ChannelOutboundHandler, RemovableChannelH
headers: response.headers
))), promise: nil)

if response.status == .noContent {
// don't send bodies for 204 (no content) requests
if response.status == .noContent || response.forHeadRequest {
// don't send bodies for 204 (no content) responses
// or HEAD requests
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: promise)
} else {
switch response.body.storage {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Vapor/Response/Response.swift
Expand Up @@ -32,6 +32,9 @@ public final class Response: CustomStringConvertible {
didSet { self.headers.updateContentLength(self.body.count) }
}

// If `true`, don't serialize the body.
var forHeadRequest: Bool

internal enum Upgrader {
case webSocket(maxFrameSize: WebSocketMaxFrameSize, onUpgrade: (WebSocket) -> ())
}
Expand Down Expand Up @@ -134,6 +137,7 @@ public final class Response: CustomStringConvertible {
self.headers = headers
self.body = body
self.storage = .init()
self.forHeadRequest = false
}
}

Expand Down

0 comments on commit 6f49c2c

Please sign in to comment.