desktop: Handle cookies and Content-Type for HTTP requests

Both of these are handled automatically by the browser in the
web backend. This makes the desktop client store cookies between
requests (though they are discarded when the desktop player is closed),
and set the "Content-Type" header based on the mime-type supplied
in the URLRequest.
This commit is contained in:
Aaron Hill 2023-10-01 11:59:57 -04:00
parent 1886a5d434
commit 25cf77c4a2
3 changed files with 17 additions and 2 deletions

7
Cargo.lock generated
View File

@ -2222,6 +2222,12 @@ dependencies = [
"itoa",
]
[[package]]
name = "httpdate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humantime"
version = "2.1.0"
@ -2414,6 +2420,7 @@ dependencies = [
"event-listener",
"futures-lite",
"http",
"httpdate",
"log",
"mime",
"once_cell",

View File

@ -26,7 +26,7 @@ webbrowser = "0.8.11"
url = "2.4.1"
arboard = "3.2.1"
dirs = "5.0"
isahc = "1.7.2"
isahc = { version = "1.7.2", features = ["cookies"] }
rfd = "0.12.0"
anyhow = "1.0"
bytemuck = "1.14.0"

View File

@ -70,6 +70,7 @@ impl ExternalNavigatorBackend {
let proxy = proxy.and_then(|url| url.as_str().parse().ok());
let builder = HttpClient::builder()
.proxy(proxy)
.cookies()
.redirect_policy(RedirectPolicy::Follow);
let client = builder.build().ok().map(Rc::new);
@ -244,6 +245,7 @@ impl NavigatorBackend for ExternalNavigatorBackend {
NavigationMethod::Get => IsahcRequest::get(processed_url.to_string()),
NavigationMethod::Post => IsahcRequest::post(processed_url.to_string()),
};
let (body_data, mime) = request.body().clone().unwrap_or_default();
if let Some(headers) = isahc_request.headers_mut() {
for (name, val) in request.headers().iter() {
headers.insert(
@ -257,9 +259,15 @@ impl NavigatorBackend for ExternalNavigatorBackend {
})?,
);
}
headers.insert(
"Content-Type",
HeaderValue::from_str(&mime).map_err(|e| ErrorResponse {
url: processed_url.to_string(),
error: Error::FetchError(e.to_string()),
})?,
);
}
let (body_data, _) = request.body().clone().unwrap_or_default();
let body = isahc_request.body(body_data).map_err(|e| ErrorResponse {
url: processed_url.to_string(),
error: Error::FetchError(e.to_string()),