Sha256: 4f54d963129ce2ed9533203959e162942457e266ffa29f9ac091dad07c2d82a6

Contents?: true

Size: 1.54 KB

Versions: 10

Compression:

Stored size: 1.54 KB

Contents

require 'java'
module Jubilee
  class Response
    include Const
    include org.jruby.jubilee.RackResponse

    def initialize(array)
      @status, @headers, @body = *array
      @status = @status.to_i
      @content_length = nil
      if @body.kind_of? Array and @body.size == 1
        @content_length = @body[0].bytesize
      end
    end

    # See Rack::Utils::
    def respond(response)
      no_body = @status < 200 || STATUS_WITH_NO_ENTITY_BODY[@status]
      write_status(response)
      write_headers(response)
      if no_body
        response.end
      else 
        if @body.respond_to?(:to_path)
          response.send_file(@body.to_path)
        else
          write_body(response)
          response.end
        end
      end
    rescue NativeException => e
      puts e
    ensure
      @body.close if @body.respond_to?(:close)
    end

    private
    def write_status(response)
      response.status_code = @status
    end

    def write_headers(response)
      @headers.each do |key, values|
        case key
        when CONTENT_LENGTH
          @content_length = values
          next
        when TRANSFER_ENCODING
          @content_length = nil
        end
        # Multiple values are joined by \n
        response.put_header(key, values)
      end
    end

    def write_body(response)
      response.put_default_headers
      if @content_length
        response.put_header(CONTENT_LENGTH, @content_length.to_s)
      else
        response.chunked = true
      end

      @body.each do |part|
        response.write(part)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
jubilee-2.1.0.Alpha1-java lib/jubilee/response.rb
jubilee-2.0.0-java lib/jubilee/response.rb
jubilee-2.0.0.beta-java lib/jubilee/response.rb
jubilee-2.0.0.alpha1-java lib/jubilee/response.rb
jubilee-1.1.3-java lib/jubilee/response.rb
jubilee-1.1.2-java lib/jubilee/response.rb
jubilee-1.1.0-java lib/jubilee/response.rb
jubilee-1.1.0.rc3 lib/jubilee/response.rb
jubilee-1.1.0.rc2 lib/jubilee/response.rb
jubilee-1.1.0.rc1 lib/jubilee/response.rb