[vlc-commits] http: add flag to open stream with request payload

Rémi Denis-Courmont git at videolan.org
Sun Oct 4 09:54:31 CEST 2020


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Oct  4 10:51:54 2020 +0300| [d08377b7b667eed8d6e905ffcf8d6a2097a00dcc] | committer: Rémi Denis-Courmont

http: add flag to open stream with request payload

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d08377b7b667eed8d6e905ffcf8d6a2097a00dcc
---

 modules/access/http/conn.h        | 11 +++++++----
 modules/access/http/connmgr.c     |  6 +++---
 modules/access/http/h1conn.c      |  9 +++++----
 modules/access/http/h1conn_test.c | 25 +++++++++++++------------
 modules/access/http/h2conn.c      |  4 ++--
 modules/access/http/h2conn_test.c | 25 +++++++++++++------------
 modules/access/http/tunnel.c      |  2 +-
 7 files changed, 44 insertions(+), 38 deletions(-)

diff --git a/modules/access/http/conn.h b/modules/access/http/conn.h
index 1f6705f8c4..9f3dbbbecf 100644
--- a/modules/access/http/conn.h
+++ b/modules/access/http/conn.h
@@ -33,7 +33,8 @@ struct vlc_http_stream;
 struct vlc_http_conn_cbs
 {
     struct vlc_http_stream *(*stream_open)(struct vlc_http_conn *,
-                                           const struct vlc_http_msg *);
+                                           const struct vlc_http_msg *,
+                                           bool has_data);
     void (*release)(struct vlc_http_conn *);
 };
 
@@ -44,9 +45,10 @@ struct vlc_http_conn
 };
 
 static inline struct vlc_http_stream *
-vlc_http_stream_open(struct vlc_http_conn *conn, const struct vlc_http_msg *m)
+vlc_http_stream_open(struct vlc_http_conn *conn, const struct vlc_http_msg *m,
+                     bool has_data)
 {
-    return conn->cbs->stream_open(conn, m);
+    return conn->cbs->stream_open(conn, m, has_data);
 }
 
 static inline void vlc_http_conn_release(struct vlc_http_conn *conn)
