Skip to content

Commit 79ebbb0

Browse files
committed
Document API for push promises in HTTP client
1 parent 8846f91 commit 79ebbb0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/reference/cro-http-client.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,41 @@ and `2`.
349349
The default is `:http<1.1>` for a HTTP request, and `:http<1.1 2>` for a HTTPS
350350
request. It is not legal to use `:http<1.1 2>` with a HTTP connection, as ALPN
351351
is the only supported mechanism for deciding which protocol to use.
352+
353+
## Push promises
354+
355+
**Upcoming Feature::** *This section describes a feature that will be included
356+
in an upcoming Cro release.*
357+
358+
HTTP/2.0 proides push promises, which allow the server to push extra resources
359+
to the client as part of the response. By default, `Cro::HTTP::Client` will
360+
instruct the remote server to **not** send push promises. To opt in to this
361+
feature, either:
362+
363+
* If making an instance of `Cro::HTTP::Client`, pass `:push-promises` to the
364+
constructor to enable them for all requests made with the client instance
365+
* Otherwise, pass `:push-promises` when making a request (for example, to
366+
the `get` method). However, when using HTTP/2.0, it's usually wise to make
367+
an instance and re-use the connection for many requests.
368+
369+
Push promises are obtained by calling the `push-promises` method of the
370+
`Cro::HTTP::Response` object that the request produces. This returns a `Supply`
371+
that emits an instance of `Cro::HTTP::PushPromise` for each push promise the
372+
server sends. Each of those in turn has a `response` property that returns a
373+
`Promise` that will be kept with a `Cro::HTTP::Response` object when the push
374+
promise is fulfilled.
375+
376+
Making a request and obtaining all push promises can therefore be achieved as
377+
follows:
378+
379+
```
380+
react {
381+
my $client = Cro::HTTP::Client.new(:push-promises);
382+
my $response = await $client.get($url);
383+
whenever $response.push-promises -> $prom {
384+
whenever $prom.response -> $resp {
385+
say "Push promise for $prom.target() had status $resp.status()";
386+
}
387+
}
388+
}
389+
```

0 commit comments

Comments
 (0)