httpkit 0.6.0.pre.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +0 -2
  3. data/.travis.yml +1 -1
  4. data/Gemfile +2 -0
  5. data/README.md +32 -25
  6. data/Rakefile +5 -0
  7. data/config/flay.yml +1 -1
  8. data/doc/api/httpkit.html +50 -0
  9. data/doc/api/httpkit/body.html +149 -0
  10. data/doc/api/httpkit/client.html +50 -0
  11. data/doc/api/httpkit/client/body_handler.html +50 -0
  12. data/doc/api/httpkit/client/keep_alive_handler.html +79 -0
  13. data/doc/api/httpkit/client/mandatory_handler.html +50 -0
  14. data/doc/api/httpkit/client/timeouts_handler.html +82 -0
  15. data/doc/api/httpkit/connection/eventmachine.html +157 -0
  16. data/doc/api/httpkit/connection/status.html +50 -0
  17. data/doc/api/httpkit/promise.html +50 -0
  18. data/doc/api/httpkit/request.html +123 -0
  19. data/doc/api/httpkit/response.html +209 -0
  20. data/doc/api/httpkit/serializer.html +150 -0
  21. data/doc/api/httpkit/server.html +50 -0
  22. data/doc/api/httpkit/server/block_handler.html +50 -0
  23. data/doc/api/httpkit/server/body_handler.html +50 -0
  24. data/doc/api/httpkit/server/keep_alive_handler.html +145 -0
  25. data/doc/api/httpkit/server/mandatory_handler.html +50 -0
  26. data/doc/api/httpkit/server/timeouts_handler.html +81 -0
  27. data/doc/api/httpkit/support/handler_manager.html +95 -0
  28. data/doc/api/httpkit/support/message.html +50 -0
  29. data/doc/api/httpkit/version.html +50 -0
  30. data/doc/examples/echo_server.html +101 -0
  31. data/doc/examples/error_handling.html +176 -0
  32. data/doc/examples/getting_started.html +161 -0
  33. data/doc/examples/streaming.html +108 -0
  34. data/doc/index.html +165 -0
  35. data/examples/echo_server.rb +14 -6
  36. data/examples/error_handling.rb +56 -0
  37. data/examples/getting_started.rb +10 -18
  38. data/examples/streaming.rb +41 -0
  39. data/lib/httpkit.rb +22 -0
  40. data/lib/httpkit/body.rb +4 -0
  41. data/lib/httpkit/client.rb +1 -9
  42. data/lib/httpkit/client/keep_alive_handler.rb +1 -0
  43. data/lib/httpkit/connection/eventmachine.rb +2 -1
  44. data/lib/httpkit/server/block_handler.rb +13 -0
  45. data/lib/httpkit/server/keep_alive_handler.rb +1 -0
  46. data/lib/httpkit/version.rb +1 -1
  47. data/spec/integration/timeouts_spec.rb +3 -2
  48. data/spec/support/helper.rb +1 -8
  49. data/spec/unit/client_spec.rb +2 -21
  50. data/spec/unit/server_spec.rb +11 -1
  51. metadata +34 -5
  52. data/Procfile +0 -1
@@ -31,6 +31,7 @@ require 'httpkit/server/body_handler'
31
31
  require 'httpkit/server/mandatory_handler'
32
32
  require 'httpkit/server/keep_alive_handler'
33
33
  require 'httpkit/server/timeouts_handler'
34
+ require 'httpkit/server/block_handler'
34
35
 
35
36
  module HTTPkit
36
37
  def self.run
@@ -56,4 +57,25 @@ module HTTPkit
56
57
  EM.add_timer(duration) { promise.fulfill }
57
58
  promise.sync
58
59
  end
60
+
61
+ def self.random_port(address)
62
+ server = TCPServer.new(address, 0)
63
+ server.addr[1]
64
+ ensure
65
+ server.shutdown if server
66
+ end
67
+
68
+ def self.server(uri, &block)
69
+ uri = URI(uri)
70
+ Server.start(address: uri.host, port: uri.port,
71
+ handlers: [Server::KeepAliveHandler.new,
72
+ Server::TimeoutsHandler.new,
73
+ Server::BlockHandler.new(block)])
74
+ end
75
+
76
+ def self.request(*args)
77
+ uri = URI(args[1])
78
+ client = Client.start(address: uri.host, port: uri.port)
79
+ client.request(*args)
80
+ end
59
81
  end
@@ -53,6 +53,10 @@ module HTTPkit
53
53
  each.to_a.join
54
54
  end
55
55
 
56
+ def chunks
57
+ @chunks.dup
58
+ end
59
+
56
60
  def inspect
57
61
  len = length_known? ? length : 'unknown'
58
62
  sprintf('#<%s:0x%d @length=%s>', self.class.name, object_id, len)
@@ -25,12 +25,6 @@ module HTTPkit
25
25
  Connection::EventMachine.start_client(config, self)
