You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/06/20 05:56:58 UTC

[incubator-nuttx-apps] branch master updated (92c001bbc -> fe109998c)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


    from 92c001bbc wapi/utils: add SIOCGIWSENS into string name list
     new 2be66e83a webclient_conn_s: Add a missing FAR
     new 5cfc5cd4f webclient.h: Update the state diagram after the tunnelling stuff
     new fca5b186b webclient: Make webclient_get_tunnel returns void
     new d435858c5 webclient: Export a few ops on webclient_conn_s
     new fe109998c webclient_get_tunnel: A comment about how to dispose the returned "conn"

The 5 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:
 include/netutils/webclient.h   |  50 +++++--
 netutils/webclient/webclient.c | 303 ++++++++++++++++++++++-------------------
 2 files changed, 202 insertions(+), 151 deletions(-)


[incubator-nuttx-apps] 04/05: webclient: Export a few ops on webclient_conn_s

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit d435858c533c0a84f8b3be10385033886d366855
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Mon Jun 20 09:51:01 2022 +0900

    webclient: Export a few ops on webclient_conn_s
    
    * Make webclient_conn_s self-contained so that it can be
      used without webclient_context.
    
    * Add missing FAR.
---
 include/netutils/webclient.h   |  12 +++
 netutils/webclient/webclient.c | 213 ++++++++++++++++++++++-------------------
 2 files changed, 126 insertions(+), 99 deletions(-)

diff --git a/include/netutils/webclient.h b/include/netutils/webclient.h
index 8bd78efce..692e76ffa 100644
--- a/include/netutils/webclient.h
+++ b/include/netutils/webclient.h
@@ -501,6 +501,11 @@ struct webclient_conn_s
   /* for tls */
 
   FAR struct webclient_tls_connection *tls_conn;
