Skip to content

Commit

Permalink
clean up keep-alive stuff
Browse files Browse the repository at this point in the history
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
Ryan Dahl committed Mar 24, 2008
1 parent 0067b53 commit 5fb8170
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
7 changes: 6 additions & 1 deletion benchmark/application.rb
Expand Up @@ -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
29 changes: 25 additions & 4 deletions ruby_lib/ebb.rb
Expand Up @@ -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) }
Expand Down Expand Up @@ -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

Expand Down
17 changes: 6 additions & 11 deletions src/ebb.c
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/ebb.h
Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions src/ebb_ruby.c
Expand Up @@ -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");
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 5fb8170

Please sign in to comment.