Class: HTTPray::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/httpray.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, timeout = 1, ssl_context = nil, retry_count = 1) ⇒ Connection

Returns a new instance of Connection.



17
18
19
20
21
22
23
24
# File 'lib/httpray.rb', line 17

def initialize(host, port, timeout = 1, ssl_context = nil, retry_count = 1)
  @host = host
  @port = port
  @timeout = timeout
  @ssl_context = ssl_context
  @retry_count = retry_count
  @socket = connect
end

Instance Method Details

#connectObject



102
103
104
105
# File 'lib/httpray.rb', line 102

def connect
  socket, _ = connect2
  socket
end

#connect2Object

private



66
67
68
69
70
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
# File 'lib/httpray.rb', line 66

def connect2
  address = Socket.getaddrinfo(@host, nil, Socket::AF_INET).first[3]
  socket_address = Socket.pack_sockaddr_in(@port, address)

  socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

  expire_time = Time.now + @timeout
  begin
    raise Timeout if Time.now > expire_time
    socket.connect_nonblock(socket_address)
  rescue IO::WaitReadable, IO::WaitWritable
    select_timeout = expire_time - Time.now
    select_timeout = 0 if select_timeout < 0
    IO.select([socket], [socket], [socket], select_timeout)
    retry
  end

  original_socket = socket
  if @ssl_context
    socket = OpenSSL::SSL::SSLSocket.new(socket, @ssl_context)
    socket.hostname = @host
    socket.sync_close = true
    begin
      raise Timeout if Time.now > expire_time
      socket.connect_nonblock
    rescue IO::WaitReadable, IO::WaitWritable
      select_timeout = expire_time - Time.now
      select_timeout = 0 if select_timeout < 0
      IO.select([socket.io], [socket.io], [socket.io], select_timeout)
      retry
    end
  end
  return socket, original_socket
end

#request(*args) {|socket| ... } ⇒ Object

Yields:



59
60
61
62
# File 'lib/httpray.rb', line 59

def request(*args)
  socket = request!(*args)
  yield(socket) if block_given?
end

#request!(method, uri, headers = {}, body = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/httpray.rb', line 32

def request!(method, uri, headers = {}, body = nil)
  tries ||= 0
  begin
    IO.select([@socket], [@socket], [@socket], @timeout) if @socket
  rescue; end
  @socket = connect unless @socket && !@socket.closed?
  socket = @socket
  uri = URI.parse(uri) unless URI === uri

  headers = DEFAULT_HEADERS.merge(headers).merge("Host" => uri.host)
  headers["Content-Length"] = body.bytesize if body

  socket.write_nonblock "#{method} #{uri.request_uri} HTTP/1.0\r\n"
  headers.each do |header, value|
    socket.write_nonblock "#{header}: #{value}\r\n"
  end
  socket.write_nonblock "\r\n"
  socket.write_nonblock body if body
  socket
rescue
  @socket.close
  if tries < @retry_count
    tries += 1
    retry
  end
end

#socketObject

public



28
29
30
# File 'lib/httpray.rb', line 28

def socket
  @socket
end