diff options
author | adamw <adam@warski.org> | 2017-07-21 14:43:20 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-21 14:43:20 +0200 |
commit | a6b4a71d59da928ddb326671b1058501bb1a45c5 (patch) | |
tree | 8b1a2bc5d4196ff252d5ccd00cd546657f92fd14 /akka-http-handler/src/main | |
parent | 2d099f6832f6e362b9a4cd48e81a16c8d77adeaf (diff) | |
download | sttp-a6b4a71d59da928ddb326671b1058501bb1a45c5.tar.gz sttp-a6b4a71d59da928ddb326671b1058501bb1a45c5.tar.bz2 sttp-a6b4a71d59da928ddb326671b1058501bb1a45c5.zip |
AcceptEncoding + response decompression
Diffstat (limited to 'akka-http-handler/src/main')
-rw-r--r-- | akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala index fbb00a3..716118c 100644 --- a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala +++ b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala @@ -1,10 +1,13 @@ package com.softwaremill.sttp.akkahttp +import java.io.UnsupportedEncodingException + import akka.actor.{ActorSystem, Terminated} import akka.http.scaladsl.Http +import akka.http.scaladsl.coding.{Deflate, Gzip, NoCoding} import akka.http.scaladsl.model.HttpHeader.ParsingResult import akka.http.scaladsl.model._ -import akka.http.scaladsl.model.headers.`Content-Type` +import akka.http.scaladsl.model.headers.{HttpEncodings, `Content-Type`} import akka.http.scaladsl.model.ContentTypes.`application/octet-stream` import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Source, StreamConverters} @@ -34,7 +37,7 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) .flatMap(Http().singleRequest(_)) .flatMap { hr => val code = hr.status.intValue() - bodyFromAkka(r.responseAs, hr) + bodyFromAkka(r.responseAs, decodeAkkaResponse(hr)) .map(Response(_, code, headersFromAkka(hr))) } } @@ -152,6 +155,19 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) private def isContentType(header: (String, String)) = header._1.toLowerCase.contains(`Content-Type`.lowercaseName) + // http://doc.akka.io/docs/akka-http/10.0.7/scala/http/common/de-coding.html + private def decodeAkkaResponse(response: HttpResponse): HttpResponse = { + val decoder = response.encoding match { + case HttpEncodings.gzip => Gzip + case HttpEncodings.deflate => Deflate + case HttpEncodings.identity => NoCoding + case ce => + throw new UnsupportedEncodingException(s"Unsupported encoding: $ce") + } + + decoder.decodeMessage(response) + } + def close(): Future[Terminated] = { actorSystem.terminate() } |