refactor: use reqwest to handle http-get requests

Closes #82
This commit is contained in:
Friedel Ziegelmayer
2019-05-27 09:06:55 +02:00
committed by GitHub
parent a247e5b143
commit f0cfcef864
5 changed files with 21 additions and 123 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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!(
" {}",

View File

@@ -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;

View File

@@ -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,