Skip to content

Commit bce97b5

Browse files
committed
Add agent context.
1 parent 4b69b48 commit bce97b5

10 files changed

+1304
-1
lines changed

context/design-overview.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Design Overview
2+
3+
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.
4+
5+
## Request/Response Model
6+
7+
The main model we support is the request/response model. A client sends a request to a server which return response. The protocol is responsible for serializing the request and response objects.
8+
9+
```mermaid
10+
sequenceDiagram
11+
participant CA as Application
12+
participant Client
13+
participant Server
14+
participant SA as Application
15+
CA->>+Client: Request
16+
Client->>+Server: Request
17+
Server->>+SA: Request
18+
SA->>+Server: Response
19+
Server->>+Client: Response
20+
Client->>+CA: Response
21+
```
22+
23+
We provide an interface for request and response objects. This provides performance, predictability and robustness. This model has proven itself over several years, handling a variety of different use cases.
24+
25+
~~~ ruby
26+
class Request
27+
attr :method
28+
attr :target
29+
attr :headers
30+
attr :body
31+
end
32+
33+
class Response
34+
attr :status
35+
attr :headers
36+
attr :body
37+
end
38+
~~~
39+
40+
One other advantage is that it's symmetrical between clients and servers with a clear mapping, i.e. the protocol is responsible for transiting requests from the client to the server, and responses from the server back to the client. This helps us separate and define request/response interfaces independently from protocol implementation.
41+
42+
### Client Design
43+
44+
A request/response model implies that you create a request and receive a response back. This maps to a normal function call where the request is the argument and the response is the returned value.
45+
46+
~~~ ruby
47+
request = Request.new("GET", url)
48+
response = client.call(request)
49+
50+
response.headers
51+
response.read
52+
~~~
53+
54+
## Stream Model
55+
56+
An alternative model is the stream model. This model is more suitable for WebSockets and other persistent bi-directional channels.
57+
58+
```mermaid
59+
sequenceDiagram
60+
participant CA as Application
61+
participant Client
62+
participant Server
63+
participant SA as Application
64+
CA->>+Client: Stream
65+
Client->>+Server: Stream
66+
Server->>+SA: Stream
67+
```
68+
69+
The interfaces for streaming can be implemented a bit differently, since a response is not returned but rather assigned to the stream, and the streaming occurs in the same execution context as the client or server handling the request.
70+
71+
~~~ ruby
72+
class Stream
73+
# Request details.
74+
attr :method
75+
attr :target
76+
attr :headers
77+
78+
attr :response
79+
80+
# Write the response and start streaming the output body.
81+
def respond(status, headers)
82+
response.status = status
83+
response.headers = headers
84+
end
85+
86+
# Request body.
87+
attr_accessor :input
88+
89+
# Response body.
90+
attr_accessor :output
91+
92+
# Write to the response body.
93+
def write(...)
94+
@output.write(...)
95+
end
96+
97+
# Read from the request body.
98+
def read
99+
@input.read
100+
end
101+
end
102+
103+
class Response
104+
def initialize(method, target)
105+
@input = Body::Writable.new
106+
@output = Body::Writable.new
107+
end
108+
109+
attr_accessor :status
110+
attr_accessor :headers
111+
112+
# Prepare a stream for making a request.
113+
def request(method, target, headers)
114+
# Create a request stream suitable for writing into the buffered response:
115+
Stream.new(method, target, headers, self, @input, @output)
116+
end
117+
118+
# Write to the request body.
119+
def write(...)
120+
@input.write(...)
121+
end
122+
123+
# Read from the response body.
124+
def read
125+
@output.read
126+
end
127+
end
128+
~~~
129+
130+
### Client Design
131+
132+
A stream model implies that you create a stream which contains both the request and response bodies. This maps to a normal function call where the argument is the stream and the returned value is ignored.
133+
134+
~~~ ruby
135+
response = Response.new
136+
stream = response.request("GET", url)
137+
138+
client.call(stream)
139+
140+
response.headers
141+
response.read
142+
~~~
143+
144+
## Differences
145+
146+
The request/response model has a symmetrical design which naturally uses the return value for the result of executing the request. The result encapsulates the behaviour of how to read the response status, headers and body. Because of that, streaming input and output becomes a function of the result object itself. As in:
147+
148+
~~~ ruby
149+
def call(request)
150+
body = Body::Writable.new
151+
152+
Fiber.schedule do
153+
while chunk = request.input.read
154+
body.write(chunk.reverse)
155+
end
156+
end
157+
158+
return Response[200, headers, body]
159+
end
160+
161+
input = Body::Writable.new
162+
response = call(... body ...)
163+
164+
input.write("Hello World")
165+
input.close
166+
response.read -> "dlroW olleH"
167+
~~~
168+
169+
The streaming model does not have the same symmetry, and instead opts for a uni-directional flow of information.
170+
171+
~~~ruby
172+
def call(stream)
173+
stream.respond(200, headers)
174+
175+
Fiber.schedule do
176+
while chunk = stream.read
177+
stream.write(chunk.reverse)
178+
end
179+
end
180+
end
181+
182+
input = Body::Writable.new
183+
response = Response.new(...input...)
184+
call(response.stream)
185+
186+
input.write("Hello World")
187+
input.close
188+
response.read -> "dlroW olleH"
189+
~~~
190+
191+
The value of this uni-directional flow is that it is natural for the stream to be taken out of the scope imposed by the nested `call(request)` model. However, the user must explicitly close the stream, since it's no longer scoped to the client and/or server.
192+
193+
## Interim Response Handling
194+
195+
Interim responses are responses that are sent before the final response. They are used for things like `103 Early Hints` and `100 Continue`. These responses are sent before the final response, and are used to signal to the client that the server is still processing the request.
196+
197+
```ruby
198+
body = Body::Writable.new
199+
200+
interim_response_callback = proc do |status, headers|
201+
if status == 100
202+
# Continue sending the request body.
203+
body.write("Hello World")
204+
body.close
205+
end
206+
end
207+
208+
response = client.post("/upload", {'expect' => '100-continue'}, body, interim_response: interim_response_callback)
209+
```

