[vlc-devel] [PATCH 03/10][RFC][WIP] http: the connection will now sent a payload if requested
Diogo Silva
dbtdsilva at gmail.com
Thu Aug 17 03:08:11 CEST 2017
This was a necessary step in order to support POST, PUT, etc. The current code
had already support for custom headers and parameters, but not a payload on
the request message.
The payload field (stream) from the message is (was) always NULL when sent on
a request message, so it can be easily use the same way it is for reading
responses. I completed both versions (http1 and http2) in order to read that
payload and send it to the respective connection.
---
modules/access/http/h1conn.c | 15 ++++++++++++++-
modules/access/http/h2conn.c | 26 ++++++++++++++++++++++++--
modules/access/http/message.c | 3 +--
modules/access/http/message.h | 2 +-
4 files changed, 40 insertions(+), 6 deletions(-)
diff --git a/modules/access/http/h1conn.c b/modules/access/http/h1conn.c
index 2fdd26614e..c2056b72d3 100644
--- a/modules/access/http/h1conn.c
+++ b/modules/access/http/h1conn.c
@@ -161,8 +161,21 @@ static struct vlc_http_stream *vlc_h1_stream_open(struct vlc_http_conn *c,
if (val < (ssize_t)len)
return vlc_h1_stream_fatal(conn);
+ // Stream the data in blocks
+ size_t body_streamed_size = 0;
+ struct block_t* block = vlc_http_msg_read( req );
+ while (block != NULL)
+ {
+ vlc_tls_Write(conn->conn.tls, block->p_buffer, block->i_buffer);
+
+ body_streamed_size += block->i_buffer;
+ block_Release(block);
+
+ block = vlc_http_msg_read( req );
+ }
+
conn->active = true;
- conn->content_length = 0;
+ conn->content_length = body_streamed_size;
conn->connection_close = false;
return &conn->stream;
}
diff --git a/modules/access/http/h2conn.c b/modules/access/http/h2conn.c
index fb66b8ab24..eea5020ce2 100644
--- a/modules/access/http/h2conn.c
+++ b/modules/access/http/h2conn.c
@@ -428,12 +428,34 @@ 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);
+ // Pre-read one block in order to see if there is payload or not
+ struct block_t *block = vlc_http_msg_read( msg );
+ bool last_block = block == NULL;
+
+ // Queue headers (HEADERS)
+ struct vlc_h2_frame *f = vlc_http_msg_h2_frame(msg, s->id, last_block);
if (f == NULL)
goto error;
-
vlc_h2_conn_queue(conn, f);
+ // Stream the data in blocks
+ while (!last_block)
+ {
+ // Read next block in order to inform if the current frame is the last
+ // or not from the stream
+ struct block_t *block_next = vlc_http_msg_read( msg );
+ last_block = block_next == NULL;
+
+ struct vlc_h2_frame *frame_data = vlc_h2_frame_data(s->id,
+ block->p_buffer, block->i_buffer, last_block);
+ if (frame_data == NULL)
+ goto error;
+ vlc_h2_conn_queue(conn, frame_data);
+
+ block_Release(block);
+ block = block_next;
+ }
+
s->older = conn->streams;
if (s->older != NULL)
s->older->newer = s;
diff --git a/modules/access/http/message.c b/modules/access/http/message.c
index 6dbcd0d310..27669cc2b1 100644
--- a/modules/access/http/message.c
+++ b/modules/access/http/message.c
@@ -285,7 +285,7 @@ struct vlc_http_msg *vlc_http_msg_get_final(struct vlc_http_msg *m)
return m;
}
-block_t *vlc_http_msg_read(struct vlc_http_msg *m)
+block_t *vlc_http_msg_read(const struct vlc_http_msg *m)
{
if (m->payload == NULL)
return NULL;
@@ -316,7 +316,6 @@ char *vlc_http_msg_format(const struct vlc_http_msg *m, size_t *restrict lenp,
for (unsigned i = 0; i < m->count; i++)
vlc_memstream_printf(&stream, "%s: %s\r\n",
m->headers[i][0], m->headers[i][1]);
-
vlc_memstream_puts(&stream, "\r\n");
if (vlc_memstream_close(&stream))
diff --git a/modules/access/http/message.h b/modules/access/http/message.h
index 83835f265d..80ad65a104 100644
--- a/modules/access/http/message.h
+++ b/modules/access/http/message.h
@@ -286,7 +286,7 @@ struct vlc_http_msg *vlc_http_msg_get_final(struct vlc_http_msg *) VLC_USED;
* @retval NULL on end-of-stream
* @retval vlc_http_error on fatal error
*/
-struct block_t *vlc_http_msg_read(struct vlc_http_msg *) VLC_USED;
+struct block_t *vlc_http_msg_read(const struct vlc_http_msg *) VLC_USED;
/** @} */
--
2.14.1
More information about the vlc-devel
mailing list