Module: GRHttp::Base::HTTPHandler

Extended by:
HTTP
Defined in:
lib/grhttp/http_handler.rb

Overview

The GReactor’s HTTP handler used by the GRHttp.

Class Method Summary collapse

Methods included from HTTP

add_param_to_hash, decode, encode, encode_url, escape, make_utf8!, try_utf8!

Class Method Details

.call(io) ⇒ Object

This method is called by the reactor. By default, this method reads the data from the IO and calls the `#on_message data` method.

This method is called within a lock on the connection (Mutex) - craeful from double locking.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grhttp/http_handler.rb', line 16

def call io
	data = StringIO.new io.read.to_s
	until data.eof?
		if HTTP._parse_http io, data
			request = io[:request]; io[:request] = nil
			response = HTTPResponse.new request
			break if io.io.closed?
			begin
				if request.upgrade?
					WSHandler.http_handshake request, response, (io.params[:upgrade_handler] || NO_HANDLER).call(request, response) if WSHandler.is_valid_request?(request, response)
				else
					ret = (io.params[:http_handler] || NO_HANDLER).call(request, response)
					if ret.is_a?(String) && !response.finished?
						response << ret 
					elsif ret == false
						response.clear && (response.status = 404) && (response << HTTPResponse::STATUS_CODES[404])
					end
					response.try_finish
				end							
			rescue => e
				GReactor.error e
				response = HTTPResponse.new request, 500, {}, HTTPResponse::STATUS_CODES[500]
				response.try_finish
			end
		end
	end
	data.string.clear
	data.close
end