+
+  /* for tls, same as webclient_context */
+
+  FAR const struct webclient_tls_ops *tls_ops;
+  FAR void *tls_ctx;
 };
 
 /****************************************************************************
@@ -564,6 +569,13 @@ int webclient_get_poll_info(FAR struct webclient_context *ctx,
 void webclient_get_tunnel(FAR struct webclient_context *ctx,
                           FAR struct webclient_conn_s **connp);
 
+ssize_t webclient_conn_send(FAR struct webclient_conn_s *conn,
+                            FAR const void *buffer, size_t len);
+ssize_t webclient_conn_recv(FAR struct webclient_conn_s *conn,
+                            FAR void *buffer, size_t len);
+void webclient_conn_close(FAR struct webclient_conn_s *conn);
+void webclient_conn_free(FAR struct webclient_conn_s *conn);
+
 #undef EXTERN
 #ifdef __cplusplus
 }
diff --git a/netutils/webclient/webclient.c b/netutils/webclient/webclient.c
index d2d7ef261..b13ab618c 100644
--- a/netutils/webclient/webclient.c
+++ b/netutils/webclient/webclient.c
@@ -266,96 +266,13 @@ static const char g_httpcache[]      = "Cache-Control: no-cache";
 
 static void free_ws(FAR struct wget_s *ws)
 {
-  free(ws->conn);
-  free(ws->tunnel);
-  free(ws);
-}
-
-/****************************************************************************
- * Name: conn_send
- ****************************************************************************/
-
-static ssize_t conn_send(struct webclient_context *ctx,
-                         struct webclient_conn_s *conn,
-                         const void *buffer, size_t len)
-{
-  if (conn->tls)
-    {
-      return ctx->tls_ops->send(ctx->tls_ctx, conn->tls_conn, buffer, len);
-    }
-
-  while (true)
-    {
-      ssize_t ret = send(conn->sockfd, buffer, len, 0);
-      if (ret == -1)
-        {
-          if (errno == EINTR)
-            {
-              continue;
-            }
-
-          if (errno == EAGAIN)
-            {
-              conn->flags |= CONN_WANT_WRITE;
-            }
-
-          return -errno;
-        }
-
-      return ret;
-    }
-}
-
-/****************************************************************************
- * Name: conn_recv
- ****************************************************************************/
-
-static ssize_t conn_recv(struct webclient_context *ctx,
-                         struct webclient_conn_s *conn,
-                         void *buffer, size_t len)
-{
-  if (conn->tls)
+  if (ws->conn != NULL)
     {
-      return ctx->tls_ops->recv(ctx->tls_ctx, conn->tls_conn, buffer, len);
+      webclient_conn_free(ws->conn);
     }
 
-  while (true)
-    {
-      ssize_t ret = recv(conn->sockfd, buffer, len, 0);
-      if (ret == -1)
-        {
-          if (errno == EINTR)
-            {
-              continue;
-            }
-
-          if (errno == EAGAIN)
-            {
-              conn->flags |= CONN_WANT_READ;
-            }
-
-          return -errno;
-        }
-
-      return ret;
-    }
-}
-
-/****************************************************************************
- * Name: conn_close
- ****************************************************************************/
-
-static void conn_close(struct webclient_context *ctx,
-                       struct webclient_conn_s *conn)
-{
-  if (conn->tls)
-    {
-      ctx->tls_ops->close(ctx->tls_ctx, conn->tls_conn);
-    }
-  else
-    {
-      close(conn->sockfd);
-    }
+  free(ws->tunnel);
+  free(ws);
 }
 
 /****************************************************************************
@@ -1147,6 +1064,100 @@ static int wget_gethostip(FAR char *hostname, FAR struct in_addr *dest)
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: webclient_conn_send
+ ****************************************************************************/
+
+ssize_t webclient_conn_send(FAR struct webclient_conn_s *conn,
+                            FAR const void *buffer, size_t len)
+{
+  if (conn->tls)
+    {
+      return conn->tls_ops->send(conn->tls_ctx, conn->tls_conn, buffer, len);
+    }
+
+  while (true)
+    {
+      ssize_t ret = send(conn->sockfd, buffer, len, 0);
+      if (ret == -1)
+        {
+          if (errno == EINTR)
+            {
+              continue;
+            }
+
+          if (errno == EAGAIN)
+            {
+              conn->flags |= CONN_WANT_WRITE;
+            }
+
+          return -errno;
+        }
+
+      return ret;
+    }
+}
+
+/****************************************************************************
+ * Name: webclient_conn_recv
+ ****************************************************************************/
+
+ssize_t webclient_conn_recv(FAR struct webclient_conn_s *conn,
+                            FAR void *buffer, size_t len)
+{
+  if (conn->tls)
+    {
+      return conn->tls_ops->recv(conn->tls_ctx, conn->tls_conn, buffer, len);
+    }
+
+  while (true)
+    {
+      ssize_t ret = recv(conn->sockfd, buffer, len, 0);
+      if (ret == -1)
+        {
+          if (errno == EINTR)
+            {
+              continue;
+            }
+
+          if (errno == EAGAIN)
+            {
+              conn->flags |= CONN_WANT_READ;
+            }
+
+          return -errno;
+        }
+
+      return ret;
+    }
+}
+
+/****************************************************************************
+ * Name: webclient_conn_close
+ ****************************************************************************/
+
+void webclient_conn_close(FAR struct webclient_conn_s *conn)
+{
+  if (conn->tls)
+    {
+      conn->tls_ops->close(conn->tls_ctx, conn->tls_conn);
+    }
+  else
+    {
+      close(conn->sockfd);
+    }
+}
+
+/****************************************************************************
+ * Name: webclient_conn_free
+ ****************************************************************************/
+
+void webclient_conn_free(FAR struct webclient_conn_s *conn)
+{
+  DEBUGASSERT(conn != NULL);
+  free(conn);
+}
+
 /****************************************************************************
  * Name: webclient_perform
  *
@@ -1273,6 +1284,8 @@ int webclient_perform(FAR struct webclient_context *ctx)
           else if (!strcmp(ws->target.scheme, "https") && tls_ops != NULL)
             {
               conn->tls = true;
+              conn->tls_ops = tls_ops;
+              conn->tls_ctx = ctx->tls_ctx;
             }
           else if (!strcmp(ws->target.scheme, "http"))
             {
@@ -1480,15 +1493,15 @@ int webclient_perform(FAR struct webclient_context *ctx)
                           DEBUGASSERT(ret != -EAGAIN &&
                                       ret != -EINPROGRESS &&
                                       ret != -EALREADY);
-                          conn_close(ctx, tunnel_conn);
-                          free(tunnel_conn);
+                          webclient_conn_close(tunnel_conn);
+                          webclient_conn_free(tunnel_conn);
                         }
                     }
                   else
                     {
                       conn->sockfd = tunnel_conn->sockfd;
                       ws->need_conn_close = true;
-                      free(tunnel_conn);
+                      webclient_conn_free(tunnel_conn);
                     }
                 }
             }
@@ -1767,8 +1780,9 @@ int webclient_perform(FAR struct webclient_context *ctx)
         {
           ssize_t ssz;
 
-          ssz = conn_send(ctx, conn, ws->buffer + ws->state_offset,
-                          ws->state_len);
+          ssz = webclient_conn_send(conn,
+                                    ws->buffer + ws->state_offset,
+                                    ws->state_len);
           if (ssz < 0)
             {
               ret = ssz;
@@ -1841,9 +1855,10 @@ int webclient_perform(FAR struct webclient_context *ctx)
               size_t bytes_to_send = ws->data_len - ws->state_offset;
 
               DEBUGASSERT(bytes_to_send <= ws->state_len);
-              ssize_t ssz = conn_send(ctx, conn,
-                                      ws->data_buffer + ws->state_offset,
-                                      bytes_to_send);
+              ssize_t ssz = webclient_conn_send(conn,
+                                                ws->data_buffer +
+                                                ws->state_offset,
+                                                bytes_to_send);
               if (ssz < 0)
                 {
                   ret = ssz;
@@ -1898,7 +1913,7 @@ int webclient_perform(FAR struct webclient_context *ctx)
                       want = 1;
                     }
 
-                  ssz = conn_recv(ctx, conn, ws->buffer, want);
+                  ssz = webclient_conn_recv(conn, ws->buffer, want);
                   if (ssz < 0)
                     {
                       ret = ssz;
@@ -2121,7 +2136,7 @@ int webclient_perform(FAR struct webclient_context *ctx)
 
       if (ws->state == WEBCLIENT_STATE_CLOSE)
         {
-          conn_close(ctx, conn);
+          webclient_conn_close(conn);
           ws->need_conn_close = false;
           if (ws->redirected)
             {
@@ -2163,7 +2178,7 @@ errout_with_errno:
 
   if (ws->need_conn_close)
     {
-      conn_close(ctx, conn);
+      webclient_conn_close(conn);
     }
 
   free_ws(ws);
@@ -2200,7 +2215,7 @@ void webclient_abort(FAR struct webclient_context *ctx)
     {
       struct webclient_conn_s *conn = ws->conn;
 
-      conn_close(ctx, conn);
+      webclient_conn_close(conn);
     }
 
   if (ws->tunnel != NULL)


[incubator-nuttx-apps] 02/05: webclient.h: Update the state diagram after the tunnelling stuff

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 5cfc5cd4f2cb546423ff7bcfc81871f65f7e6d9f
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Fri Jun 17 15:00:09 2022 +0900

    webclient.h: Update the state diagram after the tunnelling stuff
---
 include/netutils/webclient.h | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/include/netutils/webclient.h b/include/netutils/webclient.h
index 5d38df82f..33aa4ac93 100644
--- a/include/netutils/webclient.h
+++ b/include/netutils/webclient.h
@@ -295,13 +295,27 @@ struct webclient_tls_ops
  *      |        |
  * webclient_perform
  *      |
- *      +---------------+
- *      |               |
- *      |             non-blocking mode,
- *      |             returns -EAGAIN
- *      |               |
- *      v               v
- *     DONE           IN-PROGRESS
+ *      |
+ *      +-- non-blocking mode, returns -EAGAIN ---> IN-PROGRESS
+ *      |
+ *      +-- returns -errno ---> DONE
+ *      |
+ *     returns 0
+ *      |
+ *      +-- !WEBCLIENT_FLAG_TUNNEL --> DONE
+ *      |
+ *      +-- WEBCLIENT_FLAG_TUNNEL, http_status 2xx -----> TUNNEL_ESTABLISHED
+ *      |
+ *      +-- WEBCLIENT_FLAG_TUNNEL, http_status others --> DONE
+ *
+ *
+ *  TUNNEL_ESTABLISHED
+ *      |
+ * webclient_get_tunnel
+ *      |
+ *      v
+ *     DONE
+ *
  *
  * (uninitialized):
  *   After the memory for webclient_context is allocated,
@@ -341,6 +355,10 @@ struct webclient_tls_ops
  *
  *   If the application wants to reuse the context for another request,
  *   it should initialize it with webclient_set_defaults() again.
+ *
+ * TUNNEL_ESTABLISHED
+ *   webclient_get_tunnel() should be called exactly once to return
+ *   the established tunnel.
  */
 
 struct webclient_context


