diff options
Diffstat (limited to 'lib/kcar/response.rb')
-rw-r--r-- | lib/kcar/response.rb | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/kcar/response.rb b/lib/kcar/response.rb index ef93f12..38d35ac 100644 --- a/lib/kcar/response.rb +++ b/lib/kcar/response.rb @@ -4,7 +4,7 @@ module Kcar # This may be used to generate a Rack response # -class Response < Struct.new(:sock, :unchunk, :hdr, :buf, :parser) +class Response < Struct.new(:sock, :hdr, :unchunk, :buf, :parser) # :stopdoc: LAST_CHUNK = "0\r\n" @@ -17,15 +17,21 @@ class Response < Struct.new(:sock, :unchunk, :hdr, :buf, :parser) # 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, unchunk = true, hdr = {}) - super(sock, unchunk, hdr, "", Parser.new) + def initialize(sock, hdr = {}, unchunk = true) + super(sock, hdr, unchunk, "", Parser.new) end - # returns a 3-element array suitable for use as a Rack response: + # returns a 3-element array that resembles a Rack response, but is + # more useful for additional processing by other code. + # # [ status, headers, body ] # + # Use Kcar::Response#rack if you want to guaranteee a proper Rack response. + # # this method will not return until the response headers are fully parsed, # but the body returned will be this Kcar::Response handler itself. + # +unchunk+ must be true to guarantee trailers will be stored in the + # returned +header+ object def read buf << sock.readpartial(READ_SIZE) if buf.empty? while (response = parser.headers(hdr, buf)).nil? @@ -34,6 +40,17 @@ class Response < Struct.new(:sock, :unchunk, :hdr, :buf, :parser) response << self end + # returns a 3-element array suitable for use as a Rack response: + # [ status, headers, body ] + # + # this method will not return until the response headers are fully parsed, + # but the body returned will be this Kcar::Response handler itself. + # It is not guaranteed that trailers will be stored in the returned +header+ + def rack + self.unchunk = false + read + end + # this is expected to be called by our Rack server, it will close # our given +sock+ object if keepalive is not used otherwise it # will just reset the parser and clear the header object |