1 /* A ruby binding to the ebb web server
2 * Copyright (c) 2008 Ry Dahl. This software is released under the MIT
3 * License. See README file for details.
14 static VALUE global_http_prefix
;
15 static VALUE global_request_method
;
16 static VALUE global_request_uri
;
17 static VALUE global_fragment
;
18 static VALUE global_request_path
;
19 static VALUE global_query_string
;
20 static VALUE global_http_version
;
21 static VALUE global_request_body
;
22 static VALUE global_server_port
;
23 static VALUE global_path_info
;
24 static VALUE global_content_length
;
25 static VALUE global_http_host
;
27 /* You don't want to run more than one server per Ruby VM. Really
28 * I'm making this explicit by not defining a Ebb::Server class but instead
29 * initializing a single server and single event loop on module load.
31 static ebb_server
*server
;
33 struct ev_idle idle_watcher
;
35 /* Variables with a leading underscore are C-level variables */
37 #define ASCII_UPPER(ch) ('a' <= ch && ch <= 'z' ? ch - 'a' + 'A' : ch)
39 # define RSTRING_PTR(s) (RSTRING(s)->ptr)
40 # define RSTRING_LEN(s) (RSTRING(s)->len)
43 static void attach_idle_watcher()
45 if(!ev_is_active(&idle_watcher
)) {
46 ev_idle_start (loop
, &idle_watcher
);
51 static void detach_idle_watcher()
53 ev_idle_stop(loop
, &idle_watcher
);
56 static int clients_in_use_p()
59 for(i
= 0; i
< EBB_MAX_CLIENTS
; i
++)
60 if(server
->clients
[i
].in_use
) return TRUE
;
64 void request_cb(ebb_client
*client
, void *data
)
66 VALUE waiting_clients
= (VALUE
)data
;
67 VALUE rb_client
= Data_Wrap_Struct(cClient
, 0, 0, client
);
68 rb_ary_push(waiting_clients
, rb_client
);
69 attach_idle_watcher();
72 VALUE
server_listen_on_port(VALUE _
, VALUE port
)
74 if(ebb_server_listen_on_port(server
, FIX2INT(port
)) < 0)
75 rb_sys_fail("Problem listening on port");
80 idle_cb (struct ev_loop
*loop
, struct ev_idle
*w
, int revents
) {
81 if(clients_in_use_p()) {
83 } else if(!rb_thread_alone()) {
84 /* if you have another long running thread running besides the ones used
85 * for the webapp's requests you will run into performance problems in
86 * ruby 1.8.x because rb_thread_select is slow.
87 * (Don't worry - you're probably not doing this.)
89 struct timeval select_timeout
= { tv_sec
: 0, tv_usec
: 50000 };
91 FD_ZERO(&server_fd_set
);
92 FD_SET(server
->fd
, &server_fd_set
);
93 rb_thread_select(server
->fd
+1, &server_fd_set
, 0, 0, &select_timeout
);
95 detach_idle_watcher();
99 VALUE
server_process_connections(VALUE _
)
102 ev_loop(loop
, EVLOOP_ONESHOT
);
108 VALUE
server_unlisten(VALUE _
)
110 ebb_server_unlisten(server
);
114 VALUE
server_open(VALUE _
)
116 return server
->open
? Qtrue
: Qfalse
;
119 VALUE
env_field(struct ebb_env_item
*item
)
123 case EBB_FIELD_VALUE_PAIR
:
124 f
= rb_str_new(NULL
, RSTRING_LEN(global_http_prefix
) + item
->field_length
);
125 memcpy( RSTRING_PTR(f
)
126 , RSTRING_PTR(global_http_prefix
)
127 , RSTRING_LEN(global_http_prefix
)
130 for(i
= 0; i
< item
->field_length
; i
++) {
131 char *ch
= RSTRING_PTR(f
) + RSTRING_LEN(global_http_prefix
) + i
;
132 *ch
= item
->field
[i
] == '-' ? '_' : ASCII_UPPER(item
->field
[i
]);
135 case EBB_REQUEST_METHOD
: return global_request_method
;
136 case EBB_REQUEST_URI
: return global_request_uri
;
137 case EBB_FRAGMENT
: return global_fragment
;
138 case EBB_REQUEST_PATH
: return global_request_path
;
139 case EBB_QUERY_STRING
: return global_query_string
;
140 case EBB_HTTP_VERSION
: return global_http_version
;
141 case EBB_SERVER_PORT
: return global_server_port
;
142 case EBB_CONTENT_LENGTH
: return global_content_length
;
149 VALUE
env_value(struct ebb_env_item
*item
)
151 if(item
->value_length
> 0)
152 return rb_str_new(item
->value
, item
->value_length
);
158 VALUE
client_env(VALUE _
, VALUE rb_client
)
161 VALUE field
, value
, hash
= rb_hash_new();
164 Data_Get_Struct(rb_client
, ebb_client
, client
);
165 for(i
=0; i
< client
->env_size
; i
++) {
166 field
= env_field(&client
->env
[i
]);
167 value
= env_value(&client
->env
[i
]);
168 rb_hash_aset(hash
, field
, value
);
169 //printf("(%s, %s)\n", StringValuePtr(field), StringValuePtr(value));
172 rb_hash_aset(hash
, global_path_info
, rb_hash_aref(hash
, global_request_path
));
177 VALUE
client_read_input(VALUE _
, VALUE client
, VALUE size
)
182 int _size
= FIX2INT(size
);
183 Data_Get_Struct(client
, ebb_client
, _client
);
185 string
= rb_str_buf_new( _size
);
186 int nread
= ebb_client_read(_client
, RSTRING_PTR(string
), _size
);
187 #if RUBY_VERSION_CODE < 190
188 RSTRING(string
)->len
= nread
;
190 rb_str_set_len(string
, nread
);
194 rb_raise(rb_eRuntimeError
,"There was a problem reading from input (bad tmp file?)");
200 VALUE
client_write_status(VALUE _
, VALUE client
, VALUE status
, VALUE human_status
)
203 Data_Get_Struct(client
, ebb_client
, _client
);
204 ebb_client_write_status(_client
, FIX2INT(status
), StringValuePtr(human_status
));
208 VALUE
client_write_header(VALUE _
, VALUE client
, VALUE field
, VALUE value
)
211 Data_Get_Struct(client
, ebb_client
, _client
);
212 ebb_client_write_header(_client
, StringValuePtr(field
), StringValuePtr(value
));
216 VALUE
client_write_body(VALUE _
, VALUE client
, VALUE string
)
219 Data_Get_Struct(client
, ebb_client
, _client
);
220 ebb_client_write_body(_client
, RSTRING_PTR(string
), RSTRING_LEN(string
));
225 VALUE
client_release(VALUE _
, VALUE rb_client
)
228 Data_Get_Struct(rb_client
, ebb_client
, client
);
229 ebb_client_release(client
);
236 VALUE mEbb
= rb_define_module("Ebb");
237 VALUE mFFI
= rb_define_module_under(mEbb
, "FFI");
239 rb_define_const(mEbb
, "VERSION", rb_str_new2(EBB_VERSION
));
241 /** Defines global strings in the init method. */
242 #define DEF_GLOBAL(N, val) global_##N = rb_obj_freeze(rb_str_new2(val)); rb_global_variable(&global_##N)
243 DEF_GLOBAL(http_prefix
, "HTTP_");
244 DEF_GLOBAL(request_method
, "REQUEST_METHOD");
245 DEF_GLOBAL(request_uri
, "REQUEST_URI");
246 DEF_GLOBAL(fragment
, "FRAGMENT");
247 DEF_GLOBAL(request_path
, "REQUEST_PATH");
248 DEF_GLOBAL(query_string
, "QUERY_STRING");
249 DEF_GLOBAL(http_version
, "HTTP_VERSION");
250 DEF_GLOBAL(request_body
, "REQUEST_BODY");
251 DEF_GLOBAL(server_port
, "SERVER_PORT");
252 DEF_GLOBAL(path_info
, "PATH_INFO");
253 DEF_GLOBAL(content_length
, "CONTENT_LENGTH");
254 DEF_GLOBAL(http_host
, "HTTP_HOST");
256 rb_define_singleton_method(mFFI
, "server_process_connections", server_process_connections
, 0);
257 rb_define_singleton_method(mFFI
, "server_listen_on_port", server_listen_on_port
, 1);
258 rb_define_singleton_method(mFFI
, "server_unlisten", server_unlisten
, 0);
259 rb_define_singleton_method(mFFI
, "server_open?", server_open
, 0);
261 cClient
= rb_define_class_under(mEbb
, "Client", rb_cObject
);
262 rb_define_singleton_method(mFFI
, "client_read_input", client_read_input
, 2);
263 rb_define_singleton_method(mFFI
, "client_write_status", client_write_status
, 3);
264 rb_define_singleton_method(mFFI
, "client_write_header", client_write_header
, 3);
265 rb_define_singleton_method(mFFI
, "client_write_body", client_write_body
, 2);
266 rb_define_singleton_method(mFFI
, "client_env", client_env
, 1);
267 rb_define_singleton_method(mFFI
, "client_release", client_release
, 1);
269 /* initialize ebb_server */
270 loop
= ev_default_loop (0);
272 ev_idle_init (&idle_watcher
, idle_cb
);
273 attach_idle_watcher();
275 server
= ebb_server_alloc();
276 VALUE waiting_clients
= rb_ary_new();
277 rb_iv_set(mFFI
, "@waiting_clients", waiting_clients
);
278 ebb_server_init(server
, loop
, request_cb
, (void*)waiting_clients
);