Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Close HTTP connection if appropriate. Closes #6

  • Loading branch information...
commit 468552b14714f41881d231e0b75d307af6f9784c 1 parent 7c84852
Daniel Huckstep darkhelmet authored
4 lib/mongrel2/connection.rb
View
@@ -25,7 +25,9 @@ def recv
end
def reply(req, body, status = 200, headers = {})
- Response.new(@resp).send_http(req, body, status, headers)
+ resp = Response.new(@resp)
+ resp.send_http(req, body, status, headers)
+ resp.close if req.close?
end
def close
4 lib/mongrel2/request.rb
View
@@ -32,5 +32,9 @@ def initialize(uuid, conn_id, path, headers, body)
def disconnect?
headers['METHOD'] == 'JSON' && @data['type'] == 'disconnect'
end
+
+ def close?
+ headers['connection'] == 'close' || headers['VERSION'] == 'HTTP/1.0'
+ end
end
end
4 lib/mongrel2/response.rb
View
@@ -50,9 +50,9 @@ def initialize(resp)
def send_http(req, body, status, headers)
send_resp(req.uuid, req.conn_id, build_http_response(body, status, headers))
end
-
+
def close(req)
- send_resp(req.uuid, req.conn_id, "")
+ send_resp(req.uuid, req.conn_id, '')
end
private
57 spec/request_spec.rb
View
@@ -1,46 +1,47 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'mongrel2/request'
-describe "Mongrel2::Request" do
-
- it "should parse a netstring and ignore the contents of the netstring as well as the trailing comma" do
- netstring = "9:aoeu:snth,"
+describe Mongrel2::Request do
+ it 'should parse a netstring and ignore the contents of the netstring as well as the trailing comma' do
+ netstring = '9:aoeu:snth,'
result = Mongrel2::Request.parse_netstring(netstring)
result.length.should == 2
result[0].length.should == 9
- result[0].should eql("aoeu:snth")
+ result[0].should eql('aoeu:snth')
end
-
- it "should parse a netstring made up of multiple netstrings" do
- netstring = "9:aoeu:snth,16:aeou snth qwerty,"
+
+ it 'should parse a netstring made up of multiple netstrings' do
+ netstring = '9:aoeu:snth,16:aeou snth qwerty,'
result = Mongrel2::Request.parse_netstring(netstring)
result.length.should == 2
result[0].length.should == 9
- result[0].should eql("aoeu:snth")
+ result[0].should eql('aoeu:snth')
result[1].length.should == 20
- result[1].should eql("16:aeou snth qwerty,")
+ result[1].should eql('16:aeou snth qwerty,')
end
-
- it "should fail if the netstring does not end in a comma" do
- expect{Mongrel2::Request.parse_netstring("3:foo")}.to raise_error(NameError)
+
+ it 'should fail if the netstring does not end in a comma' do
+ expect { Mongrel2::Request.parse_netstring('3:foo') }.to raise_error(NameError)
end
-
+
it "should parse a Mongrel2 message and have all parts populated" do
- netstring = "UUID CON PATH 232:{\"PATH\":\"/\",\"user-agent\":\"curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3\",\"host\":\"localhost:6767\",\"accept\":\"*/*\",\"x-forwarded-for\":\"::1\",\"METHOD\":\"GET\",\"VERSION\":\"HTTP/1.1\",\"URI\":\"/\",\"PATTERN\":\"/\"},0:,"
+ netstring = "UUID CON PATH 253:{\"PATH\":\"/\",\"user-agent\":\"curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3\",\"host\":\"localhost:6767\",\"accept\":\"*/*\",\"connection\":\"close\",\"x-forwarded-for\":\"::1\",\"METHOD\":\"GET\",\"VERSION\":\"HTTP/1.1\",\"URI\":\"/\",\"PATTERN\":\"/\"},0:,"
r = Mongrel2::Request.parse(netstring)
r.should_not be_nil
- r.uuid.should eql("UUID")
- r.conn_id.should eql("CON")
- r.path.should eql("PATH")
+ r.uuid.should eql('UUID')
+ r.conn_id.should eql('CON')
+ r.path.should eql('PATH')
r.body.length.should == 0
- r.headers.length.should == 9
- r.headers["PATH"].should eql("/")
- r.headers["user-agent"].should eql("curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3")
- r.headers["host"].should eql("localhost:6767")
- r.headers["accept"].should eql("*/*")
- r.headers["x-forwarded-for"].should eql("::1")
- r.headers["METHOD"].should eql("GET")
- r.headers["VERSION"].should eql("HTTP/1.1")
- r.headers["URI"].should eql("/")
- r.headers["PATTERN"].should eql("/")
+ r.headers.length.should == 10
+ r.headers['PATH'].should eql('/')
+ r.headers['user-agent'].should eql('curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3')
+ r.headers['host'].should eql('localhost:6767')
+ r.headers['accept'].should eql('*/*')
+ r.headers['x-forwarded-for'].should eql('::1')
+ r.headers['METHOD'].should eql('GET')
+ r.headers['VERSION'].should eql('HTTP/1.1')
+ r.headers['URI'].should eql('/')
+ r.headers['PATTERN'].should eql('/')
+ r.close?.should be_true
end
end
32 spec/response_spec.rb
View
@@ -1,30 +1,30 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'mongrel2/response'
-describe "Mongrel2::Response" do
+describe Mongrel2::Response do
before(:each) do
@req = double()
@resp = double()
@response = Mongrel2::Response.new(@resp)
end
-
- it "should build the HTTP request format" do
- @req.should_receive(:uuid) {"UUID"}
- @req.should_receive(:conn_id) {"CONN_ID"}
-
+
+ it 'should build the HTTP request format' do
+ @req.should_receive(:uuid) { 'UUID' }
+ @req.should_receive(:conn_id) { 'CONN_ID' }
+
httpreq = "UUID 7:CONN_ID, HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nBoo!"
@resp.should_receive(:send_string).with(httpreq)
-
- @response.send_http(@req, "Boo!", 200, {})
+
+ @response.send_http(@req, 'Boo!', 200, {})
end
-
- it "should send a blank response to close the response" do
- @req.should_receive(:uuid) {"UUID"}
- @req.should_receive(:conn_id) {"CONN_ID"}
-
- httpreq = "UUID 7:CONN_ID, "
+
+ it 'should send a blank response to close the response' do
+ @req.should_receive(:uuid) { 'UUID' }
+ @req.should_receive(:conn_id) { 'CONN_ID' }
+
+ httpreq = 'UUID 7:CONN_ID, '
@resp.should_receive(:send_string).with(httpreq)
-
+
@response.close(@req)
end
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.