File: unix_socket.rb

package info (click to toggle)
ruby-excon 0.112.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 7,855; makefile: 5
file content (42 lines) | stat: -rw-r--r-- 1,242 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true
module Excon
  class UnixSocket < Excon::Socket

    private

    def connect
      @socket  = ::Socket.new(::Socket::AF_UNIX, ::Socket::SOCK_STREAM, 0)
      # If a Unix proxy was specified, the :path option will be set for it,
      # otherwise fall back to the :socket option.
      proxy_path = @data[:proxy] ? @data[:proxy][:path] : nil
      sockaddr = ::Socket.sockaddr_un(proxy_path || @data[:socket])
      if @nonblock
        begin
          @socket.connect_nonblock(sockaddr)
        rescue Errno::EINPROGRESS
          unless IO.select(nil, [@socket], nil, @data[:connect_timeout])
            raise(Excon::Errors::Timeout.new("connect timeout reached"))
          end
          begin
            @socket.connect_nonblock(sockaddr)
          rescue Errno::EISCONN
            0 # same return as connect_nonblock success
          end
        end
      else
        begin
          Timeout.timeout(@data[:connect_timeout]) do
            @socket.connect(sockaddr)
          end
        rescue Timeout::Error
          raise Excon::Errors::Timeout.new('connect timeout reached')
        end
      end

    rescue => error
      @socket.close rescue nil if @socket
      raise error
    end

  end
end