diff options
author | Piotr Gabara <piotr.gabara@hotmail.com> | 2017-08-27 20:06:52 +0200 |
---|---|---|
committer | Piotr Gabara <piotr.gabara@hotmail.com> | 2017-09-05 16:37:22 +0200 |
commit | e82346820797bb2d80d0fada7f17c5880871edce (patch) | |
tree | 1b972cfffadb9de0f6f0c99f842ada1d58662fb8 /core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala | |
parent | febcdbcb4448fe1e754ecd08fb4df4bf6c6a211c (diff) | |
download | sttp-e82346820797bb2d80d0fada7f17c5880871edce.tar.gz sttp-e82346820797bb2d80d0fada7f17c5880871edce.tar.bz2 sttp-e82346820797bb2d80d0fada7f17c5880871edce.zip |
Make read and connection timeout configurable
Diffstat (limited to 'core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala index 45a0448..24c81c7 100644 --- a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala @@ -11,14 +11,18 @@ import java.util.zip.{GZIPInputStream, InflaterInputStream} import scala.annotation.tailrec import scala.io.Source import scala.collection.JavaConverters._ +import scala.concurrent.duration.{Duration, FiniteDuration} -class HttpURLConnectionHandler extends SttpHandler[Id, Nothing] { +class HttpURLConnectionHandler private (connectionTimeout: FiniteDuration) + extends SttpHandler[Id, Nothing] { override def send[T](r: Request[T, Nothing]): Response[T] = { val c = new URL(r.uri.toString).openConnection().asInstanceOf[HttpURLConnection] c.setRequestMethod(r.method.m) r.headers.foreach { case (k, v) => c.setRequestProperty(k, v) } c.setDoInput(true) + c.setReadTimeout(timeout(r.readTimeout)) + c.setConnectTimeout(timeout(connectionTimeout)) // redirects are handled in SttpHandler c.setInstanceFollowRedirects(false) @@ -68,6 +72,10 @@ class HttpURLConnectionHandler extends SttpHandler[Id, Nothing] { } } + private def timeout(t: Duration): Int = + if (t.isFinite()) t.toMillis.toInt + else 0 + private def writeBasicBody(body: BasicRequestBody, os: OutputStream): Unit = { body match { case StringBody(b, encoding, _) => @@ -247,3 +255,11 @@ class HttpURLConnectionHandler extends SttpHandler[Id, Nothing] { override def close(): Unit = {} } + +object HttpURLConnectionHandler { + + def apply(connectionTimeout: FiniteDuration = SttpHandler.DefaultConnectionTimeout) + : SttpHandler[Id, Nothing] = + new FollowRedirectsHandler[Id, Nothing]( + new HttpURLConnectionHandler(connectionTimeout)) +} |