@@ -7,7 +7,6 @@ use std::{
7
7
ffi:: { c_char, c_void, CStr } ,
8
8
panic:: AssertUnwindSafe ,
9
9
ptr:: NonNull ,
10
- slice,
11
10
} ;
12
11
13
12
use http:: {
@@ -17,7 +16,7 @@ use http::{
17
16
use objc2:: {
18
17
rc:: Retained ,
19
18
runtime:: { AnyClass , AnyObject , ClassBuilder , ProtocolObject } ,
20
- ClassType ,
19
+ AllocAnyThread , ClassType ,
21
20
} ;
22
21
use objc2_foundation:: {
23
22
NSData , NSHTTPURLResponse , NSMutableDictionary , NSObject , NSObjectProtocol , NSString , NSURL ,
@@ -29,12 +28,13 @@ use crate::{wkwebview::WEBVIEW_IDS, RequestAsyncResponder, WryWebView};
29
28
30
29
pub fn create ( name : & str ) -> & AnyClass {
31
30
unsafe {
32
- let scheme_name = format ! ( "{}URLSchemeHandler" , name) ;
31
+ let scheme_name = format ! ( "{}URLSchemeHandler\0 " , name) ;
32
+ let scheme_name = CStr :: from_bytes_with_nul ( scheme_name. as_bytes ( ) ) . unwrap ( ) ;
33
33
let cls = ClassBuilder :: new ( & scheme_name, NSObject :: class ( ) ) ;
34
34
match cls {
35
35
Some ( mut cls) => {
36
- cls. add_ivar :: < * mut c_void > ( "function" ) ;
37
- cls. add_ivar :: < * mut c_char > ( "webview_id" ) ;
36
+ cls. add_ivar :: < * mut c_void > ( CStr :: from_bytes_with_nul ( b "function\0 " ) . unwrap ( ) ) ;
37
+ cls. add_ivar :: < * mut c_char > ( CStr :: from_bytes_with_nul ( b "webview_id\0 " ) . unwrap ( ) ) ;
38
38
cls. add_method (
39
39
objc2:: sel!( webView: startURLSchemeTask: ) ,
40
40
start_task as extern "C" fn ( _, _, _, _) ,
@@ -54,7 +54,7 @@ pub fn create(name: &str) -> &AnyClass {
54
54
extern "C" fn start_task (
55
55
this : & AnyObject ,
56
56
_sel : objc2:: runtime:: Sel ,
57
- webview : & ' static mut WryWebView ,
57
+ webview : & ' static WryWebView ,
58
58
task : & ' static ProtocolObject < dyn WKURLSchemeTask > ,
59
59
) {
60
60
unsafe {
@@ -65,14 +65,20 @@ extern "C" fn start_task(
65
65
let task_key = task. hash ( ) ; // hash by task object address
66
66
let task_uuid = webview. add_custom_task_key ( task_key) ;
67
67
68
- let ivar = this. class ( ) . instance_variable ( "webview_id" ) . unwrap ( ) ;
68
+ let ivar = this
69
+ . class ( )
70
+ . instance_variable ( CStr :: from_bytes_with_nul ( b"webview_id\0 " ) . unwrap ( ) )
71
+ . unwrap ( ) ;
69
72
let webview_id_ptr: * mut c_char = * ivar. load ( this) ;
70
73
let webview_id = CStr :: from_ptr ( webview_id_ptr)
71
74
. to_str ( )
72
75
. ok ( )
73
76
. unwrap_or_default ( ) ;
74
77
75
- let ivar = this. class ( ) . instance_variable ( "function" ) . unwrap ( ) ;
78
+ let ivar = this
79
+ . class ( )
80
+ . instance_variable ( CStr :: from_bytes_with_nul ( b"function\0 " ) . unwrap ( ) )
81
+ . unwrap ( ) ;
76
82
let function: & * mut c_void = ivar. load ( this) ;
77
83
if !function. is_null ( ) {
78
84
let function = & mut * ( * function
@@ -98,9 +104,7 @@ extern "C" fn start_task(
98
104
let body = request. HTTPBody ( ) ;
99
105
let body_stream = request. HTTPBodyStream ( ) ;
100
106
if let Some ( body) = body {
101
- let length = body. length ( ) ;
102
- let data_bytes = body. bytes ( ) ;
103
- sent_form_body = slice:: from_raw_parts ( data_bytes. as_ptr ( ) , length) . to_vec ( ) ;
107
+ sent_form_body = body. to_vec ( ) ;
104
108
} else if let Some ( body_stream) = body_stream {
105
109
body_stream. open ( ) ;
106
110
@@ -121,7 +125,7 @@ extern "C" fn start_task(
121
125
// get all our headers values and inject them in our request
122
126
if let Some ( all_headers) = all_headers {
123
127
for current_header in all_headers. allKeys ( ) . to_vec ( ) {
124
- let header_value = all_headers. valueForKey ( current_header) . unwrap ( ) ;
128
+ let header_value = all_headers. valueForKey ( & current_header) . unwrap ( ) ;
125
129
// inject the header into the request
126
130
http_request = http_request. header ( current_header. to_string ( ) , header_value. to_string ( ) ) ;
127
131
}
@@ -196,7 +200,7 @@ extern "C" fn start_task(
196
200
// FIXME: though we give it a static lifetime, it's not guaranteed to be valid.
197
201
task : & ' static ProtocolObject < dyn WKURLSchemeTask > ,
198
202
// FIXME: though we give it a static lifetime, it's not guaranteed to be valid.
199
- webview : & ' static mut WryWebView ,
203
+ webview : & ' static WryWebView ,
200
204
task_key : usize ,
201
205
task_uuid : Retained < NSUUID > ,
202
206
webview_id : & str ,
@@ -215,24 +219,24 @@ extern "C" fn start_task(
215
219
// default to HTTP/1.1
216
220
let wanted_version = format ! ( "{:#?}" , sent_response. version( ) ) ;
217
221
218
- let mut headers = NSMutableDictionary :: new ( ) ;
222
+ let headers = NSMutableDictionary :: new ( ) ;
219
223
if let Some ( mime) = wanted_mime {
220
- headers. insert_id (
221
- NSString :: from_str ( CONTENT_TYPE . as_str ( ) ) . as_ref ( ) ,
222
- NSString :: from_str ( mime. to_str ( ) . unwrap ( ) ) ,
224
+ headers. insert (
225
+ & * NSString :: from_str ( CONTENT_TYPE . as_str ( ) ) ,
226
+ & * NSString :: from_str ( mime. to_str ( ) . unwrap ( ) ) ,
223
227
) ;
224
228
}
225
- headers. insert_id (
226
- NSString :: from_str ( CONTENT_LENGTH . as_str ( ) ) . as_ref ( ) ,
227
- NSString :: from_str ( & content. len ( ) . to_string ( ) ) ,
229
+ headers. insert (
230
+ & * NSString :: from_str ( CONTENT_LENGTH . as_str ( ) ) ,
231
+ & * NSString :: from_str ( & content. len ( ) . to_string ( ) ) ,
228
232
) ;
229
233
230
234
// add headers
231
235
for ( name, value) in sent_response. headers ( ) . iter ( ) {
232
236
if let Ok ( value) = value. to_str ( ) {
233
- headers. insert_id (
234
- NSString :: from_str ( name. as_str ( ) ) . as_ref ( ) ,
235
- NSString :: from_str ( value) ,
237
+ headers. insert (
238
+ & * NSString :: from_str ( name. as_str ( ) ) ,
239
+ & * NSString :: from_str ( value) ,
236
240
) ;
237
241
}
238
242
}
@@ -334,7 +338,7 @@ extern "C" fn start_task(
334
338
extern "C" fn stop_task (
335
339
_this : & ProtocolObject < dyn WKURLSchemeHandler > ,
336
340
_sel : objc2:: runtime:: Sel ,
337
- webview : & mut WryWebView ,
341
+ webview : & WryWebView ,
338
342
task : & ProtocolObject < dyn WKURLSchemeTask > ,
339
343
) {
340
344
webview. remove_custom_task_key ( task. hash ( ) ) ;
0 commit comments