Class: NetX::HTTPUnix

Inherits:
Net::HTTP
  • Object
show all
Defined in:
lib/net_x/http_unix.rb

Constant Summary collapse

BufferedIO =
::Net::BufferedIO
UNIX_REGEXP =
%r{^unix://}i

Instance Method Summary collapse

Constructor Details

#initialize(address, port = nil) ⇒ HTTPUnix

Returns a new instance of HTTPUnix.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/net_x/http_unix.rb', line 7

def initialize(address, port=nil)
  super(address, port)
  case address
  when UNIX_REGEXP
    @socket_type = 'unix'
    @address = address.sub(UNIX_REGEXP, '')
    @port = nil
  else
    @socket_type = 'inet'
  end
end

Instance Method Details

#connectObject



19
20
21
22
23
24
25
# File 'lib/net_x/http_unix.rb', line 19

def connect
  if @socket_type == 'unix'
    connect_unix
  else
    super
  end
end

#connect_unixObject

connect_unix is an alternative implementation of Net::HTTP#connect specific to the use case of using a Unix Domain Socket.



30
31
32
33
34
35
36
37
38
39
# File 'lib/net_x/http_unix.rb', line 30

def connect_unix
  D "opening connection to #{conn_address()}..."
  s = timeout(@open_timeout) { UNIXSocket.open(conn_address()) }
  D "opened"
  @socket = BufferedIO.new(s)
  @socket.read_timeout = @read_timeout
  @socket.continue_timeout = @continue_timeout
  @socket.debug_output = @debug_output
  on_connect
end