Class: Libevent::HttpRequest

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

Instance Method Summary (collapse)

Constructor Details

- (Object) initialize

Initialize HttpRequest object

Raises:

  • (ArgumentError)

    if object created withot evhttp_request c data



# File 'ext/libevent_ext/http_request.c'

static VALUE t_initialize(VALUE self) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);
  if ( !http_request->ev_request )
    rb_raise(rb_eArgError, "http_request C data is not given");

  return self;
}

Instance Method Details

- (true false) add_output_header

Add output header

Parameters:

  • key (String)

    a header key

  • value (String)

    a header value

Returns:

  • (true false)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_add_output_header(VALUE self, VALUE key, VALUE value) {
  Libevent_HttpRequest *http_request;
  struct evkeyvalq *ev_headers;
  int status;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);
  ev_headers = evhttp_request_get_output_headers(http_request->ev_request);
  status = evhttp_add_header(ev_headers, RSTRING_PTR(key), RSTRING_PTR(value));

  return ( status == -1 ? Qfalse : Qtrue);
}

- (nil) clear_output_headers

Removes all output headers.

Returns:

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_clear_output_headers(VALUE self) { 
  Libevent_HttpRequest *http_request;
  struct evkeyvalq *ev_headers;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);
  ev_headers = evhttp_request_get_output_headers(http_request->ev_request);
  evhttp_clear_headers(ev_headers);

  return Qnil;
}

- (String) get_body

Read body from request input buffer.

Returns:

  • (String)

    body



# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_body(VALUE self) {
  Libevent_HttpRequest *http_request;
  struct evbuffer *ev_buffer;
  size_t length;
  VALUE body;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  ev_buffer = evhttp_request_get_input_buffer(http_request->ev_request);

  length = evbuffer_get_length(ev_buffer);
  body = rb_str_new(0, length);
  evbuffer_copyout(ev_buffer, RSTRING_PTR(body), length);

  return body;
}

- (String?) get_command

Get request command (i.e method)

Examples:

GET

Returns:

  • (String)

    http method for known command

  • (nil)

    if method is not supported



# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_command(VALUE self) {
Libevent_HttpRequest *http_request;
VALUE command;

Data_Get_Struct(self, Libevent_HttpRequest, http_request);

switch ( evhttp_request_get_command(http_request->ev_request) ) {
  case EVHTTP_REQ_GET     : command = rb_str_new2("GET");     break;
  case EVHTTP_REQ_POST    : command = rb_str_new2("POST");    break;
  case EVHTTP_REQ_HEAD    : command = rb_str_new2("HEAD");    break;
  case EVHTTP_REQ_PUT     : command = rb_str_new2("PUT");     break;
  case EVHTTP_REQ_DELETE  : command = rb_str_new2("DELETE");  break;
  case EVHTTP_REQ_OPTIONS : command = rb_str_new2("OPTIONS"); break;
  case EVHTTP_REQ_TRACE   : command = rb_str_new2("TRACE");   break;
  case EVHTTP_REQ_CONNECT : command = rb_str_new2("CONNECT"); break;
  case EVHTTP_REQ_PATCH   : command = rb_str_new2("PATCH");   break;
  default: command = Qnil; break;
}

- (String) get_host

Returns the host associated with the request. If a client sends an absolute URI, the host part of that is preferred. Otherwise, the input headers are searched for a Host: header. NULL is returned if no absolute URI or Host: header is provided.

Returns:

  • (String)

    host



# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_host(VALUE self) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  return rb_str_new2(evhttp_request_get_host(http_request->ev_request));
}

- (String) get_http_version

Get request HTTP version

Returns:

  • (String)

    http version



# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_http_version(VALUE self) {
  Libevent_HttpRequest *http_request;
  char http_version[3];

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);
  sprintf(http_version, "HTTP/%d.%d", http_request->ev_request->major, http_request->ev_request->minor);

  return rb_str_new2(http_version);
}

- (Hash) get_input_headers

Get request input headers

Returns:

  • (Hash)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_input_headers(VALUE self) {
Libevent_HttpRequest *http_request;
struct evkeyvalq *ev_headers;
struct evkeyval *ev_header;
VALUE headers;

Data_Get_Struct(self, Libevent_HttpRequest, http_request);

headers = rb_hash_new();

ev_headers = evhttp_request_get_input_headers(http_request->ev_request);

for ( ev_header = ev_headers->tqh_first; ev_header; ev_header = ev_header->next.tqe_next ) {
  rb_hash_aset(headers, rb_str_new2(ev_header->key), rb_str_new2(ev_header->value));
}

- (String) get_remote_host

Get the remote address of associated connection

Returns:

  • (String)

    IP address



# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_remote_host(VALUE self) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  return rb_str_new2(http_request->ev_request->remote_host);
}

