Module: GRHttp

Defined in:
lib/grhttp.rb,
lib/grhttp/api.rb,
lib/grhttp/version.rb,
lib/grhttp/session.rb,
lib/grhttp/ws_event.rb,
lib/grhttp/ws_client.rb,
lib/grhttp/ws_handler.rb,
lib/grhttp/http_request.rb,
lib/grhttp/http_helpers.rb,
lib/grhttp/http_handler.rb,
lib/grhttp/rack_support.rb,
lib/grhttp/http_cookies.rb,
lib/grhttp/http_response.rb

Overview

please read the README file for an introduction to GRHttp.

here's the famous Hello World to get you thinking:

require 'grhttp'

GRHttp.start {   GRHttp.listen {|request, response| 'Hello World!' }      }

Defined Under Namespace

Modules: Base, HTTP, SessionManager Classes: HTTPRequest, HTTPResponse, WSClient, WSEvent

Constant Summary collapse

VERSION =
"0.0.23"

Class Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Defers any missing methods to the GReactor Library.



42
43
44
45
# File 'lib/grhttp/api.rb', line 42

def method_missing name, *args, &block
	return super unless REACTOR_METHODS.include? name
	GReactor.send name, *args, &block
end

Class Method Details

.listen(params = {}, &block) ⇒ Object

Opens a web-service (HTTP and Websocket) on the specified port.

Accepts a parameter Hash and an optional block.

The following parameters are recognized:

port

The port number to listen to. Defaults to 3000 and increments by 1 with every portless listen call (3000, 3001, 3002…).

timeout

The timeout for the connection (Keep-Alive HTTP/1.1 is the default behavior). Defaults to 5 seconds.

http_handler

The object that will handle the connection. Should answer to `#call request, response`.

upgrade_handler

The object that will handle WebSocket upgrade requests. Should answer to `#call request, response`, should return the new WebSocket Handler while setting any cookies for the response or false (ro refuse the request).

handler

A unified handler. Sets both http_handler and upgrade_handler to the value of handler unless they are specifically set.

ssl

Set this parameter to true to listen and react to SSL connections with a self signed certificate.

ssl_key + ssl_cert

Set both these parameters to listen to an SSL connection with a registered certificate. Falls back to a self registered certificate if one of the two is missing.



21
22
23
24
25
26
27
# File 'lib/grhttp/api.rb', line 21

def listen params = {}, &block
	params[:http_handler] ||= params[:handler] ||= block
	params[:upgrade_handler] ||= params[:handler]
	params[:handler] = Base::HTTPHandler
	params[:timeout] ||= 5
	GReactor.listen params
end

.method_missing(name, *args, &block) ⇒ Object

Defers any missing methods to the GReactor Library.



42
43
44
45
# File 'lib/grhttp/api.rb', line 42

def method_missing name, *args, &block
	return super unless REACTOR_METHODS.include? name
	GReactor.send name, *args, &block
end

.respond_to_missing?(name, include_private = false) ⇒ Boolean

Defers any missing methods to the GReactor Library.



47
48
49
# File 'lib/grhttp/api.rb', line 47

def respond_to_missing?(name, include_private = false)
	REACTOR_METHODS.include?(name) or super
end

.session_tokenObject

Returns the server's session's token name



66
67
68
# File 'lib/grhttp/api.rb', line 66

def session_token
	@session_token ||= SecureRandom.uuid
end

.session_token=(value) ⇒ Object

Sets the server's session's token name



70
71
72
# File 'lib/grhttp/api.rb', line 70

def session_token= value
	@session_token = value
end

.ws_connect(url, options = {}, &block) ⇒ Object

Connects to a websocket as a websocket client and returns the connected client object or raises an exception.

The method will block until eaither a connection is established or the timeout (defaults to 5 seconds) had been reached.

It's possible to use this method within a GReactor.run_async for asynchronous handling.

this is actually a shortcut for GRHttp::WSClient.connect.



36
37
38
# File 'lib/grhttp/api.rb', line 36

def ws_connect url, options={}, &block
	GRHttp::WSClient.connect url, options={}, &block
end

.ws_message_size_limitObject

Gets the message byte size limit for a Websocket message. Defaults to 0 (no limit)



61
62
63
# File 'lib/grhttp/api.rb', line 61

def ws_message_size_limit
	Base::WSHandler.message_size_limit
end

.ws_message_size_limit=(val) ⇒ Object

Sets the message byte size limit for a Websocket message. Defaults to 0 (no limit)

Although memory will be allocated for the latest TCP/IP frame, this allows the websocket to disconnect if the incoming expected message size exceeds the allowed maximum size.

If the sessage size limit is exceeded, the disconnection will be immidiate as an attack will be assumed. The protocol's normal disconnect sequesnce will be discarded.



57
58
59
# File 'lib/grhttp/api.rb', line 57

def ws_message_size_limit=val
	Base::WSHandler.message_size_limit = val
end