em-http-request 0.2.10 → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Changelog.md +36 -0
- data/LICENSE +58 -58
- data/README.md +168 -0
- data/Rakefile +9 -5
- data/VERSION +1 -1
- data/em-http-request.gemspec +12 -9
- data/examples/fetch.rb +30 -30
- data/examples/fibered-http.rb +38 -38
- data/examples/oauth-tweet.rb +49 -49
- data/examples/socks5.rb +26 -0
- data/examples/websocket-handler.rb +28 -28
- data/examples/websocket-server.rb +8 -8
- data/ext/buffer/extconf.rb +53 -53
- data/ext/http11_client/ext_help.h +14 -14
- data/ext/http11_client/extconf.rb +6 -6
- data/ext/http11_client/http11_client.c +328 -328
- data/ext/http11_client/http11_parser.c +418 -418
- data/ext/http11_client/http11_parser.h +48 -48
- data/ext/http11_client/http11_parser.rl +170 -170
- data/lib/em-http/client.rb +268 -42
- data/lib/em-http/http_options.rb +4 -2
- data/lib/em-http/mock.rb +90 -50
- data/lib/em-http/multi.rb +21 -17
- data/lib/em-http/request.rb +1 -0
- data/lib/em-http.rb +20 -19
- data/spec/encoding_spec.rb +34 -0
- data/spec/fixtures/google.ca +20 -21
- data/spec/helper.rb +3 -2
- data/spec/mock_spec.rb +79 -34
- data/spec/multi_spec.rb +27 -10
- data/spec/request_spec.rb +301 -68
- data/spec/spec.opts +7 -0
- data/spec/stallion.rb +64 -3
- data/spec/stub_server.rb +22 -22
- metadata +16 -9
- data/README.rdoc +0 -138
- data/lib/em-http/core_ext/hash.rb +0 -53
- data/spec/hash_spec.rb +0 -24
data/examples/oauth-tweet.rb
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
# Courtesy of Darcy Laycock:
|
|
2
|
-
# http://gist.github.com/265261
|
|
3
|
-
#
|
|
4
|
-
|
|
5
|
-
require 'rubygems'
|
|
6
|
-
|
|
7
|
-
require 'em-http'
|
|
8
|
-
require 'oauth'
|
|
9
|
-
|
|
10
|
-
# At a minimum, require 'oauth/request_proxy/em_http_request'
|
|
11
|
-
# for this example, we'll use Net::HTTP like support.
|
|
12
|
-
require 'oauth/client/em_http'
|
|
13
|
-
|
|
14
|
-
# You need two things: an oauth consumer and an access token.
|
|
15
|
-
# You need to generate an access token, I suggest looking elsewhere how to do that or wait for a full tutorial.
|
|
16
|
-
# For a consumer key / consumer secret, signup for an app at:
|
|
17
|
-
# http://twitter.com/apps/new
|
|
18
|
-
|
|
19
|
-
# Edit in your details.
|
|
20
|
-
CONSUMER_KEY = ""
|
|
21
|
-
CONSUMER_SECRET = ""
|
|
22
|
-
ACCESS_TOKEN = ""
|
|
23
|
-
ACCESS_TOKEN_SECRET = ""
|
|
24
|
-
|
|
25
|
-
def twitter_oauth_consumer
|
|
26
|
-
@twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def twitter_oauth_access_token
|
|
30
|
-
@twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
EM.run do
|
|
34
|
-
|
|
35
|
-
request = EventMachine::HttpRequest.new('http://twitter.com/statuses/update.json')
|
|
36
|
-
http = request.post(:body => {'status' => 'Hello Twitter from em-http-request with OAuth'}, :head => {"Content-Type" => "application/x-www-form-urlencoded"}) do |client|
|
|
37
|
-
twitter_oauth_consumer.sign!(client, twitter_oauth_access_token)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
http.callback do
|
|
41
|
-
puts "Response: #{http.response} (Code: #{http.response_header.status})"
|
|
42
|
-
EM.stop_event_loop
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
http.errback do
|
|
46
|
-
puts "Failed to post"
|
|
47
|
-
EM.stop_event_loop
|
|
48
|
-
end
|
|
49
|
-
|
|
1
|
+
# Courtesy of Darcy Laycock:
|
|
2
|
+
# http://gist.github.com/265261
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
require 'rubygems'
|
|
6
|
+
|
|
7
|
+
require 'em-http'
|
|
8
|
+
require 'oauth'
|
|
9
|
+
|
|
10
|
+
# At a minimum, require 'oauth/request_proxy/em_http_request'
|
|
11
|
+
# for this example, we'll use Net::HTTP like support.
|
|
12
|
+
require 'oauth/client/em_http'
|
|
13
|
+
|
|
14
|
+
# You need two things: an oauth consumer and an access token.
|
|
15
|
+
# You need to generate an access token, I suggest looking elsewhere how to do that or wait for a full tutorial.
|
|
16
|
+
# For a consumer key / consumer secret, signup for an app at:
|
|
17
|
+
# http://twitter.com/apps/new
|
|
18
|
+
|
|
19
|
+
# Edit in your details.
|
|
20
|
+
CONSUMER_KEY = ""
|
|
21
|
+
CONSUMER_SECRET = ""
|
|
22
|
+
ACCESS_TOKEN = ""
|
|
23
|
+
ACCESS_TOKEN_SECRET = ""
|
|
24
|
+
|
|
25
|
+
def twitter_oauth_consumer
|
|
26
|
+
@twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def twitter_oauth_access_token
|
|
30
|
+
@twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
EM.run do
|
|
34
|
+
|
|
35
|
+
request = EventMachine::HttpRequest.new('http://twitter.com/statuses/update.json')
|
|
36
|
+
http = request.post(:body => {'status' => 'Hello Twitter from em-http-request with OAuth'}, :head => {"Content-Type" => "application/x-www-form-urlencoded"}) do |client|
|
|
37
|
+
twitter_oauth_consumer.sign!(client, twitter_oauth_access_token)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
http.callback do
|
|
41
|
+
puts "Response: #{http.response} (Code: #{http.response_header.status})"
|
|
42
|
+
EM.stop_event_loop
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
http.errback do
|
|
46
|
+
puts "Failed to post"
|
|
47
|
+
EM.stop_event_loop
|
|
48
|
+
end
|
|
49
|
+
|
|
50
50
|
end
|
data/examples/socks5.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'eventmachine'
|
|
3
|
+
require '../lib/em-http'
|
|
4
|
+
|
|
5
|
+
EM.run do
|
|
6
|
+
# Establish a SOCKS5 tunnel via SSH
|
|
7
|
+
# ssh -D 8000 some_remote_machine
|
|
8
|
+
|
|
9
|
+
# http = EM::HttpRequest.new('http://whatismyip.org/').get({
|
|
10
|
+
http = EM::HttpRequest.new('http://igvita.com/').get({
|
|
11
|
+
:proxy => {:host => '127.0.0.1', :port => 8000, :type => :socks},
|
|
12
|
+
:redirects => 2
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
http.callback {
|
|
16
|
+
puts "#{http.response_header.status} - #{http.response.length} bytes\n"
|
|
17
|
+
puts http.response
|
|
18
|
+
EM.stop
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
http.errback {
|
|
22
|
+
puts "Error: " + http.error
|
|
23
|
+
puts http.inspect
|
|
24
|
+
EM.stop
|
|
25
|
+
}
|
|
26
|
+
end
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
require 'rubygems'
|
|
2
|
-
require 'lib/em-http'
|
|
3
|
-
|
|
4
|
-
module KBHandler
|
|
5
|
-
include EM::Protocols::LineText2
|
|
6
|
-
|
|
7
|
-
def receive_line(data)
|
|
8
|
-
p "Want to send: #{data}"
|
|
9
|
-
p "Error status: #{$http.error?}"
|
|
10
|
-
$http.send(data)
|
|
11
|
-
p "After send"
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
EventMachine.run {
|
|
16
|
-
$http = EventMachine::HttpRequest.new("ws://localhost:8080/").get :timeout => 0
|
|
17
|
-
|
|
18
|
-
$http.disconnect { puts 'oops' }
|
|
19
|
-
$http.callback {
|
|
20
|
-
puts "WebSocket connected!"
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
$http.stream { |msg|
|
|
24
|
-
puts "Recieved: #{msg}"
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
EM.open_keyboard(KBHandler)
|
|
28
|
-
}
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'lib/em-http'
|
|
3
|
+
|
|
4
|
+
module KBHandler
|
|
5
|
+
include EM::Protocols::LineText2
|
|
6
|
+
|
|
7
|
+
def receive_line(data)
|
|
8
|
+
p "Want to send: #{data}"
|
|
9
|
+
p "Error status: #{$http.error?}"
|
|
10
|
+
$http.send(data)
|
|
11
|
+
p "After send"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
EventMachine.run {
|
|
16
|
+
$http = EventMachine::HttpRequest.new("ws://localhost:8080/").get :timeout => 0
|
|
17
|
+
|
|
18
|
+
$http.disconnect { puts 'oops' }
|
|
19
|
+
$http.callback {
|
|
20
|
+
puts "WebSocket connected!"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
$http.stream { |msg|
|
|
24
|
+
puts "Recieved: #{msg}"
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
EM.open_keyboard(KBHandler)
|
|
28
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
require 'rubygems'
|
|
2
|
-
require 'em-websocket'
|
|
3
|
-
|
|
4
|
-
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
|
|
5
|
-
ws.onopen { ws.send "Hello Client!"}
|
|
6
|
-
ws.onmessage { |msg| p "got: #{msg}"; ws.send "Pong: #{msg}" }
|
|
7
|
-
ws.onclose { puts "WebSocket closed" }
|
|
8
|
-
end
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'em-websocket'
|
|
3
|
+
|
|
4
|
+
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
|
|
5
|
+
ws.onopen { ws.send "Hello Client!"}
|
|
6
|
+
ws.onmessage { |msg| p "got: #{msg}"; ws.send "Pong: #{msg}" }
|
|
7
|
+
ws.onclose { puts "WebSocket closed" }
|
|
8
|
+
end
|
data/ext/buffer/extconf.rb
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
require 'mkmf'
|
|
2
|
-
|
|
3
|
-
libs = []
|
|
4
|
-
|
|
5
|
-
$defs << "-DRUBY_VERSION_CODE=#{RUBY_VERSION.gsub(/\D/, '')}"
|
|
6
|
-
|
|
7
|
-
if have_func('rb_thread_blocking_region')
|
|
8
|
-
$defs << '-DHAVE_RB_THREAD_BLOCKING_REGION'
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
if have_func('rb_str_set_len')
|
|
12
|
-
$defs << '-DHAVE_RB_STR_SET_LEN'
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
if have_header('sys/select.h')
|
|
16
|
-
$defs << '-DEV_USE_SELECT'
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
if have_header('poll.h')
|
|
20
|
-
$defs << '-DEV_USE_POLL'
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
if have_header('sys/epoll.h')
|
|
24
|
-
$defs << '-DEV_USE_EPOLL'
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
if have_header('sys/event.h') and have_header('sys/queue.h')
|
|
28
|
-
$defs << '-DEV_USE_KQUEUE'
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
if have_header('port.h')
|
|
32
|
-
$defs << '-DEV_USE_PORT'
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
if have_header('openssl/ssl.h')
|
|
36
|
-
$defs << '-DHAVE_OPENSSL_SSL_H'
|
|
37
|
-
libs << '-lssl -lcrypto'
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# ncpu detection specifics
|
|
41
|
-
case RUBY_PLATFORM
|
|
42
|
-
when /linux/
|
|
43
|
-
$defs << '-DHAVE_LINUX_PROCFS'
|
|
44
|
-
else
|
|
45
|
-
if have_func('sysctlbyname', ['sys/param.h', 'sys/sysctl.h'])
|
|
46
|
-
$defs << '-DHAVE_SYSCTLBYNAME'
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
$LIBS << ' ' << libs.join(' ')
|
|
51
|
-
|
|
52
|
-
dir_config('em_buffer')
|
|
53
|
-
create_makefile('em_buffer')
|
|
1
|
+
require 'mkmf'
|
|
2
|
+
|
|
3
|
+
libs = []
|
|
4
|
+
|
|
5
|
+
$defs << "-DRUBY_VERSION_CODE=#{RUBY_VERSION.gsub(/\D/, '')}"
|
|
6
|
+
|
|
7
|
+
if have_func('rb_thread_blocking_region')
|
|
8
|
+
$defs << '-DHAVE_RB_THREAD_BLOCKING_REGION'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
if have_func('rb_str_set_len')
|
|
12
|
+
$defs << '-DHAVE_RB_STR_SET_LEN'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
if have_header('sys/select.h')
|
|
16
|
+
$defs << '-DEV_USE_SELECT'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
if have_header('poll.h')
|
|
20
|
+
$defs << '-DEV_USE_POLL'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
if have_header('sys/epoll.h')
|
|
24
|
+
$defs << '-DEV_USE_EPOLL'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
if have_header('sys/event.h') and have_header('sys/queue.h')
|
|
28
|
+
$defs << '-DEV_USE_KQUEUE'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
if have_header('port.h')
|
|
32
|
+
$defs << '-DEV_USE_PORT'
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
if have_header('openssl/ssl.h')
|
|
36
|
+
$defs << '-DHAVE_OPENSSL_SSL_H'
|
|
37
|
+
libs << '-lssl -lcrypto'
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# ncpu detection specifics
|
|
41
|
+
case RUBY_PLATFORM
|
|
42
|
+
when /linux/
|
|
43
|
+
$defs << '-DHAVE_LINUX_PROCFS'
|
|
44
|
+
else
|
|
45
|
+
if have_func('sysctlbyname', ['sys/param.h', 'sys/sysctl.h'])
|
|
46
|
+
$defs << '-DHAVE_SYSCTLBYNAME'
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
$LIBS << ' ' << libs.join(' ')
|
|
51
|
+
|
|
52
|
+
dir_config('em_buffer')
|
|
53
|
+
create_makefile('em_buffer')
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
#ifndef ext_help_h
|
|
2
|
-
#define ext_help_h
|
|
3
|
-
|
|
4
|
-
#define RAISE_NOT_NULL(T) if(T == NULL) rb_raise(rb_eArgError, "NULL found for " # T " when shouldn't be.");
|
|
5
|
-
#define DATA_GET(from,type,name) Data_Get_Struct(from,type,name); RAISE_NOT_NULL(name);
|
|
6
|
-
#define REQUIRE_TYPE(V, T) if(TYPE(V) != T) rb_raise(rb_eTypeError, "Wrong argument type for " # V " required " # T);
|
|
7
|
-
|
|
8
|
-
#ifdef DEBUG
|
|
9
|
-
#define TRACE() fprintf(stderr, "> %s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__)
|
|
10
|
-
#else
|
|
11
|
-
#define TRACE()
|
|
12
|
-
#endif
|
|
13
|
-
|
|
14
|
-
#endif
|
|
1
|
+
#ifndef ext_help_h
|
|
2
|
+
#define ext_help_h
|
|
3
|
+
|
|
4
|
+
#define RAISE_NOT_NULL(T) if(T == NULL) rb_raise(rb_eArgError, "NULL found for " # T " when shouldn't be.");
|
|
5
|
+
#define DATA_GET(from,type,name) Data_Get_Struct(from,type,name); RAISE_NOT_NULL(name);
|
|
6
|
+
#define REQUIRE_TYPE(V, T) if(TYPE(V) != T) rb_raise(rb_eTypeError, "Wrong argument type for " # V " required " # T);
|
|
7
|
+
|
|
8
|
+
#ifdef DEBUG
|
|
9
|
+
#define TRACE() fprintf(stderr, "> %s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__)
|
|
10
|
+
#else
|
|
11
|
+
#define TRACE()
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
#endif
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
require 'mkmf'
|
|
2
|
-
|
|
3
|
-
dir_config("http11_client")
|
|
4
|
-
have_library("c", "main")
|
|
5
|
-
|
|
6
|
-
create_makefile("http11_client")
|
|
1
|
+
require 'mkmf'
|
|
2
|
+
|
|
3
|
+
dir_config("http11_client")
|
|
4
|
+
have_library("c", "main")
|
|
5
|
+
|
|
6
|
+
create_makefile("http11_client")
|