Class: UV::HttpEndpoint

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

Constant Summary collapse

TRANSFER_ENCODING =
"TRANSFER_ENCODING".freeze
CONTENT_ENCODING =
"CONTENT_ENCODING".freeze
CONTENT_LENGTH =
"CONTENT_LENGTH".freeze
CONTENT_TYPE =
"CONTENT_TYPE".freeze
LAST_MODIFIED =
"LAST_MODIFIED".freeze
KEEP_ALIVE =
"CONNECTION".freeze
LOCATION =
"LOCATION".freeze
HOST =
"HOST".freeze
ETAG =
"ETAG".freeze
CRLF =
"\r\n".freeze
HTTPS =
"https://".freeze
HTTP =
"http://".freeze
@@defaults =
{
    :path => '/',
    :keepalive => true
}

Instance Attribute Summary collapse

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?, #post_init, #resume

Constructor Details

#initialize(uri, options = {}) ⇒ HttpEndpoint

Returns a new instance of HttpEndpoint.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/uv-rays/http_endpoint.rb', line 55

def initialize(uri, options = {})
    @inactivity_timeout  = options[:inactivity_timeout] ||= 10000   # default connection inactivity (post-setup) timeout
    @ntlm_creds = options[:ntlm]

    uri = uri.kind_of?(Addressable::URI) ? uri : Addressable::URI::parse(uri.to_s)
    @https = uri.scheme == "https"
    uri.port ||= (@https ? 443 : 80)
    @scheme = @https ? HTTPS : HTTP


    @loop = Libuv::Loop.current || Libuv::Loop.default
    @host = uri.host
    @port = uri.port
    #@transport = @loop.tcp

    # State flags
    @closed = true
    @closing = false
    @connecting = false

    # Current requests
    @pending_requests = []
    @staging_request = nil
    @waiting_response = nil
    @cookiejar = CookieJar.new

    # Callback methods
    @idle_timeout_method = method(:idle_timeout)

    # Manages the tokenising of response from the input stream
    @response = Http::Response.new

    # Timeout timer
    if @inactivity_timeout > 0
        @timer = @loop.timer
    end
end

Instance Attribute Details

#cookiejarObject (readonly)

Returns the value of attribute cookiejar.



52
53
54
# File 'lib/uv-rays/http_endpoint.rb', line 52

def cookiejar
  @cookiejar
end

#hostObject (readonly)

Returns the value of attribute host.



52
53
54
# File 'lib/uv-rays/http_endpoint.rb', line 52

def host
  @host
end

#inactivity_timeoutObject (readonly)

Returns the value of attribute inactivity_timeout.



53
54
55
# File 'lib/uv-rays/http_endpoint.rb', line 53

def inactivity_timeout
  @inactivity_timeout
end

#loopObject (readonly)

Returns the value of attribute loop.



52
53
54
# File 'lib/uv-rays/http_endpoint.rb', line 52

def loop
  @loop
end

#portObject (readonly)

Returns the value of attribute port.



52
53
54
# File 'lib/uv-rays/http_endpoint.rb', line 52

def port
  @port
end

#schemeObject (readonly)

Returns the value of attribute scheme.



52
53
54
# File 'lib/uv-rays/http_endpoint.rb', line 52

def scheme
  @scheme
end

#using_tlsObject (readonly)

Returns the value of attribute using_tls.



52
53
54
# File 'lib/uv-rays/http_endpoint.rb', line 52

def using_tls
  @using_tls
end

Instance Method Details

#close_connection(after_writing = false) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/uv-rays/http_endpoint.rb', line 248

def close_connection(after_writing = false)
    stop_timer
    reqs = @pending_requests
    @pending_requests.clear
    reqs.each do |request|
        request.reject(:close_connection)
    end
    super(after_writing) if @transport
end

#delete(options = {}, &blk) ⇒ Object



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

def delete   options = {}, &blk;  request(:delete,  options, &blk); end

#get(options = {}, &blk) ⇒ Object



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

def get      options = {}, &blk;  request(:get,     options, &blk); end

#head(options = {}, &blk) ⇒ Object



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

def head     options = {}, &blk;  request(:head,    options, &blk); end

#middlewareObject



233
234
235
236
# File 'lib/uv-rays/http_endpoint.rb', line 233

def middleware
    # TODO:: allow for middle ware
    []
end

#next_requestObject



142
143
144
145
# File 'lib/uv-rays/http_endpoint.rb', line 142

def next_request
    @staging_request = @pending_requests.shift
    process_request unless @staging_request.nil?
