Class: UV::HttpEndpoint

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

Overview

CookieJar

Defined Under Namespace

Classes: Connection

Constant Summary collapse

@@defaults =
{
    :path => '/',
    :keepalive => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HttpEndpoint.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/uv-rays/http_endpoint.rb', line 75

def initialize(host, options = {})
    @queue = []
    @parser = Http::Parser.new
    @thread = Libuv::Loop.current || Libuv::Loop.default
    @connection = nil

    @options = @@defaults.merge(options)
    @tls_options = options[:tls_options] || {}
    @inactivity_timeout = options[:inactivity_timeout] || 10000
    @idle_timeout_method = method(:idle_timeout)

    if @inactivity_timeout > 0
        @timer = @thread.timer
    end

    uri = URI.parse host
    @port = uri.port
    @host = uri.host
    @scheme = uri.scheme
    @tls = @scheme == 'https'
    @cookiejar = CookieJar.new
    @middleware = []
end

Instance Attribute Details

#cookiejarObject (readonly)

Returns the value of attribute cookiejar.



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

def cookiejar
  @cookiejar
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#inactivity_timeoutObject (readonly)

Returns the value of attribute inactivity_timeout.



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

def inactivity_timeout
  @inactivity_timeout
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



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

def middleware
  @middleware
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#schemeObject (readonly)

Returns the value of attribute scheme.



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

def scheme
  @scheme
end

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

#tlsObject (readonly)

Returns the value of attribute tls.



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

def tls
  @tls
end

#tls_optionsObject (readonly)

Returns the value of attribute tls_options.



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

def tls_options
  @tls_options
end

Instance Method Details

#cancel_allObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/uv-rays/http_endpoint.rb', line 171

def cancel_all
    @queue.each do |request|
        request.reject(:cancelled)
    end
    if @parser.request
        @parser.request.reject(:cancelled)
        @parser.eof
    end
    @queue = []
    close_connection
end

#connection_closed(request) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/uv-rays/http_endpoint.rb', line 153

def connection_closed(request)
    # We may have closed a previous connection

    if @parser.request && (request.nil? || request == @parser.request)
        @connection = nil
        stop_timer

        @parser.eof
    elsif request.nil? && @parser.request.nil? && @queue.length > 0
        req = @queue.pop
        req.reject :connection_failure
    end
end

#connection_readyObject

Callbacks



144
145
146
147
148
149
150
151
# File 'lib/uv-rays/http_endpoint.rb', line 144

def connection_ready
    if @queue.length > 0
        restart_timer
        next_request
    else
        close_connection
    end
end

#data_received(data) ⇒ Object



166
167
168
169
# File 'lib/uv-rays/http_endpoint.rb', line 166

def data_received(data)
    restart_timer
    close_connection if @parser.received(data)
end

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



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

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

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



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

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

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



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

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

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



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

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

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



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

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

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



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

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

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



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

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

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



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
140
141
# File 'lib/uv-rays/http_endpoint.rb', line 114

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

    # Setup the request with callbacks

    request = Http::Request.new(self, options)
    request.then(proc { |response|
        if response.keep_alive
            restart_timer
        else

            close_connection
        end

        next_request

        response
    }, proc { |err|
        close_connection
        next_request
        ::Libuv::Q.reject(@thread, err)
    })

    @queue.unshift(request)

    next_request
    request
end