Sha256: c5761d26c7817d19f197d54bb7d42ec4d5b6cc68f4cc1abd7dab043cdec0e388

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 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['Transfer-Encoding'] && response.headers['Transfer-Encoding'].downcase == '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
        elsif response.headers['Connection'] && response.headers['Connection'].downcase == '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
        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

5 entries across 5 versions & 2 rubygems

Version Path
excon-0.2.6 lib/excon/response.rb
bbcloud-0.8.1 lib/bbcloud/vendor/excon-0.2.4/lib/excon/response.rb
excon-0.2.4 lib/excon/response.rb
excon-0.2.3 lib/excon/response.rb
excon-0.2.2 lib/excon/response.rb