Error reports

FossilOrigin-Name: e31feb25149901188f1dd4df8f9a7ab5633e51b75c1d36216575dbd437fad214
This commit is contained in:
me@ow.nekobit.net 2022-02-27 06:02:08 +00:00
parent 99df5eaf51
commit 196931b7d2
5 changed files with 104 additions and 23 deletions

24
include/mastodont_error.h Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef MASTODONT_ERROR_H
#define MASTODONT_ERROR_H
#include <mastodont_types.h>
#include <mastodont_fetch.h>
int mstdnt_check_error(struct mstdnt_fetch_results* result,
struct mstdnt_storage* storage);
#endif // MASTODONT_ERROR_H

View file

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

View file

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

36
src/error.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <mastodont_query.h>
#include <mastodont_json_helper.h>
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;
}

View file

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