excon 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of excon might be problematic. Click here for more details.

data/Gemfile CHANGED
@@ -6,8 +6,9 @@ group :benchmark do
6
6
  gem 'em-http-request'
7
7
  gem 'httparty'
8
8
  gem 'rest-client'
9
- gem 'tach', '0.0.5'
9
+ gem 'tach', '0.0.8'
10
10
  gem 'typhoeus'
11
11
  gem 'sinatra'
12
12
  gem 'streamly_ffi'
13
+ gem 'curb'
13
14
  end
@@ -6,7 +6,7 @@ Bundler.require(:benchmark)
6
6
 
7
7
  require 'sinatra/base'
8
8
 
9
- require File.join(File.dirname(__FILE__), '..', 'lib', 'excon')
9
+ require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'excon')
10
10
 
11
11
  module Excon
12
12
  class Server < Sinatra::Base
@@ -58,14 +58,35 @@ with_server do
58
58
 
59
59
  Tach.meter(1000) do
60
60
 
61
- tach('em-http-request') do
62
- EventMachine.run {
63
- http = EventMachine::HttpRequest.new(url).get
61
+ tach('curb (persistent)') do |n|
62
+ curb = Curl::Easy.new
63
+
64
+ n.times do
65
+ curb.url = url
66
+ curb.http_get
67
+ curb.body_str
68
+ end
69
+ end
64
70
 
65
- http.callback {
66
- http.response
67
- EventMachine.stop
68
- }
71
+ tach('em-http-request') do |n|
72
+ EventMachine.run {
73
+ count = 0
74
+
75
+ n.times do
76
+ http = EventMachine::HttpRequest.new(url).get
77
+
78
+ http.callback {
79
+ http.response
80
+ count += 1
81
+ EM.stop if count == n
82
+ }
83
+
84
+ http.errback {
85
+ http.response
86
+ count += 1
87
+ EM.stop if count == n
88
+ }
89
+ end
69
90
  }
70
91
  end
71
92
 
@@ -135,4 +156,4 @@ end
135
156
  # | open-uri | 2.936382 |
136
157
  # +--------------------------+----------+
137
158
  # | em-http-request | 3.933757 |
138
- # +--------------------------+----------+
159
+ # +--------------------------+----------+
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'excon'
16
- s.version = '0.2.8'
17
- s.date = '2010-11-30'
16
+ s.version = '0.3.0'
17
+ s.date = '2010-12-09'
18
18
  s.rubyforge_project = 'excon'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -13,7 +13,7 @@ require 'excon/response'
13
13
  module Excon
14
14
 
15
15
  unless const_defined?(:VERSION)
16
- VERSION = '0.2.8'
16
+ VERSION = '0.3.0'
17
17
  end
18
18
 
19
19
  unless const_defined?(:CHUNK_SIZE)
@@ -33,7 +33,9 @@ module Excon
33
33
  %w{connect delete get head options post put trace}.each do |method|
34
34
  eval <<-DEF
35
35
  def self.#{method}(url, params = {}, &block)
36
- new(url).request(params.merge!(:method => :#{method}), &block)
36
+ connection = new(url)
37
+ connection.reset
38
+ connection.request(params.merge!(:method => :#{method}), &block)
37
39
  end
38
40
  DEF
39
41
  end
@@ -19,7 +19,7 @@ module Excon
19
19
  def initialize(url, params = {})
20
20
  uri = URI.parse(url)
21
21
  @connection = {
22
- :headers => {},
22
+ :headers => { 'Host' => uri.host },
23
23
  :host => uri.host,
24
24
  :path => uri.path,
25
25
  :port => uri.port,
@@ -42,31 +42,37 @@ module Excon
42
42
  # @option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
43
43
  def request(params, &block)
44
44
  begin
45
- params[:path] ||= @connection[:path]
45
+ # connection has defaults, merge in new params to override
46
+ params = @connection.merge(params)
47
+ params[:headers] = @connection[:headers].merge(params[:headers])
48
+
49
+ # if path is empty or doesn't start with '/', insert one
46
50
  unless params[:path][0..0] == '/'
47
51
  params[:path].insert(0, '/')
48
52
  end
49
53
 
54
+ # start with "METHOD /path"
50
55
  request = params[:method].to_s.upcase << ' ' << params[:path]
51
- if query = (params[:query] || @connection[:query])
56
+
57
+ # add query to path, if there is one
58
+ case params[:query]
59
+ when String
60
+ request << '?' << params[:query]
61
+ when Hash
52
62
  request << '?'
53
- case query
54
- when String
55
- request << query
56
- else
57
- for key, values in query
58
- for value in [*values]
59
- value_string = value && ('=' << CGI.escape(value.to_s))
60
- request << key.to_s << value_string.to_s << '&'
61
- end
63
+ for key, values in params[:query]
64
+ for value in [*values]
65
+ value_string = value && ('=' << CGI.escape(value.to_s))
66
+ request << key.to_s << value_string.to_s << '&'
62
67
  end
63
- request.chop! # remove trailing '&'
64
68
  end
69
+ request.chop! # remove trailing '&'
65
70
  end
71
+
72
+ # finish first line with "HTTP/1.1\r\n"
66
73
  request << HTTP_1_1
67
- params[:headers] ||= @connection[:headers]
68
- params[:headers]['Host'] ||= params[:host] || @connection[:host]
69
- params[:body] ||= @connection[:body]
74
+
75
+ # calculate content length and set to handle non-ascii
70
76
  params[:headers]['Content-Length'] = case params[:body]
71
77
  when File
72
78
  params[:body].binmode
@@ -79,12 +85,19 @@ module Excon
79
85
  else
80
86
  0
81
87
  end
88
+
89
+ # add headers to request
82
90
  for key, value in params[:headers]
83
91
  request << key.to_s << ': ' << value.to_s << CR_NL
84
92
  end
93
+
94
+ # add additional "\r\n" to indicate end of headers
85
95
  request << CR_NL
96
+
97
+ # write out the request, sans body
86
98
  socket.write(request)
87
99
 
100
+ # write out the body
88
101
  if params[:body]
89
102
  if params[:body].is_a?(String)
90
103
  socket.write(params[:body])
@@ -95,6 +108,7 @@ module Excon
95
108
  end
96
109
  end
97
110
 
111
+ # read the response
98
112
  response = Excon::Response.parse(socket, params, &block)
99
113
  if response.headers['Connection'] == 'close'
100
114
  reset
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 8
9
- version: 0.2.8
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - geemus (Wesley Beary)
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-30 00:00:00 -08:00
17
+ date: 2010-12-09 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency