use std::sync::Arc;
use std::time::Duration;
use anyhow::{anyhow, Result as AnyResult};
use futures::channel::oneshot;
use futures::future::BoxFuture;
use futures::AsyncReadExt;
use gpui::http_client::{http, AsyncBody, HttpClient, Inner, RedirectPolicy, RequestTimeout, Url};
use sk_runtime::redjs::webapi::{cookies::Credentials, http as net};
const CONNECT_TIMEOUT: Duration = Duration::from_secs(30);
const IO_TIMEOUT: Duration = Duration::from_secs(60);
pub struct RedjsHttpClient;
impl RedjsHttpClient {
pub fn new() -> Self {
RedjsHttpClient
}
}
struct AbortOnDrop(Option<Arc<net::Cancel>>);
impl AbortOnDrop {
fn done(&mut self) {
self.0 = None;
}
}
impl Drop for AbortOnDrop {
fn drop(&mut self) {
if let Some(c) = self.0.take() {
c.abort();
}
}
}
#[cfg_attr(not(feature = "storages"), allow(dead_code))]
pub fn cache_mb(window_json: Option<&str>) -> u64 {
if let Ok(v) = std::env::var("SK_HTTP_CACHE") {
let v = v.trim();
if v == "0" || v.eq_ignore_ascii_case("off") {
return 0;
}
if let Ok(n) = v.parse::<u64>() {
return n;
}
log::warn!("SK_HTTP_CACHE={v:?} is neither 0/off nor a size in MB; ignored");
}
config_mb(window_json).unwrap_or(DEFAULT_CACHE_MB)
}
#[cfg_attr(not(feature = "storages"), allow(dead_code))]
fn config_mb(window_json: Option<&str>) -> Option<u64> {
window_json
.and_then(crate::json::parse)
.and_then(|w| match w.get("httpCache")? {
crate::json::Json::Bool(false) => Some(0),
o => o
.get("maxMB")
.and_then(crate::json::Json::as_f64)
.filter(|n| *n >= 0.0)
.map(|n| n as u64),
})
}
#[cfg_attr(not(feature = "storages"), allow(dead_code))]
const DEFAULT_CACHE_MB: u64 = 256;
impl HttpClient for RedjsHttpClient {
fn user_agent(&self) -> Option<&http::HeaderValue> {
None
}
fn proxy(&self) -> Option<&Url> {
None
}
fn send(
&self,
req: http::Request<AsyncBody>,
) -> BoxFuture<'static, AnyResult<http::Response<AsyncBody>>> {
let (parts, body) = req.into_parts();
match body.0 {
Inner::Empty => send(parts, None),
Inner::Bytes(bytes) => {
let bytes = bytes.into_inner().to_vec();
send(parts, (!bytes.is_empty()).then_some(bytes))
}
reader => Box::pin(async move {
let mut bytes = Vec::new();
AsyncBody(reader).read_to_end(&mut bytes).await?;
send(parts, Some(bytes)).await
}),
}
}
}
fn send(
parts: http::request::Parts,
body: Option<Vec<u8>>,
) -> BoxFuture<'static, AnyResult<http::Response<AsyncBody>>> {
{
let headers = parts
.headers
.iter()
.filter_map(|(k, v)| Some((k.as_str().to_string(), v.to_str().ok()?.to_string())))
.collect();
let io_timeout = parts
.extensions
.get::<RequestTimeout>()
.map_or(IO_TIMEOUT, |t| t.0);
let request = net::Request {
method: parts.method.as_str().to_string(),
url: parts.uri.to_string(),
headers,
body,
redirect: match parts.extensions.get::<RedirectPolicy>() {
Some(RedirectPolicy::NoFollow) => net::Redirect::Error,
_ => net::Redirect::Follow,
},
credentials: Credentials::Omit,
connect_timeout: CONNECT_TIMEOUT,
io_timeout,
observer: None,
cache: net::cache::CacheMode::Default,
};
let cancel = net::Cancel::new();
let (tx, rx) = oneshot::channel();
#[cfg(sk_devtools)]
let row = crate::devtools::LoaderRow::start(&request.method, &request.url);
let started = net::start(
request,
(*cancel).clone(),
Box::new(move |result| {
#[cfg(sk_devtools)]
row.finish(&result);
let _ = tx.send(result);
}),
);
Box::pin(async move {
let mut guard = AbortOnDrop(Some(cancel));
started.map_err(|e| anyhow!("{}", e.message))?;
let result = rx.await.map_err(|_| anyhow!("the request was dropped"))?;
guard.done();
let response = result.map_err(|e| anyhow!("{}", e.message))?;
let mut builder = http::Response::builder().status(response.status);
for (k, v) in &response.headers {
if k.eq_ignore_ascii_case("set-cookie") {
continue;
}
builder = builder.header(k.as_str(), v.as_str());
}
Ok(builder.body(AsyncBody::from(response.body))?)
})
}
}
#[cfg(test)]
mod tests {
use super::config_mb;
#[test]
fn the_config_names_a_cap_turns_the_cache_off_or_says_nothing() {
let w = |s: &str| config_mb(Some(s));
assert_eq!(w(r#"{"httpCache":{"maxMB":3}}"#), Some(3));
assert_eq!(w(r#"{"httpCache":{"maxMB":0}}"#), Some(0));
assert_eq!(w(r#"{"httpCache":false}"#), Some(0));
assert_eq!(w(r#"{"httpCache":{"maxMB":-1}}"#), None);
assert_eq!(w(r#"{"httpCache":true}"#), None);
assert_eq!(w(r#"{"title":"x"}"#), None);
assert_eq!(config_mb(None), None);
}
}