ry / ebb fork watch download tarball
public
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
Search Repo:
clean up keep-alive stuff

ebb will keep the connection alive if
ebb_client_write_header(client, "Connection", 
"Keep-Alive")
is executed. This gives control to the Ebb library user to decide when to
use the feature. ebb.rb was updated to reflect this.
Ryan Dahl (author)
Mon Mar 24 03:31:11 -0700 2008
commit  5fb8170d45ed424e73c51ad58874c99beb2ced76
tree    765213088e88a891eb0b1eda3b6cb31486da92e4
parent  0067b531f823399b43fd6d828b226eadb23a3dd3
...
68
69
70
71
 
 
 
72
73
74
75
76
 
 
 
77
78
79
...
68
69
70
 
71
72
73
74
75
76
77
78
79
80
81
82
83
84
0
@@ -68,12 +68,17 @@
0
       body = "Undefined url"
0
     end
0
     
0
- [status, {'Content-Type' => 'text/plain'}, body + "\r\n\r\n"]
0
+ body += "\r\n"
0
+ headers = {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }
0
+ [status, headers, body]
0
   end
0
 end
0
 
0
 
0
 if $0 == __FILE__
0
+ require 'rubygems'
0
+ require 'ruby-debug'
0
+ Debugger.start
0
   require DIR + '/../ruby_lib/ebb'
0
   server = Ebb::start_server(SimpleApp.new, :port => 4001)
0
 end
...
52
53
54
55
 
 
 
 
 
56
57
58
59
 
 
 
 
 
 
 
 
 
60
61
62
...
127
128
129
130
131
 
 
 
 
 
 
 
 
 
 
 
132
133
134
...
52
53
54
 
55
56
57
58
59
60
61
 
62
63
64
65
66
67
68
69
70
71
72
73
74
...
139
140
141
 
 
142
143
144
145
146
147
148
149
150
151
152
153
154
155
0
@@ -52,11 +52,23 @@
0
     client.write_status(status)
0
     
0
     # Add Content-Length to the headers.
0
- if headers.respond_to?(:[]=) and body.respond_to?(:length) and status != 304
0
+ if headers['Content-Length'].nil? and
0
+ headers.respond_to?(:[]=) and
0
+ body.respond_to?(:length) and
0
+ status != 304
0
+ then
0
       headers['Content-Length'] = body.length.to_s
0
     end
0
- headers['Connection'] = client.keep_alive? ? 'Keep-Alive' : 'close'
0
     
0
+ # Decide if we should keep the connection alive or not
0
+ if headers['Connection'].nil?
0
+ if headers['Content-Length'] and client.should_keep_alive?
0
+ headers['Connection'] = 'Keep-Alive'
0
+ else
0
+ headers['Connection'] = 'close'
0
+ end
0
+ end
0
+
0
     # Write the headers
0
     headers.each { |field, value| client.write_header(field, value) }
0
     
0
@@ -127,8 +139,17 @@
0
       FFI::client_release(self)
0
     end
0
     
0
- def keep_alive?
0
- FFI::client_keep_alive?(self)
0
+ def set_keep_alive
0
+ FFI::client_set_keep_alive(self)
0
+ end
0
+
0
+ def should_keep_alive?
0
+ if env['HTTP_VERSION'] == 'HTTP/1.0'
0
+ return true if env['HTTP_CONNECTION'] =~ /Keep-Alive/i
0
+ else
0
+ return true unless env['HTTP_CONNECTION'] =~ /close/i
0
+ end
0
+ false
0
     end
0
   end
0
   
...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
343
344
345
346
 
347
348
349
350
...
581
582
583
 
584
585
586
587
588
589
 
 
 
 
590
591
592
...
81
82
83
 
 
 
 
 
 
 
 
 
 
84
85
86
...
333
334
335
 
336
337
338
339
340
...
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
0
@@ -81,16 +81,6 @@
0
 void on_element(void *data, int type, const char *at, size_t length)
0
 {
0
   ebb_client *client = (ebb_client*)(data);
0
- switch(type) {
0
- case MONGREL_HTTP_VERSION:
0
- /* "HTTP/1.1" by default is keep-alive true, otherwise not*/
0
- /* note that the version will always come first */
0
- client->keep_alive = (strncmp(at+5, "1.1", 3) == 0);
0
- break;
0
- case MONGREL_CONNECTION:
0
- client->keep_alive = (strncmp(at, "close", 5) != 0);
0
- break;
0
- }
0
   env_add_const(client, type, at, length);
0
 }
0
 
0
@@ -343,7 +333,7 @@
0
     /* Only allocate the request_buffer once */
0
     client->request_buffer = (char*)malloc(EBB_BUFFERSIZE);
0
   }
0
- client->keep_alive = TRUE;
0
+ client->keep_alive = FALSE;
0
   client->status_written = client->headers_written = client->body_written = FALSE;
0
   client->written = 0;
0
   /* here we do not free the already allocated GString client->response_buffer
0
0
@@ -581,12 +571,17 @@
0
   client->status_written = TRUE;
0
 }
0
 
0
+
0
 void ebb_client_write_header(ebb_client *client, const char *field, const char *value)
0
 {
0
   assert(client->in_use);
0
   if(!client->open) return;
0
   assert(client->status_written == TRUE);
0
   assert(client->headers_written == FALSE);
0
+
0
+ if(strcmp(field, "Connection") == 0 && strcmp(value, "Keep-Alive") == 0) {
0
+ client->keep_alive = TRUE;
0
+ }
0
   g_string_append_printf( client->response_buffer
0
                         , "%s: %s\r\n"
0
                         , field
...
32
33
34
35
36
37
38
39
...
32
33
34
 
 
35
36
37
0
@@ -32,8 +32,6 @@
0
 void ebb_client_write_header(ebb_client*, const char *field, const char *value);
0
 void ebb_client_write_body(ebb_client*, const char *data, int length);
0
 
0
-void ebb_client_begin_transmission( ebb_client *client);
0
-
0
 struct ebb_env_item {
0
  int type;
0
  const char *field;
...
239
240
241
242
243
244
245
246
247
248
249
250
251
...
280
281
282
283
284
285
286
...
239
240
241
 
 
 
 
 
 
 
242
243
244
...
273
274
275
 
276
277
278
0
@@ -239,13 +239,6 @@
0
   return Qnil;
0
 }
0
 
0
-VALUE client_keep_alive_p(VALUE _, VALUE rb_client)
0
-{
0
- ebb_client *client;
0
- Data_Get_Struct(rb_client, ebb_client, client);
0
- return client->keep_alive ? Qtrue : Qfalse;
0
-}
0
-
0
 void Init_ebb_ext()
0
 {
0
   VALUE mEbb = rb_define_module("Ebb");
0
@@ -280,7 +273,6 @@
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_body", client_write_body, 2);
0
- rb_define_singleton_method(mFFI, "client_keep_alive?", client_keep_alive_p, 1);
0
   rb_define_singleton_method(mFFI, "client_env", client_env, 1);
0
   rb_define_singleton_method(mFFI, "client_release", client_release, 1);
0
   

Comments

    No one has commented yet.