26
26
  end
27
27
 
28
- def self.request(*args)
29
- uri = URI(args[1])
30
- client = start(address: uri.host, port: uri.port)
31
- client.request(*args)
32
- end
33
-
34
28
  attr_reader :config
35
29
 
36
30
  def initialize(config, connection)
@@ -60,10 +54,8 @@ module HTTPkit
60
54
  request, served = find_request
61
55
 
62
56
  if request
63
- # XXX: fulfillment should happen after invoke(:receive), so that all
64
- # handlers have run when the application code resumes
65
- served.fulfill(response)
66
57
  request, response = @handlers.invoke(:receive, request, response)
58
+ served.fulfill(response)
67
59
  response.closed { finish(request) }
68
60
  end
69
61
  end
@@ -4,6 +4,7 @@ module HTTPkit
4
4
  class Client::KeepAliveHandler
5
5
  def perform(request)
6
6
  @previous, previous = request, @previous
7
+ # TODO: move to Serializer
7
8
  previous.closed! if previous
8
9
  end
9
10
  end
@@ -32,7 +32,8 @@ module HTTPkit
32
32
  # p data
33
33
  @parser << data
34
34
  rescue => ex
35
- # puts [ex.message, *ex.backtrace].join("\n\t")
35
+ # message = ex.message.empty? ? ex.class : ex.message
36
+ # puts [message, *ex.backtrace].join("\n\t")
36
37
  close(ex)
37
38
  end
38
39
 
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module HTTPkit
4
+ class Server::BlockHandler
5
+ def initialize(block)
6
+ @block = block
7
+ end
8
+
9
+ def serve(request, served)
10
+ @block.call(request, served)
11
+ end
12
+ end
13
+ end
@@ -47,6 +47,7 @@ module HTTPkit
47
47
 
48
48
  def synchronize_responses(request)
49
49
  served = @requests[sequence(request) - 1]
50
+ # TODO: only wait for served, move rest to Serializer
50
51
  served.sync.closed! if served
51
52
  end
52
53
 
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module HTTPkit
4
- VERSION = '0.6.0.pre.5'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -6,7 +6,7 @@ describe HTTPkit::Client, 'in connecting state', reactor: true do
6
6
  # connect to a non-existing host to provoke connection timeout
7
7
  let(:address) { '1.2.3.4' }
8
8
  let(:client_config) do
