scope minimization, more consistent naming of variables

This commit is contained in:
Christian Grothoff 2020-03-01 14:02:30 +01:00
parent 13eed90b73
commit f4ce758d68
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC

View File

@ -1,6 +1,6 @@
/* /*
This file is part of TALER This file is part of TALER
Copyright (C) 2014-2019 Taler Systems SA Copyright (C) 2014-2020 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the 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 terms of the GNU Affero General Public License as published by the Free Software
@ -60,6 +60,8 @@ TALER_MHD_add_global_headers (struct MHD_Response *response)
MHD_add_response_header (response, MHD_add_response_header (response,
MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_CONNECTION,
"close")); "close"));
/* The wallet, operating from a background page, needs CORS to
be disabled otherwise browsers block access. */
GNUNET_break (MHD_YES == GNUNET_break (MHD_YES ==
MHD_add_response_header (response, MHD_add_response_header (response,
MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
@ -155,7 +157,7 @@ TALER_MHD_body_compress (void **buf,
struct MHD_Response * struct MHD_Response *
TALER_MHD_make_json (const json_t *json) TALER_MHD_make_json (const json_t *json)
{ {
struct MHD_Response *resp; struct MHD_Response *response;
char *json_str; char *json_str;
json_str = json_dumps (json, json_str = json_dumps (json,
@ -165,21 +167,21 @@ TALER_MHD_make_json (const json_t *json)
GNUNET_break (0); GNUNET_break (0);
return NULL; return NULL;
} }
resp = MHD_create_response_from_buffer (strlen (json_str), response = MHD_create_response_from_buffer (strlen (json_str),
json_str, json_str,
MHD_RESPMEM_MUST_FREE); MHD_RESPMEM_MUST_FREE);
if (NULL == resp) if (NULL == response)
{ {
free (json_str); free (json_str);
GNUNET_break (0); GNUNET_break (0);
return NULL; return NULL;
} }
TALER_MHD_add_global_headers (resp); TALER_MHD_add_global_headers (response);
GNUNET_break (MHD_YES == GNUNET_break (MHD_YES ==
MHD_add_response_header (resp, MHD_add_response_header (response,
MHD_HTTP_HEADER_CONTENT_TYPE, MHD_HTTP_HEADER_CONTENT_TYPE,
"application/json")); "application/json"));
return resp; return response;
} }
@ -196,11 +198,10 @@ TALER_MHD_reply_json (struct MHD_Connection *connection,
const json_t *json, const json_t *json,
unsigned int response_code) unsigned int response_code)
{ {
struct MHD_Response *resp; struct MHD_Response *response;
void *json_str; void *json_str;
size_t json_len; size_t json_len;
int ret; int is_compressed;
int comp;
json_str = json_dumps (json, json_str = json_dumps (json,
JSON_INDENT (2)); JSON_INDENT (2));
@ -218,44 +219,49 @@ TALER_MHD_reply_json (struct MHD_Connection *connection,
} }
json_len = strlen (json_str); json_len = strlen (json_str);
/* try to compress the body */ /* try to compress the body */
comp = MHD_NO; is_compressed = MHD_NO;
if (MHD_YES == if (MHD_YES ==
TALER_MHD_can_compress (connection)) TALER_MHD_can_compress (connection))
comp = TALER_MHD_body_compress (&json_str, is_compressed = TALER_MHD_body_compress (&json_str,
&json_len); &json_len);
resp = MHD_create_response_from_buffer (json_len, response = MHD_create_response_from_buffer (json_len,
json_str, json_str,
MHD_RESPMEM_MUST_FREE); MHD_RESPMEM_MUST_FREE);
if (NULL == resp) if (NULL == response)
{ {
free (json_str); free (json_str);
GNUNET_break (0); GNUNET_break (0);
return MHD_NO; return MHD_NO;
} }
TALER_MHD_add_global_headers (resp); TALER_MHD_add_global_headers (response);
GNUNET_break (MHD_YES == GNUNET_break (MHD_YES ==
MHD_add_response_header (resp, MHD_add_response_header (response,
MHD_HTTP_HEADER_CONTENT_TYPE, MHD_HTTP_HEADER_CONTENT_TYPE,
"application/json")); "application/json"));
if (MHD_YES == comp) if (MHD_YES == is_compressed)
{ {
/* Need to indicate to client that body is compressed */ /* Need to indicate to client that body is compressed */
if (MHD_NO == if (MHD_NO ==
MHD_add_response_header (resp, MHD_add_response_header (response,
MHD_HTTP_HEADER_CONTENT_ENCODING, MHD_HTTP_HEADER_CONTENT_ENCODING,
"deflate")) "deflate"))
{ {
GNUNET_break (0); GNUNET_break (0);
MHD_destroy_response (resp); MHD_destroy_response (response);
return MHD_NO; return MHD_NO;
} }
} }
{
int ret;
ret = MHD_queue_response (connection, ret = MHD_queue_response (connection,
response_code, response_code,
resp); response);
MHD_destroy_response (resp); MHD_destroy_response (response);
return ret; return ret;
} }
}
/** /**
@ -268,28 +274,32 @@ TALER_MHD_reply_json (struct MHD_Connection *connection,
int int
TALER_MHD_reply_cors_preflight (struct MHD_Connection *connection) TALER_MHD_reply_cors_preflight (struct MHD_Connection *connection)
{ {
struct MHD_Response *resp; struct MHD_Response *response;
int ret;
resp = MHD_create_response_from_buffer (0, response = MHD_create_response_from_buffer (0,
NULL, NULL,
MHD_RESPMEM_PERSISTENT); MHD_RESPMEM_PERSISTENT);
if (NULL == resp) if (NULL == response)
return MHD_NO; return MHD_NO;
/* This adds the Access-Control-Allow-Origin header. /* This adds the Access-Control-Allow-Origin header.
* All endpoints of the exchange allow CORS. */ * All endpoints of the exchange allow CORS. */
TALER_MHD_add_global_headers (resp); TALER_MHD_add_global_headers (response);
GNUNET_break (MHD_YES == GNUNET_break (MHD_YES ==
MHD_add_response_header (resp, MHD_add_response_header (response,
/* Not available as MHD constant yet */ /* Not available as MHD constant yet */
"Access-Control-Allow-Headers", "Access-Control-Allow-Headers",
"*")); "*"));
{
int ret;
ret = MHD_queue_response (connection, ret = MHD_queue_response (connection,
MHD_HTTP_NO_CONTENT, MHD_HTTP_NO_CONTENT,
resp); response);
MHD_destroy_response (resp); MHD_destroy_response (response);
return ret; return ret;
} }
}
/** /**
@ -309,13 +319,20 @@ TALER_MHD_reply_json_pack (struct MHD_Connection *connection,
...) ...)
{ {
json_t *json; json_t *json;
va_list argp;
int ret;
json_error_t jerror; json_error_t jerror;
va_start (argp, fmt); {
json = json_vpack_ex (&jerror, 0, fmt, argp); va_list argp;
va_start (argp,
fmt);
json = json_vpack_ex (&jerror,
0,
fmt,
argp);
va_end (argp); va_end (argp);
}
if (NULL == json) if (NULL == json)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@ -325,12 +342,17 @@ TALER_MHD_reply_json_pack (struct MHD_Connection *connection,
GNUNET_break (0); GNUNET_break (0);
return MHD_NO; return MHD_NO;
} }
{
int ret;
ret = TALER_MHD_reply_json (connection, ret = TALER_MHD_reply_json (connection,
json, json,
response_code); response_code);
json_decref (json); json_decref (json);
return ret; return ret;
} }
}
/** /**
@ -345,16 +367,19 @@ TALER_MHD_make_json_pack (const char *fmt,
...) ...)
{ {
json_t *json; json_t *json;
va_list argp;
struct MHD_Response *ret;
json_error_t jerror; json_error_t jerror;
{
va_list argp;
va_start (argp, fmt); va_start (argp, fmt);
json = json_vpack_ex (&jerror, json = json_vpack_ex (&jerror,
0, 0,
fmt, fmt,
argp); argp);
va_end (argp); va_end (argp);
}
if (NULL == json) if (NULL == json)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@ -364,9 +389,14 @@ TALER_MHD_make_json_pack (const char *fmt,
GNUNET_break (0); GNUNET_break (0);
return MHD_NO; return MHD_NO;
} }
ret = TALER_MHD_make_json (json);
{
struct MHD_Response *response;
response = TALER_MHD_make_json (json);
json_decref (json); json_decref (json);
return ret; return response;
}
} }
@ -419,21 +449,25 @@ TALER_MHD_reply_with_error (struct MHD_Connection *connection,
int int
TALER_MHD_reply_request_too_large (struct MHD_Connection *connection) TALER_MHD_reply_request_too_large (struct MHD_Connection *connection)
{ {
struct MHD_Response *resp; struct MHD_Response *response;
int ret;
resp = MHD_create_response_from_buffer (0, response = MHD_create_response_from_buffer (0,
NULL, NULL,
MHD_RESPMEM_PERSISTENT); MHD_RESPMEM_PERSISTENT);
if (NULL == resp) if (NULL == response)
return MHD_NO; return MHD_NO;
TALER_MHD_add_global_headers (resp); TALER_MHD_add_global_headers (response);
{
int ret;
ret = MHD_queue_response (connection, ret = MHD_queue_response (connection,
MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, MHD_HTTP_REQUEST_ENTITY_TOO_LARGE,
resp); response);
MHD_destroy_response (resp); MHD_destroy_response (response);
return ret; return ret;
} }
}
/** /**
@ -451,7 +485,6 @@ TALER_MHD_reply_agpl (struct MHD_Connection *connection,
const char *agpl = const char *agpl =
"This server is licensed under the Affero GPL. You will now be redirected to the source code."; "This server is licensed under the Affero GPL. You will now be redirected to the source code.";
struct MHD_Response *response; struct MHD_Response *response;
int ret;
response = MHD_create_response_from_buffer (strlen (agpl), response = MHD_create_response_from_buffer (strlen (agpl),
(void *) agpl, (void *) agpl,
@ -475,12 +508,17 @@ TALER_MHD_reply_agpl (struct MHD_Connection *connection,
MHD_destroy_response (response); MHD_destroy_response (response);
return MHD_NO; return MHD_NO;
} }
{
int ret;
ret = MHD_queue_response (connection, ret = MHD_queue_response (connection,
MHD_HTTP_FOUND, MHD_HTTP_FOUND,
response); response);
MHD_destroy_response (response); MHD_destroy_response (response);
return ret; return ret;
} }
}
/** /**
@ -502,7 +540,6 @@ TALER_MHD_reply_static (struct MHD_Connection *connection,
size_t body_size) size_t body_size)
{ {
struct MHD_Response *response; struct MHD_Response *response;
int ret;
response = MHD_create_response_from_buffer (body_size, response = MHD_create_response_from_buffer (body_size,
(void *) body, (void *) body,
@ -518,12 +555,16 @@ TALER_MHD_reply_static (struct MHD_Connection *connection,
MHD_add_response_header (response, MHD_add_response_header (response,
MHD_HTTP_HEADER_CONTENT_TYPE, MHD_HTTP_HEADER_CONTENT_TYPE,
mime_type)); mime_type));
{
int ret;
ret = MHD_queue_response (connection, ret = MHD_queue_response (connection,
http_status, http_status,
response); response);
MHD_destroy_response (response); MHD_destroy_response (response);
return ret; return ret;
} }
}
/* end of mhd_responses.c */ /* end of mhd_responses.c */