@@ -90,6 +92,7 @@ struct vlc_http_stream *vlc_chunked_open(struct vlc_http_stream *,
  * \param req HTTP request message
  * \param idempotent whether the HTTP request is idempotent (e.g. GET),
  *                   or not (e.g. POST)
+ * \param has_data whether the HTTP request will have a request payload
  * \param connp pointer to storage space for the established HTTP connection
  *              (or NULL if the connection is not to be reused) [OUT]
  *              can be NULL if the connection is not meant to be reused
@@ -99,7 +102,7 @@ struct vlc_http_stream *vlc_chunked_open(struct vlc_http_stream *,
 struct vlc_http_stream *vlc_h1_request(void *ctx, const char *hostname,
                                        unsigned port, bool proxy,
                                        const struct vlc_http_msg *req,
-                                       bool idempotent,
+                                       bool idempotent, bool has_data,
                                        struct vlc_http_conn **restrict connp);
 
 /** @} */
diff --git a/modules/access/http/connmgr.c b/modules/access/http/connmgr.c
index f0f7616e40..5105fdc3fe 100644
--- a/modules/access/http/connmgr.c
+++ b/modules/access/http/connmgr.c
@@ -130,7 +130,7 @@ struct vlc_http_msg *vlc_http_mgr_reuse(struct vlc_http_mgr *mgr,
     if (conn == NULL)
         return NULL;
 
-    struct vlc_http_stream *stream = vlc_http_stream_open(conn, req);
+    struct vlc_http_stream *stream = vlc_http_stream_open(conn, req, false);
     if (stream != NULL)
     {
         struct vlc_http_msg *m = vlc_http_msg_get_initial(stream);
@@ -232,7 +232,7 @@ static struct vlc_http_msg *vlc_http_request(struct vlc_http_mgr *mgr,
         if (url.psz_host != NULL)
             stream = vlc_h1_request(mgr->logger, url.psz_host,
                                     url.i_port ? url.i_port : 80, true, req,
-                                    true, &conn);
+                                    true, false, &conn);
         else
             stream = NULL;
 
@@ -240,7 +240,7 @@ static struct vlc_http_msg *vlc_http_request(struct vlc_http_mgr *mgr,
     }
     else
         stream = vlc_h1_request(mgr->logger, host, port ? port : 80, false,
-                                req, true, &conn);
+                                req, true, false, &conn);
 
     if (stream == NULL)
         return NULL;
diff --git a/modules/access/http/h1conn.c b/modules/access/http/h1conn.c
index 2b5b508a8f..5a427f41a6 100644
--- a/modules/access/http/h1conn.c
+++ b/modules/access/http/h1conn.c
@@ -141,7 +141,7 @@ static struct vlc_h1_conn *vlc_h1_stream_conn(struct vlc_http_stream *stream)
 }
 
 static struct vlc_http_stream *vlc_h1_stream_open(struct vlc_http_conn *c,
-                                                const struct vlc_http_msg *req)
+                                 const struct vlc_http_msg *req, bool has_data)
 {
     struct vlc_h1_conn *conn = container_of(c, struct vlc_h1_conn, conn);
     size_t len;
@@ -150,7 +150,7 @@ static struct vlc_http_stream *vlc_h1_stream_open(struct vlc_http_conn *c,
     if (conn->active || conn->conn.tls == NULL)
         return NULL;
 
-    char *payload = vlc_http_msg_format(req, &len, conn->proxy, false);
+    char *payload = vlc_http_msg_format(req, &len, conn->proxy, has_data);
     if (unlikely(payload == NULL))
         return NULL;
 
@@ -340,7 +340,7 @@ struct vlc_http_conn *vlc_h1_conn_create(void *ctx, vlc_tls_t *tls, bool proxy)
 struct vlc_http_stream *vlc_h1_request(void *ctx, const char *hostname,
                                        unsigned port, bool proxy,
                                        const struct vlc_http_msg *req,
-                                       bool idempotent,
+                                       bool idempotent, bool has_data,
                                        struct vlc_http_conn **restrict connp)
 {
     struct addrinfo hints =
@@ -376,7 +376,8 @@ struct vlc_http_stream *vlc_h1_request(void *ctx, const char *hostname,
         }
 
         /* Send the HTTP request */
-        struct vlc_http_stream *stream = vlc_http_stream_open(conn, req);
+        struct vlc_http_stream *stream = vlc_http_stream_open(conn, req,
+                                                              has_data);
 
         if (stream != NULL)
         {
diff --git a/modules/access/http/h1conn_test.c b/modules/access/http/h1conn_test.c
index d908b4f846..bb0f7b08f0 100644
--- a/modules/access/http/h1conn_test.c
+++ b/modules/access/http/h1conn_test.c
@@ -84,13 +84,14 @@ static void conn_destroy(void)
     vlc_tls_SessionDelete(external_tls);
 }
 
-static struct vlc_http_stream *stream_open(void)
+static struct vlc_http_stream *stream_open(bool has_data)
 {
-    struct vlc_http_msg *m = vlc_http_req_create("GET", "https",
+    const char *verb = has_data ? "POST" : "GET";
+    struct vlc_http_msg *m = vlc_http_req_create(verb, "https",
                                                  "www.example.com", "/");
     assert(m != NULL);
 
-    struct vlc_http_stream *s = vlc_http_stream_open(conn, m);
+    struct vlc_http_stream *s = vlc_http_stream_open(conn, m, has_data);
     vlc_http_msg_destroy(m);
     return s;
 }
@@ -108,7 +109,7 @@ int main(void)
     /* Test rejected connection */
     conn_create();
     conn_shutdown(SHUT_RD);
-    s = stream_open();
+    s = stream_open(false);
     if (s != NULL)
         /* Remote read shutdown does not result in an error on some systems. */
         vlc_http_stream_close(s, true);
@@ -116,7 +117,7 @@ int main(void)
 
     /* Test rejected stream */
     conn_create();
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_shutdown(SHUT_WR);
 
@@ -131,13 +132,13 @@ int main(void)
     m = vlc_http_msg_get_initial(s);
     assert(m == NULL);
 
-    s = stream_open();
+    s = stream_open(false);
     assert(s == NULL);
     conn_destroy();
 
     /* Test garbage */
     conn_create();
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_send("Go away!\r\n\r\n");
     conn_shutdown(SHUT_WR);
@@ -151,7 +152,7 @@ int main(void)
 
     /* Test HTTP/1.0 stream */
     conn_create();
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_send("HTTP/1.0 200 OK\r\n\r\n");
     m = vlc_http_msg_get_initial(s);
@@ -171,7 +172,7 @@ int main(void)
 
     /* Test HTTP/1.1 with closed connection */
     conn_create();
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n");
     m = vlc_http_msg_get_initial(s);
@@ -191,7 +192,7 @@ int main(void)
 
     /* Test HTTP/1.1 with chunked transfer encoding */
     conn_create();
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_send("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
               "Content-Length: 1000000\r\n\r\n"); /* length must be ignored */
@@ -211,7 +212,7 @@ int main(void)
 
     /* Test HTTP/1.1 with content length */
     conn_create();
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_send("HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\n");
     m = vlc_http_msg_get_initial(s);
@@ -230,7 +231,7 @@ int main(void)
 
     /* Test HTTP/1.1 with content length, shortened by error */
     conn_create();
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_send("HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\n");
     m = vlc_http_msg_get_initial(s);
diff --git a/modules/access/http/h2conn.c b/modules/access/http/h2conn.c
index 34f009e2e2..54022168e9 100644
--- a/modules/access/http/h2conn.c
+++ b/modules/access/http/h2conn.c
@@ -421,7 +421,7 @@ static const struct vlc_http_stream_cbs vlc_h2_stream_callbacks =
  * \return an HTTP stream, or NULL on error
  */
 static struct vlc_http_stream *vlc_h2_stream_open(struct vlc_http_conn *c,
-                                                const struct vlc_http_msg *msg)
+                                 const struct vlc_http_msg *msg, bool has_data)
 {
     struct vlc_h2_conn *conn = container_of(c, struct vlc_h2_conn, conn);
     struct vlc_h2_stream *s = malloc(sizeof (*s));
@@ -453,7 +453,7 @@ static struct vlc_http_stream *vlc_h2_stream_open(struct vlc_http_conn *c,
     s->id = conn->next_id;
     conn->next_id += 2;
 
-    struct vlc_h2_frame *f = vlc_http_msg_h2_frame(msg, s->id, true);
+    struct vlc_h2_frame *f = vlc_http_msg_h2_frame(msg, s->id, !has_data);
     if (f == NULL)
         goto error;
 
diff --git a/modules/access/http/h2conn_test.c b/modules/access/http/h2conn_test.c
index 8e375d1009..749a3c3bcb 100644
--- a/modules/access/http/h2conn_test.c
+++ b/modules/access/http/h2conn_test.c
@@ -121,13 +121,14 @@ static void conn_destroy(void)
     vlc_tls_SessionDelete(external_tls);
 }
 
-static struct vlc_http_stream *stream_open(void)
+static struct vlc_http_stream *stream_open(bool has_data)
 {
-    struct vlc_http_msg *m = vlc_http_req_create("GET", "https",
+    const char *verb = has_data ? "POST" : "GET";
+    struct vlc_http_msg *m = vlc_http_req_create(verb, "https",
                                                  "www.example.com", "/");
     assert(m != NULL);
 
-    struct vlc_http_stream *s = vlc_http_stream_open(conn, m);
+    struct vlc_http_stream *s = vlc_http_stream_open(conn, m, has_data);
     vlc_http_msg_destroy(m);
     return s;
 }
@@ -174,7 +175,7 @@ int main(void)
 
     /* Test rejected stream */
     sid += 2;
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_expect(HEADERS);
     conn_send(vlc_h2_frame_rst_stream(sid, VLC_H2_REFUSED_STREAM));
@@ -187,7 +188,7 @@ int main(void)
 
     /* Test accepted stream */
     sid += 2;
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     stream_reply(sid, false);
     m = vlc_http_msg_get_initial(s);
@@ -204,7 +205,7 @@ int main(void)
 
     /* Test continuation then accepted stream */
     sid += 2;
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     stream_continuation(sid);
     m = vlc_http_msg_get_initial(s);
@@ -232,12 +233,12 @@ int main(void)
 
     /* Test accepted stream after continuation */
     sid += 2;
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     stream_continuation(sid);
     stream_reply(sid, true);
     sid += 2;
-    s2 = stream_open(); /* second stream to enforce test timing/ordering */
+    s2 = stream_open(false); /* 2nd stream to enforce test timing/ordering */
     assert(s2 != NULL);
     stream_reply(sid, true);
     m = vlc_http_msg_get_initial(s2);
@@ -260,10 +261,10 @@ int main(void)
 
     /* Test multiple streams in non-LIFO order */
     sid += 2;
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     sid += 2;
-    s2 = stream_open();
+    s2 = stream_open(false);
     assert(s2 != NULL);
     stream_reply(sid, false);
     stream_reply(sid - 2, true);
@@ -283,14 +284,14 @@ int main(void)
 
     /* Test graceful connection termination */
     sid += 2;
-    s = stream_open();
+    s = stream_open(false);
     assert(s != NULL);
     conn_send(vlc_h2_frame_goaway(sid - 2, VLC_H2_NO_ERROR));
     m = vlc_http_stream_read_headers(s);
     assert(m == NULL);
 
     /* Test stream after connection shut down */
-    assert(stream_open() == NULL);
+    assert(stream_open(false) == NULL);
 
     /* Test releasing connection before stream */
     conn_destroy();
diff --git a/modules/access/http/tunnel.c b/modules/access/http/tunnel.c
index 3d607ec732..b9c3c4bca0 100644
--- a/modules/access/http/tunnel.c
+++ b/modules/access/http/tunnel.c
@@ -68,7 +68,7 @@ static struct vlc_http_msg *vlc_http_tunnel_open(struct vlc_http_conn *conn,
         vlc_http_msg_add_creds_basic(req, true, username,
                                      (password != NULL) ? password : "");
 
-    struct vlc_http_stream *stream = vlc_http_stream_open(conn, req);
+    struct vlc_http_stream *stream = vlc_http_stream_open(conn, req, false);
 
     vlc_http_msg_destroy(req);
     if (stream == NULL)



More information about the vlc-commits mailing list