-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
artsiomsamasadau
committed
May 26, 2022
1 parent
eff1be2
commit af85082
Showing
6 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package zhttp.http | ||
import io.netty.handler.proxy.HttpProxyHandler | ||
import zhttp.http.middleware.Auth.Credentials | ||
|
||
import java.net.InetSocketAddress | ||
|
||
/** | ||
* Represents the connection to the forward proxy before running the request | ||
* | ||
* @param url: | ||
* url address of the proxy server | ||
* @param credentials: | ||
* credentials for the proxy server. Encodes credentials with basic auth and | ||
* put under the 'proxy-authorization' header | ||
* @param headers: | ||
* headers for the request to the proxy server | ||
*/ | ||
final case class Proxy( | ||
url: URL, | ||
credentials: Option[Credentials] = None, | ||
headers: Headers = Headers.empty, | ||
) { self => | ||
|
||
def withUrl(url: URL): Proxy = self.copy(url = url) | ||
def withCredentials(credentials: Credentials): Proxy = self.copy(credentials = Some(credentials)) | ||
def withHeaders(headers: Headers): Proxy = self.copy(headers = headers) | ||
|
||
/** | ||
* Converts a Proxy to [io.netty.handler.proxy.HttpProxyHandler] | ||
*/ | ||
private[zhttp] def encode: Option[HttpProxyHandler] = credentials.fold(unauthorizedProxy)(authorizedProxy) | ||
|
||
private def authorizedProxy(credentials: Credentials): Option[HttpProxyHandler] = for { | ||
proxyAddress <- buildProxyAddress | ||
uname = credentials.uname | ||
upassword = credentials.upassword | ||
encodedHeaders = headers.encode | ||
} yield new HttpProxyHandler(proxyAddress, uname, upassword, encodedHeaders) | ||
|
||
private def unauthorizedProxy: Option[HttpProxyHandler] = for { | ||
proxyAddress <- buildProxyAddress | ||
encodedHeaders = headers.encode | ||
} yield { | ||
new HttpProxyHandler(proxyAddress, encodedHeaders) | ||
} | ||
|
||
private def buildProxyAddress: Option[InetSocketAddress] = for { | ||
proxyHost <- url.host | ||
proxyPort <- url.port | ||
} yield new InetSocketAddress(proxyHost, proxyPort) | ||
} | ||
|
||
object Proxy { | ||
val empty: Proxy = Proxy(URL.empty) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package zhttp.http | ||
|
||
import zhttp.http.middleware.Auth.Credentials | ||
import zio.test.Assertion.{equalTo, isNone, isNull, isSome} | ||
import zio.test._ | ||
|
||
object ProxySpec extends DefaultRunnableSpec { | ||
private val validUrl = URL.fromString("http://localhost:8123").toOption.getOrElse(URL.empty) | ||
|
||
override def spec = suite("Proxy")( | ||
suite("Authenticated Proxy") { | ||
test("successfully encode valid proxy") { | ||
val username = "unameTest" | ||
val password = "upassTest" | ||
val proxy = Proxy(validUrl, Some(Credentials(username, password))) | ||
val encoded = proxy.encode | ||
|
||
assert(encoded.map(_.username()))(isSome(equalTo(username))) && | ||
assert(encoded.map(_.password()))(isSome(equalTo(password))) && | ||
assert(encoded.map(_.authScheme()))(isSome(equalTo("basic"))) | ||
} + | ||
test("fail to encode invalid proxy") { | ||
val proxy = Proxy(URL.empty) | ||
val encoded = proxy.encode | ||
|
||
assert(encoded.map(_.username()))(isNone) | ||
} | ||
} + suite("Unauthenticated proxy") { | ||
test("successfully encode valid proxy") { | ||
val proxy = Proxy(validUrl) | ||
val encoded = proxy.encode | ||
|
||
assert(encoded)(isSome) && | ||
assert(encoded.map(_.username()))(isSome(isNull)) && | ||
assert(encoded.map(_.password()))(isSome(isNull)) && | ||
assert(encoded.map(_.authScheme()))(isSome(equalTo("none"))) | ||
} | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters