protocol-http 0.29.0 → 0.31.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: e5839d2adb8415083224423beb2d5a3c0a50e22090a75424276e1a0939cc800a
4
- data.tar.gz: 0517ecffe3b1f6c486b1cba2b5cbf4b3b7a8daf2cb11469b73d0d61cb5492cb1
3
+ metadata.gz: 924ae96ed9d907e15583c52df242c08481c7740a677e6c3a594f9d76ce2aad6e
4
+ data.tar.gz: b7f82710d54ba3e2bb2ffb974a2e46d82c734f53d909ea391986bfde260ea16d
5
5
  SHA512:
6
- metadata.gz: 5fe865bc6ae53bfbd34ca9e43c6c38d2dcb3874b0655b1960936960b690cfefc0203ecde64ea7ea3af17a8a1093382d98f419e5d88b24708af99ef210a282e7e
7
- data.tar.gz: 5c576ba8d79c0c6d21022d224b936f307640e2a785685cb82ec2eb0da588fcc8cbb215d2b7a05572ab91274978acc1f3a39edc0dfb5946fbef32dafa51389296
6
+ metadata.gz: 0ea73874b0712492e11a18d8b4d3339cf70604cf60c3b2da62d3cbd437a5af083dacee44dc222be71373abd726051ecf8cd6a063914cb96ad42ef54f35446ed5
7
+ data.tar.gz: 10f2982481609e45edd91c5b9a71bc7cac26957849151455359eb0ba9e55cff79688564f39b4611ea57de622296b1474e8c2d5190c7bbb1a9ea1a559366f6fa0
checksums.yaml.gz.sig CHANGED
Binary file
@@ -57,6 +57,11 @@ module Protocol
57
57
  def call(stream)
58
58
  while chunk = self.read
59
59
  stream.write(chunk)
60
+
61
+ # Flush the stream unless we are immediately expecting more data:
62
+ unless self.ready?
63
+ stream.flush
64
+ end
60
65
  end
61
66
  ensure
62
67
  stream.close
@@ -69,10 +69,10 @@ module Protocol
69
69
  end
70
70
  end
71
71
 
72
- self.each do |name, value|
73
- define_method(name) do |location, headers = nil, body = nil|
72
+ self.each do |name, method|
73
+ define_method(name) do |location, *arguments, **options|
74
74
  self.call(
75
- Request[value, location.to_s, Headers[headers], body]
75
+ Request[method, location.to_s, *arguments, **options]
76
76
  )
77
77
  end
78
78
  end
@@ -25,7 +25,7 @@ module Protocol
25
25
  class Request
26
26
  prepend Body::Reader
27
27
 
28
- def initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = Headers.new, body = nil, protocol = nil)
28
+ def initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = Headers.new, body = nil, protocol = nil, interim_response = nil)
29
29
  @scheme = scheme
30
30
  @authority = authority
31
31
  @method = method
@@ -34,6 +34,7 @@ module Protocol
34
34
  @headers = headers
35
35
  @body = body
36
36
  @protocol = protocol
37
+ @interim_response = interim_response
37
38
  end
38
39
 
39
40
  # @attribute [String] the request scheme, usually `"http"` or `"https"`.
@@ -60,11 +61,30 @@ module Protocol
60
61
  # @attribute [String | Array(String) | Nil] the request protocol, usually empty, but occasionally `"websocket"` or `"webtransport"`. In HTTP/1, it is used to request a connection upgrade, and in HTTP/2 it is used to indicate a specfic protocol for the stream.
61
62
  attr_accessor :protocol
62
63
 
64
+ # @attribute [Proc] a callback which is called when an interim response is received.
65
+ attr_accessor :interim_response
66
+
63
67
  # Send the request to the given connection.
64
68
  def call(connection)
65
69
  connection.call(self)
66
70
  end
67
71
 
72
+ # Send an interim response back to the origin of this request, if possible.
73
+ def send_interim_response(status, headers)
74
+ @interim_response&.call(status, headers)
75
+ end
76
+
77
+ def on_interim_response(&block)
78
+ if interim_response = @interim_response
79
+ @interim_response = ->(status, headers) do
80
+ block.call(status, headers)
81
+ interim_response.call(status, headers)
82
+ end
83
+ else
84
+ @interim_response = block
85
+ end
86
+ end
87
+
68
88
  # Whether this is a HEAD request: no body is expected in the response.
69
89
  def head?
70
90
  @method == Methods::HEAD
@@ -81,11 +101,11 @@ module Protocol
81
101
  # @parameter path [String] The path, e.g. `"/index.html"`, `"/search?q=hello"`, etc.
82
102
  # @parameter headers [Hash] The headers, e.g. `{"accept" => "text/html"}`, etc.
83
103
  # @parameter body [String | Array(String) | Body::Readable] The body, e.g. `"Hello, World!"`, etc. See {Body::Buffered.wrap} for more information about .
84
- def self.[](method, path, headers = nil, body = nil)
104
+ def self.[](method, path, _headers = nil, _body = nil, scheme: nil, authority: nil, headers: _headers, body: _body, protocol: nil, interim_response: nil)
85
105
  body = Body::Buffered.wrap(body)
86
- headers = ::Protocol::HTTP::Headers[headers]
106
+ headers = Headers[headers]
87
107
 
88
- self.new(nil, nil, method, path, nil, headers, body)
108
+ self.new(scheme, authority, method, path, nil, headers, body, protocol, interim_response)
89
109
  end
90
110
 
91
111
  # Whether the request can be replayed without side-effects.
@@ -130,9 +130,9 @@ module Protocol
130
130
  # @parameter status [Integer] The HTTP status code, e.g. `200`, `404`, etc.
131
131
  # @parameter headers [Hash] The headers, e.g. `{"content-type" => "text/html"}`, etc.
132
132
  # @parameter body [String | Array(String) | Body::Readable] The body, e.g. `"Hello, World!"`, etc. See {Body::Buffered.wrap} for more information about .
133
- def self.[](status, headers = nil, body = nil, protocol = nil)
133
+ def self.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil)
134
134
  body = Body::Buffered.wrap(body)
135
- headers = ::Protocol::HTTP::Headers[headers]
135
+ headers = Headers[headers]
136
136
 
137
137
  self.new(nil, status, headers, body, protocol)
138
138
  end
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module HTTP
8
- VERSION = "0.29.0"
8
+ VERSION = "0.31.0"
9
9
  end
10
10
  end
data/readme.md CHANGED
@@ -18,6 +18,29 @@ Please see the [project documentation](https://socketry.github.io/protocol-http/
18
18
 
19
19
  - [Design Overview](https://socketry.github.io/protocol-http/guides/design-overview/index) - This guide explains the high level design of `protocol-http` in the context of wider design patterns that can be used to implement HTTP clients and servers.
20
20
 
21
+ ## Releases
22
+
23
+ Please see the [project releases](https://socketry.github.io/protocol-http/releases/index) for all releases.
24
+
25
+ ### v0.31.0
26
+
27
+ - Ensure chunks are flushed if required, when streaming.
28
+
29
+ ### v0.30.0
30
+
31
+ - [`Request[]` and `Response[]` Keyword Arguments](https://socketry.github.io/protocol-http/releases/index#request[]-and-response[]-keyword-arguments)
32
+ - [Interim Response Handling](https://socketry.github.io/protocol-http/releases/index#interim-response-handling)
33
+
34
+ ## See Also
35
+
36
+ - [protocol-http1](https://github.com/socketry/protocol-http1) — HTTP/1 client/server implementation using this
37
+ interface.
38
+ - [protocol-http2](https://github.com/socketry/protocol-http2) — HTTP/2 client/server implementation using this
39
+ interface.
40
+ - [async-http](https://github.com/socketry/async-http) — Asynchronous HTTP client and server, supporting multiple HTTP
41
+ protocols & TLS.
42
+ - [async-websocket](https://github.com/socketry/async-websocket) — Asynchronous client and server WebSockets.
43
+
21
44
  ## Contributing
22
45
 
23
46
  We welcome contributions to this project.
@@ -35,13 +58,3 @@ In order to protect users of this project, we require all contributors to comply
35
58
  ### Community Guidelines
36
59
 
37
60
  This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
38
-
39
- ## See Also
40
-
41
- - [protocol-http1](https://github.com/socketry/protocol-http1) — HTTP/1 client/server implementation using this
42
- interface.
43
- - [protocol-http2](https://github.com/socketry/protocol-http2) — HTTP/2 client/server implementation using this
44
- interface.
45
- - [async-http](https://github.com/socketry/async-http) — Asynchronous HTTP client and server, supporting multiple HTTP
46
- protocols & TLS.
47
- - [async-websocket](https://github.com/socketry/async-websocket) — Asynchronous client and server WebSockets.
data/releases.md ADDED
@@ -0,0 +1,44 @@
1
+ # Releases
2
+
3
+ ## v0.31.0
4
+
5
+ - Ensure chunks are flushed if required, when streaming.
6
+
7
+ ## v0.30.0
8
+
9
+ ### `Request[]` and `Response[]` Keyword Arguments
10
+
11
+ The `Request[]` and `Response[]` methods now support keyword arguments as a convenient way to set various positional arguments.
12
+
13
+ ``` ruby
14
+ # Request keyword arguments:
15
+ client.get("/", headers: {"accept" => "text/html"}, authority: "example.com")
16
+
17
+ # Response keyword arguments:
18
+ def call(request)
19
+ return Response[200, headers: {"content-Type" => "text/html"}, body: "Hello, World!"]
20
+ ```
21
+
22
+ ### Interim Response Handling
23
+
24
+ The `Request` class now exposes a `#interim_response` attribute which can be used to handle interim responses both on the client side and server side.
25
+
26
+ On the client side, you can pass a callback using the `interim_response` keyword argument which will be invoked whenever an interim response is received:
27
+
28
+ ``` ruby
29
+ client = ...
30
+ response = client.get("/index", interim_response: proc{|status, headers| ...})
31
+ ```
32
+
33
+ On the server side, you can send an interim response using the `#send_interim_response` method:
34
+
35
+ ``` ruby
36
+ def call(request)
37
+ if request.headers["expect"] == "100-continue"
38
+ # Send an interim response:
39
+ request.send_interim_response(100)
40
+ end
41
+
42
+ # ...
43
+ end
44
+ ```
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.29.0
4
+ version: 0.31.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -47,7 +47,7 @@ cert_chain:
47
47
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
48
48
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
49
49
  -----END CERTIFICATE-----
50
- date: 2024-08-28 00:00:00.000000000 Z
50
+ date: 2024-09-03 00:00:00.000000000 Z
51
51
  dependencies: []
52
52
  description:
53
53
  email:
@@ -93,6 +93,7 @@ files:
93
93
  - lib/protocol/http/version.rb
94
94
  - license.md
95
95
  - readme.md
96
+ - releases.md
96
97
  homepage: https://github.com/socketry/protocol-http
97
98
  licenses:
98
99
  - MIT
metadata.gz.sig CHANGED
Binary file