context/getting-started.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Getting Started
2+
3+
This guide explains how to use `protocol-http` for building abstract HTTP interfaces.
4+
5+
## Installation
6+
7+
Add the gem to your project:
8+
9+
~~~ bash
10+
$ bundle add protocol-http
11+
~~~
12+
13+
## Core Concepts
14+
15+
`protocol-http` has several core concepts:
16+
17+
- A {ruby Protocol::HTTP::Request} instance which represents an abstract HTTP request. Specific versions of HTTP may subclass this to track additional state.
18+
- A {ruby Protocol::HTTP::Response} instance which represents an abstract HTTP response. Specific versions of HTTP may subclass this to track additional state.
19+
- A {ruby Protocol::HTTP::Middleware} interface for building HTTP applications.
20+
- A {ruby Protocol::HTTP::Headers} interface for storing HTTP headers with semantics based on documented specifications (RFCs, etc).
21+
- A set of {ruby Protocol::HTTP::Body} classes which handle the internal request and response bodies, including bi-directional streaming.
22+
23+
## Integration
24+
25+
This gem does not provide any specific client or server implementation, rather it's used by several other gems.
26+
27+
- [Protocol::HTTP1](https://github.com/socketry/protocol-http1) & [Protocol::HTTP2](https://github.com/socketry/protocol-http2) which provide client and server implementations.
28+
- [Async::HTTP](https://github.com/socketry/async-http) which provides connection pooling and concurrency.
29+
30+
## Usage
31+
32+
### Request
33+
34+
{ruby Protocol::HTTP::Request} represents an HTTP request which can be used both server and client-side.
35+
36+
``` ruby
37+
require 'protocol/http/request'
38+
39+
# Short form (recommended):
40+
request = Protocol::HTTP::Request["GET", "/index.html", {"accept" => "text/html"}]
41+
42+
# Long form:
43+
headers = Protocol::HTTP::Headers[["accept", "text/html"]]
44+
request = Protocol::HTTP::Request.new("http", "example.com", "GET", "/index.html", "HTTP/1.1", headers)
45+
46+
# Access request properties
47+
request.method # => "GET"
48+
request.path # => "/index.html"
49+
request.headers # => Protocol::HTTP::Headers instance
50+
```
51+
52+
### Response
53+
54+
{ruby Protocol::HTTP::Response} represents an HTTP response which can be used both server and client-side.
55+
56+
``` ruby
57+
require 'protocol/http/response'
58+
59+
# Short form (recommended):
60+
response = Protocol::HTTP::Response[200, {"content-type" => "text/html"}, "Hello, World!"]
61+
62+
# Long form:
63+
headers = Protocol::HTTP::Headers["content-type" => "text/html"]
64+
body = Protocol::HTTP::Body::Buffered.wrap("Hello, World!")
65+
response = Protocol::HTTP::Response.new("HTTP/1.1", 200, headers, body)
66+
67+
# Access response properties
68+
response.status # => 200
69+
response.headers # => Protocol::HTTP::Headers instance
70+
response.body # => Body instance
71+
72+
# Status checking methods
73+
response.success? # => true (200-299)
74+
response.ok? # => true (200)
75+
response.redirection? # => false (300-399)
76+
response.failure? # => false (400-599)
77+
```
78+
79+
### Headers
80+
81+
{ruby Protocol::HTTP::Headers} provides semantically meaningful interpretation of header values and implements case-normalising keys.
82+
83+
#### Basic Usage
84+
85+
``` ruby
86+
require 'protocol/http/headers'
87+
88+
headers = Protocol::HTTP::Headers.new
89+
90+
# Assignment by title-case key:
91+
headers['Content-Type'] = "image/jpeg"
92+
93+
# Lookup by lower-case (normalized) key:
94+
headers['content-type']
95+
# => "image/jpeg"
96+
```
97+
98+
#### Semantic Processing
99+
100+
Many headers receive special semantic processing, automatically splitting comma-separated values and providing structured access:
101+
102+
``` ruby
103+
# Accept header with quality values:
104+
headers['Accept'] = 'text/html, application/json;q=0.8, */*;q=0.1'
105+
accept = headers['accept']
106+
# => ["text/html", "application/json;q=0.8", "*/*;q=0.1"]
107+
108+
# Access parsed media ranges with quality factors:
109+
accept.media_ranges.each do |range|
110+
puts "#{range.type}/#{range.subtype} (q=#{range.quality_factor})"
111+
end
112+
# text/html (q=1.0)
113+
# application/json (q=0.8)
114+
# */* (q=0.1)
115+
116+
# Accept-Encoding automatically splits values:
117+
headers['Accept-Encoding'] = 'gzip, deflate, br;q=0.9'
118+
headers['accept-encoding']
119+
# => ["gzip", "deflate", "br;q=0.9"]
120+
121+
# Cache-Control splits directives:
122+
headers['Cache-Control'] = 'max-age=3600, no-cache, must-revalidate'
123+
headers['cache-control']
124+
# => ["max-age=3600", "no-cache", "must-revalidate"]
125+
126+
# Vary header normalizes field names to lowercase:
127+
headers['Vary'] = 'Accept-Encoding, User-Agent'
128+
headers['vary']
129+
# => ["accept-encoding", "user-agent"]
130+
```

0 commit comments

Comments
 (0)