-
Notifications
You must be signed in to change notification settings - Fork 63
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
Implement Connection persistence per RFC 9112, Section 9.3 #913
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import org.http4s.Entity.Empty | |
import org.http4s.Header | ||
import org.http4s.Header.Raw | ||
import org.http4s.Headers | ||
import org.http4s.HttpVersion | ||
import org.http4s.InvalidBodyException | ||
import org.http4s.Method | ||
import org.http4s.Request | ||
|
@@ -67,6 +68,7 @@ private[blaze] trait Http1Stage[F[_]] { self: TailStage[ByteBuffer] => | |
protected def contentComplete(): Boolean | ||
|
||
/** Check Connection header and add applicable headers to response */ | ||
@deprecated("Use checkRequestCloseConnection(Request) instead", "0.23.17") | ||
protected final def checkCloseConnection(conn: Connection, rr: StringWriter): Boolean = | ||
if (conn.hasKeepAlive) { // connection, look to the request | ||
logger.trace("Found Keep-Alive header") | ||
|
@@ -83,6 +85,32 @@ private[blaze] trait Http1Stage[F[_]] { self: TailStage[ByteBuffer] => | |
true | ||
} | ||
|
||
private[http4s] final def checkRequestCloseConnection(req: Request[F]): Boolean = { | ||
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 tried to fix the Boolean blindness, but it ripples to many places. |
||
val conn = req.headers.get[Connection] | ||
if (conn.fold(false)(_.hasClose)) { | ||
logger.trace(s"Closing ${conn} due to explicit close option in request's Connection header") | ||
true | ||
} else if (req.httpVersion >= HttpVersion.`HTTP/1.1`) { | ||
logger.trace(s"Keeping ${conn} alive per default behavior of HTTP >= 1.1") | ||
false | ||
} else if (req.httpVersion == HttpVersion.`HTTP/1.0`) { | ||
if (conn.fold(false)(_.hasKeepAlive)) { | ||
logger.trace( | ||
s"Keeping ${conn} alive due to explicit keep-alive option in request's Connection header" | ||
) | ||
false | ||
} else { | ||
logger.trace(s"Closing ${conn} per default behavior of HTTP/1.0") | ||
true | ||
} | ||
} else { | ||
// It would be strange to serve HTTP/0.x, but we need a value and | ||
// this is the rule. | ||
logger.trace(s"Closing ${conn} for HTTP < 1.0") | ||
true | ||
} | ||
} | ||
|
||
/** Get the proper body encoder based on the request */ | ||
protected final def getEncoder( | ||
req: Request[F], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,12 +245,7 @@ private[blaze] class Http1ServerStage[F[_]]( | |
// Need to decide which encoder and if to close on finish | ||
val closeOnFinish = respConn | ||
.map(_.hasClose) | ||
.orElse { | ||
req.headers.get[Connection].map(checkCloseConnection(_, rr)) | ||
} | ||
.getOrElse( | ||
parser.minorVersion() == 0 | ||
) // Finally, if nobody specifies, http 1.0 defaults to close | ||
.getOrElse(checkRequestCloseConnection(req)) | ||
|
||
// choose a body encoder. Will add a Transfer-Encoding header if necessary | ||
val bodyEncoder: Http1Writer[F] = | ||
|
@@ -274,10 +269,14 @@ private[blaze] class Http1ServerStage[F[_]]( | |
case _ => // nop | ||
} | ||
|
||
// add KeepAlive to Http 1.0 responses if the header isn't already present | ||
rr << (if (!closeOnFinish && parser.minorVersion() == 0 && respConn.isEmpty) | ||
"Connection: keep-alive\r\n\r\n" | ||
else "\r\n") | ||
closeOnFinish match { | ||
case true if respConn.isEmpty => | ||
rr << "Connection: close\r\n\r\n" | ||
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. This was a buried side effect in the old code. |
||
case false if parser.minorVersion() == 0 && respConn.isEmpty => | ||
rr << "Connection: keep-alive\r\n\r\n" | ||
case _ => | ||
rr << "\r\n" | ||
} | ||
|
||
new BodylessWriter[F](this, closeOnFinish) | ||
} else | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Kept for binary compatibility, though we could do the MiMa dance.