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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/uv-rays/http_endpoint.rb', line 57

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

    if proxy
        super(proxy[:host], proxy[:port])
        if tls
            @negotiating = true
            @proxy = proxy
            @connect_host = host
            @connect_port = port
        end
    else
        super(host, port)
        start_tls if tls
    end
end

Instance Attribute Details

#reasonObject

Returns the value of attribute reason.



91
92
93
# File 'lib/uv-rays/http_endpoint.rb', line 91

def reason
  @reason
end

#requestObject

Returns the value of attribute request.



91
92
93
# File 'lib/uv-rays/http_endpoint.rb', line 91

def request
  @request
end

Instance Method Details

#close_connection(request = nil) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/uv-rays/http_endpoint.rb', line 127

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



81
82
83
84
85
86
87
88
89
# File 'lib/uv-rays/http_endpoint.rb', line 81

def connect_send_handshake(target_host, target_port, proxy)
    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_closeObject



119
120
121
122
123
124
125
# File 'lib/uv-rays/http_endpoint.rb', line 119

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

#on_connect(transport) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/uv-rays/http_endpoint.rb', line 111

def on_connect(transport)
    if @negotiating
        connect_send_handshake(@connect_host, @connect_port, @proxy)
    else
        @client.connection_ready
    end
end

#on_read(data, *args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/uv-rays/http_endpoint.rb', line 93

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



108
109
# File 'lib/uv-rays/http_endpoint.rb', line 108

def post_init(*args)
end

#start_tlsObject



76
77
78
79
# File 'lib/uv-rays/http_endpoint.rb', line 76

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