Class: Libevent::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/libevent/http.rb,
ext/libevent_ext/http.c

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Object) initialize {|self| ... }

Initialize http instance and allocate evhttp structure

Parameters:

Yields:

  • (self)


# 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

Note:

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.

Examples:

A vhost with the pattern *.example.com may have other vhosts with patterns foo.example.com and bar.example.com associated with it.

Parameters:

  • domain (String)

    the glob pattern against which the hostname is matched. The match is case insensitive and follows otherwise regular shell matching.

  • vhttp (Http)

    the virtual host to add the regular http server

Returns:

  • (true false)


# 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.

Parameters:

  • address (String)

    IP address

  • port (Fixnum)

    port to bind

Returns:

  • (true)

    on success

  • (false)

    on failure



# 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

Parameters:

  • block


18
19
20
# File 'lib/libevent/http.rb', line 18

def handler(&block)
  set_request_handler(block)
end

- (nil) set_request_handler

Note:

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.

Parameters:

  • handler (Object)

    object that response to :call

Returns:

  • (nil)


# 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

Parameters:

  • timeout (Fixnum)

    he timeout, in seconds



# 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

Parameters:

  • domain (String)

    a domain for virtual server

Yields:

  • (http)

Returns:

  • (Http)

    http instance



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