lib/net/http/server/daemon.rb in net-http-server-0.1.0 vs lib/net/http/server/daemon.rb in net-http-server-0.2.0
- old
+ new
@@ -1,8 +1,10 @@
require 'net/http/server/parser'
require 'net/http/server/requests'
require 'net/http/server/responses'
+require 'net/http/server/stream'
+require 'net/http/server/chunked_stream'
require 'net/protocol'
require 'gserver'
module Net
@@ -34,11 +36,11 @@
# The port to listen on.
#
# @option options [Integer] :max_connections (MAX_CONNECTIONS)
# The maximum number of simultaneous connections.
#
- # @option options [IO] :log (STDERR)
+ # @option options [IO] :log ($stderr)
# The log to write errors to.
#
# @option options [#call] :handler
# The HTTP Request Handler object.
#
@@ -53,11 +55,11 @@
#
def initialize(options={},&block)
host = options.fetch(:host,DEFAULT_HOST)
port = options.fetch(:port,DEFAULT_PORT).to_i
max_connections = options.fetch(:max_connections,MAX_CONNECTIONS)
- log = options.fetch(:log,STDERR)
+ log = options.fetch(:log,$stderr)
super(port,host,max_connections,log,false,true)
handler(options[:handler],&block)
end
@@ -66,18 +68,18 @@
# Sets the HTTP Request Handler.
#
# @param [#call, nil] object
# The HTTP Request Handler object.
#
- # @yield [request, socket]
+ # @yield [request, stream]
# If a block is given, it will be used to process HTTP Requests.
#
# @yieldparam [Hash{Symbol => String,Array,Hash}] request
# The HTTP Request.
#
- # @yieldparam [TCPSocket] socket
- # The TCP socket of the client.
+ # @yieldparam [Stream, ChunkedStream] stream
+ # The stream of the HTTP Request body.
#
# @raise [ArgumentError]
# The HTTP Request Handler must respond to `#call`.
#
def handler(object=nil,&block)
@@ -108,11 +110,17 @@
return Responses::BAD_REQUEST
end
normalize_request(request)
+ stream = if request[:headers]['Transfer-Encoding'] == 'chunked'
+ ChunkedStream.new(socket)
+ else
+ Stream.new(socket)
+ end
+
# rack compliant
- status, headers, body = @handler.call(request,socket)
+ status, headers, body = @handler.call(request,stream)
write_response(socket,status,headers,body)
end
end