-
Notifications
You must be signed in to change notification settings - Fork 102
Better support for connection upgrade and bi-directional streaming. #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
c5d050d
8bf8c89
e9f7c2e
2ac0fa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,11 @@ class InvalidHeader < StandardError | |
|
||
attr_reader :sent_size | ||
|
||
## | ||
# Set the response body proc as an streaming/upgrade response. | ||
|
||
attr_accessor :upgrade | ||
|
||
## | ||
# Creates a new HTTP response object. WEBrick::Config::HTTP is the | ||
# default configuration. | ||
|
@@ -217,6 +222,16 @@ def keep_alive? | |
@keep_alive | ||
end | ||
|
||
## | ||
# Sets the response to be a streaming/upgrade response. | ||
# This will disable keep-alive and chunked transfer encoding. | ||
|
||
def upgrade!(protocol) | ||
@upgrade = protocol | ||
@keep_alive = false | ||
@chunked = false | ||
end | ||
|
||
## | ||
# Sends the response on +socket+ | ||
|
||
|
@@ -242,6 +257,14 @@ def setup_header() # :nodoc: | |
@header['server'] ||= @config[:ServerSoftware] | ||
@header['date'] ||= Time.now.httpdate | ||
|
||
if @upgrade | ||
@header['connection'] = 'upgrade' | ||
@header['upgrade'] = @upgrade | ||
@keep_alive = false | ||
|
||
return | ||
end | ||
|
||
# HTTP/0.9 features | ||
if @request_http_version < "1.0" | ||
@http_version = HTTPVersion.new("0.9") | ||
|
@@ -268,11 +291,10 @@ def setup_header() # :nodoc: | |
elsif %r{^multipart/byteranges} =~ @header['content-type'] | ||
@header.delete('content-length') | ||
elsif @header['content-length'].nil? | ||
if @body.respond_to? :readpartial | ||
elsif @body.respond_to? :call | ||
make_body_tempfile | ||
jeremyevans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else | ||
if @body.respond_to?(:bytesize) | ||
@header['content-length'] = (@body ? @body.bytesize : 0).to_s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah, good call, I just copied the previous code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, fixed. |
||
else | ||
@header['connection'] = 'close' | ||
end | ||
end | ||
|
||
|
@@ -517,14 +539,16 @@ def send_body_proc(socket) | |
@body.call(ChunkedWrapper.new(socket, self)) | ||
socket.write("0#{CRLF}#{CRLF}") | ||
else | ||
size = @header['content-length'].to_i | ||
if @bodytempfile | ||
@bodytempfile.rewind | ||
IO.copy_stream(@bodytempfile, socket) | ||
else | ||
@body.call(socket) | ||
end | ||
@sent_size = size | ||
|
||
if content_length = @header['content-length'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out that since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I'm fine with that behaviour, or alternatively we could set it to some other value, e.g. |
||
@sent_size = content_length.to_i | ||
end | ||
end | ||
end | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.