From 196931b7d29ddaae47242d371acf895c0c646958 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Sun, 27 Feb 2022 06:02:08 +0000 Subject: [PATCH] Error reports FossilOrigin-Name: e31feb25149901188f1dd4df8f9a7ab5633e51b75c1d36216575dbd437fad214 --- include/mastodont_error.h | 24 +++++++++++++++++ include/mastodont_types.h | 3 ++- src/application.c | 54 +++++++++++++++++++++++---------------- src/error.c | 36 ++++++++++++++++++++++++++ src/json_helper.c | 10 ++++++++ 5 files changed, 104 insertions(+), 23 deletions(-) create mode 100644 include/mastodont_error.h create mode 100644 src/error.c diff --git a/include/mastodont_error.h b/include/mastodont_error.h new file mode 100644 index 0000000..c186714 --- /dev/null +++ b/include/mastodont_error.h @@ -0,0 +1,24 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONT_ERROR_H +#define MASTODONT_ERROR_H +#include +#include + +int mstdnt_check_error(struct mstdnt_fetch_results* result, + struct mstdnt_storage* storage); + +#endif // MASTODONT_ERROR_H diff --git a/include/mastodont_types.h b/include/mastodont_types.h index c0d01db..ad2f6b3 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -23,7 +23,6 @@ #define MSTDNT_URISIZE 512 typedef unsigned char mstdnt_bool; - typedef struct mastodont { char* url; @@ -36,6 +35,8 @@ struct mstdnt_storage { int needs_cleanup; /* Double free safe */ cJSON* root; + char* error; + char* error_description; }; #endif /* MASTODONT_TYPES_H */ diff --git a/src/application.c b/src/application.c index 5ad4b92..35eaefc 100644 --- a/src/application.c +++ b/src/application.c @@ -26,20 +26,19 @@ static int mstdnt_read_app_result(struct mstdnt_storage* storage, if (_mstdnt_json_init(&root, results, storage)) return 1; - struct _mstdnt_str_val strings[] = { - { "id", &(app->id) }, - { "name", &(app->name) }, - { "website", &(app->website) }, - { "redirect_uri", &(app->redirect_uri) }, - { "client_id", &(app->client_id) }, - { "client_secret", &(app->client_secret) }, - { "vapid_key", &(app->vapid_key) }, + struct _mstdnt_val_ref refs[] = { + { "id", &(app->id), _mstdnt_val_string_call }, + { "name", &(app->name), _mstdnt_val_string_call }, + { "website", &(app->website), _mstdnt_val_string_call }, + { "redirect_uri", &(app->redirect_uri), _mstdnt_val_string_call }, + { "client_id", &(app->client_id), _mstdnt_val_string_call }, + { "client_secret", &(app->client_secret), _mstdnt_val_string_call }, + { "vapid_key", &(app->vapid_key), _mstdnt_val_string_call }, }; for (v = root->child; v; v = v->next) { - if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings), - NULL, 0) == 1) + if (_mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs))) { return 1; } @@ -55,18 +54,17 @@ static int mstdnt_read_token_result(struct mstdnt_storage* storage, if (_mstdnt_json_init(&root, results, storage)) return 1; - struct _mstdnt_str_val strings[] = { - { "access_token", &(app->access_token) }, - { "token_type", &(app->token_type) }, - { "scope", &(app->scope) }, - { "id", &(app->id) }, - { "me", &(app->me) }, + struct _mstdnt_val_ref refs[] = { + { "access_token", &(app->access_token), _mstdnt_val_string_call }, + { "token_type", &(app->token_type), _mstdnt_val_string_call }, + { "scope", &(app->scope), _mstdnt_val_string_call }, + { "id", &(app->id), _mstdnt_val_string_call }, + { "me", &(app->me), _mstdnt_val_string_call }, }; - for (v = root->child; v; v = v->next) + for (v = root; v; v = v->next) { - if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings), - NULL, 0) == 1) + if (_mstdnt_key_val_ref(v->child, refs, _mstdnt_arr_len(refs)) == 1) { return 1; } @@ -120,11 +118,18 @@ int mastodont_register_app(mastodont_t* data, res = 1; goto cleanup; } +/* + if (mstdnt_check_error(&results, storage)) + { + res = 1; + goto cleanup_fetch; + } +*/ res = mstdnt_read_app_result(storage, &results, app); +cleanup_fetch: mastodont_fetch_results_cleanup(&results); - cleanup: free(post); return res; @@ -167,7 +172,6 @@ int mastodont_obtain_oauth_token(mastodont_t* data, u_username.s = args->username; u_password.s = args->password; - struct _mstdnt_query_param params[] = { { _MSTDNT_QUERY_STRING, "grant_type", u_grant_type }, { _MSTDNT_QUERY_STRING, "client_id", u_client_id }, @@ -180,7 +184,6 @@ int mastodont_obtain_oauth_token(mastodont_t* data, }; char* post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params)); - curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post); if (mastodont_fetch_curl(data, "oauth/token", &results, CURLOPT_POST) != CURLE_OK) @@ -189,8 +192,15 @@ int mastodont_obtain_oauth_token(mastodont_t* data, goto cleanup; } + if (mstdnt_check_error(&results, storage)) + { + res = 1; + goto cleanup_fetch; + } + res = mstdnt_read_token_result(storage, &results, token); +cleanup_fetch: mastodont_fetch_results_cleanup(&results); cleanup: diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..612528a --- /dev/null +++ b/src/error.c @@ -0,0 +1,36 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#include +#include + +int mstdnt_check_error(struct mstdnt_fetch_results* results, + struct mstdnt_storage* storage) +{ + int res = 0; + cJSON* root, *v; + if (_mstdnt_json_init(&root, results, storage)) + return 1; + + struct _mstdnt_val_ref refs[] = { + { "error", &(storage->error), _mstdnt_val_string_call }, + { "error_description", &(storage->error_description), _mstdnt_val_string_call }, + }; + + for (v = root->child; v; v = v->next) + if (_mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs)) == 0) + res = 1; + return res; +} diff --git a/src/json_helper.c b/src/json_helper.c index 5912edc..13db60d 100644 --- a/src/json_helper.c +++ b/src/json_helper.c @@ -45,12 +45,22 @@ int _mstdnt_key_val_ref(cJSON* v, struct _mstdnt_val_ref* refs, void _mstdnt_val_string_call(cJSON* v, void* _type) { char** type = _type; + if (!cJSON_IsString(v)) + { + *type = NULL; + return; + } *type = v->valuestring; } void _mstdnt_val_bool_call(cJSON* v, void* _type) { mstdnt_bool* type = _type; + if (!cJSON_IsBool(v)) + { + *type = -1; + return; + } *type = cJSON_IsTrue(v); }