- (Fixnum) get_remote_port

Get the remote port of associated connection

Returns:

  • (Fixnum)

    port



# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_remote_port(VALUE self) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  return INT2FIX(http_request->ev_request->remote_port);
}

- (String) get_uri

Get request URI

Returns:

  • (String)

    uri string



# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_uri(VALUE self) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  return rb_str_new2(evhttp_request_get_uri(http_request->ev_request));
}

- (String?) get_uri_path

Get request URI path

Returns:

  • (String)
  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_uri_path(VALUE self) {
  Libevent_HttpRequest *http_request;
  const char *path;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  path = evhttp_uri_get_path(http_request->ev_request->uri_elems);

  return(path ? rb_str_new2(path) : Qnil);
}

- (String?) get_uri_query

Get request URI query

Returns:

  • (String)
  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_uri_query(VALUE self) {
  Libevent_HttpRequest *http_request;
  const char *query;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  query = evhttp_uri_get_query(http_request->ev_request->uri_elems);

  return(query ? rb_str_new2(query) : Qnil);
}

- (String?) get_uri_scheme

Get request URI scheme

Returns:

  • (String)

    http or https

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_get_uri_scheme(VALUE self) {
  Libevent_HttpRequest *http_request;
  const struct evhttp_uri *ev_uri;
  const char* scheme;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  ev_uri = evhttp_request_get_evhttp_uri(http_request->ev_request);
  scheme = evhttp_uri_get_scheme(ev_uri);

  return(scheme ? rb_str_new2(scheme) : Qnil);
}

- (nil) send_error

Send error to client

Parameters:

  • code (Fixnum)

    HTTP code

  • reason (String)

    (optional) short error descriptor

Returns:

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_send_error(VALUE self, VALUE code, VALUE reason) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  evhttp_send_error(http_request->ev_request, FIX2INT(code), reason == Qnil ? NULL : RSTRING_PTR(reason));

  return Qnil;
}

- (nil) send_reply

Send reply to client

Parameters:

  • code (Fixnum)

    HTTP code

  • headers (Hash)

    hash of http output headers

  • body (Object)

    object that response to each method that returns strings

Returns:

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_send_reply(VALUE self, VALUE code, VALUE headers, VALUE body) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);
  Check_Type(code, T_FIXNUM);
  Check_Type(headers, T_HASH);

  t_set_output_headers(self, headers);

  evhttp_send_reply_start(http_request->ev_request, FIX2INT(code), NULL);
  rb_iterate(rb_each, body, t_send_chunk, self);
  evhttp_send_reply_end(http_request->ev_request);

  return Qnil;
}

- (nil) send_reply_chunk

Send chunk of data to client

Parameters:

  • chunk (String)

    string

Returns:

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_send_reply_chunk(VALUE self, VALUE chunk) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);

  evbuffer_add(http_request->ev_buffer, RSTRING_PTR(chunk), RSTRING_LEN(chunk));
  evhttp_send_reply_chunk(http_request->ev_request, http_request->ev_buffer);

  return Qnil;
}

- (nil) send_reply_end

Stop reply to client

Returns:

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_send_reply_end(VALUE self) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);
  evhttp_send_reply_end(http_request->ev_request);

  return Qnil;
}

- (nil) send_reply_start

Start reply to client

Parameters:

  • code (Fixnum)

    HTTP code

  • reason (String)

    (optional) response descriptor

Returns:

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_send_reply_start(VALUE self, VALUE code, VALUE reason) {
  Libevent_HttpRequest *http_request;

  Data_Get_Struct(self, Libevent_HttpRequest, http_request);
  Check_Type(code, T_FIXNUM);

  evhttp_send_reply_start(http_request->ev_request, FIX2INT(code), reason == Qnil ? NULL : RSTRING_PTR(reason));

  return Qnil;
}

- (nil) set_output_headers

Set request output headers

Parameters:

  • headers (Hash Array)

Returns:

  • (nil)


# File 'ext/libevent_ext/http_request.c'

static VALUE t_set_output_headers(VALUE self, VALUE headers) { 
Libevent_HttpRequest *http_request;
VALUE pairs;
VALUE pair;
VALUE key;
VALUE val;
struct evkeyvalq *ev_headers;
int i;

Data_Get_Struct(self, Libevent_HttpRequest, http_request);

ev_headers = evhttp_request_get_output_headers(http_request->ev_request);

pairs = rb_funcall(headers, rb_intern("to_a"), 0);

for ( i=0 ; i < RARRAY_LEN(pairs); i++ ) {
  pair = rb_ary_entry(pairs, i);
  key = rb_ary_entry(pair, 0);
  val = rb_ary_entry(pair, 1);
  evhttp_add_header(ev_headers, RSTRING_PTR(key), RSTRING_PTR(val));
}