Class: UV::HttpEndpoint::Connection

Inherits:
OutboundConnection show all
Defined in:
lib/uv-rays/http_endpoint.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#using_tls

Instance Method Summary collapse

Methods inherited from OutboundConnection

#reconnect, #use_tls

Methods inherited from TcpConnection

#keepalive, #stream_file, #write

Methods inherited from Connection

#pause, #paused?, #resume

Constructor Details

#initialize(host, port, tls, proxy, client) ⇒ Connection

Returns a new instance of Connection.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/uv-rays/http_endpoint.rb', line 51

def initialize(host, port, tls, proxy, client)
    @target_host = host
    @client = client
    @request = nil

    if proxy
        super(proxy[:host], proxy[:port])
        connect_send_handshake(host, port, proxy) if tls
    else
        super(host, port)
        start_tls if tls
    end
end

Instance Attribute Details

#reason ⇒ Object

Returns the value of attribute reason.



81
82
83
# File 'lib/uv-rays/http_endpoint.rb', line 81

def reason
  @reason
end

#request ⇒ Object

Returns the value of attribute request.



81
82
83
# File 'lib/uv-rays/http_endpoint.rb', line 81

def request
  @request
end

Instance Method Details

#close_connection(request = nil) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/uv-rays/http_endpoint.rb', line 113

def close_connection(request = nil)
    if request.is_a? Http::Request
        @request = request
        super(:after_writing)
    else
        super(request)
    end
end

#connect_send_handshake(target_host, target_port, proxy) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/uv-rays/http_endpoint.rb', line 70

def connect_send_handshake(target_host, target_port, proxy)
    @negotiating = true
    header = String.new("CONNECT #{target_host}:#{target_port} HTTP/1.0\r\n")
    if proxy[:username] || proxy[:password]
        encoded_credentials = Base64.strict_encode64([proxy[:username], proxy[:password]].join(":"))
        header << "Proxy-Authorization: Basic #{encoded_credentials}\r\n"
    end
    header << "\r\n"
    write(header)
end

#on_close ⇒ Object



105
106
107
108
109
110
111
# File 'lib/uv-rays/http_endpoint.rb', line 105

def on_close
    @client.connection_closed(@request, @reason)
ensure
    @request = nil
    @client = nil
    @reason = nil
end

#on_connect(transport) ⇒ Object



101
102
103
# File 'lib/uv-rays/http_endpoint.rb', line 101

def on_connect(transport)
    @client.connection_ready unless @negotiating
end

#on_read(data, *args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/uv-rays/http_endpoint.rb', line 83

def on_read(data, *args)
    if @negotiating
        @negotiating = false
        if data =~ %r{\AHTTP/1\.[01] 200 .*\r\n\r\n}m
            start_tls
            @client.connection_ready
        else
            @reason = "Unexpected response from proxy: #{data}"
            close_connection
        end
    else
        @client.data_received(data)
    end
end

#post_init(*args) ⇒ Object



98
99
# File 'lib/uv-rays/http_endpoint.rb', line 98

def post_init(*args)
end

#start_tls ⇒ Object



65
66
67
68
# File 'lib/uv-rays/http_endpoint.rb', line 65

def start_tls
    opts = {host_name: @target_host}.merge(@client.tls_options)
    use_tls(opts)
end