use std::borrow::Cow;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
mod client;
#[cfg(all(feature = "reqwest", not(target_arch = "wasm32")))]
mod reqwest_provider;
mod session;
pub mod simulation;
mod statistics;
mod validators;
pub use session::{Provider, Session};
pub use statistics::{Statistics, TransferStatistics};
pub use validators::CacheValidators;
#[cfg(not(target_arch = "wasm32"))]
pub mod testing;
pub use client::{
Body, Cache, CachePolicy, Capabilities, Challenge, ChallengeReply, Client, ClientBuilder,
Collecting, Connecting, Cookie, CookieJar, Cookies, Fetching, Form, Head, Hop, Identity,
InFlight, Message, Metrics, NextChunk, NextMessage, RedirectReply, Redirects, Scheme, Sending,
ServerTrust, Streaming, Trust, TrustReply, WebSocket, WsSender,
};
pub(crate) use client::{Payload, ProgressFn};
pub mod transport {
pub use crate::client::{
Answer, AuthQuestion, Completion, Event, Events, Prepared, PreparedBody, Question,
QuestionId, SharedReader, Socket, Transfer, Transport, TransportConfig, WsEvent, WsEvents,
};
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Method {
Get,
Post,
Put,
Delete,
Patch,
Head,
Options,
Propfind,
Proppatch,
Mkcol,
Copy,
Move,
Lock,
Unlock,
Report,
}
impl Method {
pub fn as_str(self) -> &'static str {
match self {
Method::Get => "GET",
Method::Post => "POST",
Method::Put => "PUT",
Method::Delete => "DELETE",
Method::Patch => "PATCH",
Method::Head => "HEAD",
Method::Options => "OPTIONS",
Method::Propfind => "PROPFIND",
Method::Proppatch => "PROPPATCH",
Method::Mkcol => "MKCOL",
Method::Copy => "COPY",
Method::Move => "MOVE",
Method::Lock => "LOCK",
Method::Unlock => "UNLOCK",
Method::Report => "REPORT",
}
}
pub fn parse(token: &str) -> Option<Method> {
[
Method::Get,
Method::Post,
Method::Put,
Method::Delete,
Method::Patch,
Method::Head,
Method::Options,
Method::Propfind,
Method::Proppatch,
Method::Mkcol,
Method::Copy,
Method::Move,
Method::Lock,
Method::Unlock,
Method::Report,
]
.into_iter()
.find(|m| m.as_str().eq_ignore_ascii_case(token))
}
}
#[derive(Clone, Debug)]
pub struct Request {
pub(crate) method: Method,
pub(crate) url: String,
pub(crate) headers: Vec<(String, String)>,
pub(crate) payload: Payload,
pub(crate) timeout: Option<Duration>,
pub(crate) timeout_total: Option<Duration>,
pub(crate) allow_expensive: bool,
pub(crate) allow_constrained: bool,
pub(crate) cache: CachePolicy,
pub(crate) priority: Option<f32>,
pub(crate) protocols: Vec<String>,
pub(crate) upload_progress: Option<ProgressFn>,
}
impl Request {
pub fn new(method: Method, url: impl Into<String>) -> Request {
Request {
method,
url: url.into(),
headers: Vec::new(),
payload: Payload::Empty,
timeout: None,
timeout_total: None,
allow_expensive: true,
allow_constrained: true,
cache: CachePolicy::Default,
priority: None,
protocols: Vec::new(),
upload_progress: None,
}
}
pub fn get(url: impl Into<String>) -> Request {
Self::new(Method::Get, url)
}
pub fn post(url: impl Into<String>, body: Vec<u8>) -> Request {
Self::new(Method::Post, url).body(body)
}
pub fn put(url: impl Into<String>, body: Vec<u8>) -> Request {
Self::new(Method::Put, url).body(body)
}
pub fn delete(url: impl Into<String>) -> Request {
Self::new(Method::Delete, url)
}
pub fn patch(url: impl Into<String>, body: Vec<u8>) -> Request {
Self::new(Method::Patch, url).body(body)
}
pub fn head(url: impl Into<String>) -> Request {
Self::new(Method::Head, url)
}
pub fn method(&self) -> Method {
self.method
}
pub fn url(&self) -> &str {
&self.url
}
pub fn headers(&self) -> &[(String, String)] {
&self.headers
}
pub fn header(mut self, name: &str, value: &str) -> Self {
self.headers.push((name.to_string(), value.to_string()));
self
}
pub fn bearer(self, token: &str) -> Self {
self.header("Authorization", &format!("Bearer {token}"))
}
pub fn basic_auth(self, user: &str, password: &str) -> Self {
let value = client::basic_authorization(user, password);
self.header("Authorization", &value)
}
pub fn body(mut self, bytes: Vec<u8>) -> Self {
self.payload = Payload::Bytes(Arc::new(bytes));
self
}
pub fn body_file(mut self, path: impl Into<PathBuf>) -> Self {
self.payload = Payload::File(path.into());
self
}
pub fn body_stream(
mut self,
reader: impl std::io::Read + Send + 'static,
len: Option<u64>,
) -> Self {
self.payload = Payload::Stream(transport::SharedReader::new(reader), len);
self
}
pub fn form(mut self, form: Form) -> Self {
self.payload = Payload::Form(form);
self
}
pub fn timeout(mut self, d: Duration) -> Self {
self.timeout = Some(d);
self
}
pub fn timeout_total(mut self, d: Duration) -> Self {
self.timeout_total = Some(d);
self
}
pub fn allow_expensive(mut self, allowed: bool) -> Self {
self.allow_expensive = allowed;
self
}
pub fn allow_constrained(mut self, allowed: bool) -> Self {
self.allow_constrained = allowed;
self
}
pub fn cache(mut self, policy: CachePolicy) -> Self {
self.cache = policy;
self
}
pub fn priority(mut self, priority: f32) -> Self {
self.priority = Some(priority.clamp(0.0, 1.0));
self
}
pub fn protocols<S: Into<String>>(mut self, protocols: impl IntoIterator<Item = S>) -> Self {
self.protocols = protocols.into_iter().map(Into::into).collect();
self
}
pub fn upload_progress(
mut self,
observe: impl Fn(u64, Option<u64>) + Send + Sync + 'static,
) -> Self {
self.upload_progress = Some(ProgressFn(Arc::new(observe)));
self
}
}
#[derive(Clone, Debug)]
pub struct Response {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
pub url: String,
pub metrics: Option<Metrics>,
}
impl Response {
pub fn new(status: u16, headers: Vec<(String, String)>, body: Vec<u8>) -> Response {
Response {
status,
headers,
body,
url: String::new(),
metrics: None,
}
}
pub(crate) fn assemble(head: Head, body: Vec<u8>, metrics: Option<Metrics>) -> Response {
Response {
status: head.status,
headers: head.headers,
body,
url: head.url,
metrics,
}
}
pub fn text(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.body)
}
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
}
#[derive(Clone, Debug)]
pub struct Download {
pub status: u16,
pub headers: Vec<(String, String)>,
pub bytes_written: u64,
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HttpError {
BadUrl(String),
Timeout,
Dns,
Connect,
Tls(String),
Io(String),
Cancelled,
Unsupported,
TooManyRedirects,
Status(u16),
BodyTooLarge { limit: usize },
}
impl std::fmt::Display for HttpError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HttpError::BadUrl(u) => write!(f, "bad url: {u}"),
HttpError::Timeout => write!(f, "request timed out"),
HttpError::Dns => write!(f, "host name resolution failed"),
HttpError::Connect => write!(f, "connection failed"),
HttpError::Tls(m) => write!(f, "TLS failure: {m}"),
HttpError::Io(m) => write!(f, "{m}"),
HttpError::Cancelled => write!(f, "request cancelled"),
HttpError::Unsupported => write!(f, "not supported on this platform"),
HttpError::TooManyRedirects => write!(f, "too many redirects"),
HttpError::Status(s) => write!(f, "unexpected status {s}"),
HttpError::BodyTooLarge { limit } => write!(f, "response exceeds {limit} bytes"),
}
}
}
impl std::error::Error for HttpError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tier {
NativeStack,
Unavailable,
}
impl Tier {
pub fn label(self) -> &'static str {
match self {
Tier::NativeStack => "native",
Tier::Unavailable => "unavailable",
}
}
}
pub fn tier() -> Tier {
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
{
imp::tier()
}
#[cfg(not(all(target_os = "linux", not(target_env = "ohos"))))]
{
imp::TIER
}
}
pub fn capabilities() -> Capabilities {
platform_capabilities()
}
pub(crate) fn platform_capabilities() -> Capabilities {
imp::capabilities()
}
pub(crate) fn platform_transport(
config: &transport::TransportConfig,
) -> Arc<dyn transport::Transport> {
imp::transport(config)
}
fn stateless() -> &'static Client {
static CLIENT: std::sync::OnceLock<Client> = std::sync::OnceLock::new();
CLIENT.get_or_init(|| {
Client::builder()
.cookies(Cookies::Off)
.cache(Cache::Off)
.build()
})
}
pub async fn fetch_limited_future(req: Request, max_bytes: usize) -> Result<Response, HttpError> {
stateless().fetch_limited_future(req, max_bytes).await
}
pub async fn fetch_limited_with_progress_future(
req: Request,
max_bytes: usize,
progress: impl FnMut(u64, Option<u64>),
) -> Result<Response, HttpError> {
stateless()
.fetch_limited_with_progress_future(req, max_bytes, progress)
.await
}
#[cfg(not(target_arch = "wasm32"))]
fn streamed_through(
client: &Client,
req: &Request,
sink: &mut dyn StreamSink,
) -> Result<Download, HttpError> {
#[cfg(all(target_os = "linux", target_env = "ohos"))]
if day_bridge::arkts::on_js_thread() {
return Err(HttpError::Unsupported);
}
let streaming = client::wait(client.send_future(req.clone()))?;
let (status, headers) = (streaming.status(), streaming.headers().to_vec());
if !sink.head(status, &headers) {
return Err(HttpError::Io("aborted".into()));
}
let mut body = streaming.into_body();
let mut bytes_written = 0u64;
while let Some(chunk) = body.next_blocking() {
let chunk = chunk?;
sink.chunk(&chunk)?;
bytes_written += chunk.len() as u64;
}
Ok(Download {
status,
headers,
bytes_written,
})
}
pub fn fetch(req: &Request) -> Result<Response, HttpError> {
#[cfg(target_arch = "wasm32")]
{
let _ = req;
Err(HttpError::Unsupported)
}
#[cfg(not(target_arch = "wasm32"))]
{
#[cfg(all(target_os = "linux", target_env = "ohos"))]
if day_bridge::arkts::on_js_thread() {
return Err(HttpError::Unsupported);
}
client::wait(stateless().fetch_future(req.clone()))
}
}
pub fn fetch_async(
req: Request,
on_done: impl FnOnce(Result<Response, HttpError>) + Send + 'static,
) {
let _ = start_cancellable(req, Box::new(on_done));
}
pub fn fetch_to_file(req: &Request, dest: &Path) -> Result<Download, HttpError> {
#[cfg(target_arch = "wasm32")]
let out = {
let _ = req;
Err(HttpError::Unsupported)
};
#[cfg(not(target_arch = "wasm32"))]
let out = {
struct FileSink(std::fs::File);
impl StreamSink for FileSink {
fn chunk(&mut self, data: &[u8]) -> Result<(), HttpError> {
use std::io::Write;
self.0
.write_all(data)
.map_err(|e| HttpError::Io(e.to_string()))
}
}
std::fs::File::create(dest)
.map_err(|e| HttpError::Io(e.to_string()))
.and_then(|file| streamed_through(stateless(), req, &mut FileSink(file)))
};
if out.is_err() {
let _ = std::fs::remove_file(dest);
}
out
}
pub fn fetch_future(req: Request) -> FetchFuture {
let shared = Arc::new(Mutex::new(FutureState {
result: None,
waker: None,
cancelled: false,
}));
let deliver = shared.clone();
let cancel = start_cancellable(
req,
Box::new(move |result| deliver_future(&deliver, result)),
);
FetchFuture {
shared,
cancel,
done: false,
}
}
struct FutureState {
result: Option<Result<Response, HttpError>>,
waker: Option<std::task::Waker>,
cancelled: bool,
}
pub struct FetchFuture {
shared: Arc<Mutex<FutureState>>,
cancel: Option<Box<dyn FnOnce() + Send>>,
done: bool,
}
fn lock(m: &Mutex<FutureState>) -> std::sync::MutexGuard<'_, FutureState> {
match m.lock() {
Ok(g) => g,
Err(poisoned) => poisoned.into_inner(),
}
}
impl Future for FetchFuture {
type Output = Result<Response, HttpError>;
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let mut st = lock(&self.shared);
if let Some(r) = st.result.take() {
drop(st);
self.done = true;
return std::task::Poll::Ready(r);
}
st.waker = Some(cx.waker().clone());
std::task::Poll::Pending
}
}
impl Drop for FetchFuture {
fn drop(&mut self) {
if self.done {
return;
}
{
let mut st = lock(&self.shared);
st.cancelled = true;
st.waker = None;
}
if let Some(cancel) = self.cancel.take() {
cancel();
}
}
}
fn deliver_future(shared: &Arc<Mutex<FutureState>>, result: Result<Response, HttpError>) {
let waker = {
let mut st = lock(shared);
if st.cancelled {
return;
}
st.result = Some(result);
st.waker.take()
};
if let Some(w) = waker {
w.wake();
}
}
type Completion = Box<dyn FnOnce(Result<Response, HttpError>) + Send>;
type CancelFn = Box<dyn FnOnce() + Send>;
fn start_cancellable(req: Request, on_done: Completion) -> Option<CancelFn> {
Some(stateless().fetch_cancellable(req, on_done))
}
pub fn fetch_to_file_async(
req: Request,
dest: PathBuf,
on_done: impl FnOnce(Result<Download, HttpError>) + Send + 'static,
) {
#[cfg(target_arch = "wasm32")]
{
on_done(fetch_to_file(&req, &dest));
}
#[cfg(not(target_arch = "wasm32"))]
std::thread::spawn(move || on_done(fetch_to_file(&req, &dest)));
}
pub trait StreamSink {
fn head(&mut self, _status: u16, _headers: &[(String, String)]) -> bool {
true
}
fn chunk(&mut self, data: &[u8]) -> Result<(), HttpError>;
}
pub fn fetch_streamed(req: &Request, sink: &mut dyn StreamSink) -> Result<Download, HttpError> {
#[cfg(target_arch = "wasm32")]
{
let _ = (req, sink);
Err(HttpError::Unsupported)
}
#[cfg(not(target_arch = "wasm32"))]
{
streamed_through(stateless(), req, sink)
}
}
mod bridge;
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[path = "apple.rs"]
mod imp;
#[cfg(target_os = "windows")]
#[path = "windows.rs"]
mod imp;
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
#[path = "linux.rs"]
mod imp;
#[cfg(all(test, target_os = "macos"))]
#[path = "linux.rs"]
#[allow(dead_code)]
mod curl;
#[cfg(any(
target_os = "android",
all(target_os = "linux", target_env = "ohos"),
target_arch = "wasm32"
))]
#[path = "bridged.rs"]
mod imp;
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "windows",
target_os = "linux",
target_os = "android",
target_arch = "wasm32"
)))]
mod imp {
use std::sync::Arc;
use super::{HttpError, Tier};
use crate::client::{
Answer, Capabilities, Event, Events, Prepared, QuestionId, Transfer, Transport,
TransportConfig,
};
pub const TIER: Tier = Tier::Unavailable;
pub(crate) fn capabilities() -> Capabilities {
Capabilities::default()
}
pub(crate) fn transport(_config: &TransportConfig) -> Arc<dyn Transport> {
Arc::new(Unavailable)
}
struct Unavailable;
impl Transport for Unavailable {
fn capabilities(&self) -> Capabilities {
capabilities()
}
fn start(&self, _request: Prepared, events: Events) -> Arc<dyn Transfer> {
events(Event::Failed(HttpError::Unsupported));
Arc::new(Unavailable)
}
}
impl Transfer for Unavailable {
fn demand(&self, _chunks: u32) {}
fn answer(&self, _id: QuestionId, _answer: Answer) {}
fn cancel(&self) {}
}
}