[incubator-nuttx-apps] 03/05: webclient: Make webclient_get_tunnel returns void

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit fca5b186b172cca83b1059fd1d56088ae1b80170
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Fri Jun 17 15:03:46 2022 +0900

    webclient: Make webclient_get_tunnel returns void
    
    As it does never fail.
---
 include/netutils/webclient.h   |  4 +-
 netutils/webclient/webclient.c | 83 ++++++++++++++++++++----------------------
 2 files changed, 41 insertions(+), 46 deletions(-)

diff --git a/include/netutils/webclient.h b/include/netutils/webclient.h
index 33aa4ac93..8bd78efce 100644
--- a/include/netutils/webclient.h
+++ b/include/netutils/webclient.h
@@ -561,8 +561,8 @@ void webclient_set_static_body(FAR struct webclient_context *ctx,
                                size_t bodylen);
 int webclient_get_poll_info(FAR struct webclient_context *ctx,
                             FAR struct webclient_poll_info *info);
-int webclient_get_tunnel(FAR struct webclient_context *ctx,
-                         FAR struct webclient_conn_s **connp);
+void webclient_get_tunnel(FAR struct webclient_context *ctx,
+                          FAR struct webclient_conn_s **connp);
 
 #undef EXTERN
 #ifdef __cplusplus
