Class: HttpRequest::Connection
- Inherits:
-
Object
- Object
- HttpRequest::Connection
- Defined in:
- lib/http-request/connection.rb
Instance Attribute Summary collapse
-
#auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#keep_alive ⇒ Object
Returns the value of attribute keep_alive.
-
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#time_since_last_io ⇒ Object
readonly
Returns the value of attribute time_since_last_io.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #dead? ⇒ Boolean
-
#initialize(url, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #keep_alive? ⇒ Boolean
- #open ⇒ Object
- #opened? ⇒ Boolean
- #request(verb, path, headers = {}, body = nil) ⇒ Object
- #server_id ⇒ Object
- #socket ⇒ Object
- #ssl? ⇒ Boolean
Constructor Details
#initialize(url, options = {}) ⇒ Connection
Returns a new instance of Connection.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/http-request/connection.rb', line 9 def initialize(url, ={}) # Connection settings @user_agent = [:user_agent] || HttpRequest.config[:user_agent] @max_retries = [:max_retries] || HttpRequest.config[:max_retries] @timeout = [:timeout] || HttpRequest.config[:timeout] @auto_redirect = [:auto_redirect] || HttpRequest.config[:auto_redirect] @max_redirects = [:max_redirects] || HttpRequest.config[:max_redirects] @keep_alive = [:keep_alive] || HttpRequest.config[:keep_alive] # Let's do this @uri = parse_uri(url) open end |
Instance Attribute Details
#auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
6 7 8 |
# File 'lib/http-request/connection.rb', line 6 def auto_redirect @auto_redirect end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
5 6 7 |
# File 'lib/http-request/connection.rb', line 5 def http @http end |
#keep_alive ⇒ Object
Returns the value of attribute keep_alive.
6 7 8 |
# File 'lib/http-request/connection.rb', line 6 def keep_alive @keep_alive end |
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
6 7 8 |
# File 'lib/http-request/connection.rb', line 6 def max_redirects @max_redirects end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
6 7 8 |
# File 'lib/http-request/connection.rb', line 6 def max_retries @max_retries end |
#time_since_last_io ⇒ Object (readonly)
Returns the value of attribute time_since_last_io.
5 6 7 |
# File 'lib/http-request/connection.rb', line 5 def time_since_last_io @time_since_last_io end |
#timeout ⇒ Object
Returns the value of attribute timeout.
6 7 8 |
# File 'lib/http-request/connection.rb', line 6 def timeout @timeout end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
5 6 7 |
# File 'lib/http-request/connection.rb', line 5 def uri @uri end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
6 7 8 |
# File 'lib/http-request/connection.rb', line 6 def user_agent @user_agent end |
Instance Method Details
#close ⇒ Object
48 49 50 |
# File 'lib/http-request/connection.rb', line 48 def close http.finish if http end |
#closed? ⇒ Boolean
56 57 58 |
# File 'lib/http-request/connection.rb', line 56 def closed? !(!http.nil? && http.started?) end |
#dead? ⇒ Boolean
60 61 62 63 |
# File 'lib/http-request/connection.rb', line 60 def dead? return false if !opened? # Can't be dead if it hasn't strated return !keep_alive? || socket.closed? end |
#keep_alive? ⇒ Boolean
65 66 67 68 69 70 71 |
# File 'lib/http-request/connection.rb', line 65 def keep_alive? if keep_alive > 0 && time_since_last_io (Time.now.to_i - time_since_last_io) < keep_alive else false end end |
#open ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/http-request/connection.rb', line 23 def open uri.port ||= 443 if ssl? @http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = timeout http.read_timeout = timeout if ssl? http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.start if socket && socket.respond_to?(:io) && Socket.const_defined?(:TCP_NODELAY) socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true) end @time_since_last_io = Time.now.to_i end |
#opened? ⇒ Boolean
52 53 54 |
# File 'lib/http-request/connection.rb', line 52 def opened? !closed? end |
#request(verb, path, headers = {}, body = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/http-request/connection.rb', line 89 def request(verb, path, headers={}, body=nil) if path =~ /^http/i new_uri = parse_uri(path) if new_uri.origin != uri.origin # TODO.. get an error class for this.. raise "Ummm... you're trying to talk to another server" end @uri = new_uri else # Update the path and query # Note: also normalize the path in case its malformed.. dum dums uri.join! parse_uri(path.to_s.gsub('//','/')).request_uri end # Open the connection if its closed or hasn't been started.. open if closed? # Default headers headers['User-Agent'] ||= user_agent headers['Connection'] ||= 'keep-alive' if keep_alive? headers['Content-Length'] ||= body.bytesize.to_s if body headers['Accept-Encoding'] ||= 'gzip' # TODO: add deflate support # Build request req_klass = instance_eval("Net::HTTP::"+verb.to_s.capitalize) req = req_klass.new(uri.request_uri, headers) req.body = body if !body.nil? && !body.empty? # do auth if user and password provided req.basic_auth uri.user, uri.password if uri.user && uri.password # Make the HTTP request retries = max_retries begin response = http.request(req) @time_since_last_io = Time.now.to_i rescue EOFError, Errno::EPIPE # Something happened to our connection, lets try this again if retries > 0 retries -= 1 open retry end end # Let's make sure we close the connection if we don't intend to # have persistent connections.. close if !keep_alive? response end |
#server_id ⇒ Object
78 79 80 |
# File 'lib/http-request/connection.rb', line 78 def server_id uri.normalized_site end |
#socket ⇒ Object
73 74 75 76 |
# File 'lib/http-request/connection.rb', line 73 def socket return if http.nil? http.instance_variable_get(:@socket) end |
#ssl? ⇒ Boolean
44 45 46 |
# File 'lib/http-request/connection.rb', line 44 def ssl? uri.normalized_scheme == "https" end |