end

#ntlm_auth_header(challenge = nil) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/uv-rays/http_endpoint.rb', line 258

def ntlm_auth_header(challenge = nil)
    if @ntlm_auth && challenge.nil?
        return @ntlm_auth
    elsif challenge
        scheme, param_str = parse_ntlm_challenge_header(challenge)
        if param_str.nil?
            @ntlm_auth = nil
            return ntlm_auth_header(creds)
        else
            t2 = Net::NTLM::Message.decode64(param_str)
            t3 = t2.response(@ntlm_creds, ntlmv2: true)
            @ntlm_auth = "NTLM #{t3.encode64}"
            return @ntlm_auth
        end
    else
        domain = @ntlm_creds[:domain]
        t1 = Net::NTLM::Message::Type1.new()
        t1.domain = domain if domain
        @ntlm_auth = "NTLM #{t1.encode64}"
        return @ntlm_auth
    end
end

#on_closeObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/uv-rays/http_endpoint.rb', line 179

def on_close
    @closed = true
    clear_staging = @connecting == @staging_request
    @connecting = false
    stop_timer

    # On close may be called before on data
    @loop.next_tick do
        if @closing
            @closing = false
            @connecting = false

            if @staging_request
                process_request
            else
                next_request
            end
        else
            if clear_staging
                @staging_request.reject(:connection_refused)
            elsif @waiting_response
                # Flush any processing request
                @response.eof if @response.request

                # Reject any requests waiting on a response
                @waiting_response.reject(:disconnected)
            elsif @staging_request
                # Try reconnect
                process_request
            end
        end
    end
end

#on_connect(transport) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/uv-rays/http_endpoint.rb', line 159

def on_connect(transport)
    @connecting = false
    @closed = false

    # start tls if connection is encrypted
    use_tls() if @https

    # Update timeouts
    stop_timer
    if @inactivity_timeout > 0
        @timer.progress @idle_timeout_method
        @timer.start @inactivity_timeout
    end

    # Kick off pending requests
    @response.reset!
    try_send  # we only connect if there is a request waiting
end

#on_read(data, *args) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/uv-rays/http_endpoint.rb', line 238

def on_read(data, *args)
    @timer.again if @inactivity_timeout > 0

    # returns true on error
    # Response rejects the request
    if @response.receive(data)
        @transport.close
    end
end

#options(options = {}, &blk) ⇒ Object



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

def options  options = {}, &blk;  request(:options, options, &blk); end

#patch(options = {}, &blk) ⇒ Object



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

def patch    options = {}, &blk;  request(:patch,   options, &blk); end

#post(options = {}, &blk) ⇒ Object



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

def post     options = {}, &blk;  request(:post,    options, &blk); end

#process_requestObject



147
148
149
150
151
152
153
154
155
156
# File 'lib/uv-rays/http_endpoint.rb', line 147

def process_request
    if @closed && !@connecting
        @transport = @loop.tcp

        @connecting = @staging_request
        ::UV.try_connect(@transport, self, @host, @port)
    elsif !@closing
        try_send
    end
end

#put(options = {}, &blk) ⇒ Object



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

def put      options = {}, &blk;  request(:put,     options, &blk); end

#request(method, options = {}, &blk) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/uv-rays/http_endpoint.rb', line 103

def request(method, options = {}, &blk)
    options = @@defaults.merge(options)
    options[:method] = method

    # Setup the request with callbacks
    request = Http::Request.new(self, options, @ntlm_creds)
    request.then(proc { |result|
        @waiting_response = nil

        if @closed || result[:headers].keep_alive
            next_request
        else
            @closing = true
            @transport.close
        end

        result
    }, proc { |err|
        @waiting_response = nil
        next_request

        ::Libuv::Q.reject(@loop, err)
    })

    ##
    # TODO:: Add response middleware here
    request.then blk if blk

    # Add to pending requests and schedule using the breakpoint
    @pending_requests << request
    if !@waiting_response && !@staging_request
        next_request
    end

    # return the request
    request
end

#try_sendObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/uv-rays/http_endpoint.rb', line 213

def try_send
    @waiting_response = @staging_request
    @response.request = @staging_request
    @staging_request = nil

    if @ntlm_creds
        opts = @waiting_response.options
        opts[:headers] ||= {}
        opts = opts[:headers]
        opts[:Authorization] = ntlm_auth_header
    end

    @timer.again if @inactivity_timeout > 0
    @waiting_response.execute(@transport, proc { |err|
        @transport.close
        @waiting_response.reject(err)
    })
end