protocol-http2 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59f70b2d8905a24deb2b5f6400347631c226dea2814f23a0701d64ca343c8e7e
4
- data.tar.gz: b6370e82f47701f4eb7ad21049524cf559d7c1303a839a802a88dfc8f2808c16
3
+ metadata.gz: 8d2e544f1541f928ab172ef078d2a6ef3350bef0db9e1bbd40d3fb3755501e7f
4
+ data.tar.gz: a161a66c0f62eb669d9527dee36753b2e63a653aa9e242ed554916d140f8cd65
5
5
  SHA512:
6
- metadata.gz: f2c32cb5174ed643a415b836a2faa53a325a5e3252e1db55be9fafbc117fd78beb3768b467879f27b146da001caf3a92c74137924e5cf4c9eb86006b7d5a4a42
7
- data.tar.gz: 05a417ab4821acb8e0db930440f8a7583ae265f1dd14363eea42963cc4fb313e413aa91d3c81ca4453a413f5db42ef6934e6c4d76cef5f9ac7b757f5f7c6a67e
6
+ metadata.gz: 7d2bfcf4a4d488978283147d468c1a2afc6aecc94043e626a555e154d4e826e38c79a76092b9c13cc2aad9c3e7d75fc9c9ccae45fdcbbb6d29610bb1630bab0e
7
+ data.tar.gz: b638a3348b28f9491958a5748dc884b34d2129248744832858fcd1368bad55cfe0da4d18540632414820383dff53c7cb746a69a95e5a5a39423d6f7af00a948f
@@ -47,6 +47,17 @@ module Protocol
47
47
  end
48
48
  end
49
49
 
50
+ # Accept an incoming push promise from the other side of the connection.
51
+ # On the client side, we accept push promise streams.
52
+ # On the server side, streams create push promise streams.
53
+ def accept_push_promise_stream(stream_id, &block)
54
+ accept_stream(stream_id, &block)
55
+ end
56
+
57
+ def create_push_promise_stream
58
+ raise ProtocolError, "Cannot create push promises from client!"
59
+ end
60
+
50
61
  def receive_push_promise(frame)
51
62
  if frame.stream_id == 0
52
63
  raise ProtocolError, "Cannot receive headers for stream 0!"
@@ -274,8 +274,9 @@ module Protocol
274
274
  false
275
275
  end
276
276
 
277
- # Accept a stream from the other side of the connnection.
278
- def accept_stream(stream_id)
277
+ # Accept an incoming stream from the other side of the connnection.
278
+ # On the server side, we accept requests.
279
+ def accept_stream(stream_id, &block)
279
280
  unless valid_remote_stream_id?(stream_id)
280
281
  raise ProtocolError, "Invalid remote stream id: #{stream_id}"
281
282
  end
@@ -285,14 +286,36 @@ module Protocol
285
286
  end
286
287
 
287
288
  @remote_stream_id = stream_id
288
- create_stream(stream_id)
289
+
290
+ create_stream(stream_id, &block)
291
+ end
292
+
293
+ def accept_push_promise_stream(stream_id, &block)
294
+ accept_stream(stream_id, &block)
295
+ end
296
+
297
+ # Create a stream, defaults to an outgoing stream.
298
+ # On the client side, we create requests.
299
+ # @return [Stream] the created stream.
300
+ def create_stream(stream_id = next_stream_id, &block)
301
+ if block_given?
302
+ yield(stream_id)
303
+ else
304
+ return Stream.new(self, stream_id)
305
+ end
306
+
307
+ if stream = @streams[stream_id]
308
+ return stream
309
+ else
310
+ raise ProtocolError, "Stream creation failed!"
311
+ end
289
312
  end
290
313
 
291
- # Create a stream on this side of the connection.
292
- def create_stream(stream_id = next_stream_id)
293
- @streams[stream_id] = Stream.new(self, stream_id)
314
+ def create_push_promise_stream(&block)
315
+ create_stream(&block)
294
316
  end
295
317
 
318
+ # On the server side, starts a new request.
296
319
  def receive_headers(frame)
297
320
  if frame.stream_id == 0
298
321
  raise ProtocolError, "Cannot receive headers for stream 0!"
@@ -310,6 +333,7 @@ module Protocol
310
333
  end
311
334
  end
312
335
 
336
+ # On the client and server side, sets the priority for an incoming stream.
313
337
  def receive_priority(frame)
314
338
  if stream = @streams[frame.stream_id]
315
339
  stream.receive_priority(frame)
@@ -319,6 +343,10 @@ module Protocol
319
343
  end
320
344
  end
321
345
 
346
+ def receive_push_promise(frame)
347
+ raise ProtocolError, "Unable to receive push promise!"
348
+ end
349
+
322
350
  def receive_reset_stream(frame)
323
351
  if stream = @streams[frame.stream_id]
324
352
  stream.receive_reset_stream(frame)
@@ -45,12 +45,12 @@ module Protocol
45
45
  end
46
46
  end
47
47
 
48
- def enable_push?
49
- @remote_settings.enable_push?
48
+ def accept_push_promise_stream(stream_id, &block)
49
+ raise ProtocolError, "Cannot accept push promises on server!"
50
50
  end
51
51
 
