Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for https://github.com/crystal-community/cossack/issues/16 #4

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/cossack/connection/http_connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ module Cossack
# A connection to perform a real HTTP request using the standard library.
class HTTPConnection < Connection
def call(request : Request) : Response
client = HTTP::Client.new(request.uri)
uri = request.uri
scheme = uri.scheme
uri_host = uri.host
client = if uri_host.nil?
HTTP::Client.new(uri)
else
HTTP::Client.new(uri_host, uri.port, scheme == "https")
end

client.connect_timeout = request.options.connect_timeout
client.read_timeout = request.options.read_timeout

http_response = client.exec(request.method, request.uri.path, request.headers, request.body)
uri_string = uri.to_s
base_url = uri.port.nil? ? "#{scheme}://#{uri_host}" : "#{scheme}://#{uri_host}:#{uri.port}"
path = uri_string.starts_with?(base_url) ? uri_string[base_url.size, uri_string.size - base_url.size] : uri_string
http_response = client.exec(request.method, path, request.headers, request.body)
Response.new(http_response.status_code, http_response.headers, http_response.body)
rescue err : IO::TimeoutError
raise TimeoutError.new(err.message, cause: err)
Expand Down