diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2019-05-23 20:25:55 +0200 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2019-05-23 20:25:55 +0200 |
commit | 514a101d345e9a7907b8179d2fd0c4ceda56c181 (patch) | |
tree | 63ae4f83f6d0a82a4d87ea3bdbcea9c577429873 | |
parent | ca0f567dcc4e9a4b6c65b2974c45a4bbcbaba414 (diff) | |
download | merchant-514a101d345e9a7907b8179d2fd0c4ceda56c181.tar.gz merchant-514a101d345e9a7907b8179d2fd0c4ceda56c181.tar.bz2 merchant-514a101d345e9a7907b8179d2fd0c4ceda56c181.zip |
Compressing/decompressing /proposal.
-rw-r--r-- | src/backend/taler-merchant-httpd_parsing.c | 137 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_proposal.c | 1 | ||||
-rw-r--r-- | src/lib/merchant_api_proposal.c | 83 |
3 files changed, 91 insertions, 130 deletions
diff --git a/src/backend/taler-merchant-httpd_parsing.c b/src/backend/taler-merchant-httpd_parsing.c index 3f368b9f..98b1cd5c 100644 --- a/src/backend/taler-merchant-httpd_parsing.c +++ b/src/backend/taler-merchant-httpd_parsing.c @@ -2,21 +2,25 @@ This file is part of TALER Copyright (C) 2014, 2015, 2016 GNUnet e.V. - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - TALER 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation; either version 3, + or (at your option) any later version. + + TALER 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public + License along with TALER; see the file COPYING. If not, + see <http://www.gnu.org/licenses/> */ /** * @file taler-exchange-httpd_parsing.c - * @brief functions to parse incoming requests (MHD arguments and JSON snippets) + * @brief functions to parse incoming requests + * (MHD arguments and JSON snippets) * @author Florian Dold * @author Benedikt Mueller * @author Christian Grothoff @@ -175,9 +179,9 @@ TMH_PARSE_post_cleanup_callback (void *con_cls) /** * Process a POST request containing a JSON object. This function * realizes an MHD POST processor that will (incrementally) process - * JSON data uploaded to the HTTP server. It will store the required - * state in the @a con_cls, which must be cleaned up using - * #TMH_PARSE_post_cleanup_callback(). + * JSON data uploaded to the HTTP server. It will store the + * required state in the @a con_cls, which must be cleaned up + * using #TMH_PARSE_post_cleanup_callback(). * * @param connection the MHD connection * @param con_cls the closure (points to a `struct Buffer *`) @@ -202,86 +206,41 @@ TMH_PARSE_post_json (struct MHD_Connection *connection, size_t *upload_data_size, json_t **json) { - struct Buffer *r = *con_cls; - - TALER_LOG_DEBUG ("Will parse: %.*s\n", - (int) *upload_data_size, - upload_data); - *json = NULL; - if (NULL == *con_cls) + enum GNUNET_JSON_PostResult pr; + + pr = GNUNET_JSON_post_parser (REQUEST_BUFFER_MAX, + connection, + con_cls, + upload_data, + upload_data_size, + json); + switch (pr) { - /* We are seeing a fresh POST request. */ - r = GNUNET_new (struct Buffer); - if (GNUNET_OK != - buffer_init (r, - upload_data, - *upload_data_size, - REQUEST_BUFFER_INITIAL, - REQUEST_BUFFER_MAX)) - { - *con_cls = NULL; - buffer_deinit (r); - GNUNET_free (r); - /* return GNUNET_SYSERR if this isn't even - * able to generate proper error response. */ - return (MHD_NO == TMH_RESPONSE_reply_internal_error - (connection, - TALER_EC_PARSER_OUT_OF_MEMORY, - "out of memory")) ? GNUNET_SYSERR : GNUNET_NO; - } - /* everything OK, wait for more POST data */ - *upload_data_size = 0; - *con_cls = r; - return GNUNET_YES; - } - /* When zero, upload is over. */ - if (0 != *upload_data_size) - { - TALER_LOG_INFO ("Parser asking for more data" - ", current data size is %zu\n", - *upload_data_size); - - /* We are seeing an old request with more data available. */ - if (GNUNET_OK != - buffer_append (r, - upload_data, - *upload_data_size, - REQUEST_BUFFER_MAX)) - { - /* Request too long */ - *con_cls = NULL; - buffer_deinit (r); - GNUNET_free (r); - return (MHD_NO == TMH_RESPONSE_reply_request_too_large - (connection)) ? GNUNET_SYSERR : GNUNET_NO; - } - - /* everything OK, wait for more POST data */ - *upload_data_size = 0; + case GNUNET_JSON_PR_OUT_OF_MEMORY: + return (MHD_NO == TMH_RESPONSE_reply_internal_error + (connection, + TALER_EC_PARSER_OUT_OF_MEMORY, + "out of memory")) ? GNUNET_SYSERR : GNUNET_NO; + + case GNUNET_JSON_PR_CONTINUE: return GNUNET_YES; - } - TALER_LOG_DEBUG ("About to parse: %.*s\n", - (int) r->fill, - r->data); - /* We have seen the whole request. */ - *json = json_loadb (r->data, - r->fill, - 0, - NULL); - if (NULL == *json) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Failed to parse JSON request body\n"); - return (MHD_YES == TMH_RESPONSE_reply_invalid_json - (connection)) ? GNUNET_NO : GNUNET_SYSERR; - } - buffer_deinit (r); - GNUNET_free (r); - *con_cls = NULL; + case GNUNET_JSON_PR_REQUEST_TOO_LARGE: + return (MHD_NO == TMH_RESPONSE_reply_request_too_large + (connection)) ? GNUNET_SYSERR : GNUNET_NO; - return GNUNET_YES; + case GNUNET_JSON_PR_JSON_INVALID: + return (MHD_YES == + TMH_RESPONSE_reply_invalid_json (connection)) + ? GNUNET_NO : GNUNET_SYSERR; + case GNUNET_JSON_PR_SUCCESS: + GNUNET_break (NULL != *json); + return GNUNET_YES; + } + /* this should never happen */ + GNUNET_break (0); + return GNUNET_SYSERR; } diff --git a/src/backend/taler-merchant-httpd_proposal.c b/src/backend/taler-merchant-httpd_proposal.c index be4359b6..a65bf46c 100644 --- a/src/backend/taler-merchant-httpd_proposal.c +++ b/src/backend/taler-merchant-httpd_proposal.c @@ -651,6 +651,7 @@ MH_handler_proposal_put (struct TMH_RequestHandler *rh, upload_data, upload_data_size, &root); + if (GNUNET_SYSERR == res) return MHD_NO; diff --git a/src/lib/merchant_api_proposal.c b/src/lib/merchant_api_proposal.c index 3d7218c6..9430206c 100644 --- a/src/lib/merchant_api_proposal.c +++ b/src/lib/merchant_api_proposal.c @@ -2,18 +2,21 @@ This file is part of TALER Copyright (C) 2014-2017 GNUnet e.V. and INRIA - TALER 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 2.1, or (at your option) any later version. - - TALER 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 - TALER; see the file COPYING.LGPL. If not, see - <http://www.gnu.org/licenses/> + TALER 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 2.1, + or (at your option) any later version. + + TALER 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 TALER; see the file COPYING.LGPL. If not, + see <http://www.gnu.org/licenses/> */ + /** * @file lib/merchant_api_proposal.c * @brief Implementation of the /proposal POST and GET @@ -29,6 +32,7 @@ #include "taler_merchant_service.h" #include <taler/taler_json_lib.h> #include <taler/taler_signatures.h> +#include <taler/teah_common.h> /** @@ -43,11 +47,6 @@ struct TALER_MERCHANT_ProposalOperation char *url; /** - * JSON encoding of the request to POST. - */ - char *json_enc; - - /** * Handle for the request. */ struct GNUNET_CURL_Job *job; @@ -66,6 +65,11 @@ struct TALER_MERCHANT_ProposalOperation * Reference to the execution context. */ struct GNUNET_CURL_Context *ctx; + + /** + * Minor context that holds body and headers. + */ + struct TEAH_PostContext post_ctx; }; /** @@ -208,11 +212,12 @@ handle_proposal_finished (void *cls, * @return a handle for this request, NULL on error */ struct TALER_MERCHANT_ProposalOperation * -TALER_MERCHANT_order_put (struct GNUNET_CURL_Context *ctx, - const char *backend_url, - const json_t *order, - TALER_MERCHANT_ProposalCallback proposal_cb, - void *proposal_cb_cls) +TALER_MERCHANT_order_put + (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const json_t *order, + TALER_MERCHANT_ProposalCallback proposal_cb, + void *proposal_cb_cls) { struct TALER_MERCHANT_ProposalOperation *po; json_t *req; @@ -226,32 +231,26 @@ TALER_MERCHANT_order_put (struct GNUNET_CURL_Context *ctx, req = json_pack ("{s:O}", "order", (json_t *) order); eh = curl_easy_init (); - po->json_enc = json_dumps (req, - JSON_COMPACT); - json_decref (req); - if (NULL == po->json_enc) + if (GNUNET_OK != TEAH_curl_easy_post (&po->post_ctx, + eh, + req)) { GNUNET_break (0); GNUNET_free (po); return NULL; } + json_decref (req); + GNUNET_assert (CURLE_OK == curl_easy_setopt (eh, CURLOPT_URL, po->url)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDS, - po->json_enc)); - GNUNET_assert (CURLE_OK == - curl_easy_setopt (eh, - CURLOPT_POSTFIELDSIZE, - strlen (po->json_enc))); - po->job = GNUNET_CURL_job_add (ctx, - eh, - GNUNET_YES, - &handle_proposal_finished, - po); + + po->job = GNUNET_CURL_job_add2 (ctx, + eh, + po->post_ctx.headers, + &handle_proposal_finished, + po); return po; } @@ -437,7 +436,8 @@ TALER_MERCHANT_proposal_lookup (struct GNUNET_CURL_Context *ctx, * @param po the proposal operation request handle */ void -TALER_MERCHANT_proposal_cancel (struct TALER_MERCHANT_ProposalOperation *po) +TALER_MERCHANT_proposal_cancel + (struct TALER_MERCHANT_ProposalOperation *po) { if (NULL != po->job) { @@ -445,7 +445,7 @@ TALER_MERCHANT_proposal_cancel (struct TALER_MERCHANT_ProposalOperation *po) po->job = NULL; } GNUNET_free (po->url); - GNUNET_free (po->json_enc); + GNUNET_free (po->post_ctx.json_enc); GNUNET_free (po); } @@ -456,7 +456,8 @@ TALER_MERCHANT_proposal_cancel (struct TALER_MERCHANT_ProposalOperation *po) * @param plo handle to the request to be canceled */ void -TALER_MERCHANT_proposal_lookup_cancel (struct TALER_MERCHANT_ProposalLookupOperation *plo) +TALER_MERCHANT_proposal_lookup_cancel + (struct TALER_MERCHANT_ProposalLookupOperation *plo) { if (NULL != plo->job) { |