Skip to content

Commit 7900d15

Browse files
authored
Added streaming support to scala-native curl-based sync backend (#2807)
Added sync streaming support to scala-native curl-based sync backend using curl_multi api family Before submitting pull request: - [X] Check if the project compiles by running `sbt compile` - [X] Verify docs compilation by running `sbt compileDocs` - [ ] Check if tests pass by running `sbt test` - [X] Format code by running `sbt scalafmt`
1 parent c99909b commit 7900d15

File tree

8 files changed

+895
-20
lines changed

8 files changed

+895
-20
lines changed

core/src/main/resources/scala-native/ffi.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,18 @@ const char* sttp_curl_get_version() {
88
return curl_version_info(CURLVERSION_NOW)->version;
99
}
1010
int sttp_curl_getinfo_pointer(CURL *curl, CURLINFO info, void* arg) {return curl_easy_getinfo(curl, info, arg); }
11+
12+
/* curl_multi wrappers */
13+
/* Most curl_multi functions are called directly via @extern bindings.
14+
This wrapper exists because CURLMsg contains a union that can't be
15+
safely represented in Scala Native's type system. */
16+
int sttp_curl_multi_info_read_result(CURLM *multi, CURL **easy_out) {
17+
int msgs_in_queue;
18+
CURLMsg *msg = curl_multi_info_read(multi, &msgs_in_queue);
19+
if (msg && msg->msg == CURLMSG_DONE) {
20+
if (easy_out) *easy_out = msg->easy_handle;
21+
return (int)msg->data.result;
22+
}
23+
return -1; /* no completed transfer */
24+
}
1125
#endif

core/src/main/scalanative/sttp/client4/SttpExtensions.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package sttp.client4
22

3-
import java.io.File
3+
import java.io.{File, InputStream}
44
import java.nio.file.Path
55

66
import sttp.client4.internal._
77
import sttp.model.{Part, StatusCode}
88

99
trait SttpExtensions {
10+
11+
def asInputStream[T](f: InputStream => T): ResponseAs[Either[String, T]] =
12+
asEither(asStringAlways, asInputStreamAlways(f))
13+
14+
def asInputStreamAlways[T](f: InputStream => T): ResponseAs[T] = new ResponseAs(ResponseAsInputStream(f))
15+
16+
def asInputStreamUnsafe: ResponseAs[Either[String, InputStream]] = asEither(asStringAlways, asInputStreamAlwaysUnsafe)
17+
18+
def asInputStreamAlwaysUnsafe: ResponseAs[InputStream] = new ResponseAs(ResponseAsInputStreamUnsafe)
19+
1020
def asFile(file: File): ResponseAs[Either[String, File]] = asEither(asStringAlways, asFileAlways(file))
1121

1222
def asFileAlways(file: File): ResponseAs[File] =

0 commit comments

Comments
 (0)