Skip to content

Commit

Permalink
feat(http): enable cookies and set origin header
Browse files Browse the repository at this point in the history
closes #1167
  • Loading branch information
amrbashir committed Apr 15, 2024
1 parent 1f9e7ab commit 7bcddc7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/http-cookies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"http": "patch"
---

Enable cookies store feature flag by default.
5 changes: 5 additions & 0 deletions .changes/http-origin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"http": "patch"
---

Set the request origin to the current webview url.
4 changes: 2 additions & 2 deletions examples/api/src-tauri/capabilities/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
{
"identifier": "http:default",
"allow": [
"https://tauri.app",
"https://*:*",
{
"url": "http://localhost:3003"
"url": "http://*:*"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion plugins/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ url = { workspace = true }
data-url = "0.3"

[features]
default = ["rustls-tls", "http2", "charset", "macos-system-configuration"]
default = ["rustls-tls", "http2", "charset", "macos-system-configuration", "cookies"]
multipart = ["reqwest/multipart"]
json = ["reqwest/json"]
stream = ["reqwest/stream"]
Expand Down
17 changes: 8 additions & 9 deletions plugins/http/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc, time::Duration};

use http::{header, HeaderName, HeaderValue, Method, StatusCode};
use http::{header, HeaderName, Method, StatusCode};
use reqwest::{redirect::Policy, NoProxy};
use serde::{Deserialize, Serialize};
use tauri::{
async_runtime::Mutex,
command,
ipc::{CommandScope, GlobalScope},
AppHandle, Manager, ResourceId, Runtime,
AppHandle, Manager, ResourceId, Runtime, Webview,
};

use crate::{
Expand Down Expand Up @@ -138,6 +138,7 @@ fn attach_proxy(
#[command]
pub async fn fetch<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
client_config: ClientConfig,
command_scope: CommandScope<Entry>,
global_scope: GlobalScope<Entry>,
Expand Down Expand Up @@ -194,7 +195,6 @@ pub async fn fetch<R: Runtime>(

for (name, value) in &headers {
let name = HeaderName::from_bytes(name.as_bytes())?;
let value = HeaderValue::from_bytes(value.as_bytes())?;
#[cfg(not(feature = "unsafe-headers"))]
if matches!(
name,
Expand Down Expand Up @@ -228,22 +228,21 @@ pub async fn fetch<R: Runtime>(
// POST and PUT requests should always have a 0 length content-length,
// if there is no body. https://fetch.spec.whatwg.org/#http-network-or-cache-fetch
if data.is_none() && matches!(method, Method::POST | Method::PUT) {
request = request.header(header::CONTENT_LENGTH, HeaderValue::from(0));
request = request.header(header::CONTENT_LENGTH, 0);
}

if headers.contains_key(header::RANGE.as_str()) {
// https://fetch.spec.whatwg.org/#http-network-or-cache-fetch step 18
// If httpRequest’s header list contains `Range`, then append (`Accept-Encoding`, `identity`)
request = request.header(
header::ACCEPT_ENCODING,
HeaderValue::from_static("identity"),
);
request = request.header(header::ACCEPT_ENCODING, "identity");
}

if !headers.contains_key(header::USER_AGENT.as_str()) {
request = request.header(header::USER_AGENT, HeaderValue::from_static("tauri"));
request = request.header(header::USER_AGENT, "tauri-plugin-http");
}

request = request.header(header::ORIGIN, webview.url().as_str());

if let Some(data) = data {
request = request.body(data);
}
Expand Down

0 comments on commit 7bcddc7

Please sign in to comment.