Skip to content

Commit

Permalink
fix(linux): async custom protocol crashing sending response (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 23, 2024
1 parent b6863ed commit f089964
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-custom-protocol-async-linux.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Fixes `with_asynchronous_custom_protocol` crashing when sending the response on Linux.
3 changes: 1 addition & 2 deletions examples/async_custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use tao::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::http::Request;
use wry::{
http::{header::CONTENT_TYPE, Response},
http::{header::CONTENT_TYPE, Request, Response},
WebViewBuilder,
};

Expand Down
3 changes: 1 addition & 2 deletions examples/custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use tao::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::http::Request;
use wry::{
http::{header::CONTENT_TYPE, Response},
http::{header::CONTENT_TYPE, Request, Response},
WebViewBuilder,
};

Expand Down
3 changes: 1 addition & 2 deletions examples/custom_titlebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use tao::{
event_loop::{ControlFlow, EventLoopBuilder},
window::{CursorIcon, ResizeDirection, Window, WindowBuilder},
};
use wry::http::Request;
use wry::WebViewBuilder;
use wry::{http::Request, WebViewBuilder};

#[derive(Debug)]
enum HitTestResult {
Expand Down
6 changes: 4 additions & 2 deletions examples/gtk_multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use tao::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::dpi::{LogicalPosition, LogicalSize};
use wry::{Rect, WebViewBuilder};
use wry::{
dpi::{LogicalPosition, LogicalSize},
Rect, WebViewBuilder,
};

fn main() -> wry::Result<()> {
let event_loop = EventLoop::new();
Expand Down
6 changes: 4 additions & 2 deletions examples/multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::dpi::{LogicalPosition, LogicalSize};
use wry::{Rect, WebViewBuilder};
use wry::{
dpi::{LogicalPosition, LogicalSize},
Rect, WebViewBuilder,
};

fn main() -> wry::Result<()> {
#[cfg(any(
Expand Down
3 changes: 1 addition & 2 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use tao::{
event_loop::{ControlFlow, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget},
window::{Window, WindowBuilder, WindowId},
};
use wry::http::Request;
use wry::{WebView, WebViewBuilder};
use wry::{http::Request, WebView, WebViewBuilder};

enum UserEvent {
CloseWindow(WindowId),
Expand Down
3 changes: 1 addition & 2 deletions examples/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use tao::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::http::Request;
use wry::{
http::{header::*, Response},
http::{header::*, Request, Response},
WebViewBuilder,
};

Expand Down
6 changes: 4 additions & 2 deletions examples/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
window::Window,
};
use wry::dpi::{LogicalPosition, LogicalSize};
use wry::{Rect, WebViewBuilder};
use wry::{
dpi::{LogicalPosition, LogicalSize},
Rect, WebViewBuilder,
};

async fn run(event_loop: EventLoop<()>, window: Window) {
let size = window.inner_size();
Expand Down
60 changes: 37 additions & 23 deletions src/webkitgtk/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! Unix platform extensions for [`WebContext`](super::WebContext).

use crate::{web_context::WebContextData, Error, RequestAsyncResponder};
use gtk::glib;
use gtk::glib::{self, MainContext};
use http::{header::CONTENT_TYPE, HeaderName, HeaderValue, Request, Response as HttpResponse};
use soup::{MessageHeaders, MessageHeadersType};
use std::{
Expand All @@ -21,9 +21,9 @@ use std::{
};
use webkit2gtk::{
ApplicationInfo, AutomationSessionExt, CookiePersistentStorage, DownloadExt, LoadEvent,
SecurityManagerExt, URIRequest, URIRequestExt, URISchemeRequestExt, URISchemeResponse,
URISchemeResponseExt, UserContentManager, WebContext, WebContextExt as Webkit2gtkContextExt,
WebView, WebViewExt,
SecurityManagerExt, URIRequest, URIRequestExt, URISchemeRequest, URISchemeRequestExt,
URISchemeResponse, URISchemeResponseExt, UserContentManager, WebContext,
WebContextExt as Webkit2gtkContextExt, WebView, WebViewExt,
};

#[derive(Debug)]
Expand Down Expand Up @@ -361,28 +361,31 @@ where
}
};

let request_ = request.clone();
let request_ = MainThreadRequest(request.clone());
let responder: Box<dyn FnOnce(HttpResponse<Cow<'static, [u8]>>)> =
Box::new(move |http_response| {
let buffer = http_response.body();
let input = gtk::gio::MemoryInputStream::from_bytes(&gtk::glib::Bytes::from(buffer));
let content_type = http_response
.headers()
.get(CONTENT_TYPE)
.and_then(|h| h.to_str().ok());

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 {
response.set_content_type(content_type);
}
MainContext::default().invoke(move || {
let buffer = http_response.body();
let input = gtk::gio::MemoryInputStream::from_bytes(&gtk::glib::Bytes::from(buffer));
let content_type = http_response
.headers()
.get(CONTENT_TYPE)
.and_then(|h| h.to_str().ok());

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 {
response.set_content_type(content_type);
}

let headers = MessageHeaders::new(MessageHeadersType::Response);
for (name, value) in http_response.headers().into_iter() {
headers.append(name.as_str(), value.to_str().unwrap_or(""));
}
response.set_http_headers(headers);
request_.finish_with_response(&response);
});

let headers = MessageHeaders::new(MessageHeadersType::Response);
for (name, value) in http_response.headers().into_iter() {
headers.append(name.as_str(), value.to_str().unwrap_or(""));
}
response.set_http_headers(headers);
request_.finish_with_response(&response);
});

#[cfg(feature = "tracing")]
Expand All @@ -399,6 +402,17 @@ where
Ok(())
}

struct MainThreadRequest(URISchemeRequest);

impl MainThreadRequest {
fn finish_with_response(&self, response: &URISchemeResponse) {
self.0.finish_with_response(response);
}
}

unsafe impl Send for MainThreadRequest {}
unsafe impl Sync for MainThreadRequest {}

#[derive(Debug)]
struct WebviewUriRequest {
webview: WebView,
Expand Down
6 changes: 4 additions & 2 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ use std::{
sync::{Arc, Mutex},
};

use core_graphics::base::CGFloat;
use core_graphics::geometry::{CGPoint, CGRect, CGSize};
use core_graphics::{
base::CGFloat,
geometry::{CGPoint, CGRect, CGSize},
};

use objc::{
declare::ClassDecl,
Expand Down

0 comments on commit f089964

Please sign in to comment.