ry / ebb fork watch download tarball
public this repo is viewable by everyone
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
add header writing to ebb-core
Ryan Dahl (author)
2 months ago
commit  eb823db6bb07b9c2f411f0aa62b923d3f8198838
tree    2ab3bf24cd359d84f810217d36f1f1ee40927bcf
parent  9bccca53b6ae1533edf3211ad0ba83a48ed9b5a9
...
43
44
45
46
 
47
48
49
...
43
44
45
 
46
47
48
49
0
@@ -43,7 +43,7 @@ task(:wc) { sh "wc -l ruby_lib/*.rb src/ebb*.{c,h}" }
0
 
0
 task(:test => :compile)
0
 Rake::TestTask.new do |t|
0
- t.test_files = 'test/test.rb'
0
+ t.test_files = 'test/basic_test.rb'
0
   t.verbose = true
0
 end
0
 
...
39
40
41
 
 
 
 
 
 
 
 
42
43
44
...
113
114
115
116
 
117
118
119
 
120
121
122
123
 
 
124
125
126
...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
...
121
122
123
 
124
125
126
 
127
128
129
130
 
131
132
133
134
135
0
@@ -39,6 +39,14 @@ module Ebb
0
     def write(data)
0
       FFI::client_write(self, data)
0
     end
0
+
0
+ def write_status(status)
0
+ FFI::client_write_status(self, status.to_i, HTTP_STATUS_CODES[status])
0
+ end
0
+
0
+ def write_header(field, value)
0
+ FFI::client_write_header(self, field.to_s, value.to_s)
0
+ end
0
   end
0
   
0
   class RequestBody
0
@@ -113,14 +121,15 @@ module Ebb
0
         body = "Internal Server Error\n"
0
       end
0
       
0
- client.write "HTTP/1.1 %d %s\r\n" % [status, HTTP_STATUS_CODES[status]]
0
+ client.write_status(status)
0
       
0
       if body.respond_to? :length and status != 304
0
- client.write "Connection: close\r\n"
0
+ headers['Connection'] = 'close'
0
         headers['Content-Length'] = body.length
0
       end
0
       
0
- headers.each { |k, v| client.write "#{k}: #{v}\r\n" }
0
+ headers.each { |k, v| client.write_header(k,v) }
0
+
0
       client.write "\r\n"
0
       
0
       # Not many apps use streaming yet so i'll hold off on that feature
...
358
359
360
 
 
 
 
361
362
363
...
526
527
528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
530
531
...
358
359
360
361
362
363
364
365
366
367
...
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
0
@@ -358,6 +358,10 @@ void on_request(struct ev_loop *loop, ev_io *watcher, int revents)
0
   client->response_buffer->len = 0; /* see note in ebb_client_close */
0
   client->content_length = 0;
0
   
0
+ client->status_sent = FALSE;
0
+ client->headers_sent = FALSE;
0
+ client->body_sent = FALSE;
0
+
0
   /* SETUP READ AND TIMEOUT WATCHERS */
0
   client->read_watcher.data = client;
0
   ev_init(&client->read_watcher, on_readable);
0
@@ -526,6 +530,27 @@ void on_client_writable(struct ev_loop *loop, ev_io *watcher, int revents)
0
     ebb_client_close(client);
0
 }
0
 
0
+void ebb_client_write_status(ebb_client *client, int status, const char *human_status)
0
+{
0
+ assert(client->status_sent == FALSE);
0
+ g_string_append_printf( client->response_buffer
0
+ , "HTTP/1.1 %d %s\r\n"
0
+ , status
0
+ , human_status
0
+ );
0
+ client->status_sent = TRUE;
0
+}
0
+
0
+void ebb_client_write_header(ebb_client *client, const char *field, const char *value)
0
+{
0
+ assert(client->status_sent == TRUE);
0
+ assert(client->headers_sent == FALSE);
0
+ g_string_append_printf( client->response_buffer
0
+ , "%s: %s\r\n"
0
+ , field
0
+ , value
0
+ );
0
+}
0
 
0
 void ebb_client_write(ebb_client *client, const char *data, int length)
