1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-06 03:50:08 +02:00

add DC_EVENT_HTTP_POST, needed to regenerate OAuth2 tokens

This commit is contained in:
B. Petersen 2019-02-15 18:32:45 +01:00
parent fee032b513
commit 1c5ab7444d
No known key found for this signature in database
GPG key ID: 3B88E92DEA8E9AFC
3 changed files with 43 additions and 3 deletions

View file

@ -67,17 +67,33 @@ static uintptr_t receive_event(dc_context_t* context, int event, uintptr_t data1
break; break;
case DC_EVENT_HTTP_GET: case DC_EVENT_HTTP_GET:
case DC_EVENT_HTTP_POST:
{ {
char* url = dc_strdup((char*)data1);
char* param = strchr(url, '?');
if (param) {
*param = 0;
param++;
}
else {
param = "";
}
char* ret = NULL; char* ret = NULL;
char* tempFile = dc_get_fine_pathNfilename(context, context->blobdir, "curl.result"); char* tempFile = dc_get_fine_pathNfilename(context, context->blobdir, "curl.result");
char* cmd = dc_mprintf("curl --silent --location --fail --insecure %s > %s", (char*)data1, tempFile); /* --location = follow redirects */ char* cmd = event==DC_EVENT_HTTP_GET?
dc_mprintf("curl --silent --location --fail --insecure %s%s%s > %s", url, param[0]? "?" : "", param, tempFile) :
dc_mprintf("curl --silent -d \"%s\" %s > %s", param, url, tempFile);
int error = system(cmd); int error = system(cmd);
if (error == 0) { /* -1=system() error, !0=curl errors forced by -f, 0=curl success */ if (error == 0) { /* -1=system() error, !0=curl errors forced by -f, 0=curl success */
size_t bytes = 0; size_t bytes = 0;
dc_read_file(context, tempFile, (void**)&ret, &bytes); dc_read_file(context, tempFile, (void**)&ret, &bytes);
} }
free(cmd); free(cmd);
free(tempFile); free(tempFile);
free(url);
return (uintptr_t)ret; return (uintptr_t)ret;
} }

View file

@ -1059,4 +1059,8 @@ void stress_functions(dc_context_t* context)
assert( res->id != 0 ); assert( res->id != 0 );
dc_lot_unref(res); dc_lot_unref(res);
} }
{
context->cb(context, DC_EVENT_HTTP_POST, (uintptr_t)"https://accounts.google.com/o/oauth2/token?client_id=959970109878-t6pl4k9fmsdvfnobae862urapdmhfvbe.apps.googleusercontent.com&client_secret=g2f_Gc1YUJ-fWjnTkdsuk4Xo&&grant_type=refresh_token&refresh_token=1/RMq1d0QKVF-BN4yJSuBwjzukWTF_puI3IBYtIjtPhi8&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob", 0);
}
} }

View file

@ -1062,20 +1062,40 @@ time_t dc_lot_get_timestamp (const dc_lot_t*);
/** /**
* Request a HTTP-file or HTTPS-file from the frontend. * 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. * @param data1 (const char*) Null-terminated UTF-8 string containing the URL.
* The string starts with https:// or http://. * The string starts with https:// or http://.
* Must not be free()'d or modified and is valid only until the callback returns. * Must not be free()'d or modified and is valid only until the callback returns.
* @param data2 0 * @param data2 0
* @return (const char*) The content of the requested file as a null-terminated UTF-8 string; * @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, which may be binary, should be returned. * Response headers, encodings etc. must be stripped.
* Only the raw file should be returned.
* CAVE: The string will be free()'d by the core, * CAVE: The string will be free()'d by the core,
* so make sure it is allocated using malloc() or a compatible function. * 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. * If you cannot provide the content, just return 0 or an empty string.
*/ */
#define DC_EVENT_HTTP_GET 2100 #define DC_EVENT_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.
*/
#define DC_EVENT_HTTP_POST 2110
/** /**
* @} * @}
*/ */