~ubuntu-branches/ubuntu/wily/ruby-excon/wily

« back to all changes in this revision

Viewing changes to lib/excon/response.rb

  • Committer: Package Import Robot
  • Author(s): Praveen Arimbrathodiyil
  • Date: 2013-05-16 20:49:12 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130516204912-bngjq57uhy40lzzq
Tags: 0.21.0-1
* Team upload
* New upstream release
* std version bunped to 3.9.4
* Update build dependencies (gem2deb (>= 0.3.0~), rake, ruby-rspec, 
  ruby-chronic, ruby-shindo, ruby-sinatra, ruby-open4, ruby-activesupport,
   ruby-delorean, ruby-eventmachine)
* debian/patches/02_remove_gemfile_lock.patch: Remove Gemfile.lock
* debian/patches/03_remove_rubygems_bundler_add_requires_from_gemspec.patch:
  Remove rubygems and bundler, add require lines instead of Bundler.require

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
module Excon
2
2
  class Response
3
3
 
4
 
    attr_accessor :body, :headers, :status
5
 
 
6
 
    def attributes
7
 
      {
8
 
        :body     => body,
9
 
        :headers  => headers,
10
 
        :status   => status
 
4
    attr_accessor :data
 
5
 
 
6
    # backwards compatability reader/writers
 
7
    def body=(new_body)
 
8
      @data[:body] = new_body
 
9
    end
 
10
    def body
 
11
      @data[:body]
 
12
    end
 
13
    def headers=(new_headers)
 
14
      @data[:headers] = new_headers
 
15
    end
 
16
    def headers
 
17
      @data[:headers]
 
18
    end
 
19
    def status=(new_status)
 
20
      @data[:status] = new_status
 
21
    end
 
22
    def status
 
23
      @data[:status]
 
24
    end
 
25
    def remote_ip=(new_remote_ip)
 
26
      @data[:remote_ip] = new_remote_ip
 
27
    end
 
28
    def remote_ip
 
29
      @data[:remote_ip]
 
30
    end
 
31
 
 
32
    def self.parse(socket, datum)
 
33
      datum[:response] = {
 
34
        :body       => '',
 
35
        :headers    => {},
 
36
        :status     => socket.read(12)[9, 11].to_i,
 
37
        :remote_ip  => socket.respond_to?(:remote_ip) && socket.remote_ip
11
38
      }
12
 
    end
13
 
 
14
 
    def initialize(attrs={})
15
 
      @body    = attrs[:body]    || ''
16
 
      @headers = attrs[:headers] || {}
17
 
      @status  = attrs[:status]
18
 
    end
19
 
 
20
 
    def self.parse(socket, params={})
21
 
      response = new(:status => socket.readline[9, 11].to_i)
 
39
      socket.readline # read the rest of the status line and CRLF
22
40
 
23
41
      until ((data = socket.readline).chop!).empty?
24
42
        key, value = data.split(/:\s*/, 2)
25
 
        response.headers[key] = ([*response.headers[key]] << value).compact.join(', ')
 
43
        datum[:response][:headers][key] = ([*datum[:response][:headers][key]] << value).compact.join(', ')
26
44
        if key.casecmp('Content-Length') == 0
27
45
          content_length = value.to_i
28
46
        elsif (key.casecmp('Transfer-Encoding') == 0) && (value.casecmp('chunked') == 0)
30
48
        end
31
49
      end
32
50
 
33
 
      unless (params[:method].to_s.casecmp('HEAD') == 0) || NO_ENTITY.include?(response.status)
 
51
      unless (['HEAD', 'CONNECT'].include?(datum[:method].to_s.upcase)) || NO_ENTITY.include?(datum[:response][:status])
34
52
 
35
53
        # check to see if expects was set and matched
36
 
        expected_status = !params.has_key?(:expects) || [*params[:expects]].include?(response.status)
 
54
        expected_status = !datum.has_key?(:expects) || [*datum[:expects]].include?(datum[:response][:status])
37
55
 
38
56
        # if expects matched and there is a block, use it
39
 
        if expected_status && params.has_key?(:response_block)
 
57
        if expected_status && datum.has_key?(:response_block)
40
58
          if transfer_encoding_chunked
41
59
            # 2 == "/r/n".length
42
60
            while (chunk_size = socket.readline.chop!.to_i(16)) > 0
43
 
              params[:response_block].call(socket.read(chunk_size + 2).chop!, nil, nil)
 
61
              datum[:response_block].call(socket.read(chunk_size + 2).chop!, nil, nil)
44
62
            end
45
63
            socket.read(2)
46
64
          elsif remaining = content_length
47
 
            remaining = content_length
48
65
            while remaining > 0
49
 
              params[:response_block].call(socket.read([CHUNK_SIZE, remaining].min), [remaining - CHUNK_SIZE, 0].max, content_length)
50
 
              remaining -= CHUNK_SIZE
 
66
              datum[:response_block].call(socket.read([datum[:chunk_size], remaining].min), [remaining - datum[:chunk_size], 0].max, content_length)
 
67
              remaining -= datum[:chunk_size]
51
68
            end
52
69
          else
53
 
            while remaining = socket.read(CHUNK_SIZE)
54
 
              params[:response_block].call(remaining, remaining.length, content_length)
 
70
            while remaining = socket.read(datum[:chunk_size])
 
71
              datum[:response_block].call(remaining, remaining.length, content_length)
55
72
            end
56
73
          end
57
74
        else # no block or unexpected status
58
75
          if transfer_encoding_chunked
59
76
            while (chunk_size = socket.readline.chop!.to_i(16)) > 0
60
 
              response.body << socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
 
77
              datum[:response][:body] << socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
61
78
            end
62
79
            socket.read(2) # 2 == "/r/n".length
63
80
          elsif remaining = content_length
64
81
            while remaining > 0
65
 
              response.body << socket.read([CHUNK_SIZE, remaining].min)
66
 
              remaining -= CHUNK_SIZE
 
82
              datum[:response][:body] << socket.read([datum[:chunk_size], remaining].min)
 
83
              remaining -= datum[:chunk_size]
67
84
            end
68
85
          else
69
 
            response.body << socket.read
 
86
            datum[:response][:body] << socket.read
70
87
          end
71
88
        end
72
89
      end
73
 
 
74
 
      response
 
90
      datum
 
91
    end
 
92
 
 
93
    def initialize(params={})
 
94
      @data = {
 
95
        :body     => '',
 
96
        :headers  => {}
 
97
      }.merge(params)
 
98
      @body      = @data[:body]
 
99
      @headers   = @data[:headers]
 
100
      @status    = @data[:status]
 
101
      @remote_ip = @data[:remote_ip]
 
102
    end
 
103
 
 
104
    def [](key)
 
105
      @data[key]
 
106
    end
 
107
 
 
108
    def params
 
109
      $stderr.puts("Excon::Response#params is deprecated use Excon::Response#data instead (#{caller.first})")
 
110
      data
75
111
    end
76
112
 
77
113
    # Retrieve a specific header value. Header names are treated case-insensitively.