diff options
author | adamw <adam@warski.org> | 2017-07-26 22:03:53 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-26 22:03:53 +0200 |
commit | 1173aa8b702cd45afc106b3a07316a5812ffce50 (patch) | |
tree | ebaea4d5bcc160e483f5391805a0357098070e78 /async-http-client-handler | |
parent | c6657a682e3641a5b7bb751533f991cc041d9f5f (diff) | |
download | sttp-1173aa8b702cd45afc106b3a07316a5812ffce50.tar.gz sttp-1173aa8b702cd45afc106b3a07316a5812ffce50.tar.bz2 sttp-1173aa8b702cd45afc106b3a07316a5812ffce50.zip |
#2: onCompleted() in async-http-client-handler sometimes wasn't called, calling it earlier from onStream()
Diffstat (limited to 'async-http-client-handler')
-rw-r--r-- | async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala index ab35725..d634446 100644 --- a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala +++ b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala @@ -80,6 +80,7 @@ abstract class AsyncHttpClientHandler[R[_], S]( new StreamedAsyncHandler[Unit] { private val builder = new AsyncResponse.ResponseBuilder() private var publisher: Option[Publisher[ByteBuffer]] = None + private var completed = false override def onStream( p: Publisher[HttpResponseBodyPart]): AsyncHandler.State = { @@ -95,6 +96,10 @@ abstract class AsyncHttpClientHandler[R[_], S]( s.onSubscribe(v) }) }) + // #2: sometimes onCompleted() isn't called, only onStream(); this + // seems to be true esp for https sites. For these cases, completing + // the request here. + doComplete() State.CONTINUE } @@ -116,11 +121,20 @@ abstract class AsyncHttpClientHandler[R[_], S]( } override def onCompleted(): Unit = { - val baseResponse = readResponseNoBody(builder.build()) - val p = publisher.getOrElse(EmptyPublisher) - val s = publisherToStreamBody(p) - val t = responseAs.responseIsStream(s) - success(rm.unit(baseResponse.copy(body = t))) + // if the request had no body, onStream() will never be called + doComplete() + } + + private def doComplete(): Unit = { + if (!completed) { + completed = true + + val baseResponse = readResponseNoBody(builder.build()) + val p = publisher.getOrElse(EmptyPublisher) + val s = publisherToStreamBody(p) + val t = responseAs.responseIsStream(s) + success(rm.unit(baseResponse.copy(body = t))) + } } override def onThrowable(t: Throwable): Unit = { |