commit cb6d74c412ec697a379842f9d653dc351a0b6d5e
parent 03002036f7f973ca6414cb215991ac141e627fd7
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Sun, 21 Dec 2025 18:06:59 +0100
mhd_enum_to_str.c: re-written to ensure that values are always correctly mapped and not missing
Diffstat:
1 file changed, 119 insertions(+), 33 deletions(-)
diff --git a/src/mhd2/mhd_enum_to_str.c b/src/mhd2/mhd_enum_to_str.c
@@ -54,43 +54,129 @@
MHD_EXTERN_ MHD_FN_CONST_ const struct MHD_String *
MHD_protocol_version_to_string (enum MHD_HTTP_ProtocolVersion pv)
{
- static const struct MHD_String results[] = {
- mhd_MSTR_INIT ("invalid"),
- mhd_MSTR_INIT ("HTTP/1.0"),
- mhd_MSTR_INIT ("HTTP/1.1"),
- mhd_MSTR_INIT ("HTTP/2"),
- mhd_MSTR_INIT ("HTTP/3")
- };
-
- if (0 > pv)
- return NULL;
- if (mhd_ARR_NUM_ELEMS (results) >= (unsigned int) pv)
- return NULL;
-
- return results + (unsigned int) pv;
+ switch (pv)
+ {
+ case MHD_HTTP_VERSION_INVALID:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("[invalid]");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_VERSION_1_0:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("HTTP/1.0");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_VERSION_1_1:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("HTTP/1.1");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_VERSION_2:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("HTTP/2");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_VERSION_3:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("HTTP/3");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_VERSION_FUTURE:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("[future version]");
+ return &ret;
+ }
+ break;
+ default:
+ break;
+ }
+
+ return NULL;
}
MHD_EXTERN_ MHD_FN_CONST_ const struct MHD_String *
MHD_http_method_to_string (enum MHD_HTTP_Method method)
{
- static const struct MHD_String results[] = {
- mhd_MSTR_INIT ("invalid"),
- mhd_MSTR_INIT ("GET"),
- mhd_MSTR_INIT ("HEAD"),
- mhd_MSTR_INIT ("POST"),
- mhd_MSTR_INIT ("PUT"),
- mhd_MSTR_INIT ("DELETE"),
- mhd_MSTR_INIT ("CONNECT"),
- mhd_MSTR_INIT ("OPTIONS"),
- mhd_MSTR_INIT ("TRACE"),
- mhd_MSTR_INIT ("*")
- };
-
- if (0 > method)
- return NULL;
- if (mhd_ARR_NUM_ELEMS (results) >= (unsigned int) method)
- return NULL;
-
- return results + (unsigned int) method;
+ switch (method)
+ {
+ case MHD_HTTP_METHOD_GET:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("GET");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_HEAD:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("HEAD");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_POST:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("POST");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_PUT:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("PUT");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_DELETE:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("DELETE");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_CONNECT:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("CONNECT");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_OPTIONS:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("OPTIONS");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_TRACE:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("TRACE");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_ASTERISK:
+ if (1)
+ {
+ static const struct MHD_String ret = mhd_MSTR_INIT ("*");
+ return &ret;
+ }
+ break;
+ case MHD_HTTP_METHOD_OTHER: /* Handled as unknown value */
+ default:
+ }
+
+ return NULL;
}