[FFmpeg-devel] [PATCH 7/9] lavf/http: increase range for listen, handle connection closing accordingly, add http_handshake and move handshake logic there

Stephan Holljes klaxa1337 at googlemail.com
Sat Jul 4 07:47:05 CEST 2015


Signed-off-by: Stephan Holljes <klaxa1337 at googlemail.com>
---
 libavformat/http.c | 44 +++++++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 13 deletions(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index 3c1ec35..6338d80 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -129,7 +129,7 @@ static const AVOption options[] = {
     { "end_offset", "try to limit the request to bytes preceding this offset", OFFSET(end_off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
     { "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
     { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
-    { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D | E },
+    { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
     { NULL }
 };
 
@@ -305,8 +305,11 @@ static void handle_http_errors(URLContext *h, int error)
     static const char bad_request[] = "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n400 Bad Request\r\n";
     static const char internal_server_error[] = "HTTP/1.1 500 Internal server error\r\nContent-Type: text/plain\r\n\r\n500 Internal server error\r\n";
     HTTPContext *s = h->priv_data;
+    av_assert0(error < 0);
     if (h->is_connected) {
         switch(error) {
+            case 0:
+                break;
             case AVERROR_HTTP_BAD_REQUEST:
                 ffurl_write(s->hd, bad_request, strlen(bad_request));
                 break;
@@ -317,15 +320,33 @@ static void handle_http_errors(URLContext *h, int error)
     }
 }
 
+static int http_handshake(URLContext *c) {
+    int ret, err, new_location;
+    HTTPContext *ch = c->priv_data;
+    URLContext *cl = ch->hd;
+    static const char header[] = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nTransfer-Encoding: chunked\r\n\r\n";
+    if ((ret = ffurl_handshake(cl)) < 0)
+        return ret;
+    if ((err = http_read_header(c, &new_location)) < 0)
+        goto fail;
+    if ((ret = ffurl_write(cl, header, strlen(header))) < 0)
+        return ret;
+    // Avoid returning a positive value from ffurl_write()
+    ret = ret > 0 ? 0 : ret;
+    return ret;
+fail:
+    handle_http_errors(c, err);
+    return ret;
+}
+
 static int http_listen(URLContext *h, const char *uri, int flags,
                        AVDictionary **options) {
     HTTPContext *s = h->priv_data;
     int ret;
-    static const char header[] = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nTransfer-Encoding: chunked\r\n\r\n";
     char hostname[1024], proto[10];
     char lower_url[100];
     const char *lower_proto = "tcp";
-    int port, new_location;
+    int port;
     s->chunked_post = 1;
     av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
                  NULL, 0, uri);
@@ -333,18 +354,14 @@ static int http_listen(URLContext *h, const char *uri, int flags,
         lower_proto = "tls";
     ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
                 NULL);
-    av_dict_set(options, "listen", "1", 0);
+    if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
+        goto fail;
     if ((ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
                           &h->interrupt_callback, options)) < 0)
         goto fail;
-    if ((ret = http_read_header(h, &new_location)) < 0)
-         goto fail;
-    if ((ret = ffurl_write(s->hd, header, strlen(header))) < 0)
-         goto fail;
-    return 0;
-
+    if (s->listen == 1) /* single client */
+        ret = http_handshake(h);
 fail:
-    handle_http_errors(h, ret);
     av_dict_free(&s->chained_options);
     return ret;
 }
@@ -1262,8 +1279,7 @@ static int http_shutdown(URLContext *h, int flags)
     HTTPContext *s = h->priv_data;
 
     /* signal end of chunked encoding if used */
-    if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
-        ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
+    if (((flags & AVIO_FLAG_WRITE) && s->chunked_post)) {
         ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
         ret = ret > 0 ? 0 : ret;
         s->end_chunked_post = 1;
@@ -1365,6 +1381,8 @@ HTTP_CLASS(http);
 URLProtocol ff_http_protocol = {
     .name                = "http",
     .url_open2           = http_open,
+    .url_accept          = http_accept,
+    .url_handshake       = http_handshake,
     .url_read            = http_read,
     .url_write           = http_write,
     .url_seek            = http_seek,
-- 
2.1.0



More information about the ffmpeg-devel mailing list