forked from igrigorik/em-http-request
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split connection and request options
- Loading branch information
Showing
7 changed files
with
118 additions
and
105 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
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,57 @@ | ||
class HttpClientOptions | ||
attr_reader :uri, :method, :host, :port, :proxy | ||
attr_reader :headers, :file, :body, :query | ||
attr_reader :keepalive, :redirects, :pass_cookies | ||
|
||
attr_accessor :followed | ||
|
||
def initialize(uri, options, method) | ||
set_uri(uri, options) | ||
|
||
@keepalive = options[:keepalive] || false # default to single request per connection | ||
@redirects = options[:redirects] ||= 0 # default number of redirects to follow | ||
@followed = options[:followed] ||= 0 # keep track of number of followed requests | ||
|
||
@method = method.to_s.upcase | ||
@headers = options[:head] || {} | ||
@proxy = options[:proxy] || {} | ||
@query = options[:query] | ||
|
||
@file = options[:file] | ||
@body = options[:body] | ||
|
||
@pass_cookies = options[:pass_cookies] || false | ||
end | ||
|
||
def follow_redirect?; @followed < @redirects; end | ||
def http_proxy?; @proxy && [nil, :http].include?(@proxy[:type]); end | ||
def ssl?; @uri.scheme == "https" || @uri.port == 443; end | ||
|
||
def set_uri(uri, options) | ||
uri = uri.kind_of?(Addressable::URI) ? uri : Addressable::URI::parse(uri.to_s) | ||
|
||
uri.path = '/' if uri.path.empty? | ||
if path = options.delete(:path) | ||
uri.path = path | ||
end | ||
|
||
@uri = uri | ||
|
||
# Make sure the ports are set as Addressable::URI doesn't | ||
# set the port if it isn't there | ||
if @uri.scheme == "https" | ||
@uri.port ||= 443 | ||
else | ||
@uri.port ||= 80 | ||
end | ||
|
||
if proxy = options[:proxy] | ||
@host = proxy[:host] | ||
@port = proxy[:port] | ||
else | ||
@host = @uri.host | ||
@port = @uri.port | ||
end | ||
|
||
end | ||
end |
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,23 @@ | ||
class HttpConnectionOptions | ||
attr_reader :host, :port, :tls, :proxy | ||
attr_reader :connect_timeout, :inactivity_timeout | ||
|
||
def initialize(uri, options) | ||
@connect_timeout = options[:connect_timeout] || 5 # default connection setup timeout | ||
@inactivity_timeout = options[:inactivity_timeout] ||= 10 # default connection inactivity (post-setup) timeout | ||
|
||
@tls = options[:tls] || options[:ssl] || {} | ||
@proxy = options[:proxy] | ||
|
||
uri = uri.kind_of?(Addressable::URI) ? uri : Addressable::URI::parse(uri.to_s) | ||
uri.port = (uri.scheme == "https" ? (uri.port || 443) : (uri.port || 80)) | ||
|
||
if proxy = options[:proxy] | ||
@host = proxy[:host] | ||
@port = proxy[:port] | ||
else | ||
@host = uri.host | ||
@port = uri.port | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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