Skip to content

Commit

Permalink
On Linux, add header feature flag to fix version regression (#766)
Browse files Browse the repository at this point in the history
Co-authored-by: Wu Yu Wei <wusyong9104@gmail.com>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
3 people committed Nov 24, 2022
1 parent 0bb43cd commit cf447f6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changes/header.md
@@ -0,0 +1,5 @@
---
"wry": patch
---

On Linux, add `linux-headers` feature flag to fix version regression. The minimum webkit2gtk version remains v2.22.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -31,6 +31,7 @@ tray = [ "tao/tray" ]
devtools = [ ]
transparent = [ ]
fullscreen = [ ]
linux-headers = [ "webkit2gtk/v2_36" ]

[dependencies]
libc = "0.2"
Expand All @@ -50,7 +51,7 @@ dirs = "4.0.0"
base64 = "0.13.1"

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
webkit2gtk = { version = "0.18.2", features = [ "v2_36" ] }
webkit2gtk = { version = "0.18.2", features = [ "v2_22" ] }
webkit2gtk-sys = "0.18"
gio = "0.15"
glib = "0.15"
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -64,6 +64,8 @@
//! Avoid this in release build if your app needs to publish to App Store.
//! - `dox`: Enables this in `package.metadata.docs.rs` section to skip linking some **Linux**
//! libraries and prevent from building documentation on doc.rs fails.
//! - `linux-headers`: Enables headers support of custom protocol request on Linux. Requires
//! webkit2gtk v2.36 or above.
//!
//! [tao]: https://crates.io/crates/tao
//! [`EventLoop`]: crate::application::event_loop::EventLoop
Expand Down
35 changes: 15 additions & 20 deletions src/webview/webkitgtk/web_context.rs
Expand Up @@ -6,11 +6,7 @@

use crate::{webview::web_context::WebContextData, Error};
use glib::FileError;
use http::{
header::{HeaderName, CONTENT_TYPE},
HeaderValue, Request, Response,
};
use soup::{MessageHeaders, MessageHeadersType};
use http::{header::CONTENT_TYPE, Request, Response};
use std::{
cell::RefCell,
collections::{HashSet, VecDeque},
Expand All @@ -24,13 +20,9 @@ use std::{
};
use url::Url;
use webkit2gtk::{
traits::*, ApplicationInfo, CookiePersistentStorage, LoadEvent, URISchemeResponse,
UserContentManager, WebContext, WebContextBuilder, WebView, WebsiteDataManagerBuilder,
traits::*, ApplicationInfo, CookiePersistentStorage, LoadEvent, UserContentManager, WebContext,
WebContextBuilder, WebView, WebsiteDataManagerBuilder,
};
use webkit2gtk_sys::webkit_get_minor_version;

// header support was introduced in webkit2gtk 2.36
const HEADER_MINOR_RELEASE: u32 = 36;

#[derive(Debug)]
pub struct WebContextImpl {
Expand All @@ -40,7 +32,6 @@ pub struct WebContextImpl {
registered_protocols: HashSet<String>,
automation: bool,
app_info: Option<ApplicationInfo>,
webkit2gtk_minor: u32,
}

impl WebContextImpl {
Expand Down Expand Up @@ -87,16 +78,13 @@ impl WebContextImpl {
.expect("invalid wry version patch"),
);

let webkit2gtk_minor = unsafe { webkit_get_minor_version() };

Self {
context,
automation,
manager: UserContentManager::new(),
registered_protocols: Default::default(),
webview_uri_loader: Rc::default(),
app_info: Some(app_info),
webkit2gtk_minor,
}
}

Expand Down Expand Up @@ -290,7 +278,6 @@ where
F: Fn(&Request<Vec<u8>>) -> crate::Result<Response<Vec<u8>>> + 'static,
{
use webkit2gtk::traits::*;
let webkit2gtk_minor = context.os.webkit2gtk_minor;
let context = &context.os.context;
// Enable secure context
context
Expand All @@ -303,8 +290,12 @@ where
let uri = uri.as_str();

// FIXME: Read the body (forms post)
#[allow(unused_mut)]
let mut http_request = Request::builder().uri(uri).method("GET");
if webkit2gtk_minor >= HEADER_MINOR_RELEASE {
#[cfg(feature = "linux-headers")]
{
use http::{header::HeaderName, HeaderValue};

if let Some(mut headers) = request.http_headers() {
if let Some(map) = http_request.headers_mut() {
headers.foreach(move |k, v| {
Expand Down Expand Up @@ -341,7 +332,11 @@ where
.headers()
.get(CONTENT_TYPE)
.and_then(|h| h.to_str().ok());
if webkit2gtk_minor >= HEADER_MINOR_RELEASE {
#[cfg(feature = "linux-headers")]
{
use soup::{MessageHeaders, MessageHeadersType};
use webkit2gtk::URISchemeResponse;

let response = URISchemeResponse::new(&input, buffer.len() as i64);
response.set_status(http_response.status().as_u16() as u32, None);
if let Some(content_type) = content_type {
Expand All @@ -355,9 +350,9 @@ where
response.set_http_headers(&mut headers);

request.finish_with_response(&response);
} else {
request.finish(&input, buffer.len() as i64, content_type)
}
#[cfg(not(feature = "header"))]
request.finish(&input, buffer.len() as i64, content_type)
}
Err(_) => request.finish_error(&mut glib::Error::new(
FileError::Exist,
Expand Down

0 comments on commit cf447f6

Please sign in to comment.