gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [libmicrohttpd] branch master updated (0c2fd79b -> ddac6d53


From: gnunet
Subject: [GNUnet-SVN] [libmicrohttpd] branch master updated (0c2fd79b -> ddac6d53)
Date: Sun, 04 Jun 2017 23:33:23 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from 0c2fd79b Added variable missing in 
0a7707a81137f146d8fa7c01b60d3cf884d4665e
     new 3777686c Do not use errno to return errors from 
recv_param_adapter()/recv_tls_adapter()
     new ddac6d53 Do not use errno to return errors from 
send_param_adapter()/send_tls_adapter()

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/microhttpd/connection.c       | 179 +++++++++++++++++++-------------------
 src/microhttpd/connection.h       |  30 +++++++
 src/microhttpd/connection_https.c |  26 +++---
 3 files changed, 129 insertions(+), 106 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index baa5eb56..27a4efb3 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -118,7 +118,8 @@
  * @param connection the MHD connection structure
  * @param other where to write received data to
  * @param i maximum size of other (in bytes)
- * @return number of bytes actually received
+ * @return positive value for number of bytes actually received or
+ *         negative value for error number MHD_ERR_xxx_
  */
 static ssize_t
 recv_param_adapter (struct MHD_Connection *connection,
@@ -130,8 +131,7 @@ recv_param_adapter (struct MHD_Connection *connection,
   if ( (MHD_INVALID_SOCKET == connection->socket_fd) ||
        (MHD_CONNECTION_CLOSED == connection->state) )
     {
-      MHD_socket_set_error_ (MHD_SCKT_ENOTCONN_);
-      return -1;
+      return MHD_ERR_NOTCONN_;
     }
   if (i > MHD_SCKT_SEND_MAX_SIZE_)
     i = MHD_SCKT_SEND_MAX_SIZE_; /* return value limit */
@@ -139,16 +139,26 @@ recv_param_adapter (struct MHD_Connection *connection,
   ret = MHD_recv_ (connection->socket_fd,
                    other,
                    i);
-#ifdef EPOLL_SUPPORT
   if (0 > ret)
     {
-      /* Got EAGAIN --- no longer read-ready */
-      if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()))
-        connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY;
+      const int err = MHD_socket_get_error_ ();
+      if (MHD_SCKT_ERR_IS_EAGAIN_ (err))
+        {
+#ifdef EPOLL_SUPPORT
+          /* Got EAGAIN --- no longer read-ready */
+          connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY;
+#endif /* EPOLL_SUPPORT */
+          return MHD_ERR_AGAIN_;
+        }
+      if (MHD_SCKT_ERR_IS_EINTR_ (err))
+        return MHD_ERR_AGAIN_;
+      /* Treat any other error as hard error. */
+      return MHD_ERR_CONNRESET_;
     }
+#ifdef EPOLL_SUPPORT
   else if (i > (size_t)ret)
     connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY;
-#endif
+#endif /* EPOLL_SUPPORT */
   return ret;
 }
 
@@ -159,7 +169,8 @@ recv_param_adapter (struct MHD_Connection *connection,
  * @param connection the MHD connection structure
  * @param other data to write
  * @param i number of bytes to write
- * @return actual number of bytes written
+ * @return positive value for number of bytes actually sent or
+ *         negative value for error number MHD_ERR_xxx_
  */
 static ssize_t
 send_param_adapter (struct MHD_Connection *connection,
@@ -167,13 +178,11 @@ send_param_adapter (struct MHD_Connection *connection,
                     size_t i)
 {
   ssize_t ret;
-  int err;
 
   if ( (MHD_INVALID_SOCKET == connection->socket_fd) ||
        (MHD_CONNECTION_CLOSED == connection->state) )
     {
-      MHD_socket_set_error_ (MHD_SCKT_ENOTCONN_);
-      return -1;
+      return MHD_ERR_NOTCONN_;
     }
   if (i > MHD_SCKT_SEND_MAX_SIZE_)
     i = MHD_SCKT_SEND_MAX_SIZE_; /* return value limit */
@@ -181,23 +190,27 @@ send_param_adapter (struct MHD_Connection *connection,
   ret = MHD_send_ (connection->socket_fd,
                    other,
                    i);
-  err = MHD_socket_get_error_();
-#ifdef EPOLL_SUPPORT
   if (0 > ret)
     {
-      /* EAGAIN --- no longer write-ready */
+      const int err = MHD_socket_get_error_();
+
       if (MHD_SCKT_ERR_IS_EAGAIN_(err))
-        connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
+        {
+#ifdef EPOLL_SUPPORT
+          /* EAGAIN --- no longer write-ready */
+          connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
+#endif /* EPOLL_SUPPORT */
+          return MHD_ERR_AGAIN_;
+        }
+      if (MHD_SCKT_ERR_IS_EINTR_ (err))
+        return MHD_ERR_AGAIN_;
+      /* Treat any other error as hard error. */
+      return MHD_ERR_CONNRESET_;
     }
+#ifdef EPOLL_SUPPORT
   else if (i > (size_t)ret)
     connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
-#endif
-  /* Handle broken kernel / libc, returning -1 but not setting errno;
-     kill connection as that should be safe; reported on mailinglist here:
-     http://lists.gnu.org/archive/html/libmicrohttpd/2014-10/msg00023.html */
-  if ( (0 > ret) &&
-       (0 == err) )
-    MHD_socket_set_error_ (MHD_SCKT_ECONNRESET_);
+#endif /* EPOLL_SUPPORT */
   return ret;
 }
 
@@ -213,7 +226,6 @@ static ssize_t
 sendfile_adapter (struct MHD_Connection *connection)
 {
   int ret;
-  int err;
   const int file_fd = connection->response->fd;
   uint64_t left;
   uint64_t offsetu64;
@@ -226,61 +238,60 @@ sendfile_adapter (struct MHD_Connection *connection)
 
   offsetu64 = connection->response_write_position + 
connection->response->fd_off;
   left = connection->response->total_size - 
connection->response_write_position;
-  ret = 0;
+  if (left > SSIZE_MAX)
+    left = SSIZE_MAX;
 #ifndef HAVE_SENDFILE64
   if ((uint64_t)OFF_T_MAX < offsetu64)
-    MHD_socket_set_error_to_ENOMEM ();
-  else
-    {
-      offset = (off_t) offsetu64;
-      ret = sendfile (connection->socket_fd,
-                      file_fd,
-                      &offset,
-                      left);
-    }
+    { /* Retry to send with standard 'send()'. */
+      connection->resp_sender = MHD_resp_sender_std;
+      return MHD_ERR_AGAIN_;
+    }
+    offset = (off_t) offsetu64;
+    ret = sendfile (connection->socket_fd,
+                    file_fd,
+                    &offset,
+                    left);
 #else  /* HAVE_SENDFILE64 */
   if ((uint64_t)OFF64_T_MAX < offsetu64)
-    MHD_socket_set_error_to_ENOMEM ();
-  else
-    {
-      offset = (off64_t) offsetu64;
-      ret = sendfile64 (connection->socket_fd,
-                        file_fd,
-                        &offset,
-                        left);
+    { /* Retry to send with standard 'send()'. */
+      connection->resp_sender = MHD_resp_sender_std;
+      return MHD_ERR_AGAIN_;
     }
+    offset = (off64_t) offsetu64;
+    ret = sendfile64 (connection->socket_fd,
+                      file_fd,
+                      &offset,
+                      left);
 #endif /* HAVE_SENDFILE64 */
-  if (0 < ret)
+  if (0 > ret)
     {
-      /* write successful */
+      const int err = MHD_socket_get_error_();
+      if (MHD_SCKT_ERR_IS_EAGAIN_(err))
+        {
 #ifdef EPOLL_SUPPORT
-      if (left > (uint64_t)ret)
-        connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
+          /* EAGAIN --- no longer write-ready */
+          connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
 #endif /* EPOLL_SUPPORT */
-      return ret;
+          return MHD_ERR_AGAIN_;
+        }
+      if (MHD_SCKT_ERR_IS_EINTR_ (err))
+        return MHD_ERR_AGAIN_;
+      if (MHD_SCKT_ERR_IS_(err,
+                           MHD_SCKT_EBADF_))
+        return MHD_ERR_BADF_;
+      /* sendfile() failed with EINVAL if mmap()-like operations are not
+         supported for FD or other 'unusual' errors occurred, so we should try
+         to fall back to 'SEND'; see also this thread for info on
+         odd libc/Linux behavior with sendfile:
+         http://lists.gnu.org/archive/html/libmicrohttpd/2011-02/msg00015.html 
*/
+      connection->resp_sender = MHD_resp_sender_std;
+      return MHD_ERR_AGAIN_;
     }
-  err = MHD_socket_get_error_();
 #ifdef EPOLL_SUPPORT
-  if ( (0 > ret) && (MHD_SCKT_ERR_IS_EAGAIN_(err)) )
-    {
-      /* EAGAIN --- no longer write-ready */
-      connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
-    }
-#endif
-  if (MHD_SCKT_ERR_IS_EINTR_ (err) ||
-      MHD_SCKT_ERR_IS_EAGAIN_ (err))
-    return 0;
-  if (MHD_SCKT_ERR_IS_(err,
-                       MHD_SCKT_EBADF_))
-    return -1;
-  /* sendfile() failed with EINVAL if mmap()-like operations are not
-     supported for FD or other 'unusual' errors occurred, so we should try
-     to fall back to 'SEND'; see also this thread for info on
-     odd libc/Linux behavior with sendfile:
-     http://lists.gnu.org/archive/html/libmicrohttpd/2011-02/msg00015.html */
-  connection->resp_sender = MHD_resp_sender_std;
-  MHD_socket_set_error_ (MHD_SCKT_EAGAIN_);
-  return 0;
+  else if (left > (uint64_t)ret)
+        connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
+#endif /* EPOLL_SUPPORT */
+  return ret;
 }
 #endif /* __linux__ */
 
@@ -2660,11 +2671,9 @@ MHD_connection_handle_read (struct MHD_Connection 
*connection)
                                      connection->read_buffer_offset);
   if (bytes_read < 0)
     {
-      const int err = MHD_socket_get_error_ ();
-      if (MHD_SCKT_ERR_IS_EINTR_ (err) ||
-          MHD_SCKT_ERR_IS_EAGAIN_ (err))
+      if (MHD_ERR_AGAIN_ == bytes_read)
           return MHD_YES; /* No new data to process. */
-      if (MHD_SCKT_ERR_IS_REMOTE_DISCNN_ (err))
+      if (MHD_ERR_CONNRESET_ == bytes_read)
         {
            CONNECTION_CLOSE_ERROR (connection,
                                    (MHD_CONNECTION_INIT == connection->state) ?
@@ -2778,16 +2787,12 @@ MHD_connection_handle_write (struct MHD_Connection 
*connection)
                                       
connection->continue_message_write_offset);
           if (ret < 0)
             {
-              const int err = MHD_socket_get_error_ ();
-
-              if (MHD_SCKT_ERR_IS_EINTR_ (err) ||
-                  MHD_SCKT_ERR_IS_EAGAIN_ (err))
+              if (MHD_ERR_AGAIN_ == ret)
                 break;
 #ifdef HAVE_MESSAGES
               MHD_DLOG (connection->daemon,
-                        _("Failed to send data in request for %s: %s\n"),
-                        connection->url,
-                        MHD_socket_strerr_ (err));
+                        _("Failed to send data in request for %s.\n"),
+                        connection->url);
 #endif
              CONNECTION_CLOSE_ERROR (connection,
                                       NULL);
@@ -2816,9 +2821,7 @@ MHD_connection_handle_write (struct MHD_Connection 
*connection)
                                         connection->write_buffer_send_offset);
           if (ret < 0)
             {
-              const int err = MHD_socket_get_error_ ();
-              if (MHD_SCKT_ERR_IS_EINTR_ (err) ||
-                  MHD_SCKT_ERR_IS_EAGAIN_ (err))
+              if (MHD_ERR_AGAIN_ == ret)
                 break;
               CONNECTION_CLOSE_ERROR (connection,
                                       _("Connection was closed while sending 
response headers.\n"));
@@ -2881,9 +2884,7 @@ MHD_connection_handle_write (struct MHD_Connection 
*connection)
               MHD_mutex_unlock_chk_ (&response->mutex);
             if (ret < 0)
               {
-                err = MHD_socket_get_error_ ();
-                if (MHD_SCKT_ERR_IS_EINTR_ (err) ||
-                    MHD_SCKT_ERR_IS_EAGAIN_ (err))
+                if (MHD_ERR_AGAIN_ == ret)
                   return MHD_YES;
 #ifdef HAVE_MESSAGES
                 MHD_DLOG (connection->daemon,
@@ -2913,9 +2914,7 @@ MHD_connection_handle_write (struct MHD_Connection 
*connection)
                                         connection->write_buffer_send_offset);
           if (ret < 0)
             {
-              const int err = MHD_socket_get_error_ ();
-              if (MHD_SCKT_ERR_IS_EINTR_ (err) ||
-                  MHD_SCKT_ERR_IS_EAGAIN_ (err))
+              if (MHD_ERR_AGAIN_ == ret)
                 break;
               CONNECTION_CLOSE_ERROR (connection,
                                       _("Connection was closed while sending 
response body.\n"));
@@ -2943,9 +2942,7 @@ MHD_connection_handle_write (struct MHD_Connection 
*connection)
                                         connection->write_buffer_send_offset);
           if (ret < 0)
             {
-              const int err = MHD_socket_get_error_ ();
-              if (MHD_SCKT_ERR_IS_EINTR_ (err) ||
-                  MHD_SCKT_ERR_IS_EAGAIN_ (err))
+              if (MHD_ERR_AGAIN_ == ret)
                 break;
               CONNECTION_CLOSE_ERROR (connection,
                                       _("Connection was closed while sending 
response body.\n"));
diff --git a/src/microhttpd/connection.h b/src/microhttpd/connection.h
index d885f3f7..147d7343 100644
--- a/src/microhttpd/connection.h
+++ b/src/microhttpd/connection.h
@@ -30,6 +30,36 @@
 
 #include "internal.h"
 
+/**
+ * Error code similar to EGAIN or EINTR
+ */
+#define MHD_ERR_AGAIN_ (-3073)
+
+/**
+ * "Connection (remote) reset" error code
+ */
+#define MHD_ERR_CONNRESET_ (-3074)
+
+/**
+ * "Not connected" error code
+ */
+#define MHD_ERR_NOTCONN_ (-3075)
+
+/**
+ * "Not enough memory" error code
+ */
+#define MHD_ERR_NOMEM_ (-3076)
+
+/**
+ * "Bad FD" error code
+ */
+#define MHD_ERR_BADF_ (-3077)
+
+/**
+ * Error code similar to EINVAL
+ */
+#define MHD_ERR_INVAL_ (-3078)
+
 
 /**
  * Set callbacks for this connection to those for HTTP.
diff --git a/src/microhttpd/connection_https.c 
b/src/microhttpd/connection_https.c
index dac4be65..e87aca56 100644
--- a/src/microhttpd/connection_https.c
+++ b/src/microhttpd/connection_https.c
@@ -41,7 +41,8 @@
  * @param connection the MHD_Connection structure
  * @param other where to write received data to
  * @param i maximum size of other (in bytes)
- * @return number of bytes actually received
+ * @return positive value for number of bytes actually received or
+ *         negative value for error number MHD_ERR_xxx_
  */
 static ssize_t
 recv_tls_adapter (struct MHD_Connection *connection,
@@ -59,21 +60,18 @@ recv_tls_adapter (struct MHD_Connection *connection,
   if ( (GNUTLS_E_AGAIN == res) ||
        (GNUTLS_E_INTERRUPTED == res) )
     {
-      MHD_socket_set_error_ (MHD_SCKT_EINTR_);
 #ifdef EPOLL_SUPPORT
       if (GNUTLS_E_AGAIN == res)
         connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY;
 #endif
-      return -1;
+      return MHD_ERR_AGAIN_;
     }
   if (res < 0)
     {
       /* Likely 'GNUTLS_E_INVALID_SESSION' (client communication
-         disrupted); set errno to something caller will interpret
-         correctly as a hard error */
-      MHD_socket_set_error_ (MHD_SCKT_ECONNRESET_);
+         disrupted); interpret as a hard error */
       connection->tls_read_ready = false;
-      return res;
+      return MHD_ERR_CONNRESET_;
     }
 
 #ifdef EPOLL_SUPPORT
@@ -95,7 +93,8 @@ recv_tls_adapter (struct MHD_Connection *connection,
  * @param connection the MHD connection structure
  * @param other data to write
  * @param i number of bytes to write
- * @return actual number of bytes written
+ * @return positive value for number of bytes actually sent or
+ *         negative value for error number MHD_ERR_xxx_
  */
 static ssize_t
 send_tls_adapter (struct MHD_Connection *connection,
@@ -118,16 +117,13 @@ send_tls_adapter (struct MHD_Connection *connection,
       if (GNUTLS_E_AGAIN == res)
         connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY;
 #endif
-      return -1;
+      return MHD_ERR_AGAIN_;
     }
   if (res < 0)
     {
-      /* some other GNUTLS error, should set 'errno'; as we do not
-         really understand the error (not listed in GnuTLS
-         documentation explicitly), we set 'errno' to something that
-         will cause the connection to fail. */
-      MHD_socket_set_error_ (MHD_SCKT_ECONNRESET_);
-      return -1;
+      /* Likely 'GNUTLS_E_INVALID_SESSION' (client communication
+         disrupted); interpret as a hard error */
+      return MHD_ERR_CONNRESET_;
     }
 #ifdef EPOLL_SUPPORT
   /* If NOT all available data was sent - socket is not write ready anymore. */

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]