-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
092a8d0
commit 4b5a428
Showing
6 changed files
with
479 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,22 @@ jobs: | |
matrix: | ||
scala-version: [ "2.12", "2.13", "3" ] | ||
target-platform: [ "JVM", "JS", "Native" ] | ||
java: [ "11", "21" ] | ||
exclude: | ||
- java: "21" | ||
include: # Restricted to build only specific Loom-based modules | ||
- scala-version: "3" | ||
target-platform: "JVM" | ||
java: "21" | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Set up JDK 11 | ||
uses: actions/setup-java@v1 | ||
- name: Set up JDK | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: 11 | ||
distribution: 'temurin' | ||
cache: 'sbt' | ||
java-version: ${{ matrix.java }} | ||
- name: Cache sbt | ||
uses: actions/cache@v2 | ||
with: | ||
|
@@ -40,14 +49,18 @@ jobs: | |
sudo apt-get update | ||
sudo apt-get install libidn2-dev libcurl3-dev | ||
echo "STTP_NATIVE=1" >> $GITHUB_ENV | ||
- name: Enable Loom-specific modules | ||
if: matrix.java == '21' | ||
run: echo "ONLY_LOOM=1" >> $GITHUB_ENV | ||
- name: Compile | ||
run: sbt -v "compileScoped ${{ matrix.scala-version }} ${{ matrix.target-platform }}" | ||
- name: Compile documentation | ||
if: matrix.target-platform == 'JVM' | ||
if: matrix.target-platform == 'JVM' && matrix.java == '11' | ||
run: sbt -v compileDocs | ||
- name: Test | ||
run: sbt -v "testScoped ${{ matrix.scala-version }} ${{ matrix.target-platform }}" | ||
- name: Prepare release notes | ||
if: matrix.java == '11' | ||
uses: release-drafter/release-drafter@v5 | ||
with: | ||
config-name: release-drafter.yml | ||
|
@@ -100,16 +113,19 @@ jobs: | |
name: Publish release | ||
needs: [ci] | ||
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v')) | ||
runs-on: ubuntu-20.04 | ||
env: | ||
STTP_NATIVE: 1 | ||
runs-on: ubuntu-22.04 | ||
strategy: | ||
matrix: | ||
java: [ "11", "21" ] | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Set up JDK 11 | ||
uses: actions/setup-java@v1 | ||
- name: Set up JDK | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: 11 | ||
distribution: 'temurin' | ||
java-version: ${{ matrix.java }} | ||
cache: 'sbt' | ||
- name: Cache sbt | ||
uses: actions/cache@v2 | ||
with: | ||
|
@@ -122,6 +138,12 @@ jobs: | |
run: | | ||
sudo apt-get update | ||
sudo apt-get install libidn2-dev libcurl3-dev | ||
- name: Enable Native-specific modules | ||
if: matrix.java == '11' | ||
run: echo "STTP_NATIVE=1" >> $GITHUB_ENV | ||
- name: Enable Loom-specific modules | ||
if: matrix.java == '21' | ||
run: echo "ONLY_LOOM=1" >> $GITHUB_ENV | ||
- name: Compile | ||
run: sbt compile | ||
- name: Publish artifacts | ||
|
@@ -132,12 +154,14 @@ jobs: | |
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} | ||
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} | ||
- name: Extract version from commit message | ||
if: matrix.java == '11' | ||
run: | | ||
version=${GITHUB_REF/refs\/tags\/v/} | ||
echo "VERSION=$version" >> $GITHUB_ENV | ||
env: | ||
COMMIT_MSG: ${{ github.event.head_commit.message }} | ||
- name: Publish release notes | ||
if: matrix.java == '11' | ||
uses: release-drafter/release-drafter@v5 | ||
with: | ||
config-name: release-drafter.yml | ||
|
@@ -191,4 +215,4 @@ jobs: | |
uses: "pascalgn/[email protected]" | ||
env: | ||
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | ||
MERGE_METHOD: "squash" | ||
MERGE_METHOD: "squash" |
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
119 changes: 119 additions & 0 deletions
119
effects/ox/src/main/scala/sttp/client4/impl/ox/ws/OxWebSockets.scala
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package sttp.client4.impl.ox.ws | ||
|
||
import ox.* | ||
import ox.channels.* | ||
import sttp.client4.ws.SyncWebSocket | ||
import sttp.ws.WebSocketFrame | ||
|
||
import scala.util.control.NonFatal | ||
|
||
/** Converts a [[SyncWebSocket]] into a pair of `Source` of server responses and a `Sink` for client requests. The | ||
* `Source` starts receiving frames immediately, its internal buffer size can be adjusted with an implicit | ||
* [[ox.channels.StageCapacity]]. Make sure that the `Source` is contiunually read. This will guarantee that | ||
* server-side Close signal is received and handled. If you don't want to process frames from the server, you can at | ||
* least handle it with a `fork { source.drain() }`. | ||
* | ||
* You don't need to manually call `ws.close()` when using this approach, this will be handled automatically | ||
* underneath, according to following rules: | ||
* - If the request `Sink` is closed due to an upstream error, a Close frame is sent, and the `Source` with incoming | ||
* responses gets completed as `Done`. | ||
* - If the request `Sink` completes as `Done`, a `Close` frame is sent, and the response `Sink` keeps receiving | ||
* responses until the server closes communication. | ||
* - If the response `Source` is closed by a Close frome from the server or due to an error, the request Sink is | ||
* closed as `Done`, which will still send all outstanding buffered frames, and then finish. | ||
* | ||
* @param ws | ||
* a `SyncWebSocket` where the underlying `Sink` will send requests, and where the `Source` will pull responses from. | ||
* @param concatenateFragmented | ||
* whether fragmented frames from the server should be concatenated into a single frame (true by default). | ||
*/ | ||
def asSourceAndSink(ws: SyncWebSocket, concatenateFragmented: Boolean = true)(using | ||
Ox, | ||
StageCapacity | ||
): (Source[WebSocketFrame], Sink[WebSocketFrame]) = | ||
val requestsChannel = StageCapacity.newChannel[WebSocketFrame] | ||
val responsesChannel = StageCapacity.newChannel[WebSocketFrame] | ||
fork { | ||
try | ||
repeatWhile { | ||
ws.receive() match | ||
case frame: WebSocketFrame.Data[_] => | ||
responsesChannel.sendOrClosed(frame) match | ||
case _: ChannelClosed => false | ||
case _ => true | ||
case WebSocketFrame.Close(status, msg) if status > 1001 => | ||
responsesChannel.errorOrClosed(new WebSocketClosedWithError(status, msg)).discard | ||
false | ||
case _: WebSocketFrame.Close => | ||
responsesChannel.doneOrClosed().discard | ||
false | ||
case ping: WebSocketFrame.Ping => | ||
requestsChannel.sendOrClosed(WebSocketFrame.Pong(ping.payload)).discard | ||
// Keep receiving even if pong couldn't be send due to closed request channel. We want to process | ||
// whatever responses there are still coming from the server until it signals the end with a Close frome. | ||
true | ||
case _: WebSocketFrame.Pong => | ||
// ignore pongs | ||
true | ||
} | ||
catch | ||
case NonFatal(err) => | ||
responsesChannel.errorOrClosed(err).discard | ||
finally requestsChannel.doneOrClosed().discard | ||
}.discard | ||
|
||
fork { | ||
try | ||
repeatWhile { | ||
requestsChannel.receiveOrClosed() match | ||
case closeFrame: WebSocketFrame.Close => | ||
ws.send(closeFrame) | ||
false | ||
case frame: WebSocketFrame => | ||
ws.send(frame) | ||
true | ||
case ChannelClosed.Done => | ||
ws.close() | ||
false | ||
case ChannelClosed.Error(err) => | ||
// There's no proper "client error" status. Statuses 4000+ are available for custom cases | ||
ws.send(WebSocketFrame.Close(4000, "Client error")) | ||
responsesChannel.doneOrClosed().discard | ||
false | ||
} | ||
catch | ||
case NonFatal(err) => | ||
// If responses are closed, server finished the communication and we can ignore that send() failed | ||
if (!responsesChannel.isClosedForReceive) requestsChannel.errorOrClosed(err).discard | ||
}.discard | ||
|
||
(optionallyConcatenateFrames(responsesChannel, concatenateFragmented), requestsChannel) | ||
|
||
final case class WebSocketClosedWithError(statusCode: Int, msg: String) | ||
extends Exception(s"WebSocket closed with status $statusCode: $msg") | ||
|
||
private def optionallyConcatenateFrames(s: Source[WebSocketFrame], doConcatenate: Boolean)(using | ||
Ox | ||
): Source[WebSocketFrame] = | ||
if doConcatenate then | ||
type Accumulator = Option[Either[Array[Byte], String]] | ||
s.mapStateful(() => None: Accumulator) { | ||
case (None, f: WebSocketFrame.Ping) => (None, Some(f)) | ||
case (None, f: WebSocketFrame.Pong) => (None, Some(f)) | ||
case (None, f: WebSocketFrame.Close) => (None, Some(f)) | ||
case (None, f: WebSocketFrame.Data[_]) if f.finalFragment => (None, Some(f)) | ||
case (None, f: WebSocketFrame.Text) => (Some(Right(f.payload)), None) | ||
case (None, f: WebSocketFrame.Binary) => (Some(Left(f.payload)), None) | ||
case (Some(Left(acc)), f: WebSocketFrame.Binary) if f.finalFragment => | ||
(None, Some(f.copy(payload = acc ++ f.payload))) | ||
case (Some(Left(acc)), f: WebSocketFrame.Binary) if !f.finalFragment => (Some(Left(acc ++ f.payload)), None) | ||
case (Some(Right(acc)), f: WebSocketFrame.Text) if f.finalFragment => | ||
(None, Some(f.copy(payload = acc + f.payload))) | ||
case (Some(Right(acc)), f: WebSocketFrame.Text) if !f.finalFragment => (Some(Right(acc + f.payload)), None) | ||
case (acc, f) => | ||
throw new IllegalStateException( | ||
s"Unexpected WebSocket frame received during concatenation. Frame received: ${f.getClass | ||
.getSimpleName()}, accumulator type: ${acc.map(_.getClass.getSimpleName)}" | ||
) | ||
}.collectAsView { case Some(f: WebSocketFrame) => f } | ||
else s |
Oops, something went wrong.