0
 {
...
30
31
32
 
 
33
34
35
...
73
74
75
 
 
 
 
76
77
78
...
30
31
32
33
34
35
36
37
...
75
76
77
78
79
80
81
82
83
84
0
@@ -30,6 +30,8 @@ typedef struct ebb_client ebb_client;
0
 /*** Ebb Client ***/
0
 void ebb_client_close(ebb_client*);
0
 int ebb_client_read(ebb_client *client, char *buffer, int length);
0
+void ebb_client_write_status(ebb_client*, int status, const char *human_status);
0
+void ebb_client_write_header(ebb_client*, const char *field, const char *value);
0
 void ebb_client_write(ebb_client*, const char *data, int length);
0
 void ebb_client_finished( ebb_client *client);
0
 
0
@@ -73,6 +75,10 @@ struct ebb_client {
0
   
0
   ev_timer timeout_watcher;
0
   
0
+ int status_sent;
0
+ int headers_sent;
0
+ int body_sent;
0
+
0
   /* the ENV structure */
0
   int env_size;
0
   struct ebb_env_item env[EBB_MAX_ENV];
...
11
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
16
...
56
57
58
59
 
60
61
62
...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
...
69
70
71
 
72
73
74
75
0
@@ -11,6 +11,19 @@ typedef struct {
0
     ebb_server *server;
0
 } Server;
0
 
0
+void Server_alloc()
0
+{
0
+
0
+}
0
+
0
+static void
0
+Server_dealloc(Server* self)
0
+{
0
+ ebb_server_free(self->server);
0
+ self->ob_type->tp_free((PyObject*)self);
0
+}
0
+
0
+
0
 static PyMethodDef Server_methods[] = {
0
     {"name", (PyCFunction)Noddy_name, METH_NOARGS,
0
      "Return the name, combining the first and last name"
0
@@ -56,7 +69,7 @@ static PyTypeObject ServerType = {
0
     0, /* tp_descr_set */
0
     0, /* tp_dictoffset */
0
     (initproc)Server_init, /* tp_init */
0
- 0, /* tp_alloc */
0
+ Server_alloc, /* tp_alloc */
0
     Server_new, /* tp_new */
0
 };
0
 
...
10
11
12
13
14
15
16
17
...
26
27
28
 
 
29
30
31
32
33
34
35
36
37
 
38
39
40
...
45
46
47
 
48
49
50
...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
111
112
113
 
114
115
116
...
119
120
121
122
 
123
124
125
...
150
151
152
 
153
154
155
...
158
159
160
 
161
162
163
...
198
199
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
202
203
...
222
223
224
225
226
227
228
229
230
231
...
244
245
246
247
248
249
250
...
252
253
254
 
 
255
256
257
258
259
...
10
11
12
 
 
13
14
15
...
24
25
26
27
28
29
30
31
32
33
 
34
35
36
37
38
39
40
...
45
46
47
48
49
50
51
...
57
58
59
 
 
 
 
 
 
 
 
 
 
60
61
62
...
102
103
104
105
106
107
108
...
111
112
113
 
114
115
116
117
...
142
143
144
145
146
147
148
...
151
152
153
154
155
156
157
...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
...
231
232
233
 
 
 
 
234
235
236
...
249
250
251
 
252
253
254
...
256
257
258
259
260
261
262
263
 
264
0
@@ -10,8 +10,6 @@
0
 
0
 static VALUE cServer;
0
 static VALUE cClient;
0
-static VALUE eParserError;
0
-
0
 static VALUE global_http_prefix;
0
 static VALUE global_request_method;
0
 static VALUE global_request_uri;
0
@@ -26,15 +24,17 @@ static VALUE global_path_info;
0
 static VALUE global_content_length;
0
 static VALUE global_http_host;
0
 
0
+/* Variables with a leading underscore are C-level variables */
0
+
0
 #define ASCII_UPPER(ch) ('a' <= ch && ch <= 'z' ? ch - 'a' + 'A' : ch)
0
 
0
 VALUE client_new(ebb_client *_client)
0
 {
0
   VALUE client = Data_Wrap_Struct(cClient, 0, 0, _client);
0
- // rb_iv_set(client, "@content_length", INT2FIX(_client->content_length));
0
   return client;
0
 }
0
 
0
+
0
 void request_cb(ebb_client *_client, void *data)
0
 {
0
   VALUE server = (VALUE)data;
0
@@ -45,6 +45,7 @@ void request_cb(ebb_client *_client, void *data)
0
   rb_ary_push(waiting_clients, client);
0
 }
0
 
0
+
0
 VALUE server_alloc(VALUE self)
0
 {
0
   struct ev_loop *loop = ev_default_loop (0);
0
@@ -56,16 +57,6 @@ VALUE server_alloc(VALUE self)
0
 }
0
 
0
 
0
-// VALUE server_initialize(VALUE x, VALUE server)
0
-// {
0
-// struct ev_loop *loop = ev_default_loop (0);
0
-// ebb_server *_server;
0
-//
0
-// Data_Get_Struct(server, ebb_server, _server);
0
-// return Qnil;
0
-// }
0
-
0
-
0
 VALUE server_listen_on_port(VALUE x, VALUE server, VALUE port)
0
 {
0
   ebb_server *_server;
0
@@ -111,6 +102,7 @@ VALUE server_process_connections(VALUE x, VALUE server)
0
     return Qfalse;
0
 }
0
 
0
+
0
 VALUE server_unlisten(VALUE x, VALUE server)
0
 {
0
   ebb_server *_server;
0
@@ -119,7 +111,7 @@ VALUE server_unlisten(VALUE x, VALUE server)
0
   return Qnil;
0
 }
0
 
0
-/* Variables with an underscore are C-level variables */
0
+
0
 VALUE env_field(struct ebb_env_item *item)
0
 {
0
   VALUE f;
0
@@ -150,6 +142,7 @@ VALUE env_field(struct ebb_env_item *item)
0
   return Qnil;
0
 }
0
 
0
+
0
 VALUE env_value(struct ebb_env_item *item)
0
 {
0
   if(item->value_length > 0)
0
@@ -158,6 +151,7 @@ VALUE env_value(struct ebb_env_item *item)
0
     return Qnil;
0
 }
0
 
0
+
0
 VALUE client_env(VALUE x, VALUE client)
0
 {
0
   ebb_client *_client;
0
@@ -198,6 +192,21 @@ VALUE client_read_input(VALUE x, VALUE client, VALUE size)
0
   return string;
0
 }
0
 
0
+VALUE client_write_status(VALUE x, VALUE client, VALUE status, VALUE human_status)
0
+{
0
+ ebb_client *_client;
0
+ Data_Get_Struct(client, ebb_client, _client);
0
+ ebb_client_write_status(_client, FIX2INT(status), StringValuePtr(human_status));
0
+ return Qnil;
0
+}
0
+
0
+VALUE client_write_header(VALUE x, VALUE client, VALUE field, VALUE value)
0
+{
0
+ ebb_client *_client;
0
+ Data_Get_Struct(client, ebb_client, _client);
0
+ ebb_client_write_header(_client, StringValuePtr(field), StringValuePtr(value));
0
+ return Qnil;
0
+}
0
 
0
 VALUE client_write(VALUE x, VALUE client, VALUE string)
0
 {
0
@@ -222,10 +231,6 @@ void Init_ebb_ext()
0
   VALUE mEbb = rb_define_module("Ebb");
0
   VALUE mFFI = rb_define_module_under(mEbb, "FFI");
0
   
0
-
0
- eParserError = rb_define_class_under(mEbb, "ParserError", rb_eIOError);
0
-
0
-
0
   /** Defines global strings in the init method. */
0
 #define DEF_GLOBAL(N, val) global_##N = rb_obj_freeze(rb_str_new2(val)); rb_global_variable(&global_##N)
0
   DEF_GLOBAL(http_prefix, "HTTP_");
0
@@ -244,7 +249,6 @@ void Init_ebb_ext()
0
   
0
   cServer = rb_define_class_under(mEbb, "Server", rb_cObject);
0
   rb_define_alloc_func(cServer, server_alloc);
0
- // rb_define_singleton_method(mFFI, "server_initialize", server_initialize, 1);
0
   rb_define_singleton_method(mFFI, "server_process_connections", server_process_connections, 1);
0
   rb_define_singleton_method(mFFI, "server_listen_on_port", server_listen_on_port, 2);
0
   rb_define_singleton_method(mFFI, "server_listen_on_socket", server_listen_on_socket, 2);
0
@@ -252,8 +256,9 @@ void Init_ebb_ext()
0
   
0
   cClient = rb_define_class_under(mEbb, "Client", rb_cObject);
0
   rb_define_singleton_method(mFFI, "client_read_input", client_read_input, 2);
0
+ rb_define_singleton_method(mFFI, "client_write_status", client_write_status, 3);
0
+ rb_define_singleton_method(mFFI, "client_write_header", client_write_header, 3);
0
   rb_define_singleton_method(mFFI, "client_write", client_write, 2);
0
   rb_define_singleton_method(mFFI, "client_finished", client_finished, 1);
0
   rb_define_singleton_method(mFFI, "client_env", client_env, 1);
0
-
0
 }
...
1
2
3
4
5
6
7
...
40
41
42
43
44
45
46
47
48
49
50
51
...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
1
2
3
 
4
5
6
...
39
40
41
 
 
 
 
 
 
42
43
44
...
98
99
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
102
 
 
 
 
103
104
105
0
@@ -1,7 +1,6 @@
0
 require File.dirname(__FILE__) + '/../ruby_lib/ebb'
0
 require 'test/unit'
0
 require 'net/http'
0
-require 'base64'
0
 require 'socket'
0
 require 'rubygems'
0
 require 'json'
0
@@ -40,12 +39,6 @@ class EbbTest < Test::Unit::TestCase
0
       raise "bytes called with n <= 0" if n <= 0
0
       body = @@responses[n] || "C"*n
0
       status = 200
0
-
0
- elsif commands.include?('env')
0
- env.delete('rack.input') # delete this because it's hard to marshal
0
- env.delete('rack.errors')
0
- body = Base64.encode64(Marshal.dump(env))
0
- status = 200
0
       
0
     elsif commands.include?('test_post_length')
0
       input_body = ""
0
@@ -105,27 +98,8 @@ class EbbTest < Test::Unit::TestCase
0
       assert_equal 200, response.code.to_i, response.body
0
     end
0
   end
0
-
0
- def test_env
0
- response = get('/env')
0
- env = Marshal.load(Base64.decode64(response.body))
0
- assert_equal '/env', env['PATH_INFO']
0
- assert_equal '/env', env['REQUEST_PATH']
0
- assert_equal 'HTTP/1.1', env['SERVER_PROTOCOL']
0
- assert_equal 'CGI/1.2', env['GATEWAY_INTERFACE']
0
- assert_equal '0.0.0.0', env['SERVER_NAME']
0
- assert_equal PORT.to_s, env['SERVER_PORT']
0
- assert_equal 'GET', env['REQUEST_METHOD']
0
- end
0
-
0
-
0
-
0
 end
0
 
0
-[
0
-
0
-]
0
-
0
 class EnvTest < Test::Unit::TestCase
0
   def call(env)
0
     env.delete('rack.input')
...
5
6
7
 
 
 
 
8
9
10
...
33
34
35
36
 
37
38
39
...
93
94
95
 
96
97
98
...
5
6
7
8
9
10
11
12
13
14
...
37
38
39
 
40
41
42
43
...
97
98
99
100
101
102
103
0
@@ -5,6 +5,10 @@ require 'test/unit'
0
 
0
 PORT = 4037
0
 
0
+# This test depends on echo_server running at port 4037. I do this so that
0
+# I can run a Python server at that port with a similar application and reuse
0
+# these tests.
0
+
0
 def send_request(request_string)
0
   socket = TCPSocket.new("0.0.0.0", PORT)
0
   socket.write(request_string)
0
@@ -33,7 +37,7 @@ def drops_request?(request_string)
0
 end
0
 
0
 class HttpParserTest < Test::Unit::TestCase
0
-
0
+
0
   def test_parse_simple
0
     env = send_request("GET / HTTP/1.1\r\n\r\n")
0
     
0
@@ -93,6 +97,7 @@ class HttpParserTest < Test::Unit::TestCase
0
     10.times do |c|
0
       req = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-Test: #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n"
0
       assert drops_request?(req), "large mangled field values are caught"
0
+ ### XXX this is broken! fix me. this test should drop the request.
0
     end
0
     
0
     # then large headers are rejected too

Comments

    No one has commented yet.