52
- def receive_push_promise
53
- raise ProtocolError, "Server cannot receive push promises."
52
+ def enable_push?
53
+ @remote_settings.enable_push?
54
54
  end
55
55
  end
56
56
  end
@@ -70,6 +70,7 @@ module Protocol
70
70
  # ES: END_STREAM flag
71
71
  # R: RST_STREAM frame
72
72
  #
73
+ # State transition methods use a trailing "!".
73
74
  class Stream
74
75
  include FlowControl
75
76
 
@@ -85,6 +86,8 @@ module Protocol
85
86
 
86
87
  @headers = nil
87
88
  @data = nil
89
+
90
+ @connection.streams[@id] = self
88
91
  end
89
92
 
90
93
  # Stream ID (odd for client initiated streams, even otherwise).
@@ -116,7 +119,7 @@ module Protocol
116
119
  end
117
120
 
118
121
  def closed?
119
- @state == :closed or @state == :reset
122
+ @state == :closed
120
123
  end
121
124
 
122
125
  def send_headers?
@@ -171,7 +174,7 @@ module Protocol
171
174
  frame = write_headers(*args)
172
175
 
173
176
  if frame.end_stream?
174
- close
177
+ close!
175
178
  end
176
179
  else
177
180
  raise ProtocolError, "Cannot send headers in state: #{@state}"
@@ -207,15 +210,16 @@ module Protocol
207
210
  frame = write_data(*args)
208
211
 
209
212
  if frame.end_stream?
210
- close
213
+ close!
211
214
  end
212
215
  else
213
216
  raise ProtocolError, "Cannot send data in state: #{@state}"
214
217
  end
215
218
  end
216
219
 
217
- def close(state = :closed)
218
- @state = state
220
+ # This is not the same as a `close` method. If you are looking for that, use `send_stream_reset`.
221
+ def close!
222
+ @state = :closed
219
223
  end
220
224
 
221
225
  def send_reset_stream(error_code = 0)
@@ -225,7 +229,7 @@ module Protocol
225
229
 
226
230
  write_frame(frame)
227
231
 
228
- close(:reset)
232
+ close!
229
233
  else
230
234
  raise ProtocolError, "Cannot reset stream in state: #{@state}"
231
235
  end
@@ -263,12 +267,10 @@ module Protocol
263
267
  @headers = process_headers(frame)
264
268
  elsif @state == :half_closed_local
265
269
  if frame.end_stream?
266
- close
270
+ close!
267
271
  end
268
272
 
269
273
  @headers = process_headers(frame)
270
- elsif @state == :reset
271
- # ignore...
272
274
  else
273
275
  raise ProtocolError, "Cannot receive headers in state: #{@state}"
274
276
  end
@@ -288,12 +290,10 @@ module Protocol
288
290
  consume_local_window(frame)
289
291
 
290
292
  if frame.end_stream?
291
- close
293
+ close!
292
294
  end
293
295
 
294
296
  @data = frame.unpack
295
- elsif @state == :reset
296
- # ignore...
297
297
  else
298
298
  raise ProtocolError, "Cannot receive data in state: #{@state}"
299
299
  end
@@ -305,7 +305,7 @@ module Protocol
305
305
 
306
306
  def receive_reset_stream(frame)
307
307
  if @state != :idle and @state != :closed
308
- close
308
+ close!
309
309
 
310
310
  return frame.unpack
311
311
  else
@@ -343,15 +343,16 @@ module Protocol
343
343
  end
344
344
  end
345
345
 
346
- def create_promise_stream(headers, stream_id)
347
- @connection.create_stream(stream_id)
346
+ # Override this function to implement your own push promise logic.
347
+ def create_push_promise_stream(headers)
348
+ @connection.create_push_promise_stream
348
349
  end
349
350
 
350
351
  # Server push is semantically equivalent to a server responding to a request; however, in this case, that request is also sent by the server, as a PUSH_PROMISE frame.
351
352
  # @param headers [Hash] contains a complete set of request header fields that the server attributes to the request.
352
- def send_push_promise(headers, stream_id = @connection.next_stream_id)
353
+ def send_push_promise(headers)
353
354
  if @state == :open or @state == :half_closed_remote
354
- promised_stream = self.create_promise_stream(headers, stream_id)
355
+ promised_stream = self.create_push_promise_stream(headers)
355
356
  promised_stream.reserved_local!
356
357
 
357
358
  write_push_promise(promised_stream.id, headers)
@@ -362,11 +363,16 @@ module Protocol
362
363
  end
363
364
  end
364
365
 
366
+ # Override this function to implement your own push promise logic.
367
+ def accept_push_promise_stream(stream_id, headers)
368
+ @connection.accept_push_promise_stream(stream_id)
369
+ end
370
+
365
371
  def receive_push_promise(frame)
366
372
  promised_stream_id, data = frame.unpack
367
373
  headers = @connection.decode_headers(data)
368
374
 
369
- stream = self.create_promise_stream(headers, promised_stream_id)
375
+ stream = self.accept_push_promise_stream(promised_stream_id, headers)
370
376
  stream.reserved_remote!
371
377
 
372
378
  return stream, headers
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Protocol
22
22
  module HTTP2
23
- VERSION = "0.4.1"
23
+ VERSION = "0.5.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-http2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams