@@ -105,6 +105,11 @@ class InvalidHeader < StandardError
105105
106106 attr_reader :sent_size
107107
108+ ##
109+ # Set the response body proc as an streaming/upgrade response.
110+
111+ attr_accessor :upgrade
112+
108113 ##
109114 # Creates a new HTTP response object. WEBrick::Config::HTTP is the
110115 # default configuration.
@@ -217,6 +222,16 @@ def keep_alive?
217222 @keep_alive
218223 end
219224
225+ ##
226+ # Sets the response to be a streaming/upgrade response.
227+ # This will disable keep-alive and chunked transfer encoding.
228+
229+ def upgrade! ( protocol )
230+ @upgrade = protocol
231+ @keep_alive = false
232+ @chunked = false
233+ end
234+
220235 ##
221236 # Sends the response on +socket+
222237
@@ -242,6 +257,14 @@ def setup_header() # :nodoc:
242257 @header [ 'server' ] ||= @config [ :ServerSoftware ]
243258 @header [ 'date' ] ||= Time . now . httpdate
244259
260+ if @upgrade
261+ @header [ 'connection' ] = 'upgrade'
262+ @header [ 'upgrade' ] = @upgrade
263+ @keep_alive = false
264+
265+ return
266+ end
267+
245268 # HTTP/0.9 features
246269 if @request_http_version < "1.0"
247270 @http_version = HTTPVersion . new ( "0.9" )
@@ -268,11 +291,10 @@ def setup_header() # :nodoc:
268291 elsif %r{^multipart/byteranges} =~ @header [ 'content-type' ]
269292 @header . delete ( 'content-length' )
270293 elsif @header [ 'content-length' ] . nil?
271- if @body . respond_to? :readpartial
272- elsif @body . respond_to? :call
273- make_body_tempfile
294+ if @body . respond_to? ( :bytesize )
295+ @header [ 'content-length' ] = @body . bytesize . to_s
274296 else
275- @header [ 'content-length ' ] = ( @body ? @body . bytesize : 0 ) . to_s
297+ @header [ 'connection ' ] = 'close'
276298 end
277299 end
278300
@@ -517,14 +539,16 @@ def send_body_proc(socket)
517539 @body . call ( ChunkedWrapper . new ( socket , self ) )
518540 socket . write ( "0#{ CRLF } #{ CRLF } " )
519541 else
520- size = @header [ 'content-length' ] . to_i
521542 if @bodytempfile
522543 @bodytempfile . rewind
523544 IO . copy_stream ( @bodytempfile , socket )
524545 else
525546 @body . call ( socket )
526547 end
527- @sent_size = size
548+
549+ if content_length = @header [ 'content-length' ]
550+ @sent_size = content_length . to_i
551+ end
528552 end
529553 end
530554
0 commit comments