-
Notifications
You must be signed in to change notification settings - Fork 123
Refactor Request Validation #391
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,6 +293,33 @@ extension HTTPClient { | |
public var port: Int { | ||
return self.url.port ?? (self.useTLS ? 443 : 80) | ||
} | ||
|
||
func createRequestHead() throws -> (HTTPRequestHead, RequestFramingMetadata) { | ||
var head = HTTPRequestHead( | ||
version: .http1_1, | ||
method: self.method, | ||
uri: self.uri, | ||
headers: self.headers | ||
) | ||
|
||
if !head.headers.contains(name: "host") { | ||
let port = self.port | ||
var host = self.host | ||
if !(port == 80 && self.scheme == "http"), !(port == 443 && self.scheme == "https") { | ||
host += ":\(port)" | ||
} | ||
head.headers.add(name: "host", value: host) | ||
} | ||
|
||
let metadata = try head.headers.validate(method: self.method, body: self.body) | ||
|
||
// This assert can go away when (if ever!) the above `if` correctly handles other HTTP versions. For example | ||
// in HTTP/1.0, we need to treat the absence of a 'connection: keep-alive' as a close too. | ||
assert(head.version == HTTPVersion(major: 1, minor: 1), | ||
"Sending a request in HTTP version \(head.version) which is unsupported by the above `if`") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I still don't think this assertion is useful. We literally set the HTTP version to a hardcoded constant just above. |
||
|
||
return (head, metadata) | ||
} | ||
} | ||
|
||
/// Represent HTTP response. | ||
|
@@ -877,46 +904,29 @@ extension TaskHandler: ChannelDuplexHandler { | |
typealias OutboundOut = HTTPClientRequestPart | ||
|
||
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { | ||
self.state = .sendingBodyWaitingResponseHead | ||
|
||
let request = self.unwrapOutboundIn(data) | ||
self.state = .sendingBodyWaitingResponseHead | ||
|
||
var head = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), | ||
method: request.method, | ||
uri: request.uri) | ||
var headers = request.headers | ||
|
||
if !request.headers.contains(name: "host") { | ||
let port = request.port | ||
var host = request.host | ||
if !(port == 80 && request.scheme == "http"), !(port == 443 && request.scheme == "https") { | ||
host += ":\(port)" | ||
} | ||
headers.add(name: "host", value: host) | ||
} | ||
let head: HTTPRequestHead | ||
let metadata: RequestFramingMetadata | ||
|
||
do { | ||
try headers.validate(method: request.method, body: request.body) | ||
(head, metadata) = try request.createRequestHead() | ||
} catch { | ||
self.errorCaught(context: context, error: error) | ||
promise?.fail(error) | ||
return | ||
} | ||
|
||
head.headers = headers | ||
|
||
if head.headers[canonicalForm: "connection"].map({ $0.lowercased() }).contains("close") { | ||
self.closing = true | ||
} | ||
// This assert can go away when (if ever!) the above `if` correctly handles other HTTP versions. For example | ||
// in HTTP/1.0, we need to treat the absence of a 'connection: keep-alive' as a close too. | ||
assert(head.version == HTTPVersion(major: 1, minor: 1), | ||
assert(head.version == .http1_1, | ||
"Sending a request in HTTP version \(head.version) which is unsupported by the above `if`") | ||
|
||
let contentLengths = head.headers[canonicalForm: "content-length"] | ||
assert(contentLengths.count <= 1) | ||
|
||
self.expectedBodyLength = contentLengths.first.flatMap { Int($0) } | ||
if case .fixedSize(let length) = metadata.body { | ||
self.expectedBodyLength = length | ||
} | ||
self.closing = metadata.connectionClose | ||
|
||
context.write(wrapOutboundOut(.head(head))).map { | ||
self.callOutToDelegateFireAndForget(value: head, self.delegate.didSendRequestHead) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make these
Hashable
:Equatable
by itself is rarely sensible.