Permalink
Browse files

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.
  • Loading branch information...
1 parent 0067b53 commit 5fb8170d45ed424e73c51ad58874c99beb2ced76 Ryan Dahl committed Mar 24, 2008
Showing with 37 additions and 26 deletions.
  1. +6 −1 benchmark/application.rb
  2. +25 −4 ruby_lib/ebb.rb
  3. +6 −11 src/ebb.c
  4. +0 −2 src/ebb.h
  5. +0 −8 src/ebb_ruby.c
View
@@ -68,12 +68,17 @@ def call(env)
body = "Undefined url"
end
- [status, {'Content-Type' => 'text/plain'}, body + "\r\n\r\n"]
+ body += "\r\n"
+ headers = {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }
+ [status, headers, body]
end
end
if $0 == __FILE__
+ require 'rubygems'
+ require 'ruby-debug'
+ Debugger.start
require DIR + '/../ruby_lib/ebb'
server = Ebb::start_server(SimpleApp.new, :port => 4001)
end
View
@@ -52,10 +52,22 @@ def self.process(app, client)
client.write_status(status)
# Add Content-Length to the headers.
- if headers.respond_to?(:[]=) and body.respond_to?(:length) and status != 304
+ if headers['Content-Length'].nil? and
+ headers.respond_to?(:[]=) and
+ body.respond_to?(:length) and
+ status != 304
+ then
headers['Content-Length'] = body.length.to_s
end
- headers['Connection'] = client.keep_alive? ? 'Keep-Alive' : 'close'
+
+ # Decide if we should keep the connection alive or not
+ if headers['Connection'].nil?
+ if headers['Content-Length'] and client.should_keep_alive?
+ headers['Connection'] = 'Keep-Alive'
+ else
+ headers['Connection'] = 'close'
+ end
+ end
# Write the headers
headers.each { |field, value| client.write_header(field, value) }
@@ -127,8 +139,17 @@ def release
FFI::client_release(self)
end
- def keep_alive?
- FFI::client_keep_alive?(self)
+ def set_keep_alive
+ FFI::client_set_keep_alive(self)
+ end
+
+ def should_keep_alive?
+ if env['HTTP_VERSION'] == 'HTTP/1.0'
+ return true if env['HTTP_CONNECTION'] =~ /Keep-Alive/i
+ else
+ return true unless env['HTTP_CONNECTION'] =~ /close/i
+ end
+ false
end
end
View
@@ -81,16 +81,6 @@ void http_field_cb(void *data, const char *field, size_t flen, const char *value
void on_element(void *data, int type, const char *at, size_t length)
{
ebb_client *client = (ebb_client*)(data);
- switch(type) {
- case MONGREL_HTTP_VERSION:
- /* "HTTP/1.1" by default is keep-alive true, otherwise not*/
- /* note that the version will always come first */
- client->keep_alive = (strncmp(at+5, "1.1", 3) == 0);
- break;
- case MONGREL_CONNECTION:
- client->keep_alive = (strncmp(at, "close", 5) != 0);
- break;
- }
env_add_const(client, type, at, length);
}
@@ -343,7 +333,7 @@ static void client_init(ebb_client *client)
/* Only allocate the request_buffer once */
client->request_buffer = (char*)malloc(EBB_BUFFERSIZE);
}
- client->keep_alive = TRUE;
+ client->keep_alive = FALSE;
client->status_written = client->headers_written = client->body_written = FALSE;
client->written = 0;
/* here we do not free the already allocated GString client->response_buffer
@@ -581,12 +571,17 @@ void ebb_client_write_status(ebb_client *client, int status, const char *human_s
client->status_written = TRUE;
}
+
void ebb_client_write_header(ebb_client *client, const char *field, const char *value)
{
assert(client->in_use);
if(!client->open) return;
assert(client->status_written == TRUE);
assert(client->headers_written == FALSE);
+
+ if(strcmp(field, "Connection") == 0 && strcmp(value, "Keep-Alive") == 0) {
+ client->keep_alive = TRUE;
+ }
g_string_append_printf( client->response_buffer
, "%s: %s\r\n"
, field
View
@@ -32,8 +32,6 @@ void ebb_client_write_status(ebb_client*, int status, const char *human_status);
void ebb_client_write_header(ebb_client*, const char *field, const char *value);
void ebb_client_write_body(ebb_client*, const char *data, int length);
-void ebb_client_begin_transmission( ebb_client *client);
-
struct ebb_env_item {
int type;
const char *field;
View
@@ -239,13 +239,6 @@ VALUE client_release(VALUE _, VALUE rb_client)
return Qnil;
}
-VALUE client_keep_alive_p(VALUE _, VALUE rb_client)
-{
- ebb_client *client;
- Data_Get_Struct(rb_client, ebb_client, client);
- return client->keep_alive ? Qtrue : Qfalse;
-}
-
void Init_ebb_ext()
{
VALUE mEbb = rb_define_module("Ebb");
@@ -280,7 +273,6 @@ void Init_ebb_ext()
rb_define_singleton_method(mFFI, "client_write_status", client_write_status, 3);
rb_define_singleton_method(mFFI, "client_write_header", client_write_header, 3);
rb_define_singleton_method(mFFI, "client_write_body", client_write_body, 2);
- rb_define_singleton_method(mFFI, "client_keep_alive?", client_keep_alive_p, 1);
rb_define_singleton_method(mFFI, "client_env", client_env, 1);
rb_define_singleton_method(mFFI, "client_release", client_release, 1);

0 comments on commit 5fb8170

Please sign in to comment.