[FFmpeg-devel] [PATCH 6/8] lavf/http: Implement server side network code.
Stephan Holljes
klaxa1337 at googlemail.com
Sat Jul 25 02:38:02 CEST 2015
add http_accept,
add http_handshake and move handshake logic there,
handle connection closing.
Signed-off-by: Stephan Holljes <klaxa1337 at googlemail.com>
---
libavformat/http.c | 184 ++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 161 insertions(+), 23 deletions(-)
Changes since last version:
- Add various constants
- Add various fields
- Expose resource, reply_code and handshake_finish via AVOption options
- Rename http_write_header() to http_write_reply()
- Rewrite http_write_reply to store HTTP reply in local buffer
- Rewrite http_handshake to use a state-based model
- Pass s->flags without filtering to ffurl_alloc()
diff --git a/libavformat/http.c b/libavformat/http.c
index 676bfd5..968b099 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,11 @@
* path names). */
#define BUFFER_SIZE MAX_URL_SIZE
#define MAX_REDIRECTS 8
+#define HTTP_SINGLE 1
+#define HTTP_MUTLI 2
+#define LOWER_PROTO 0
+#define READ_HEADERS 1
+#define FINISH 2
typedef struct HTTPContext {
const AVClass *class;
@@ -97,6 +103,11 @@ typedef struct HTTPContext {
char *method;
int reconnect;
int listen;
+ char *resource;
+ int reply_code;
+ int is_multi_client;
+ int handshake_step;
+ int handshake_finish;
} HTTPContext;
#define OFFSET(x) offsetof(HTTPContext, x)
@@ -128,7 +139,10 @@ 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 },
+ { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
+ { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
+ { "handshake_finish", "Indicate that the protocol handshake is to be finished", OFFSET(handshake_finish), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 1, E},
{ NULL }
};
@@ -299,51 +313,151 @@ int ff_http_averror(int status_code, int default_averror)
return default_averror;
}
+static int http_write_reply(URLContext* h, int status_code)
+{
+ int ret, body = 0, reply_code;
+ const char *reply_text, *content_type;
+ HTTPContext *s = h->priv_data;
+ char message[BUFFER_SIZE];
+ static const char bad_request[] = "Bad Request";
+ static const char forbidden[] = "Forbidden";
+ static const char not_found[] = "Not Found";
+ static const char internal_server_error[] = "Internal server error";
+ static const char ok[] = "OK";
+ static const char oct[] = "application/octet-stream";
+ static const char text[] = "text/plain";
+ content_type = text;
+
+ if (status_code < 0)
+ body = 1;
+ switch (status_code) {
+ case AVERROR_HTTP_BAD_REQUEST:
+ case 400:
+ reply_code = 400;
+ reply_text = bad_request;
+ break;
+ case AVERROR_HTTP_FORBIDDEN:
+ case 403:
+ reply_code = 403;
+ reply_text = forbidden;
+ break;
+ case AVERROR_HTTP_NOT_FOUND:
+ case 404:
+ reply_code = 404;
+ reply_text = not_found;
+ break;
+ case 200:
+ reply_code = 200;
+ reply_text = ok;
+ content_type = oct;
+ break;
+ case AVERROR_HTTP_SERVER_ERROR:
+ case 500:
+ reply_code = 500;
+ reply_text = internal_server_error;
+ break;
+ default:
+ return AVERROR(EINVAL);
+ }
+ if (body) {
+ s->chunked_post = 0;
+ snprintf(message, sizeof(message),
+ "HTTP/1.1 %03d %s\r\n"
+ "Content-Type: %s\r\n"
+ "Content-Length: %lu\r\n"
+ "\r\n"
+ "%03d %s\r\n",
+ reply_code,
+ reply_text,
+ content_type,
+ strlen(reply_text) + 6, // 3 digit status code + space + \r\n
+ reply_code,
+ reply_text);
+ } else {
+ s->chunked_post = 1;
+ snprintf(message, sizeof(message),
+ "HTTP/1.1 %03d %s\r\n"
+ "Content-Type: %s\r\n"
+ "Transfer-Encoding: chunked\r\n"
+ "\r\n",
+ reply_code,
+ reply_text,
+ content_type);
+ }
+ av_log(h, AV_LOG_TRACE, "%s\n", message);
+ if ((ret = ffurl_write(s->hd, message, strlen(message))) < 0)
+ return ret;
+ if (body)
+ return status_code;
+ return 0;
+}
+
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_reply(c, error);
+}
+
+static int http_handshake(URLContext *c)
+{
+ int ret, err, new_location;
+ HTTPContext *ch = c->priv_data;
+ URLContext *cl = ch->hd;
+ switch (ch->handshake_step) {
+ case LOWER_PROTO:
+ av_log(c, AV_LOG_TRACE, "Lower protocol\n");
+ if ((ret = ffurl_handshake(cl)))
+ return ret;
+ ch->handshake_step = READ_HEADERS;
+ break;
+ case READ_HEADERS:
+ av_log(c, AV_LOG_TRACE, "Read headers\n");
+ if (!ch->end_header) {
+ if ((err = http_read_header(c, &new_location)) < 0) {
+ handle_http_errors(c, err);
+ return err;
+ }
+ ch->handshake_step = FINISH;
+ }
+ break;
+ case FINISH:
+ if (ch->handshake_finish) {
+ av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
+ if ((err = http_write_reply(c, ch->reply_code)) < 0)
+ return err;
+ return 0;
}
}
+ return 1;
}
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;
- s->chunked_post = 1;
+ int port;
av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
NULL, 0, uri);
if (!strcmp(proto, "https"))
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;
-
+ s->handshake_step = LOWER_PROTO;
+ if (s->listen == HTTP_SINGLE) { /* single client */
+ s->reply_code = 200;
+ while ((ret = http_handshake(h)) > 0);
+ }
fail:
- handle_http_errors(h, ret);
av_dict_free(&s->chained_options);
return ret;
}
@@ -382,6 +496,26 @@ 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 = NULL;
+
+ av_assert0(sc->listen);
+ if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
+ goto fail;
+ cc = (*c)->priv_data;
+ if ((ret = ffurl_accept(sl, &cl)) < 0)
+ goto fail;
+ cc->hd = cl;
+ cc->is_multi_client = 1;
+fail:
+ return ret;
+}
+
static int http_getc(HTTPContext *s)
{
int len;
@@ -576,7 +710,7 @@ static int process_line(URLContext *h, char *line, int line_count,
p = line;
if (line_count == 0) {
- if (s->listen) {
+ if (s->listen || s->is_multi_client) {
// HTTP method
method = p;
while (!av_isspace(*p))
@@ -597,6 +731,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 +742,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 +1482,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