Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macos): custom protocol response with status code + error response #279

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/custom-protocol-status-code-macos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Change the custom protocol handler on macOS so it returns a response on error and a status code on success.
18 changes: 12 additions & 6 deletions src/webview/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,26 @@ impl InnerWebView {
// Send response
if let Ok(content) = function.0(&function.1, uri) {
let mime = MimeType::parse(&content, uri);
let nsurlresponse: id = msg_send![class!(NSURLResponse), alloc];
let response: id = msg_send![nsurlresponse, initWithURL:url MIMEType:NSString::new(&mime)
expectedContentLength:content.len() textEncodingName:null::<c_void>()];
let dictionary: id = msg_send![class!(NSMutableDictionary), alloc];
let headers: id = msg_send![dictionary, initWithCapacity:1];
let () = msg_send![headers, setObject:NSString::new(&mime) forKey: NSString::new("content-type")];
let () = msg_send![headers, setObject:NSString::new(&content.len().to_string()) forKey: NSString::new("content-length")];
let urlresponse: id = msg_send![class!(NSHTTPURLResponse), alloc];
let response: id = msg_send![urlresponse, initWithURL:url statusCode:200 HTTPVersion:NSString::new("HTTP/1.1") headerFields:headers];
let () = msg_send![task, didReceiveResponse: response];

// Send data
let bytes = content.as_ptr() as *mut c_void;
let data: id = msg_send![class!(NSData), alloc];
let data: id = msg_send![data, initWithBytes:bytes length:content.len()];
let () = msg_send![task, didReceiveData: data];

// Finish
let () = msg_send![task, didFinish];
} else {
let urlresponse: id = msg_send![class!(NSHTTPURLResponse), alloc];
let response: id = msg_send![urlresponse, initWithURL:url statusCode:404 HTTPVersion:NSString::new("HTTP/1.1") headerFields:null::<c_void>()];
let () = msg_send![task, didReceiveResponse: response];
}
// Finish
let () = msg_send![task, didFinish];
}
}
extern "C" fn stop_task(_: &Object, _: Sel, _webview: id, _task: id) {}
Expand Down