Sha256: eaea192b2540ebd0bc8c982f8a2c3ffad302f686b1b38c14989ffea9052a967b

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

module Excon
  class Response

    def self.parse(socket, params = {}, &block)
      if params[:block]
        print "  \e[33m[WARN] params[:block] is deprecated, please pass the block to the request\e[0m"
        block = params[:block]
      end

      response = new

      response.status = socket.readline[9..11].to_i
      while true
        data = socket.readline.chop!
        unless data.empty?
          key, value = data.split(': ')
          response.headers[key] = value
        else
          break
        end
      end

      unless params[:method] == 'HEAD'
        if !block || (params[:expects] && ![*params[:expects]].include?(response.status))
          response.body = ''
          block = lambda { |chunk| response.body << chunk }
        end

        if response.headers['Connection'] == 'close'
          block.call(socket.read)
        elsif response.headers['Content-Length']
          remaining = response.headers['Content-Length'].to_i
          while remaining > 0
            block.call(socket.read([CHUNK_SIZE, remaining].min))
            remaining -= CHUNK_SIZE
          end
        elsif response.headers['Transfer-Encoding'] == 'chunked'
          while true
            chunk_size = socket.readline.chop!.to_i(16)
            chunk = socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
            if chunk_size > 0
              block.call(chunk)
            else
              break
            end
          end
        end
      end

      response
    end

    attr_accessor :body, :headers, :status

    def initialize(attributes = {})
      @body    = attributes[:body] || ''
      @headers = attributes[:headers] || {}
      @status  = attributes[:status]
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
excon-0.2.0 lib/excon/response.rb
excon-0.1.4 lib/excon/response.rb
excon-0.1.3 lib/excon/response.rb
excon-0.1.2 lib/excon/response.rb
excon-0.1.1 lib/excon/response.rb
excon-0.1.0 lib/excon/response.rb
excon-0.0.26 lib/excon/response.rb
excon-0.0.25 lib/excon/response.rb
excon-0.0.24 lib/excon/response.rb