lib/fishwife/rack_servlet.rb
# Turns a Rack response into a Servlet response.
#
# Note that keep-alive *only* happens if we get either a pathname
# (because we can find the length ourselves), or if we get a
# Content-Length header as part of the response.  While we can
# readily buffer the response object to figure out how long it is,
# we have no guarantee that we aren't going to be buffering
# something *huge*.
#
# http://docstore.mik.ua/orelly/java-ent/servlet/ch05_03.htm
Turns a Rack response into a Servlet response.
Note that keep-alive *only* happens if we get either a pathname
(because we can find the length ourselves), or if we get a
Content-Length header as part of the response.  While we can
readily buffer the response object to figure out how long it is,
we have no guarantee that we aren't going to be buffering
something *huge*.


Turns a Rack response into a Servlet response . Note that keep - alive * only * happens if we get either a pathname ( because we can find the length ourselves ) or if we get a Content - Length header as part of the response . While we can readily buffer the response object to figure out how long it is we have no guarantee that we aren ' t going to be buffering something * huge * .
def rack_to_servlet(rack_response, request, response)
      status, headers, body = rack_response
      response.setStatus(status.to_i)
      response_hijack_callback = nil
      headers.each do |h, v|
        case h
        when 'Content-Length'
          response.setContentLength(v.to_i) if v
        when 'Content-Type'
          response.setContentType(v) if v
        when 'rack.hijack'
          response_hijack_callback = v
        else
          v.split("\n").each { |val| response.addHeader(h, val) }
        end
      end
      if response_hijack_callback
        response_hijack_callback.call( HijackedIO.new(request.start_async) )
      else
        output = response.getOutputStream
        if body.respond_to?( :to_path )
          path = body.to_path
          unless headers['Content-Length']
            response.setContentLength( File.size( path ) )
          end
          IOUtil.write_file( path, output )
        else
          IOUtil.write_body( body, output )
        end
        body.close if body.respond_to?(:close)
        output.close
      end
    end
def rack_to_servlet ( rack_response , request , response ) status , headers , body = rack_response response . setStatus ( status . to_i ) response_hijack_callback = nil headers . each do | h , v | case h when 'Content-Length' response . setContentLength ( v . to_i ) if v when 'Content-Type' response . setContentType ( v ) if v when 'rack.hijack' response_hijack_callback = v else v . split ( "\n" ) . each { | val | response . addHeader ( h , val ) } end end if response_hijack_callback response_hijack_callback . call ( HijackedIO . new ( request . start_async ) ) else output = response . getOutputStream if body . respond_to? ( :to_path ) path = body . to_path unless headers [ 'Content-Length' ] response . setContentLength ( File . size ( path ) ) end IOUtil . write_file ( path , output ) else IOUtil . write_body ( body , output ) end body . close if body . respond_to? ( :close ) output . close end end
Turns a Rack response into a Servlet response.
Turns a Rack response into a Servlet response .
# Split apart the Rack response. # Set the HTTP status code. # Add all the result headers. # FIXME: Support ranges? # Close the body if we're supposed to. # All done.
rack_response
<not_specific>
request
<not_specific>
response
<not_specific>