Skip to content

Commit

Permalink
fix(macos): custom protocol response with status code + error response (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 20, 2021
1 parent 7564104 commit 6b869b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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

0 comments on commit 6b869b1

Please sign in to comment.