Sha256: 12ac4161907bebe46652c358a375894428e023f66216a0e775ae3998e13244be

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module HTTPX
  class UNIX < TCP
    using URIExtensions

    attr_reader :path

    alias_method :host, :path

    def initialize(origin, addresses, options)
      @addresses = []
      @hostname = origin.host
      @state = :idle
      @options = Options.new(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
      else
        if @options.transport_options
          # :nocov:
          warn ":transport_options is deprecated, use :addresses instead"
          @path = @options.transport_options[:path]
          # :nocov:
        else
          @path = addresses.first
        end
      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

    def expired?
      false
    end

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

    private

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
httpx-0.24.7 lib/httpx/io/unix.rb
httpx-0.24.6 lib/httpx/io/unix.rb
httpx-0.24.5 lib/httpx/io/unix.rb
httpx-0.24.4 lib/httpx/io/unix.rb