commit 99f2df6dea40624ff82b00ebe362e587f722985f
parent 9829eb349801efd7bc89fc791bb0f9e8f18221a6
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Fri, 16 Apr 2021 17:58:14 +0300
mhd_send: finally handle sockets errors in portable way
Diffstat:
2 files changed, 36 insertions(+), 42 deletions(-)
diff --git a/src/microhttpd/mhd_send.c b/src/microhttpd/mhd_send.c
@@ -186,15 +186,17 @@ MHD_connection_set_nodelay_state_ (struct MHD_Connection *connection,
connection->sk_nodelay = nodelay_state;
return true;
}
+
err_code = MHD_socket_get_error_ ();
- switch (err_code)
+ if (MHD_SCKT_ERR_IS_ (err_code, MHD_SCKT_EINVAL_) ||
+ MHD_SCKT_ERR_IS_ (err_code, MHD_SCKT_ENOPROTOOPT_) ||
+ MHD_SCKT_ERR_IS_ (err_code, MHD_SCKT_ENOTSOCK_))
{
- case EINVAL:
- case ENOPROTOOPT:
- case ENOTSOCK:
- if (_MHD_NO == connection->is_nonip)
- {
+ if (_MHD_UNKNOWN == connection->is_nonip)
+ connection->is_nonip = _MHD_YES;
#ifdef HAVE_MESSAGES
+ else
+ {
MHD_DLOG (connection->daemon,
_ ("Setting %s option to %s state failed "
"for TCP/IP socket %d: %s\n"),
@@ -202,26 +204,20 @@ MHD_connection_set_nodelay_state_ (struct MHD_Connection *connection,
nodelay_state ? _ ("ON") : _ ("OFF"),
(int) connection->socket_fd,
MHD_socket_strerr_ (err_code));
-#endif /* HAVE_MESSAGES */
}
- else
- connection->is_nonip = _MHD_YES;
- break;
- case EBADF:
- case EFAULT:
- /* Hard errors, something is wrong. Too tricky to
- * break connection here, just log the message.
- * Shound't really happen too often. */
- default:
+#endif /* HAVE_MESSAGES */
+ }
#ifdef HAVE_MESSAGES
+ else
+ {
MHD_DLOG (connection->daemon,
_ ("Setting %s option to %s state failed: %s\n"),
"TCP_NODELAY",
nodelay_state ? _ ("ON") : _ ("OFF"),
MHD_socket_strerr_ (err_code));
-#endif /* HAVE_MESSAGES */
- break;
}
+#endif /* HAVE_MESSAGES */
+
return false;
}
@@ -255,15 +251,17 @@ connection_set_cork_state_ (struct MHD_Connection *connection,
connection->sk_corked = cork_state;
return true;
}
+
err_code = MHD_socket_get_error_ ();
- switch (err_code)
+ if (MHD_SCKT_ERR_IS_ (err_code, MHD_SCKT_EINVAL_) ||
+ MHD_SCKT_ERR_IS_ (err_code, MHD_SCKT_ENOPROTOOPT_) ||
+ MHD_SCKT_ERR_IS_ (err_code, MHD_SCKT_ENOTSOCK_))
{
- case EINVAL:
- case ENOPROTOOPT:
- case ENOTSOCK:
- if (_MHD_NO == connection->is_nonip)
- {
+ if (_MHD_UNKNOWN == connection->is_nonip)
+ connection->is_nonip = _MHD_YES;
#ifdef HAVE_MESSAGES
+ else
+ {
MHD_DLOG (connection->daemon,
_ ("Setting %s option to %s state failed "
"for TCP/IP socket %d: %s\n"),
@@ -271,30 +269,20 @@ connection_set_cork_state_ (struct MHD_Connection *connection,
nodelay_state ? _ ("ON") : _ ("OFF"),
(int) connection->socket_fd,
MHD_socket_strerr_ (err_code));
-#endif /* HAVE_MESSAGES */
}
- else
- connection->is_nonip = _MHD_YES;
- break;
- case EBADF:
- case EFAULT:
- /* Hard errors, something is wrong. Too tricky to
- * break connection here, just log the message.
- * Shound't really happen too often. */
- default:
+#endif /* HAVE_MESSAGES */
+ }
#ifdef HAVE_MESSAGES
+ else
+ {
MHD_DLOG (connection->daemon,
_ ("Setting %s option to %s state failed: %s\n"),
-#ifdef TCP_CORK
- "TCP_CORK",
-#else /* ! TCP_CORK */
- "TCP_NOPUSH",
-#endif /* ! TCP_CORK */
- cork_state ? _ ("ON") : _ ("OFF"),
+ "TCP_NODELAY",
+ nodelay_state ? _ ("ON") : _ ("OFF"),
MHD_socket_strerr_ (err_code));
-#endif /* HAVE_MESSAGES */
- break;
}
+#endif /* HAVE_MESSAGES */
+
return false;
}
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
@@ -567,6 +567,11 @@ typedef int MHD_SCKT_SEND_SIZE_;
# else /* ! ENOSYS */
# define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
# endif /* ! ENOSYS */
+# ifdef ENOPROTOOPT
+# define MHD_SCKT_ENOPROTOOPT_ ENOPROTOOPT
+# else /* ! ENOPROTOOPT */
+# define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
+# endif /* ! ENOPROTOOPT */
# ifdef ENOTSUP
# define MHD_SCKT_ENOTSUP_ ENOTSUP
# else /* ! ENOTSUP */
@@ -603,6 +608,7 @@ typedef int MHD_SCKT_SEND_SIZE_;
# define MHD_SCKT_EINVAL_ WSAEINVAL
# define MHD_SCKT_EFAUL_ WSAEFAULT
# define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
+# define MHD_SCKT_ENOPROTOOPT_ WSAENOPROTOOPT
# define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
# define MHD_SCKT_EOPNOTSUPP_ WSAEOPNOTSUPP
# define MHD_SCKT_EACCESS_ WSAEACCES