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 +2 -1
- data/benchmarks/excon_vs.rb +30 -9
- data/excon.gemspec +2 -2
- data/lib/excon.rb +4 -2
- data/lib/excon/connection.rb +30 -16
- metadata +4 -4
data/Gemfile
CHANGED
data/benchmarks/excon_vs.rb
CHANGED
@@ -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('
|
62
|
-
|
63
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
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
|
+
# +--------------------------+----------+
|
data/excon.gemspec
CHANGED
@@ -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.
|
17
|
-
s.date = '2010-
|
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
|
data/lib/excon.rb
CHANGED
@@ -13,7 +13,7 @@ require 'excon/response'
|
|
13
13
|
module Excon
|
14
14
|
|
15
15
|
unless const_defined?(:VERSION)
|
16
|
-
VERSION = '0.
|
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)
|
36
|
+
connection = new(url)
|
37
|
+
connection.reset
|
38
|
+
connection.request(params.merge!(:method => :#{method}), &block)
|
37
39
|
end
|
38
40
|
DEF
|
39
41
|
end
|
data/lib/excon/connection.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
68
|
-
|
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
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
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-
|
17
|
+
date: 2010-12-09 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|