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

Stephan Holljes klaxa1337 at googlemail.com
Tue Jul 21 05:45:15 CEST 2015


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

Changes since last version:
  - Introduce constants for different client modes
  - Add resource and http_code to AVOptions
  - Add http_write_header()
  - Implement handshake that allows multiple roundtrips between application
    and library.

diff --git a/libavformat/http.c b/libavformat/http.c
index 676bfd5..2597628 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -25,6 +25,7 @@
 #include <zlib.h>
 #endif /* CONFIG_ZLIB */
 
+#include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/opt.h"
 
@@ -44,6 +45,9 @@
  * path names). */
 #define BUFFER_SIZE   MAX_URL_SIZE
 #define MAX_REDIRECTS 8
+#define HTTP_ONESHOT      1
+#define HTTP_MUTLI        2
+#define HTTP_MULTI_CLIENT 4
 
 typedef struct HTTPContext {
     const AVClass *class;
@@ -97,6 +101,7 @@ typedef struct HTTPContext {
     char *method;
     int reconnect;
     int listen;
+    char *resource;
 } HTTPContext;
 
 #define OFFSET(x) offsetof(HTTPContext, x)
@@ -128,7 +133,9 @@ 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, 4, D | E },
+    { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "http_code", "The http code to send to a client", OFFSET(http_code), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 599, E},
     { NULL }
 };
 
@@ -299,32 +306,87 @@ int ff_http_averror(int status_code, int default_averror)
         return default_averror;
 }
 
+static int http_write_header(URLContext* h, int status_code)
+{
+    int ret;
+    const char *message;
+    // Maybe this should be done more elegantly?
+    static const char bad_request[] = "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\nContent-Length: 17\r\n400 Bad Request\r\n";
+    static const char forbidden[] = "HTTP/1.1 403 Forbidden\r\nContent-Type: text/plain\r\nContent-Length: 15\r\n\r\n403 Forbidden\r\n";
+    static const char not_found[] = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 15\r\n\r\n404 Not Found\r\n";
+    static const char internal_server_error[] = "HTTP/1.1 500 Internal server error\r\nContent-Type: text/plain\r\nContent-Length: 25\r\n\r\n500 Internal server error\r\n";
+    static const char ok[] = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nTransfer-Encoding: chunked\r\n\r\n";
+    av_log(h, AV_LOG_TRACE, "err: %d\n", status_code);
+    if (status_code == 200) {
+        message = ok;
+        goto end;
+    }
+    switch(status_code) {
+        case AVERROR_HTTP_BAD_REQUEST:
+            message = bad_request;
+            break;
+        case AVERROR_HTTP_FORBIDDEN:
+            message = forbidden;
+            break;
+        case AVERROR_HTTP_NOT_FOUND:
+            message = not_found;
+            break;
+        default:
+            message = internal_server_error;
+    }
+end:
+    if ((ret = ffurl_write(h, message, strlen(message))) < 0)
+        return ret;
+    // Avoid returning a positive value from ffurl_write()
+    ret = ret > 0 ? 0 : ret;
+    return ret;
+}
+
 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;
-    if (h->is_connected) {
-        switch(error) {
-            case AVERROR_HTTP_BAD_REQUEST:
-                ffurl_write(s->hd, bad_request, strlen(bad_request));
-                break;
-            default:
-                av_log(h, AV_LOG_ERROR, "Unhandled HTTP error.\n");
-                ffurl_write(s->hd, internal_server_error, strlen(internal_server_error));
+    URLContext *c = s->hd;
+    av_assert0(error < 0);
+    http_write_header(c, error);
+}
+
+static int http_handshake(URLContext *c)
+{
+    int ret, err, new_location;
+    HTTPContext *ch = c->priv_data;
+    URLContext *cl = ch->hd;
+    for (;;) {
+        if ((ret = ffurl_handshake(cl)) < 0)
+            return ret;
+        if (ret == 0)
+            break;
+    }
+    if (!ch->end_header) {
+        if ((err = http_read_header(c, &new_location)) < 0)
+            goto fail;
+    }
+    if (ch->http_code) {
+        if (ch->http_code == 200) {
+            http_write_header(cl, 200);
+            return 0;
         }
+        err = ff_http_averror(ch->http_code, AVERROR(EIO));
+        goto fail;
     }
+    return 1;
+fail:
+    handle_http_errors(c, err);
+    return err;
 }
 
 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);
@@ -332,18 +394,17 @@ 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 == HTTP_ONESHOT) { /* single client */
+        s->http_code = 200;
+        // Setting the http_code to 200 should ensure that http_handshake() returns 0.
+        ret = http_handshake(h);
+    }
 fail:
-    handle_http_errors(h, ret);
     av_dict_free(&s->chained_options);
     return ret;
 }
@@ -382,6 +443,25 @@ static int http_open(URLContext *h, const char *uri, int flags,
     return ret;
 }
 
+static int http_accept(URLContext *s, URLContext **c)
+{
+    int ret;
+    HTTPContext *sc = s->priv_data;
+    HTTPContext *cc;
+    URLContext *sl = sc->hd;
+    URLContext *cl;
+    av_assert0(sc->listen);
+    if ((ret = ffurl_alloc(c, s->filename, s->flags & AVIO_FLAG_READ_WRITE, &sl->interrupt_callback)) < 0)
+        goto fail;
+    cc = (*c)->priv_data;
+    if ((ret = ffurl_accept(sl, &cl)) < 0)
+        goto fail;
+    cc->hd = cl;
+    cc->listen = HTTP_MULTI_CLIENT;
+fail:
+    return ret;
+}
+
 static int http_getc(HTTPContext *s)
 {
     int len;
@@ -597,6 +677,7 @@ static int process_line(URLContext *h, char *line, int line_count,
                            "(%s autodetected %s received)\n", auto_method, method);
                     return ff_http_averror(400, AVERROR(EIO));
                 }
+                s->method = av_strdup(method);
             }
 
             // HTTP resource
@@ -607,6 +688,7 @@ static int process_line(URLContext *h, char *line, int line_count,
                 p++;
             *(p++) = '\0';
             av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
+            s->resource = av_strdup(resource);
 
             // HTTP version
             while (av_isspace(*p))
@@ -1346,6 +1428,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