Remove begin_transmission API
[ebb.git] / src / ebb_ruby.c
blob8733cd2a3c01d161255452b8b08810a11fc607e5
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.
4 */
5 #include <ruby.h>
6 #include <rubyio.h>
7 #include <rubysig.h>
8 #include <assert.h>
9 #include <fcntl.h>
10 #include <ebb.h>
11 #include <ev.h>
13 static VALUE cClient;
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;
32 struct ev_loop *loop;
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)
38 #ifndef RSTRING_PTR
39 # define RSTRING_PTR(s) (RSTRING(s)->ptr)
40 # define RSTRING_LEN(s) (RSTRING(s)->len)
41 #endif
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()
58 int i;
59 for(i = 0; i < EBB_MAX_CLIENTS; i++)
60 if(server->clients[i].in_use) return TRUE;
61 return FALSE;
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");
76 return Qnil;
79 static void
80 idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) {
81 if(clients_in_use_p()) {
82 rb_thread_schedule();
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 };
90 fd_set server_fd_set;
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);
94 } else {
95 detach_idle_watcher();
99 VALUE server_process_connections(VALUE _)
101 TRAP_BEG;
102 ev_loop(loop, EVLOOP_ONESHOT);
103 TRAP_END;
104 return Qnil;
108 VALUE server_unlisten(VALUE _)
110 ebb_server_unlisten(server);
111 return Qnil;
114 VALUE server_open(VALUE _)
116 return server->open ? Qtrue : Qfalse;
119 VALUE env_field(struct ebb_env_item *item)
121 VALUE f;
122 switch(item->type) {
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)
129 int i;
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]);
134 return f;
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;
144 assert(FALSE);
145 return Qnil;
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);
153 else
154 return Qnil;
158 VALUE client_env(VALUE _, VALUE rb_client)
160 ebb_client *client;
161 VALUE field, value, hash = rb_hash_new();
162 int i;
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));
171 //printf("\n\n");
172 rb_hash_aset(hash, global_path_info, rb_hash_aref(hash, global_request_path));
173 return hash;
177 VALUE client_read_input(VALUE _, VALUE client, VALUE size)
179 ebb_client *_client;
180 GString *_string;
181 VALUE string;
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;
189 #else
190 rb_str_set_len(string, nread);
191 #endif
193 if(nread < 0)
194 rb_raise(rb_eRuntimeError,"There was a problem reading from input (bad tmp file?)");
195 if(nread == 0)
196 return Qnil;
197 return string;
200 VALUE client_write_status(VALUE _, VALUE client, VALUE status, VALUE human_status)
202 ebb_client *_client;
203 Data_Get_Struct(client, ebb_client, _client);
204 ebb_client_write_status(_client, FIX2INT(status), StringValuePtr(human_status));
205 return Qnil;
208 VALUE client_write_header(VALUE _, VALUE client, VALUE field, VALUE value)
210 ebb_client *_client;
211 Data_Get_Struct(client, ebb_client, _client);
212 ebb_client_write_header(_client, StringValuePtr(field), StringValuePtr(value));
213 return Qnil;
216 VALUE client_write_body(VALUE _, VALUE client, VALUE string)
218 ebb_client *_client;
219 Data_Get_Struct(client, ebb_client, _client);
220 ebb_client_write_body(_client, RSTRING_PTR(string), RSTRING_LEN(string));
221 return Qnil;
225 VALUE client_release(VALUE _, VALUE rb_client)
227 ebb_client *client;
228 Data_Get_Struct(rb_client, ebb_client, client);
229 ebb_client_release(client);
230 return Qnil;
234 void Init_ebb_ext()
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);