9
- { address: address, port: random_port,
9
+ { address: address, port: 1234,
10
10
  handlers: [HTTPkit::Client::TimeoutsHandler.new] }
11
11
  end
12
12
  let!(:client) { HTTPkit::Client.start(client_config) }
@@ -20,7 +20,8 @@ describe HTTPkit::Client, 'in connecting state', reactor: true do
20
20
 
21
21
  describe 'with :connect_timeout option' do
22
22
  let(:client_config) do
23
- { address: address, port: random_port, connect_timeout: 0.01,
23
+ { address: address, port: 1234,
24
+ connect_timeout: 0.01,
24
25
  handlers: [HTTPkit::Client::TimeoutsHandler.new] }
25
26
  end
26
27
 
@@ -46,18 +46,11 @@ module SpecHelper
46
46
  '127.0.0.1'
47
47
  end
48
48
 
49
- def random_port
50
- server = TCPServer.new(localhost, 0)
51
- server.addr[1]
52
- ensure
53
- server.shutdown
54
- end
55
-
56
49
  def server_client_pair(server_config, client_config, interceptor)
57
50
  server = nil
58
51
  inspect_server(server_config, interceptor) { |_, s, _| server = s }
59
52
 
60
- config = { address: localhost, port: random_port }
53
+ config = { address: localhost, port: HTTPkit.random_port(localhost) }
61
54
  HTTPkit::Server.start(config.merge(server_config))
62
55
  client = HTTPkit::Client.start(config.merge(client_config))
63
56
 
@@ -6,7 +6,8 @@ describe HTTPkit::Client do
6
6
  let(:client) { described_class.new(config, connection) }
7
7
 
8
8
  let(:config) do
9
- { address: localhost, port: random_port, handlers: [handler] }
9
+ { address: localhost, port: HTTPkit.random_port(localhost),
10
+ handlers: [handler] }
10
11
  end
11
12
  let(:connection) do
12
13
  double('connection', :'on_message=' => nil,
@@ -31,26 +32,6 @@ describe HTTPkit::Client do
31
32
  end
32
33
  end
33
34
 
34
- describe '.request' do
35
- let(:arg) { double }
36
- let(:uri) { 'http://localhost:3000' }
37
-
38
- let(:options) { { address: 'localhost', port: 3000 } }
39
- let(:client) { double }
40
- let(:response) { double }
41
-
42
- before do
43
- allow(described_class).to receive(:start).with(options) { client }
44
- allow(client).to receive(:request).with(arg, uri, arg, arg) { response }
45
- end
46
-
47
- subject { described_class.request(arg, uri, arg, arg) }
48
-
49
- it 'starts a client and performs the request' do
50
- expect(subject).to be(response)
51
- end
52
- end
53
-
54
35
  describe '#initialize' do
55
36
  let(:on_message) { client.method(:receive) }
56
37
  let(:teadown) { client.method(:teardown) }
@@ -6,7 +6,8 @@ describe HTTPkit::Server do
6
6
  let(:server) { described_class.new(config, connection) }
7
7
 
8
8
  let(:config) do
9
- { address: localhost, port: random_port, handlers: [handler] }
9
+ { address: localhost, port: HTTPkit.random_port(localhost),
10
+ handlers: [handler] }
10
11
  end
11
12
  let(:connection) do
12
13
  double('connection', :'on_message=' => nil,
@@ -53,6 +54,13 @@ describe HTTPkit::Server do
53
54
  .to be_a(HTTPkit::Server::BodyHandler)
54
55
  end
55
56
  end
57
+
58
+ let(:request) { double(sequence: nil) }
59
+
60
+ it 'sets up sequence numbering' do
61
+ server.serve(request)
62
+ expect(request).to have_received(:sequence).with(server, 1)
63
+ end
56
64
  end
57
65
 
58
66
  describe '#serve', reactor: true do
@@ -119,4 +127,6 @@ describe HTTPkit::Server do
119
127
  end
120
128
 
121
129
  describe '#setup_connection'
130
+
131
+ describe '#serve!'
122
132
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httpkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0.pre.5
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Gierth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-19 00:00:00.000000000 Z
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -94,7 +94,6 @@ files:
94
94
  - .yardopts
95
95
  - Gemfile
96
96
  - Gemfile.devtools
97
- - Procfile
98
97
  - README.md
99
98
  - Rakefile
100
99
  - UNLICENSE
@@ -105,8 +104,37 @@ files:
105
104
  - config/reek.yml
106
105
  - config/rubocop.yml
107
106
  - config/yardstick.yml
107
+ - doc/api/httpkit.html
108
+ - doc/api/httpkit/body.html
109
+ - doc/api/httpkit/client.html
110
+ - doc/api/httpkit/client/body_handler.html
111
+ - doc/api/httpkit/client/keep_alive_handler.html
112
+ - doc/api/httpkit/client/mandatory_handler.html
113
+ - doc/api/httpkit/client/timeouts_handler.html
114
+ - doc/api/httpkit/connection/eventmachine.html
115
+ - doc/api/httpkit/connection/status.html
116
+ - doc/api/httpkit/promise.html
117
+ - doc/api/httpkit/request.html
118
+ - doc/api/httpkit/response.html
119
+ - doc/api/httpkit/serializer.html
120
+ - doc/api/httpkit/server.html
121
+ - doc/api/httpkit/server/block_handler.html
122
+ - doc/api/httpkit/server/body_handler.html
123
+ - doc/api/httpkit/server/keep_alive_handler.html
124
+ - doc/api/httpkit/server/mandatory_handler.html
125
+ - doc/api/httpkit/server/timeouts_handler.html
126
+ - doc/api/httpkit/support/handler_manager.html
127
+ - doc/api/httpkit/support/message.html
128
+ - doc/api/httpkit/version.html
129
+ - doc/examples/echo_server.html
130
+ - doc/examples/error_handling.html
131
+ - doc/examples/getting_started.html
132
+ - doc/examples/streaming.html
133
+ - doc/index.html
108
134
  - examples/echo_server.rb
135
+ - examples/error_handling.rb
109
136
  - examples/getting_started.rb
137
+ - examples/streaming.rb
110
138
  - httpkit.gemspec
111
139
  - lib/httpkit.rb
112
140
  - lib/httpkit/body.rb
@@ -122,6 +150,7 @@ files:
122
150
  - lib/httpkit/response.rb
123
151
  - lib/httpkit/serializer.rb
124
152
  - lib/httpkit/server.rb
153
+ - lib/httpkit/server/block_handler.rb
125
154
  - lib/httpkit/server/body_handler.rb
126
155
  - lib/httpkit/server/keep_alive_handler.rb
127
156
  - lib/httpkit/server/mandatory_handler.rb
@@ -171,9 +200,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
171
200
  version: '0'
172
201
  required_rubygems_version: !ruby/object:Gem::Requirement
173
202
  requirements:
174
- - - ! '>'
203
+ - - ! '>='
175
204
  - !ruby/object:Gem::Version
176
- version: 1.3.1
205
+ version: '0'
177
206
  requirements: []
178
207
  rubyforge_project:
179
208
  rubygems_version: 2.3.0
data/Procfile DELETED
@@ -1 +0,0 @@
1
- web: env PORT=$PORT ADDRESS=0.0.0.0 bundle exec ruby examples/echo_server.rb