Class: Libevent::Http
- Inherits:
-
Object
- Object
- Libevent::Http
- Defined in:
- lib/libevent/http.rb,
ext/libevent_ext/http.c
Instance Attribute Summary (collapse)
-
- (Object) base
readonly
Returns the value of attribute base.
Instance Method Summary (collapse)
-
- (true false) add_virtual_host
Adds a virtual host to the http server.
-
- (true, false) bind_socket
Binds an HTTP instance on the specified address and port.
-
- (Object) handler(&block)
Set request handler for current http instance.
-
- (Object) initialize {|self| ... }
constructor
Initialize http instance and allocate evhttp structure.
-
- (nil) set_request_handler
Set a callback for all requests that are not caught by specific callbacks.
-
- (Object) set_timeout
Set the timeout for an HTTP request.
-
- (Http) vhost(domain) {|http| ... }
Create virtual http server.
Constructor Details
- (Object) initialize {|self| ... }
Initialize http instance and allocate evhttp structure
|
|
# File 'ext/libevent_ext/http.c'
static VALUE t_initialize(VALUE self, VALUE object) {
Libevent_Http *http;
Libevent_Base *base;
Data_Get_Struct(self, Libevent_Http, http);
Data_Get_Struct(object, Libevent_Base, base);
http->ev_base = base->ev_base;
http->ev_http = evhttp_new(http->ev_base);
if (!http->ev_http) {
rb_fatal("Couldn't create evhttp");
}
|
Instance Attribute Details
- (Object) base (readonly)
Returns the value of attribute base
4 5 6 |
# File 'lib/libevent/http.rb', line 4 def base @base end |
Instance Method Details
- (true false) add_virtual_host
A virtual host is a newly initialized evhttp object that has request handler It most not have any listing sockets associated with it.
Adds a virtual host to the http server. It is possible to have hierarchical vhosts.
|
|
# File 'ext/libevent_ext/http.c'
static VALUE t_add_virtual_host(VALUE self, VALUE domain, VALUE vhttp) {
Libevent_Http *le_http;
Libevent_Http *le_vhttp;
int status;
Data_Get_Struct(self, Libevent_Http, le_http);
Data_Get_Struct(vhttp, Libevent_Http, le_vhttp);
Check_Type(domain, T_STRING);
le_vhttp->ev_http_parent = le_http->ev_http;
status = evhttp_add_virtual_host(le_http->ev_http, RSTRING_PTR(domain), le_vhttp->ev_http);
return ( status == -1 ? Qfalse : Qtrue );
}
|
- (true, false) bind_socket
Binds an HTTP instance on the specified address and port. Can be called multiple times to bind the same http server to multiple different ports.
|
|
# File 'ext/libevent_ext/http.c'
static VALUE t_bind_socket(VALUE self, VALUE address, VALUE port) {
Libevent_Http *http;
int status;
Data_Get_Struct(self, Libevent_Http, http);
Check_Type(address, T_STRING);
Check_Type(port, T_FIXNUM);
status = evhttp_bind_socket(http->ev_http, RSTRING_PTR(address), FIX2INT(port));
return ( status == -1 ? Qfalse : Qtrue );
}
|
- (Object) handler(&block)
Set request handler for current http instance
18 19 20 |
# File 'lib/libevent/http.rb', line 18 def handler(&block) set_request_handler(block) end |
- (nil) set_request_handler
handler should response to :call method.
Libevent::HttpRequest instance will be passed to handler as first argument
Set a callback for all requests that are not caught by specific callbacks.
|
|
# File 'ext/libevent_ext/http.c'
static VALUE t_set_request_handler(VALUE self, VALUE handler) {
Libevent_Http *http;
Data_Get_Struct(self, Libevent_Http, http);
if ( !rb_respond_to(handler, rb_intern("call")))
rb_raise(rb_eArgError, "handler does not response to call method");
rb_iv_set(self, "@request_handler", handler);
evhttp_set_gencb(http->ev_http, t_request_handler, (void *)handler);
return Qnil;
}
|
- (Object) set_timeout
Set the timeout for an HTTP request. return nil
|
|
# File 'ext/libevent_ext/http.c'
static VALUE t_set_timeout(VALUE self, VALUE timeout) {
Libevent_Http *http;
Data_Get_Struct(self, Libevent_Http, http);
evhttp_set_timeout(http->ev_http, NUM2INT(timeout));
return Qnil;
}
|
- (Http) vhost(domain) {|http| ... }
Create virtual http server
9 10 11 12 13 14 |
# File 'lib/libevent/http.rb', line 9 def vhost(domain) http = self.class.new(self.base) add_virtual_host(domain, http) yield(http) if block_given? http end |