Sha256: c1c038f9c5bb36d9b7b4914e8762a3fa811c79d1543bddfe6deec3d4f2cdfd7b

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

module Reel
  class Response
    class Writer

      CRLF = "\r\n"
      def initialize(socket, connection)
        @socket = socket
        @connection = connection
      end

      # Write body chunks directly to the connection
      def write(chunk)
        chunk_header = chunk.bytesize.to_s(16)
        @socket << chunk_header + CRLF
        @socket << chunk + CRLF
      end

      # Finish the response and reset the response state to header
      def finish_response
        @socket << "0#{CRLF * 2}"
      end

      # Takes a Reel::Response and renders it
      # back over the socket.
      def handle_response(response)
        @socket << render_header(response)
        if response.respond_to?(:render)
          response.render(@socket)
        else
          case response.body
          when String
            @socket << response.body
          when IO
            begin
              if !defined?(JRUBY_VERSION)
                IO.copy_stream(response.body, @socket)
              else
                # JRuby 1.6.7 doesn't support IO.copy_stream :(
                while data = response.body.read(4096)
                  @socket << data
                end
              end
            ensure
              response.body.close
            end
          when Enumerable
            response.body.each do |chunk|
              write(chunk)
            end

            finish_response
          end
        end
      end

      # Convert headers into a string
      def render_header(response)
        response_header = "#{response.version} #{response.status} #{response.reason}#{CRLF}"
        unless response.headers.empty?
          response_header << response.headers.map do |header, value|
            "#{header}: #{value}"
          end.join(CRLF) << CRLF
        end
        response_header << CRLF
      end
      private :render_header
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reel-0.4.0.pre4 lib/reel/response_writer.rb
reel-0.4.0.pre3 lib/reel/response_writer.rb
reel-0.4.0.pre2 lib/reel/response_writer.rb