diff --git a/netutils/webclient/webclient.c b/netutils/webclient/webclient.c
index 1bcdd3817..d2d7ef261 100644
--- a/netutils/webclient/webclient.c
+++ b/netutils/webclient/webclient.c
@@ -1445,54 +1445,51 @@ int webclient_perform(FAR struct webclient_context *ctx)
                 {
                   FAR struct webclient_conn_s *tunnel_conn;
 
-                  ret = webclient_get_tunnel(ws->tunnel, &tunnel_conn);
-                  if (ret == 0)
+                  webclient_get_tunnel(ws->tunnel, &tunnel_conn);
+                  DEBUGASSERT(tunnel_conn != NULL);
+                  DEBUGASSERT(!tunnel_conn->tls);
+                  free(ws->tunnel);
+                  ws->tunnel = NULL;
+
+                  if (conn->tls)
                     {
-                      DEBUGASSERT(tunnel_conn != NULL);
-                      DEBUGASSERT(!tunnel_conn->tls);
-                      free(ws->tunnel);
-                      ws->tunnel = NULL;
+                      /* Revisit: tunnel_conn here should have
+                       * timeout configured already.
+                       * Configuring it again here is redundant.
+                       */
 
-                      if (conn->tls)
+                      ret = tls_ops->init_connection(tls_ctx,
+                                                     tunnel_conn,
+                                                     ws->target.hostname,
+                                                     ctx->timeout_sec,
+                                                     &conn->tls_conn);
+                      if (ret == 0)
                         {
-                          /* Revisit: tunnel_conn here should have
-                           * timeout configured already.
-                           * Configuring it again here is redundant.
+                          /* Note: tunnel_conn has been consumed by
+                           * tls_ops->init_connection
                            */
 
-                          ret = tls_ops->init_connection(tls_ctx,
-                                                         tunnel_conn,
-                                                         ws->target.hostname,
-                                                         ctx->timeout_sec,
-                                                         &conn->tls_conn);
-                          if (ret == 0)
-                            {
-                              /* Note: tunnel_conn has been consumed by
-                               * tls_ops->init_connection
-                               */
-
-                              ws->need_conn_close = true;
-                            }
-                          else
-                            {
-                              /* Note: restarting tls_ops->init_connection
-                               * is not implemented
-                               */
-
-                              DEBUGASSERT(ret != -EAGAIN &&
-                                          ret != -EINPROGRESS &&
-                                          ret != -EALREADY);
-                              conn_close(ctx, tunnel_conn);
-                              free(tunnel_conn);
-                            }
+                          ws->need_conn_close = true;
                         }
                       else
                         {
-                          conn->sockfd = tunnel_conn->sockfd;
-                          ws->need_conn_close = true;
+                          /* Note: restarting tls_ops->init_connection
+                           * is not implemented
+                           */
+
+                          DEBUGASSERT(ret != -EAGAIN &&
+                                      ret != -EINPROGRESS &&
+                                      ret != -EALREADY);
+                          conn_close(ctx, tunnel_conn);
                           free(tunnel_conn);
                         }
                     }
+                  else
+                    {
+                      conn->sockfd = tunnel_conn->sockfd;
+                      ws->need_conn_close = true;
+                      free(tunnel_conn);
+                    }
                 }
             }
           else if (conn->tls)
