From: Stephan Bosch Date: Fri, 5 Jun 2020 19:00:03 +0000 (+0200) Subject: lib-oauth2: Move oauth2_parse_json() to oauth2-request.c. X-Git-Tag: 2.3.14.rc1~248 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=72db70205ff7d75f20415d3aa56f583fbaddda00;p=thirdparty%2Fdovecot%2Fcore.git lib-oauth2: Move oauth2_parse_json() to oauth2-request.c. --- diff --git a/src/lib-oauth2/oauth2-request.c b/src/lib-oauth2/oauth2-request.c index ce3ec64947..e72e39d824 100644 --- a/src/lib-oauth2/oauth2-request.c +++ b/src/lib-oauth2/oauth2-request.c @@ -72,6 +72,57 @@ oauth2_request_continue(struct oauth2_request *req, const char *error) oauth2_request_callback(req, &res); } +void oauth2_parse_json(struct oauth2_request *req) +{ + enum json_type type; + const char *token, *error; + int ret; + + while((ret = json_parse_next(req->parser, &type, &token)) > 0) { + if (req->field_name == NULL) { + if (type != JSON_TYPE_OBJECT_KEY) break; + /* cannot use t_strdup because we might + have to read more */ + req->field_name = p_strdup(req->pool, token); + } else if (type < JSON_TYPE_STRING) { + /* this should be last allocation */ + p_free(req->pool, req->field_name); + json_parse_skip(req->parser); + } else { + if (!array_is_created(&req->fields)) + p_array_init(&req->fields, req->pool, 4); + struct oauth2_field *field = + array_append_space(&req->fields); + field->name = req->field_name; + req->field_name = NULL; + field->value = p_strdup(req->pool, token); + } + } + + /* read more */ + if (ret == 0) return; + + io_remove(&req->io); + + if (ret > 0) { + (void)json_parser_deinit(&req->parser, &error); + error = "Invalid response data"; + } else if (i_stream_read_eof(req->is) && + req->is->v_offset == 0 && req->is->stream_errno == 0) { + /* discard error, empty response is OK. */ + (void)json_parser_deinit(&req->parser, &error); + error = NULL; + } else if (json_parser_deinit(&req->parser, &error) == 0) { + error = NULL; + } else { + i_assert(error != NULL); + } + + i_stream_unref(&req->is); + + req->json_parsed_cb(req, error); +} + static void oauth2_request_response(const struct http_response *response, struct oauth2_request *req) diff --git a/src/lib-oauth2/oauth2.c b/src/lib-oauth2/oauth2.c index 06ed743e2d..295c98c12a 100644 --- a/src/lib-oauth2/oauth2.c +++ b/src/lib-oauth2/oauth2.c @@ -1,7 +1,6 @@ /* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */ #include "lib.h" -#include "ioloop.h" #include "istream.h" #include "http-client.h" #include "json-tree.h" @@ -35,57 +34,6 @@ int oauth2_json_tree_build(const buffer_t *json, struct json_tree **tree_r, return ret; } -void oauth2_parse_json(struct oauth2_request *req) -{ - enum json_type type; - const char *token, *error; - int ret; - - while((ret = json_parse_next(req->parser, &type, &token)) > 0) { - if (req->field_name == NULL) { - if (type != JSON_TYPE_OBJECT_KEY) break; - /* cannot use t_strdup because we might - have to read more */ - req->field_name = p_strdup(req->pool, token); - } else if (type < JSON_TYPE_STRING) { - /* this should be last allocation */ - p_free(req->pool, req->field_name); - json_parse_skip(req->parser); - } else { - if (!array_is_created(&req->fields)) - p_array_init(&req->fields, req->pool, 4); - struct oauth2_field *field = - array_append_space(&req->fields); - field->name = req->field_name; - req->field_name = NULL; - field->value = p_strdup(req->pool, token); - } - } - - /* read more */ - if (ret == 0) return; - - io_remove(&req->io); - - if (ret > 0) { - (void)json_parser_deinit(&req->parser, &error); - error = "Invalid response data"; - } else if (i_stream_read_eof(req->is) && - req->is->v_offset == 0 && req->is->stream_errno == 0) { - /* discard error, empty response is OK. */ - (void)json_parser_deinit(&req->parser, &error); - error = NULL; - } else if (json_parser_deinit(&req->parser, &error) == 0) { - error = NULL; - } else { - i_assert(error != NULL); - } - - i_stream_unref(&req->is); - - req->json_parsed_cb(req, error); -} - void oauth2_request_abort(struct oauth2_request **_req) { struct oauth2_request *req = *_req;