Skip to content

Commit

Permalink
Merge pull request #39 from atais/master
Browse files Browse the repository at this point in the history
Loading system proxy values by default
  • Loading branch information
adamw committed Oct 31, 2017
2 parents ae0d7f6 + 8854c9c commit a2a6db3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
37 changes: 33 additions & 4 deletions core/src/main/scala/com/softwaremill/sttp/SttpBackendOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.softwaremill.sttp

import java.net.InetSocketAddress

import scala.concurrent.duration._
import com.softwaremill.sttp.SttpBackendOptions._

import scala.concurrent.duration._
import scala.util.Try

case class SttpBackendOptions(
connectionTimeout: FiniteDuration,
proxy: Option[Proxy]
Expand All @@ -28,6 +30,7 @@ object SttpBackendOptions {
sealed trait ProxyType {
def asJava: java.net.Proxy.Type
}

object ProxyType {
case object Http extends ProxyType {
override def asJava: java.net.Proxy.Type = java.net.Proxy.Type.HTTP
Expand All @@ -37,12 +40,38 @@ object SttpBackendOptions {
}
}

val Default: SttpBackendOptions = SttpBackendOptions(30.seconds, None)
private val Empty: SttpBackendOptions =
SttpBackendOptions(30.seconds, None)

val Default: SttpBackendOptions =
Empty.copy(proxy = loadSystemProxy)

def connectionTimeout(ct: FiniteDuration): SttpBackendOptions =
Default.connectionTimeout(ct)

def httpProxy(host: String, port: Int): SttpBackendOptions =
Default.httpProxy(host, port)
Empty.httpProxy(host, port)

def socksProxy(host: String, port: Int): SttpBackendOptions =
Default.socksProxy(host, port)
Empty.socksProxy(host, port)

private def loadSystemProxy: Option[Proxy] = {
def system(hostProp: String,
portProp: String,
make: (String, Int) => Proxy,
defaultPort: Int) = {
val host = Option(System.getProperty(hostProp))
def port = Try(System.getProperty(portProp).toInt).getOrElse(defaultPort)
host.map(make(_, port))
}

def proxy(t: ProxyType)(host: String, port: Int) = Proxy(host, port, t)

import ProxyType._
val socks = system("socksProxyHost", "socksProxyPort", proxy(Socks), 1080)
val http = system("http.proxyHost", "http.proxyPort", proxy(Http), 80)

Seq(socks, http).find(_.isDefined).flatten
}

}
11 changes: 10 additions & 1 deletion docs/conf/proxy.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
Proxy support
=============

A proxy can be specified when creating a backend::
sttp library by default checks for your System proxy properties (`docs <https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html>`_):

Following settings are checked:

1. ``socksProxyHost`` and ``socksProxyPort`` *(default: 1080)*
2. ``http.proxyHost`` and ``http.proxyPort`` *(default: 80)*

Settings are loaded **in given order** and the **first existing value** is being used.

Otherwise, proxy values can be specified manually when creating a backend::
import com.softwaremill.sttp._
Expand Down
1 change: 1 addition & 0 deletions docs/credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Credits
* `Piotr Gabara <https://github.com/bhop>`_
* `Gabriele Petronella <https://github.com/gabro>`_
* `Paweł Stawicki <https://github.com/amorfis>`_
* `Michał Siatkowski <https://github.com/atais>`_
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.0.2
sbt.version=1.0.3

0 comments on commit a2a6db3

Please sign in to comment.