File: unix.rb

package info (click to toggle)
ruby-httpx 1.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,816 kB
  • sloc: ruby: 12,209; makefile: 4
file content (71 lines) | stat: -rw-r--r-- 1,541 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

module HTTPX
  class UNIX < TCP
    using URIExtensions

    attr_reader :path

    alias_method :host, :path

    def initialize(origin, path, options)
      @addresses = []
      @hostname = origin.host
      @state = :idle
      @options = options
      @fallback_protocol = @options.fallback_protocol
      if @options.io
        @io = case @options.io
              when Hash
                @options.io[origin.authority]
              else
                @options.io
        end
        raise Error, "Given IO objects do not match the request authority" unless @io

        @path = @io.path
        @keep_open = true
        @state = :connected
      elsif path
        @path = path
      else
        raise Error, "No path given where to store the socket"
      end
      @io ||= build_socket
    end

    def connect
      return unless closed?

      begin
        if @io.closed?
          transition(:idle)
          @io = build_socket
        end
        @io.connect_nonblock(Socket.sockaddr_un(@path))
      rescue Errno::EISCONN
      end
      transition(:connected)
    rescue Errno::EINPROGRESS,
           Errno::EALREADY,
           IO::WaitReadable
    end

    # the path is always explicitly passed, so no point in resolving.
    def addresses?
      true
    end

    # :nocov:
    def inspect
      "#<#{self.class}:#{object_id} @path=#{@path}) @state=#{@state})>"
    end
    # :nocov:

    private

    def build_socket
      Socket.new(Socket::PF_UNIX, :STREAM, 0)
    end
  end
end