lib/kcar/response.rb in kcar-0.6.0 vs lib/kcar/response.rb in kcar-0.7.0
- old
+ new
@@ -1,25 +1,24 @@
# -*- encoding: binary -*-
-# This may be used to generate a Rack response
-#
-class Kcar::Response
- attr_accessor :sock, :hdr, :unchunk, :buf, :parser
+# This may be used to generate a Rack response synchronously.
+class Kcar::Response
# :stopdoc:
- Parser = Kcar::Parser
+ attr_accessor :sock, :hdr, :unchunk, :buf, :parser
# :startdoc:
# By default we readpartial at most 16K off a socket at once
READ_SIZE = 0x4000
# initializes a socket, +sock+ must respond to the "readpartial"
# method. +unchunk+ may be set to disable transparent unchunking
# +hdr+ may be a Hash, Array, or Rack::Utils::HeaderHash
def initialize(sock, hdr = {}, unchunk = true)
- @sock, @hdr, @unchunk, @buf, @parser = sock, hdr, unchunk, "", Parser.new
+ @sock, @hdr, @unchunk, @buf = sock, hdr, unchunk, ""
+ @parser = Kcar::Parser.new
end
# returns a 3-element array that resembles a Rack response, but is
# more useful for additional processing by other code.
#
@@ -61,15 +60,15 @@
# body. It may only be called once (usually by a Rack server) as it streams
# the response body off the our socket object.
def each
return if @parser.body_eof?
if @unchunk
- @parser.chunked? ? each_unchunk { |x| yield x } :
- each_identity { |x| yield x }
+ @parser.chunked? ? each_unchunk { |buf| yield buf } :
+ each_identity { |buf| yield buf }
else
- @parser.chunked? ? each_rechunk { |x| yield x } :
- each_identity { |x| yield x }
+ @parser.chunked? ? each_rechunk { |buf| yield buf } :
+ each_identity { |buf| yield buf }
end
end
# :stopdoc:
def reset
@@ -140,10 +139,10 @@
if len > 0
begin
len -= @sock.readpartial(len > READ_SIZE ? READ_SIZE : len, dst).size
yield dst
end while len > 0
- dst.respond_to?(:clear) ? dst.clear : @buf = ""
+ dst.clear
end
end
@parser.body_bytes_left = 0
end