| 1 |
# Viaduct |
| 2 |
|
| 3 |
Viaduct is our HTTP request library, which can make requests either via a |
| 4 |
rust-based (reqwest) networking stack (used on iOS and for local desktop use, |
| 5 |
for tests and the like), or using a stack that calls a function passed into it |
| 6 |
over the FFI (on android). |
| 7 |
|
| 8 |
For usage info, you can run `cargo +nightly doc -p viaduct` (the `+nightly` is |
| 9 |
optional, however some intra-doc links require it), it has several examples. |
| 10 |
|
| 11 |
## Android/FFI Backend overview |
| 12 |
|
| 13 |
On Android, the backend works as follows: |
| 14 |
|
| 15 |
1. During megazord initialization, we are passed a `Lazy<Client>` (`Client` comes |
| 16 |
from the [concept-fetch](https://github.com/mozilla-mobile/android-components/tree/master/components/concept/fetch) |
| 17 |
android component, and `Lazy` is from the Kotlin stdlib). |
| 18 |
|
| 19 |
- It also sets a flag that indicates that even if the FFI backend never gets |
| 20 |
fully initialized (e.g. with a callback), we should error rather than use |
| 21 |
the reqwest backend (which should not be compiled in, however we've had |
| 22 |
trouble ensuring this in the past, although at this point we have checks |
| 23 |
in CI to ensure it is not present). |
| 24 |
|
| 25 |
2. At this point, a JNA `Callback` instance is created and passed into Rust. |
| 26 |
- This serves to proxy the request made by Rust to the `Client`. |
| 27 |
- The `Callback` instance is never allowed to be GCed. |
| 28 |
- To Rust, it's just a `extern "C"` function pointer that get's stored in an |
| 29 |
atomic variable and never can be unset. |
| 30 |
|
| 31 |
3. When Rust makes a request: |
| 32 |
1. We serialize the request info into a protobuf record |
| 33 |
2. This record is passed into the function pointer we should have by this |
| 34 |
point (erroring if it has not been set yet). |
| 35 |
3. The callback (on the Java side now) deserializes the protobuf record, |
| 36 |
converts it to a concept-fetch Request instance, and passes it to the |
| 37 |
client. |
| 38 |
4. The response (or error) is then converted into a protobuf record. The |
| 39 |
java code then asks Rust for a buffer big enough to hold the serialized |
| 40 |
response (or error). |
| 41 |
5. The response is written to the buffer, and returned to Rust. |
| 42 |
6. Rust then decodes the protobuf, and converts it to a |
| 43 |
`viaduct::Response` object that it returns to the caller. |
| 44 |
|
| 45 |
Some notes: |
| 46 |
|
| 47 |
- This "request flow" is entirely synchronous, simplifying the implementation |
| 48 |
considerably. |
| 49 |
|
| 50 |
- Cookies are explicitly not supported at the moment, adding them would |
| 51 |
require a separate security review. |
| 52 |
|
| 53 |
- Generally, this is the way the FFI backend is expected to work on any |
| 54 |
platform, but for concreteness (and because it's the only one currently using |
| 55 |
the FFI backend), we explained it for Android. |
| 56 |
|
| 57 |
- Most of the code in `viaduct` is defining a ergonomic HTTP facade, and is |
| 58 |
unrelated to this (or to the reqwest backend). This code is more or less |
| 59 |
entirely (in the Kotlin layer and) in `src/backend/ffi.rs`. |