diff options
author | Eric Wong <e@80x24.org> | 2013-10-26 01:13:29 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-10-26 02:00:17 +0000 |
commit | f38e54f8d54f8cdfdc15f43b2394f0acfff5d413 (patch) | |
tree | 5a26e626a3fcee6787f62d1dd885137604db97c5 /lib/yahns/http_response.rb | |
parent | 8671b632849216476305573e87b5626bc34fedf1 (diff) | |
download | yahns-f38e54f8d54f8cdfdc15f43b2394f0acfff5d413.tar.gz |
The tiny responses for check_client_connection and "100-Continue" responses may occasionally fail with EAGAIN. We must be prepared for those corner cases and buffer the output appropriately. We can safely use a string for buffering here (for once(!)), since the buffer sizes are bounded and known at buffer time, unlike the response headers/bodies sent by the Rack application.
Diffstat (limited to 'lib/yahns/http_response.rb')
-rw-r--r-- | lib/yahns/http_response.rb | 72 |
1 files changed, 58 insertions, 14 deletions
diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb index a34832a..df935e9 100644 --- a/lib/yahns/http_response.rb +++ b/lib/yahns/http_response.rb @@ -2,6 +2,7 @@ # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) require_relative 'stream_file' +require_relative 'wbuf_str' # Writes a Rack response to your client using the HTTP/1.1 specification. # You use it by simply doing: @@ -18,7 +19,8 @@ module Yahns::HttpResponse # :nodoc: CONN_KA = "Connection: keep-alive\r\n\r\n" CONN_CLOSE = "Connection: close\r\n\r\n" Z = "" - RESPONSE_START = "HTTP/1.1 " + CCC_RESPONSE_START = [ 'HTTP', '/1.1 ' ].map!(&:freeze) + RESPONSE_START = CCC_RESPONSE_START.join('') R100_RAW = "HTTP/1.1 100 Continue\r\n\r\n" R100_CCC = "100 Continue\r\n\r\nHTTP/1.1 " HTTP_EXPECT = "HTTP_EXPECT" @@ -27,22 +29,13 @@ module Yahns::HttpResponse # :nodoc: @response_start_sent ? Z : RESPONSE_START end - # only used if input_buffering is true (not :lazy or false) - # :lazy/false gives control to the app - def http_100_response(env) - if env[HTTP_EXPECT] =~ /\A100-continue\z/i - kgio_write(@response_start_sent ? R100_CCC : R100_RAW) - env.delete(HTTP_EXPECT) - end - end - def response_wait_write(rv) # call the kgio_wait_readable or kgio_wait_writable method ok = __send__("kgio_#{rv}") and return ok k = self.class k.logger.info("fd=#{fileno} ip=#@kgio_addr timeout on :#{rv} after "\ "#{k.client_timeout}s") - nil + false end def err_response(code) @@ -93,7 +86,7 @@ module Yahns::HttpResponse # :nodoc: # shutdown is needed in case the app forked, we rescue here since # StreamInput may issue shutdown as well shutdown rescue nil - nil # trigger close + :close end end @@ -145,7 +138,7 @@ module Yahns::HttpResponse # :nodoc: body = nil # ensure we do not close body in ensure return rv else - response_wait_write(rv) or return + response_wait_write(rv) or return :close end end while true end @@ -176,7 +169,7 @@ module Yahns::HttpResponse # :nodoc: rv = wbuf.wbuf_write(self, chunk) break else - response_wait_write(rv) or return + response_wait_write(rv) or return :close end end while true end @@ -193,4 +186,55 @@ module Yahns::HttpResponse # :nodoc: ensure body.respond_to?(:close) and body.close end + + # returns nil on success + # :wait_readable/:wait_writable/:close for epoll + def do_ccc + @response_start_sent = true + wbuf = nil + rv = nil + CCC_RESPONSE_START.each do |buf| + if wbuf + wbuf << buf + else + case rv = kgio_trywrite(buf) + when nil + break + when String + buf = rv + when :wait_writable, :wait_readable + if self.class.output_buffering + wbuf = buf.dup + @state = Yahns::WbufStr.new(wbuf, :ccc_done) + break + else + response_wait_write(rv) or return :close + end + end while true + end + end + rv + end + + # only used if input_buffering is true (not :lazy or false) + # input_buffering==:lazy/false gives control to the app + # returns nil on success + # returns :close, :wait_writable, or :wait_readable + def http_100_response(env) + env.delete(HTTP_EXPECT) =~ /\A100-continue\z/i or return nil + buf = @response_start_sent ? R100_CCC : R100_RAW + case rv = kgio_trywrite(buf) + when String + buf = rv + when :wait_writable, :wait_readable + if self.class.output_buffering + @state = Yahns::WbufStr.new(buf, :r100_done) + return rv + else + response_wait_write(rv) or return :close + end + else + return rv + end while true + end end |