emmy-http-client 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52d1bbbfc9f210e48a6f7d8dd98631f3783e9202
4
- data.tar.gz: 85c2d04387f9e94b8ec898f9c309e3521db19fe4
3
+ metadata.gz: c27535d963d40f0d17b129ab02e4cda3ac272b58
4
+ data.tar.gz: 69c59bb5c7e76c3b0b8b3b3b71f37f93669d8f48
5
5
  SHA512:
6
- metadata.gz: 2a43d921938468c52fa7cfa850b5a42671831043a277f5207179e9bbb5f01905fba0bd30674337a9bc16b1e836153c333c38bdc132edb03a806f912df8401658
7
- data.tar.gz: fb891ed56dc5f0a45622f9c88e460fe9c15a1a3bb77df63db70be8bcf182f7c6a49e79a63e8cb59193baff7e4c099ffb3be22a2b0179ba6f02e0e83819e1a109
6
+ metadata.gz: c65de5c246c347188b8b3fd1e58e91abbfa7bc332858d3d24cdddfb2da1ecaf47e50626908e58f647bcd15dc0a1a2d15109a5e828147dd8c5cd39c199a40edd5
7
+ data.tar.gz: 56043aa98b97cd0114967e50978106d3fee22f0a967e2ea6b4e9fcb2cbc22cad0a19f9ca45156f1ebeb76c206712ee5eddce381e1b36c5abc4d6dd1d8d36f44b
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency "event_object", "~> 0.9.1"
24
24
  spec.add_dependency "fibre", "~> 0.9.7"
25
25
  spec.add_dependency "emmy-machine", "~> 0.1.7"
26
- spec.add_dependency "emmy-http", "~> 0.1.6"
26
+ #spec.add_dependency "emmy-http", "~> 0.1.6"
27
27
 
28
28
  spec.add_development_dependency "bundler", "~> 1.9"
29
29
  spec.add_development_dependency "rake", "~> 10.0"
@@ -8,6 +8,11 @@ module EmmyHttp
8
8
  def delegate=(operation)
9
9
  @operation = operation
10
10
  @client = Client::Client.new(operation.request, self)
11
+ # if already connected
12
+ if operation.connection
13
+ client.initialize_connection(operation.connection)
14
+ client.connect
15
+ end
11
16
  end
12
17
 
13
18
  def to_a
@@ -13,6 +13,7 @@ module EmmyHttp
13
13
  attr_accessor :operation
14
14
  attr_accessor :adapter
15
15
  attr_accessor :parser
16
+ attr_accessor :stop_reason
16
17
 
17
18
  attr_accessor :redirects
18
19
 
@@ -26,7 +27,6 @@ module EmmyHttp
26
27
  change_state(:idle)
27
28
  end
28
29
 
29
-
30
30
  def initialize_connection(conn)
31
31
  @connection = conn
32
32
  @parser = Client::Parser.new
@@ -36,50 +36,7 @@ module EmmyHttp
36
36
  conn.pending_connect_timeout = request.timeouts.connect
37
37
  conn.comm_inactivity_timeout = request.timeouts.inactivity
38
38
 
39
- conn.on :connect do
40
- conn.start_tls(client.request.ssl ? client.request.ssl.serializable_hash : {}) if client.request.ssl?
41
- send_request
42
- change_state(:wait_response)
43
- end
44
-
45
- conn.on :data do |chunk|
46
- begin
47
- parser << chunk
48
- rescue EmmyHttp::ParserError => e
49
- conn.error!(e.message)
50
- end
51
- end
52
-
53
- conn.on :close do
54
- if state == :body && !client.response.content_length?
55
- client.response.finish
56
- end
57
-
58
- if client.response && client.response.redirection?
59
- change_state(:redirect)
60
- client.url = URI(client.response.location)
61
- client.response = nil
62
- parser.reset!
63
- operation.reconnect
64
- end
65
-
66
- if client.response && client.response.finished?
67
- change_state(:success)
68
- operation.success!(response, operation, connection)
69
- end
70
- end
71
-
72
- conn.on :error do |message|
73
- change_state(:catch_error)
74
- end
75
-
76
- conn.on :handshake do
77
-
78
- end
79
-
80
- conn.on :verify_peer do
81
-
82
- end
39
+ attach(conn)
83
40
 
84
41
  parser.on :head do |headers|
85
42
  #parser.http_version
@@ -99,12 +56,61 @@ module EmmyHttp
99
56
 
100
57
  parser.on :completed do
101
58
  client.response.finish
102
- stop
59
+ if request.keep_alive?
60
+ change_state(:success)
61
+ dettach(conn)
62
+ operation.success!(response, operation, connection)
63
+ else
64
+ client.close
65
+ end
103
66
  end
104
67
 
105
68
  change_state(:wait_connect)
106
69
  end
107
70
 
71
+ def connect
72
+ connection.start_tls(request.ssl ? request.ssl.serializable_hash : {}) if request.ssl?
73
+ send_request
74
+ change_state(:wait_response)
75
+ end
76
+
77
+ def data(chunk)
78
+ begin
79
+ parser << chunk
80
+ rescue EmmyHttp::ParserError => e
81
+ stop(e.message)
82
+ end
83
+ end
84
+
85
+ def close(reason=nil)
86
+ if stop_reason
87
+ change_state(:catch_error)
88
+ operation.error!(stop_reason)
89
+ return
90
+ end
91
+
92
+ if state == :body && !response.content_length?
93
+ response.finish
94
+ end
95
+
96
+ if response && response.redirection?
97
+ change_state(:redirect)
98
+ self.url = URI(response.location)
99
+ self.response = nil
100
+ parser.reset!
101
+ operation.reconnect
102
+ end
103
+
104
+ if response && response.finished?
105
+ change_state(:success)
106
+ dettach(connection)
107
+ operation.success!(response, operation, connection)
108
+ else
109
+ change_state(:catch_error)
110
+ operation.error!('invalid response')
111
+ end
112
+ end
113
+
108
114
  def send_request
109
115
  headers = {}
110
116
  body = prepare_body(headers)
@@ -187,7 +193,8 @@ module EmmyHttp
187
193
  url.query ? '/' + url.path + '?' + url.query : '/' + url.path
188
194
  end
189
195
 
190
- def stop(reason='')
196
+ def stop(reason=nil)
197
+ @stop_reason ||= reason
191
198
  connection.close_connection if connection
192
199
  end
193
200
 
@@ -196,6 +203,18 @@ module EmmyHttp
196
203
  change!(state, self)
197
204
  end
198
205
 
206
+ def attach(conn)
207
+ listen conn, :connect, :connect
208
+ listen conn, :data, :data
209
+ listen conn, :close, :close
210
+ end
211
+
212
+ def dettach(conn)
213
+ stop_listen conn, :connect
214
+ stop_listen conn, :data
215
+ stop_listen conn, :close
216
+ end
217
+
199
218
  def to_a
200
219
  ["tcp://#{url.host}:#{url.port}", connection || EmmyMachine::Connection, method(:initialize_connection), self]
201
220
  end
@@ -1,5 +1,5 @@
1
1
  module EmmyHttp
2
2
  module Client
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emmy-http-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - inre
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-20 00:00:00.000000000 Z
11
+ date: 2015-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http_parser.rb
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.1.7
69
- - !ruby/object:Gem::Dependency
70
- name: emmy-http
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 0.1.6
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 0.1.6
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: bundler
85
71
  requirement: !ruby/object:Gem::Requirement