Class: Libevent::HttpRequest
- Inherits:
-
Object
- Object
- Libevent::HttpRequest
- Defined in:
- lib/libevent/http_request.rb,
ext/libevent_ext/http_request.c
Instance Method Summary (collapse)
-
- (true false) add_output_header
Add output header.
-
- (nil) clear_output_headers
Removes all output headers.
-
- (String) get_body
Read body from request input buffer.
-
- (String?) get_command
Get request command (i.e method).
-
- (String) get_host
Returns the host associated with the request.
-
- (String) get_http_version
Get request HTTP version.
-
- (Hash) get_input_headers
Get request input headers.
-
- (String) get_remote_host
Get the remote address of associated connection.
-
- (Fixnum) get_remote_port
Get the remote port of associated connection.
-
- (String) get_uri
Get request URI.
-
- (String?) get_uri_path
Get request URI path.
-
- (String?) get_uri_query
Get request URI query.
-
- (String?) get_uri_scheme
Get request URI scheme.
-
- (Object) initialize
constructor
Initialize HttpRequest object.
-
- (nil) send_error
Send error to client.
-
- (nil) send_reply
Send reply to client.
-
- (nil) send_reply_chunk
Send chunk of data to client.
-
- (nil) send_reply_end
Stop reply to client.
-
- (nil) send_reply_start
Start reply to client.
-
- (nil) set_output_headers
Set request output headers.
Constructor Details
- (Object) initialize
Initialize HttpRequest object
|
|
# 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
|
|
# 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.
|
|
# 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.
|
|
# 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)
|
|
# 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.
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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
|
|
# 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));
}
|