-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Viaduct replacement demo #6018
Open
bendk
wants to merge
1
commit into
mozilla:main
Choose a base branch
from
bendk:fairy-bridge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,448
−6
Open
Viaduct replacement demo #6018
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "fairy-bridge" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[features] | ||
default = [] | ||
backend-reqwest = ["dep:reqwest"] | ||
backend-c = ["dep:oneshot"] | ||
|
||
[dependencies] | ||
async-trait = "0.1" | ||
oneshot = { version = "0.1", optional = true } | ||
pollster = "0.3.0" | ||
serde = "1" | ||
serde_json = "1" | ||
thiserror = "1" | ||
tokio = { version = "1", features = ["rt-multi-thread"] } | ||
uniffi = { workspace = true } | ||
url = "2.2" | ||
reqwest = { version = "0.11.23", optional = true } | ||
|
||
[build-dependencies] | ||
uniffi = { workspace = true, features = ["build"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Fairy Bridge | ||
|
||
Fairy Bridge is an HTTP request bridge library that allows requests to be made using various | ||
backends, including: | ||
|
||
- The builtin reqwest backend | ||
- Custom Rust backends | ||
- Custom backends written in the foreign language | ||
|
||
The plan for this is: | ||
- iOS will use the reqwest backend | ||
- Android will use a custom backend in Kotlin using fetch | ||
(https://github.com/mozilla-mobile/firefox-android/tree/35ce01367157440f9e9daa4ed48a8022af80c8f2/android-components/components/concept/fetch) | ||
- Desktop will use a custom backend in Rust that hooks into necko | ||
|
||
## Sync / Async | ||
|
||
The backends are implemented using async code, but there's also the option to block on a request. | ||
This means `fairy-bridge` can be used in both sync and async contexts. | ||
|
||
## Cookies / State | ||
|
||
Cookies and state are outside the scope of this library. Any such functionality is the responsibility of the consumer. | ||
|
||
## Name | ||
|
||
`fairy-bridge` is named after the Fairy Bridge (Xian Ren Qiao) -- the largest known natural bridge in the world, located in northwestern Guangxi Province, China. | ||
|
||
![Picture of the Fairy Bridge](http://www.naturalarches.org/big9_files/FairyBridge1680.jpg) | ||
|
||
# Backends | ||
|
||
## Reqwest | ||
|
||
- Handle requests using the Rust [reqwest library](https://docs.rs/reqwest/latest/reqwest/). | ||
- This backend creates a tokio thread to execute the requests. | ||
- Call `fairy_bridge::init_backend_reqwest` to select this backend. | ||
|
||
## Foreign code | ||
|
||
- The foreign code can implement a backend themselves by implementing the `fairy_bridge::Backend` trait. | ||
- Pass an instance of the object that implements the trait to `fairy_bridge::init_backend` to select this backend. | ||
|
||
## C / C++ code | ||
|
||
- A backend can also be implemented in C / C++ code | ||
- Include the `c-backend-include/fairy_bridge.h` file. | ||
- Implement the `fairy_bridge_backend_c_send_request` function. | ||
- Call `fairy_bridge::init_backend_c` to select this backend (from the bindings language, not C). | ||
- See `examples/fairy-bridge-demo` for a code example. | ||
|
||
## (Coming soon) Necko backend | ||
|
||
- The geckoview `libxul` library comes with a Necko-based c backend. | ||
- Link to `libxul` and call `fairy_bridge::init_backend_c` to select this backend. |
100 changes: 100 additions & 0 deletions
100
components/fairy-bridge/c-backend-include/fairy_bridge.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include <cstdint> | ||
|
||
namespace fairy_bridge { | ||
|
||
struct BackendSettings { | ||
uint32_t timeout; | ||
uint32_t connect_timeout; | ||
uint32_t redirect_limit; | ||
}; | ||
|
||
|
||
enum class Method { | ||
Get, | ||
Head, | ||
Post, | ||
Put, | ||
Delete, | ||
Connect, | ||
Options, | ||
Trace, | ||
Patch, | ||
}; | ||
|
||
struct Header { | ||
const char* key; | ||
const char* value; | ||
}; | ||
|
||
struct Request { | ||
Method method; | ||
const char* url; | ||
Header* headers; | ||
size_t header_count; | ||
const char* body; | ||
size_t body_length; | ||
}; | ||
|
||
/** | ||
* Opaque HTTP result type | ||
*/ | ||
struct Result; | ||
|
||
/** | ||
* Calls used to build up a Result. | ||
* | ||
* Strings are passed as (const char*, size_t), pairs since this is often easier for backends to work with. | ||
*/ | ||
extern "C" { | ||
void fairy_bridge_result_set_url(Result* result, const char* url, size_t length); | ||
void fairy_bridge_result_set_status_code(Result* result, uint16_t code); | ||
void fairy_bridge_result_add_header(Result* result, const char* key, size_t key_length, const char* value, size_t value_length); | ||
void fairy_bridge_result_extend_body(Result* result, const char* data, size_t length); | ||
} | ||
|
||
/** | ||
* Complete a result | ||
* | ||
* Call this after the result has been successfully built using the previous methods. This | ||
* consumes the result pointer and it should not be used again by the backend. | ||
*/ | ||
extern "C" { | ||
void fairy_bridge_result_complete(Result* result); | ||
} | ||
|
||
/** | ||
* Complete a result with an error | ||
* | ||
* This causes an error to be returned for the result. Any previous builder calls will be | ||
* ignored. This consumes the result pointer and it should not be used again by the backend. | ||
*/ | ||
extern "C" { | ||
void fairy_bridge_result_complete_error(Result* result, const char* message, size_t length); | ||
} | ||
|
||
} // namespace fairy_bridge | ||
|
||
/** | ||
* Backend API | ||
* | ||
* This must be implemented by the backend code. | ||
*/ | ||
extern "C" { | ||
/** | ||
* Initialize the backend. This is called once at startup. | ||
*/ | ||
void fairy_bridge_backend_c_init(fairy_bridge::BackendSettings settings); | ||
|
||
/** | ||
* Perform a rquest | ||
* | ||
* The backend should schedule the request to be performed in a separate thread. | ||
* | ||
* The result is initially empty. It should be built up and completed by the | ||
* `fairy_bridge_result_*` functions. | ||
* | ||
* `request` and `result` are valid until `fairy_bridge_result_complete` or | ||
* `fairy_bridge_result_complete_error` is called. After that they should not be used. | ||
*/ | ||
void fairy_bridge_backend_c_send_request(fairy_bridge::Request* request, fairy_bridge::Result* result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use crate::{FairyBridgeError, Request, Response}; | ||
use std::sync::Arc; | ||
|
||
/// Settings for a backend instance | ||
/// | ||
/// Backend constructions should input this in order to configure themselves | ||
/// | ||
/// Repr(C) so we can pass it to the C backend | ||
#[derive(Debug, uniffi::Record)] | ||
#[repr(C)] | ||
pub struct BackendSettings { | ||
// Connection timeout in ms (0 indicates no timeout). | ||
#[uniffi(default = 0)] | ||
pub connect_timeout: u32, | ||
// Timeout for the entire request in ms (0 indicates no timeout). | ||
#[uniffi(default = 0)] | ||
pub timeout: u32, | ||
// Maximum amount of redirects to follow (0 means redirects are not allowed) | ||
#[uniffi(default = 10)] | ||
pub redirect_limit: u32, | ||
} | ||
|
||
#[uniffi::export(with_foreign)] | ||
#[async_trait::async_trait] | ||
pub trait Backend: Send + Sync { | ||
async fn send_request(self: Arc<Self>, request: Request) -> Result<Response, FairyBridgeError>; | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn init_backend(backend: Arc<dyn Backend>) -> Result<(), FairyBridgeError> { | ||
crate::REGISTERED_BACKEND | ||
.set(backend) | ||
.map_err(|_| FairyBridgeError::BackendAlreadyInitialized) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love it :)