commit 7626b96d37ca125d8b9bf34db7b0305f11d02361
parent 1e1a13c64febaf890617e59cc0f41cb9c17a025b
Author: Christian Grothoff <christian@grothoff.org>
Date: Sun, 13 Dec 2020 12:07:10 +0100
generate logic to lookup HTTP status code by ec
Diffstat:
4 files changed, 47 insertions(+), 7 deletions(-)
diff --git a/gnu-taler-error-codes/c.footer b/gnu-taler-error-codes/c.footer
@@ -1,12 +1,6 @@
-/**
- * Returns a hint for a given error code.
- *
- * @param ec the error code.
- * @return the hint if it could be found, otherwise "<no hint found>".
- */
const char *
TALER_ErrorCode_get_hint (enum TALER_ErrorCode ec)
{
@@ -31,3 +25,29 @@ TALER_ErrorCode_get_hint (enum TALER_ErrorCode ec)
}
return "<no hint found>";
}
+
+
+const unsigned int
+TALER_ErrorCode_get_http_status (enum TALER_ErrorCode ec)
+{
+ unsigned int lower = 0;
+ unsigned int upper = code_hint_pairs_length - 1;
+ unsigned int mid = upper / 2;
+ while (lower <= upper)
+ {
+ mid = (upper + lower) / 2;
+ if (code_hint_pairs[mid].ec < ec)
+ {
+ lower = mid + 1;
+ }
+ else if (code_hint_pairs[mid].ec > ec)
+ {
+ upper = mid - 1;
+ }
+ else
+ {
+ return code_hint_pairs[mid].http_status;
+ }
+ }
+ return UINT_MAX;
+}
diff --git a/gnu-taler-error-codes/c.header b/gnu-taler-error-codes/c.header
@@ -23,6 +23,10 @@
#include "taler_error_codes.h"
#include <stddef.h>
+/**
+ * MHD does not define our value for 0 (client-side generated code).
+ */
+#define MHD_HTTP_UNINITIALIZED 0
/**
* A pair containing an error code and its hint.
@@ -38,6 +42,11 @@ struct ErrorCodeAndHint
* The hint.
*/
const char *hint;
+
+ /**
+ * The HTTP status code.
+ */
+ unsigned int http_status;
};
diff --git a/gnu-taler-error-codes/c.template b/gnu-taler-error-codes/c.template
@@ -1,5 +1,6 @@
{
.ec = TALER_EC_{{Name}},
- .hint = "{{Description}}"
+ .hint = "{{Description}}",
+ .http_code = MHD_HTTP_{{HttpStatus_Identifier}}
},
diff --git a/gnu-taler-error-codes/h.footer b/gnu-taler-error-codes/h.footer
@@ -13,6 +13,16 @@ const char *
TALER_ErrorCode_get_hint (enum TALER_ErrorCode ec);
+/**
+ * Return HTTP status for a given error code.
+ *
+ * @param ec the error code.
+ * @return the HTTP status code for the given @a ec, UINT_MAX if not found
+ */
+const unsigned int
+TALER_ErrorCode_get_http_status (enum TALER_ErrorCode ec);
+
+
#if 0 /* keep Emacsens' auto-indent happy */
{
#endif