~ubuntu-branches/ubuntu/utopic/ruby-excon/utopic

« back to all changes in this revision

Viewing changes to lib/excon/connection.rb

  • Committer: Package Import Robot
  • Author(s): Laurent Bigonville
  • Date: 2013-07-06 23:48:43 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130706234843-vyj5m4o7q6hlhwo4
Tags: 0.25.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
    attr_reader :data
5
5
 
6
6
    def connection
7
 
      $stderr.puts("Excon::Connection#connection is deprecated use Excon::Connection#data instead (#{caller.first})")
 
7
      Excon.display_warning("Excon::Connection#connection is deprecated use Excon::Connection#data instead (#{caller.first})")
8
8
      @data
9
9
    end
10
10
    def connection=(new_params)
11
 
      $stderr.puts("Excon::Connection#connection= is deprecated use Excon::Connection#data= instead (#{caller.first})")
 
11
      Excon.display_warning("Excon::Connection#connection= is deprecated use Excon::Connection#data= instead (#{caller.first})")
12
12
      @data = new_params
13
13
    end
14
14
 
15
15
    def params
16
 
      $stderr.puts("Excon::Connection#params is deprecated use Excon::Connection#data instead (#{caller.first})")
 
16
      display_waring("Excon::Connection#params is deprecated use Excon::Connection#data instead (#{caller.first})")
17
17
      @data
18
18
    end
19
19
    def params=(new_params)
20
 
      $stderr.puts("Excon::Connection#params= is deprecated use Excon::Connection#data= instead (#{caller.first})")
 
20
      Excon.display_warning("Excon::Connection#params= is deprecated use Excon::Connection#data= instead (#{caller.first})")
21
21
      @data = new_params
22
22
    end
23
23
 
24
24
    def proxy
25
 
      $stderr.puts("Excon::Connection#proxy is deprecated use Excon::Connection#data[:proxy] instead (#{caller.first})")
 
25
      Excon.display_warning("Excon::Connection#proxy is deprecated use Excon::Connection#data[:proxy] instead (#{caller.first})")
26
26
      @data[:proxy]
27
27
    end
28
28
    def proxy=(new_proxy)
29
 
      $stderr.puts("Excon::Connection#proxy= is deprecated use Excon::Connection#data[:proxy]= instead (#{caller.first})")
 
29
      Excon.display_warning("Excon::Connection#proxy= is deprecated use Excon::Connection#data[:proxy]= instead (#{caller.first})") if !ENV['VERBOSE'].nil?
30
30
      @data[:proxy] = new_proxy
31
31
    end
32
32
 
106
106
          # start with "METHOD /path"
107
107
          request = datum[:method].to_s.upcase << ' '
108
108
          if @data[:proxy]
109
 
            request << datum[:scheme] << '://' << @data[:host] << ':' << @data[:port].to_s
 
109
            request << datum[:scheme] << '://' << @data[:host] << port_string(@data)
110
110
          end
111
111
          request << datum[:path]
112
112
 
149
149
          # add additional "\r\n" to indicate end of headers
150
150
          request << CR_NL
151
151
 
152
 
          # write out the request, sans body
153
 
          socket.write(request)
154
 
 
155
 
          # write out the body
156
152
          if datum.has_key?(:request_block)
157
 
            while true
 
153
            socket.write(request) # write out request + headers
 
154
            while true # write out body with chunked encoding
158
155
              chunk = datum[:request_block].call
159
156
              if FORCE_ENC
160
157
                chunk.force_encoding('BINARY')
167
164
              end
168
165
            end
169
166
          elsif !datum[:body].nil?
170
 
            if datum[:body].is_a?(String)
171
 
              unless datum[:body].empty?
172
 
                socket.write(datum[:body])
173
 
              end
174
 
            else
 
167
            if datum[:body].is_a?(String) # write out string body
 
168
              socket.write(request << datum[:body]) # write out request + headers + body
 
169
            else # write out file body
 
170
              socket.write(request) # write out request + headers
175
171
              if datum[:body].respond_to?(:binmode)
176
172
                datum[:body].binmode
177
173
              end
182
178
                socket.write(chunk)
183
179
              end
184
180
            end
 
181
          else # write out nil body
 
182
            socket.write(request) # write out request + headers
185
183
          end
186
184
        end
187
185
      rescue => error
197
195
    end
198
196
 
199
197
    def response_call(datum)
 
198
      if datum.has_key?(:response_block) && !datum[:response][:body].empty?
 
199
        content_length = remaining = datum[:response][:body].bytesize
 
200
        while remaining > 0
 
201
          datum[:response_block].call(datum[:response][:body].slice!(0, [datum[:chunk_size], remaining].min), [remaining - datum[:chunk_size], 0].max, content_length)
 
202
          remaining -= datum[:chunk_size]
 
203
        end
 
204
      end
200
205
      datum
201
206
    end
202
207
 
215
220
      datum = @data.merge(params)
216
221
      invalid_keys_warning(params, VALID_CONNECTION_KEYS)
217
222
      datum[:headers] = @data[:headers].merge(datum[:headers] || {})
218
 
      datum[:headers]['Host']   ||= '' << datum[:host] << ':' << datum[:port].to_s
 
223
 
 
224
      datum[:headers]['Host']   ||= '' << datum[:host] << port_string(datum)
219
225
      datum[:retries_remaining] ||= datum[:retry_limit]
220
226
 
221
227
      # if path is empty or doesn't start with '/', insert one
224
230
      end
225
231
 
226
232
      if block_given?
227
 
        $stderr.puts("Excon requests with a block are deprecated, pass :response_block instead (#{caller.first})")
 
233
        Excon.display_warning("Excon requests with a block are deprecated, pass :response_block instead (#{caller.first})")
228
234
        datum[:response_block] = Proc.new
229
235
      end
230
236
 
281
287
    end
282
288
 
283
289
    def retry_limit=(new_retry_limit)
284
 
      $stderr.puts("Excon::Connection#retry_limit= is deprecated, pass :retry_limit to the initializer (#{caller.first})")
 
290
      Excon.display_warning("Excon::Connection#retry_limit= is deprecated, pass :retry_limit to the initializer (#{caller.first})")
285
291
      @data[:retry_limit] = new_retry_limit
286
292
    end
287
293
 
288
294
    def retry_limit
289
 
      $stderr.puts("Excon::Connection#retry_limit is deprecated, pass :retry_limit to the initializer (#{caller.first})")
 
295
      Excon.display_warning("Excon::Connection#retry_limit is deprecated, pass :retry_limit to the initializer (#{caller.first})")
290
296
      @data[:retry_limit] ||= DEFAULT_RETRY_LIMIT
291
297
    end
292
298
 
335
341
    def invalid_keys_warning(argument, valid_keys)
336
342
      invalid_keys = argument.keys - valid_keys
337
343
      unless invalid_keys.empty?
338
 
        $stderr.puts("The following keys are invalid: #{invalid_keys.map(&:inspect).join(', ')}")
 
344
        Excon.display_warning("The following keys are invalid: #{invalid_keys.map(&:inspect).join(', ')}")
339
345
      end
340
346
    end
341
347
 
385
391
      end
386
392
    end
387
393
 
 
394
    def port_string(datum)
 
395
      if datum[:omit_default_port] && ((datum[:scheme].casecmp('http') == 0 && datum[:port].to_i == 80) || (datum[:scheme].casecmp('https') == 0 && datum[:port].to_i == 443))
 
396
        ''
 
397
      else
 
398
        ':' << datum[:port].to_s
 
399
      end
 
400
    end
388
401
  end
389
402
end