From 3555fdce0c9cf9bb6860a79fdc6843a1e96c9888 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 18 Oct 2013 18:03:51 +0000 Subject: http_response: respect setups with non-persistent connections Some users may wish to disable persistent connections for testing or whatever reason, let them. We'll also be using this feature to force SIGQUIT to expire clients. --- lib/yahns/http_response.rb | 7 ++++--- test/test_server.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb index 8967670..b6228e5 100644 --- a/lib/yahns/http_response.rb +++ b/lib/yahns/http_response.rb @@ -91,7 +91,8 @@ module Yahns::HttpResponse # :nodoc: status = CODES[status.to_i] || status offset = 0 count = hijack = nil - alive = @hs.next? + k = self.class + alive = @hs.next? && k.persistent_connections if @hs.headers? buf = "#{response_start}#{status}\r\nDate: #{httpdate}\r\n" @@ -126,7 +127,7 @@ module Yahns::HttpResponse # :nodoc: when String buf = rv # hope the skb grows when :wait_writable, :wait_readable - if self.class.output_buffering + if k.output_buffering alive = hijack ? hijack : alive rv = response_header_blocked(rv, buf, body, alive, offset, count) body = nil # ensure we do not close body in ensure @@ -158,7 +159,7 @@ module Yahns::HttpResponse # :nodoc: when String chunk = rv # hope the skb grows when we loop into the trywrite when :wait_writable, :wait_readable - if self.class.output_buffering + if k.output_buffering wbuf = Yahns::Wbuf.new(body, alive) rv = wbuf.wbuf_write(self, chunk) break diff --git a/test/test_server.rb b/test/test_server.rb index 958b4f0..289770d 100644 --- a/test/test_server.rb +++ b/test/test_server.rb @@ -380,4 +380,32 @@ class TestServer < Testcase end [ pid, host, port ] end + + def test_nonpersistent + err = @err + cfg = Yahns::Config.new + host, port = @srv.addr[3], @srv.addr[1] + cfg.instance_eval do + ru = lambda { |_| [ 200, {'Content-Length'=>'2'}, ['HI'] ] } + GTL.synchronize { + app(:rack, ru) { + listen "#{host}:#{port}" + persistent_connections false + } + } + logger(Logger.new(err.path)) + end + srv = Yahns::Server.new(cfg) + pid = fork do + ENV["YAHNS_FD"] = @srv.fileno.to_s + srv.start.join + end + c = TCPSocket.new(host, port) + c.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") + buf = Timeout.timeout(10) { c.read } + assert_match(/Connection: close/, buf) + c.close + ensure + quit_wait(pid) + end end -- cgit v1.2.3-24-ge0c7