@@ -2486,16 +2483,16 @@ int webclient_get_poll_info(FAR struct webclient_context *ctx,
  *   the tunneled connection.
  *
  *   This function should be used exactly once after a successful
- *   call of webclient_perform with WEBCLIENT_FLAG_TUNNEL.
+ *   call of webclient_perform with WEBCLIENT_FLAG_TUNNEL, with
+ *   http_status 2xx.
  *
  *   This function also disposes the given webclient_context.
- *   The context will be invalid after the successful call of this
- *   function.
+ *   The context will be invalid after a call of this function.
  *
  ****************************************************************************/
 
-int webclient_get_tunnel(FAR struct webclient_context *ctx,
-                         FAR struct webclient_conn_s **connp)
+void webclient_get_tunnel(FAR struct webclient_context *ctx,
+                          FAR struct webclient_conn_s **connp)
 {
   struct wget_s *ws;
   struct webclient_conn_s *conn;
@@ -2510,6 +2507,4 @@ int webclient_get_tunnel(FAR struct webclient_context *ctx,
   ws->conn = NULL;
   free_ws(ws);
   _SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
-
-  return 0;
 }


[incubator-nuttx-apps] 01/05: webclient_conn_s: Add a missing FAR

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 2be66e83a4f8ca37dd2557b200402613c5e80e21
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Mon Jun 20 10:05:50 2022 +0900

    webclient_conn_s: Add a missing FAR
---
 include/netutils/webclient.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/netutils/webclient.h b/include/netutils/webclient.h
index 2667eef90..5d38df82f 100644
--- a/include/netutils/webclient.h
+++ b/include/netutils/webclient.h
@@ -482,7 +482,7 @@ struct webclient_conn_s
 
   /* for tls */
 
-  struct webclient_tls_connection *tls_conn;
+  FAR struct webclient_tls_connection *tls_conn;
 };
 
 /****************************************************************************


[incubator-nuttx-apps] 05/05: webclient_get_tunnel: A comment about how to dispose the returned "conn"

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit fe109998c9a5ec6d24f7f6910ea4c893c9ccc516
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Mon Jun 20 09:59:22 2022 +0900

    webclient_get_tunnel: A comment about how to dispose the returned "conn"
---
 netutils/webclient/webclient.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/netutils/webclient/webclient.c b/netutils/webclient/webclient.c
index b13ab618c..530aa9b69 100644
--- a/netutils/webclient/webclient.c
+++ b/netutils/webclient/webclient.c
@@ -2504,6 +2504,17 @@ int webclient_get_poll_info(FAR struct webclient_context *ctx,
  *   This function also disposes the given webclient_context.
  *   The context will be invalid after a call of this function.
  *
+ *   It's the caller's responsibility to close the returned
+ *   webclient_conn_s, either using webclient_conn_close() or
+ *   using the internal knowledge about the structure. (E.g.
+ *   It's sometimes convenient/efficient for the caller to
+ *   only keep conn->sockfd descriptor and free the rest of the
+ *   structure using webclient_conn_free(). In that case, it will
+ *   need to close() the descriptor after finishing on it.)
+ *
+ *   It's the caller's responsibility to free the returned
+ *   webclient_conn_s using webclient_conn_free().
+ *
  ****************************************************************************/
 
 void webclient_get_tunnel(FAR struct webclient_context *ctx,