Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for writing informational headers #196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/Hummingbird/Server/Application+HTTPResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,10 @@ extension HBApplication {
var eventLoop: EventLoop { return self.channel.eventLoop }
var allocator: ByteBufferAllocator { return self.channel.allocator }
var remoteAddress: SocketAddress? { return self.channel.remoteAddress }

/// Used by HBRequest to write informational headers
func writeInformationalResponse(head: HTTPResponseHead) {
self.channel.writeAndFlush(HTTPPart<HTTPResponseHead, IOData>.head(head), promise: nil)
}
}
}
30 changes: 30 additions & 0 deletions Sources/Hummingbird/Server/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ public struct HBRequest: Sendable, HBSendableExtensible {
private static let globalRequestID = ManagedAtomic(0)
}

extension HBRequest {
public enum InformationalResponseStatus {
case `continue`
case switchingProtocols
case processing
case earlyHints

var httpResponseStatus: HTTPResponseStatus {
switch self {
case .continue: return .continue
case .switchingProtocols: return .switchingProtocols
case .processing: return .processing
case .earlyHints: return .custom(code: 103, reasonPhrase: "Early Hints")
}
}
}

/// Write informantional response back before finishing processing request
///
/// An informational response indicates that the request was received and understood. It is issued on a
/// provisional basis while request processing continues. It alerts the client to wait for a final response.
/// The message consists only of the status line and optional header fields
/// - Parameters:
/// - status: information response status
/// - headers: headers to include in information response
public func writeInformationalResponse(status: InformationalResponseStatus, headers: HTTPHeaders = [:]) {
self.context.writeInformationalResponse(head: .init(version: self.version, status: status.httpResponseStatus, headers: headers))
}
}

extension Logger {
/// Create new Logger with additional metadata value
/// - Parameters:
Expand Down
6 changes: 6 additions & 0 deletions Sources/Hummingbird/Server/RequestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//
//===----------------------------------------------------------------------===//

import NIOHTTP1

/// Context that created HBRequest.
public protocol HBRequestContext: Sendable {
/// EventLoop request is running on
Expand All @@ -20,4 +22,8 @@ public protocol HBRequestContext: Sendable {
var allocator: ByteBufferAllocator { get }
/// Connected host address
var remoteAddress: SocketAddress? { get }

/// Write informational response back to server before completing the request
/// - Parameter head: response head
func writeInformationalResponse(head: HTTPResponseHead)
}