-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
3 changed files
with
64 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Changelog | ||
|
||
## 0.8.1 | ||
|
||
* Small refactoring | ||
## 0.8.0 | ||
|
||
* Require Crystal >= 0.36.0 | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: http_proxy | ||
version: 0.8.0 | ||
version: 0.8.1 | ||
|
||
authors: | ||
- Theo Li <[email protected]> | ||
|
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,60 @@ | ||
require "http" | ||
|
||
module HTTP | ||
class Client | ||
getter proxy : Bool = false | ||
|
||
def set_proxy(proxy : HTTP::Proxy::Client?) | ||
return unless proxy | ||
|
||
begin | ||
@io = proxy.open( | ||
host: @host, | ||
port: @port, | ||
tls: @tls, | ||
dns_timeout: @dns_timeout, | ||
connect_timeout: @connect_timeout, | ||
read_timeout: @read_timeout | ||
) | ||
rescue ex : IO::Error | ||
raise IO::Error.new("Failed to open TCP connection to #{@host}:#{@port} (#{ex.message})") | ||
end | ||
|
||
@proxy = true | ||
|
||
if proxy.username && proxy.password | ||
proxy_basic_auth(proxy.username, proxy.password) | ||
end | ||
end | ||
|
||
# True if requests for this connection will be proxied | ||
def proxy? | ||
@proxy | ||
end | ||
|
||
private def new_request(method, path, headers, body : BodyType) | ||
# Use full URL instead of path when using HTTP proxy | ||
if proxy? && !@tls | ||
path = "http://#{host_with_port}#{path}" | ||
end | ||
|
||
HTTP::Request.new(method, path, headers, body) | ||
end | ||
|
||
private def host_with_port | ||
host = @host | ||
host = "[#{host}]" if host.includes?(":") | ||
default_port = @tls ? URI.default_port("https") : URI.default_port("http") | ||
default_port == @port ? host : "#{host}:#{@port}" | ||
end | ||
|
||
# Configures this client to perform proxy basic authentication in every | ||
# request. | ||
private def proxy_basic_auth(username : String?, password : String?) | ||
header = "Basic #{Base64.strict_encode("#{username}:#{password}")}" | ||
before_request do |request| | ||
request.headers["Proxy-Authorization"] = header | ||
end | ||
end | ||
end | ||
end |