Class: InternetHakai::RevHttpConnection
- Inherits:
-
Rev::HttpClient
- Object
- Rev::HttpClient
- InternetHakai::RevHttpConnection
- Defined in:
- lib/internethakai/hakairev/http_client.rb
Direct Known Subclasses
Constant Summary collapse
- @@_cache =
def parse_header(header)
return false if @data.empty? key = nil if idx = @data.to_str.index("\r\n\r\n") ctx = idx + 4 key = @data.to_str[0, ctx] #key = Digest::MD5::digest(@data.to_str[0, ctx]) if obj = searchcache(key) @parser_nbytes = 0 @data.read(ctx) @parser.reset @response_header = obj return true end end begin @parser_nbytes = @parser.execute(header, @data.to_str, @parser_nbytes) rescue Rev::HttpClientParserError on_error "invalid HTTP format, parsing fails" @state = :invalid end return false unless @parser.finished? if key insertcache(key, header) end # Clear parsed data from the buffer @data.read(@parser_nbytes) @parser.reset @parser_nbytes = 0 true end
{}
- @@_cache_keys =
[]
- @@_cache_max =
200
Instance Attribute Summary collapse
-
#busy ⇒ Object
readonly
Returns the value of attribute busy.
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
Instance Method Summary collapse
- #attach(loop) ⇒ Object
- #attached? ⇒ Boolean
- #close ⇒ Object
- #free ⇒ Object
- #init ⇒ Object
-
#initialize(args) ⇒ RevHttpConnection
constructor
A new instance of RevHttpConnection.
- #insertcache(key, data) ⇒ Object
- #on_body_data(data) ⇒ Object
- #on_connect_failed ⇒ Object
- #on_error(reason = 'error') ⇒ Object
- #on_failure ⇒ Object
- #on_read(s) ⇒ Object
- #on_readable ⇒ Object
- #on_request_complete ⇒ Object
- #on_resolve_failed ⇒ Object
- #on_writable ⇒ Object
- #reconnect ⇒ Object
- #searchcache(key) ⇒ Object
- #send_request ⇒ Object
- #to_response_object ⇒ Object
Methods inherited from Rev::HttpClient
Constructor Details
#initialize(args) ⇒ RevHttpConnection
Returns a new instance of RevHttpConnection.
3 4 5 6 7 |
# File 'lib/internethakai/hakairev/http_client.rb', line 3 def initialize args @remote_addr, @remote_port = args soc = TCPConnectSocket::new(::Socket::AF_INET, @remote_addr, @remote_port) super(soc) end |
Instance Attribute Details
#busy ⇒ Object (readonly)
Returns the value of attribute busy.
18 19 20 |
# File 'lib/internethakai/hakairev/http_client.rb', line 18 def busy @busy end |
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
18 19 20 |
# File 'lib/internethakai/hakairev/http_client.rb', line 18 def connected @connected end |
Instance Method Details
#attach(loop) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/internethakai/hakairev/http_client.rb', line 137 def attach loop @loop = loop unless @loop if @_connector @_connector.attach loop elsif @_read_watcher @_read_watcher.attach loop elsif @_resolver @_resolver.attache loop else return end end |
#attached? ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/internethakai/hakairev/http_client.rb', line 86 def attached? if @_connector @_connector.attached? elsif @_read_watcher @_read_watcher.attached? elsif @_resolver @_resolver.attached else super end end |
#close ⇒ Object
58 59 60 |
# File 'lib/internethakai/hakairev/http_client.rb', line 58 def close super end |
#free ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/internethakai/hakairev/http_client.rb', line 222 def free @content = '' #@response_header = nil #@chunk_header = nil @response_time = nil @_write_buffer.clear #@_write_buffer = nil @_read_watcher.detach if @_read_watcher.attached? #@_read_watcher = nil @_write_watcher.detach if @_write_watcher.attached? #@_write_watcher = nil @data.clear #@data = nil #@path = nil #@method = nil #@options = nil #@starttime = nil #@_io = nil @writable_flag = nil @send_flag = nil @connected = false end |
#init ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/internethakai/hakairev/http_client.rb', line 8 def init @content = '' @writable_flag = false @send_flag = false @busy = false @on_request_complete = nil @requested = false @response_object = InternetHakai::ResponseObject::new self end |
#insertcache(key, data) ⇒ Object
208 209 210 211 212 213 |
# File 'lib/internethakai/hakairev/http_client.rb', line 208 def insertcache key, data @@_cache[key] = data.dup if @@_cache_keys.size > @@_cache_max @@_cache.delete(@@_cache_keys.shift) end end |
#on_body_data(data) ⇒ Object
97 98 99 |
# File 'lib/internethakai/hakairev/http_client.rb', line 97 def on_body_data(data) @content += data end |
#on_connect_failed ⇒ Object
104 105 106 107 |
# File 'lib/internethakai/hakairev/http_client.rb', line 104 def on_connect_failed @exception = SocketError::new('connection error') on_error('') end |
#on_error(reason = 'error') ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/internethakai/hakairev/http_client.rb', line 116 def on_error(reason='error') unless @starttime.nil? @response_time = Time::now - @starttime else @response_time = 0 end close if @_io and !@_io.closed? @requested = false @exception = reason if @exception.nil? @busy = false end |
#on_failure ⇒ Object
112 113 114 115 |
# File 'lib/internethakai/hakairev/http_client.rb', line 112 def on_failure @exception = SocketError::new on_error('') end |
#on_read(s) ⇒ Object
19 20 21 |
# File 'lib/internethakai/hakairev/http_client.rb', line 19 def on_read s super end |
#on_readable ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/internethakai/hakairev/http_client.rb', line 22 def on_readable begin on_read @_io.read_nonblock(INPUT_SIZE) rescue Errno::EAGAIN rescue Errno::ECONNRESET, EOFError rescue IOError => e @exception = e on_error('io error') rescue Exception => e @exception = e on_error('io error') end end |
#on_request_complete ⇒ Object
100 101 102 103 |
# File 'lib/internethakai/hakairev/http_client.rb', line 100 def on_request_complete @response_time = Time::now - @starttime self.close end |
#on_resolve_failed ⇒ Object
108 109 110 111 |
# File 'lib/internethakai/hakairev/http_client.rb', line 108 def on_resolve_failed @exception = SocketError::new('dns error') on_error('') end |
#on_writable ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/internethakai/hakairev/http_client.rb', line 61 def on_writable @writable_flag = true unless @writable_flag # writable + send 両方そろったら開始時間 if @send_flag && !@starttime @starttime = Time::now end begin @_write_buffer.write_to(@_io) rescue Errno::EPIPE, Errno::ECONNRESET => e rescue IOError => e rescue Exception => e @exception = e on_error('io error') end if @_write_buffer.empty? disable_write_watcher on_write_complete end end |
#reconnect ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/internethakai/hakairev/http_client.rb', line 35 def reconnect @requested = false @busy = false free socket = TCPConnectSocket::new(::Socket::AF_INET, @remote_addr, @remote_port) @_io = socket #@_write_buffer ||= ::IO::Buffer.new @_read_watcher = Watcher.new(socket, self, :r) @_write_watcher = Watcher.new(socket, self, :w) @address_family, @remote_port, @remote_host, @remote_addr = socket.peeraddr @state = :response_header #@data = ::IO::Buffer.new @response_header = Rev::HttpResponseHeader.new @chunk_header = Rev::HttpChunkHeader.new #initialize(socket) @_connector = Connector::new(self, socket) @_connector.attach(@loop) end |
#searchcache(key) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/internethakai/hakairev/http_client.rb', line 214 def searchcache key if @@_cache.has_key?(key) @@_cache_keys << key @@_cache[key] else nil end end |
#send_request ⇒ Object
81 82 83 84 85 |
# File 'lib/internethakai/hakairev/http_client.rb', line 81 def send_request @send_flag = true @starttime = Time::now super end |
#to_response_object ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/internethakai/hakairev/http_client.rb', line 127 def to_response_object r = @response_object r.body = @content.to_s r.status = @response_header.http_status.to_i r.content_type = @response_header["CONTENT_TYPE"].to_s r.location = @response_header["LOCATION"] r. = @response_header['SET_COOKIE'] r.time = @response_time r end |