Class: HttpRequest::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/http-request/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/http-request/connection.rb', line 6

def initialize(url, options={})
  # Connection settings
  @user_agent     = options[:user_agent]    || HttpRequest.config[:user_agent]
  @max_retries    = options[:max_retries]   || HttpRequest.config[:max_retries]
  @timeout        = options[:timeout]       || HttpRequest.config[:timeout]
  @auto_redirect  = options[:auto_redirect] || HttpRequest.config[:auto_redirect]
  @max_redirects  = options[:max_redirects] || HttpRequest.config[:max_redirects]
  @keep_alive     = options[:keep_alive]    || HttpRequest.config[:keep_alive]

  # Let's do this
  @uri = parse_uri(url)
  open
end

Instance Attribute Details

#auto_redirectObject

Returns the value of attribute auto_redirect.



4
5
6
# File 'lib/http-request/connection.rb', line 4

def auto_redirect
  @auto_redirect
end

#httpObject (readonly)

Returns the value of attribute http.



3
4
5
# File 'lib/http-request/connection.rb', line 3

def http
  @http
end

#keep_aliveObject

Returns the value of attribute keep_alive.



4
5
6
# File 'lib/http-request/connection.rb', line 4

def keep_alive
  @keep_alive
end

#max_redirectsObject

Returns the value of attribute max_redirects.



4
5
6
# File 'lib/http-request/connection.rb', line 4

def max_redirects
  @max_redirects
end

#max_retriesObject

Returns the value of attribute max_retries.



4
5
6
# File 'lib/http-request/connection.rb', line 4

def max_retries
  @max_retries
end

#timeoutObject

Returns the value of attribute timeout.



4
5
6
# File 'lib/http-request/connection.rb', line 4

def timeout
  @timeout
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/http-request/connection.rb', line 3

def uri
  @uri
end

#user_agentObject

Returns the value of attribute user_agent.



4
5
6
# File 'lib/http-request/connection.rb', line 4

def user_agent
  @user_agent
end

Instance Method Details

#closeObject



43
44
45
# File 'lib/http-request/connection.rb', line 43

def close
  http.finish if http
end

#closed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/http-request/connection.rb', line 51

def closed?
  !(!http.nil? && http.started?)
end

#openObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/http-request/connection.rb', line 20

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
end

#opened?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/http-request/connection.rb', line 47

def opened?
  !closed?
end

#request(verb, path, headers = {}, body = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
# File 'lib/http-request/connection.rb', line 71

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?

  # Make the HTTP request
  retries = max_retries
  begin
    response = http.request(req)
  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_idObject



60
61
62
# File 'lib/http-request/connection.rb', line 60

def server_id
  uri.normalized_site
end

#socketObject



55
56
57
58
# File 'lib/http-request/connection.rb', line 55

def socket
  return if http.nil?
  http.instance_variable_get(:@socket)
end

#ssl?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/http-request/connection.rb', line 39

def ssl?
  uri.normalized_scheme == "https"
end