From f0cfcef8641ba8bb77deec2ce27067a84cc26af2 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Mon, 27 May 2019 09:06:55 +0200 Subject: [PATCH] refactor: use reqwest to handle http-get requests Closes #82 --- deltachat-ffi/deltachat.h | 4 +++ examples/repl/main.rs | 54 +-------------------------------------- examples/simple.rs | 17 ------------ src/constants.rs | 27 -------------------- src/dc_configure.rs | 42 ++++++++++++------------------ 5 files changed, 21 insertions(+), 123 deletions(-) diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index d58eab756..654ef3381 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -1104,6 +1104,8 @@ time_t dc_lot_get_timestamp (const dc_lot_t*); /** + * NOT_USED_ANYMORE + * * Request a HTTP-file or HTTPS-file from the frontend using HTTP-GET. * * @param data1 (const char*) Null-terminated UTF-8 string containing the URL. @@ -1121,6 +1123,8 @@ time_t dc_lot_get_timestamp (const dc_lot_t*); /** + * NOT_USED_ANYMORE + * * Request a HTTP-file or HTTPS-file from the frontend using HTTP-POST. * * @param data1 (const char*) Null-terminated UTF-8 string containing the URL. diff --git a/examples/repl/main.rs b/examples/repl/main.rs index 6f8e347b1..a8dfcc6d4 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -73,7 +73,7 @@ use self::cmdline::*; ******************************************************************************/ unsafe extern "C" fn receive_event( - context: &dc_context_t, + _context: &dc_context_t, event: Event, data1: uintptr_t, data2: uintptr_t, @@ -132,58 +132,6 @@ unsafe extern "C" fn receive_event( data2 as *mut libc::c_char, ); } - 2100 | 2110 => { - let url: *mut libc::c_char = dc_strdup(data1 as *mut libc::c_char); - let mut param: *mut libc::c_char = strchr(url, '?' as i32); - - if !param.is_null() { - *param = 0i32 as libc::c_char; - param = param.offset(1isize) - } else { - param = b"\x00" as *const u8 as *const libc::c_char as *mut libc::c_char - } - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; - let tempFile: *mut libc::c_char = dc_get_fine_pathNfilename( - context, - context.get_blobdir(), - b"curl.result\x00" as *const u8 as *const libc::c_char, - ); - let cmd: *mut libc::c_char = if event == Event::HTTP_GET { - dc_mprintf( - b"curl --silent --location --fail --insecure %s%s%s > %s\x00" as *const u8 - as *const libc::c_char, - url, - if 0 != *param.offset(0isize) as libc::c_int { - b"?\x00" as *const u8 as *const libc::c_char - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - param, - tempFile, - ) - } else { - dc_mprintf( - b"curl --silent -d \"%s\" %s > %s\x00" as *const u8 as *const libc::c_char, - param, - url, - tempFile, - ) - }; - let error: libc::c_int = system(cmd); - if error == 0i32 { - let mut bytes: size_t = 0i32 as size_t; - dc_read_file( - context, - tempFile, - &mut ret as *mut *mut libc::c_char as *mut *mut libc::c_void, - &mut bytes, - ); - } - free(cmd as *mut libc::c_void); - free(tempFile as *mut libc::c_void); - free(url as *mut libc::c_void); - return ret as uintptr_t; - } 2081 => { printf( b"\x1b[33m{{Received DC_EVENT_IS_OFFLINE()}}\n\x1b[0m\x00" as *const u8 diff --git a/examples/simple.rs b/examples/simple.rs index e36e9cea7..183853bea 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -16,7 +16,6 @@ use deltachat::dc_job::{ dc_perform_smtp_jobs, }; use deltachat::dc_lot::*; -use deltachat::x::strdup; extern "C" fn cb(_ctx: &dc_context_t, event: Event, data1: usize, data2: usize) -> usize { println!("[{:?}]", event); @@ -26,22 +25,6 @@ extern "C" fn cb(_ctx: &dc_context_t, event: Event, data1: usize, data2: usize) println!(" progress: {}", data1); 0 } - Event::HTTP_GET => { - let url = unsafe { CStr::from_ptr(data1 as *const _).to_str().unwrap() }; - - match reqwest::get(url) { - Ok(ref mut res) => { - let c_res = CString::new(res.text().unwrap()).unwrap(); - // need to use strdup to allocate the result with malloc - // so it can be `free`d later. - unsafe { strdup(c_res.as_ptr()) as usize } - } - Err(err) => { - println!("failed to download: {}: {:?}", url, err); - 0 - } - } - } Event::INFO | Event::WARNING | Event::ERROR | Event::ERROR_NETWORK => { println!( " {}", diff --git a/src/constants.rs b/src/constants.rs index 6d96701bf..dc51bb2c9 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -421,33 +421,6 @@ pub enum Event { /// Return 0 if the ui cannot provide the requested string /// the core will use a default string in english language then. GET_STRING = 2091, - - /// Request a HTTP-file or HTTPS-file from the frontend using HTTP-GET. - /// @param data1 (const char*) Null-terminated UTF-8 string containing the URL. - /// The string starts with https:// or http://. - /// Must not be free()'d or modified and is valid only until the callback returns. - /// @param data2 0 - /// @return (const char*) The content of the requested file as a null-terminated UTF-8 string; - /// Response headers, encodings etc. must be stripped. - /// Only the raw file should be returned. - /// CAVE: The string will be free()'d by the core, - /// so make sure it is allocated using malloc() or a compatible function. - /// If you cannot provide the content, just return 0 or an empty string. - HTTP_GET = 2100, - - /// Request a HTTP-file or HTTPS-file from the frontend using HTTP-POST. - /// @param data1 (const char*) Null-terminated UTF-8 string containing the URL. - /// The string starts with https:// or http://. - /// Must not be free()'d or modified and is valid only until the callback returns. - /// Parameter to POST are added to the url after `?`. - /// @param data2 0 - /// @return (const char*) The content of the requested file as a null-terminated UTF-8 string; - /// Response headers, encodings etc. must be stripped. - /// Only the raw file should be returned. - /// CAVE: The string will be free()'d by the core, - /// so make sure it is allocated using malloc() or a compatible function. - /// If you cannot provide the content, just return 0 or an empty string. - HTTP_POST = 2110, } pub const DC_EVENT_FILE_COPIED: usize = 2055; // deprecated; diff --git a/src/dc_configure.rs b/src/dc_configure.rs index e08e1fb39..1765bd79f 100644 --- a/src/dc_configure.rs +++ b/src/dc_configure.rs @@ -1521,34 +1521,24 @@ unsafe fn moz_autoconfigure_starttag_cb( (*moz_ac).tag_config = 12i32 }; } -unsafe fn read_autoconf_file( - context: &dc_context_t, - url: *const libc::c_char, -) -> *mut libc::c_char { - let filecontent: *mut libc::c_char; - dc_log_info( - context, - 0i32, - b"Testing %s ...\x00" as *const u8 as *const libc::c_char, - url, - ); - filecontent = (context.cb)( - context, - Event::HTTP_GET, - url as uintptr_t, - 0i32 as uintptr_t, - ) as *mut libc::c_char; - if filecontent.is_null() || *filecontent.offset(0isize) as libc::c_int == 0i32 { - free(filecontent as *mut libc::c_void); - dc_log_info( - context, - 0i32, - b"Can\'t read file.\x00" as *const u8 as *const libc::c_char, - ); - return 0 as *mut libc::c_char; + +fn read_autoconf_file(context: &dc_context_t, url: *const libc::c_char) -> *mut libc::c_char { + info!(context, 0, "Testing %s ...", url); + + match reqwest::Client::new() + .get(to_str(url)) + .send() + .and_then(|mut res| res.text()) + { + Ok(res) => unsafe { libc::strdup(to_cstring(res).as_ptr()) }, + Err(_err) => { + info!(context, 0, "Can\'t read file.",); + + std::ptr::null_mut() + } } - return filecontent; } + unsafe fn outlk_autodiscover( context: &dc_context_t, url__: *const libc::c_char,