diff options
author | Michael Niedermayer <michael@niedermayer.cc> | 2015-08-01 03:00:01 +0200 |
---|---|---|
committer | Michael Niedermayer <michael@niedermayer.cc> | 2015-08-01 03:08:04 +0200 |
commit | 55ea31ab8981716a3ba1d2ae5521e62ff4494791 (patch) | |
tree | 32357f472fffb499f40cc28861881c1d425daf87 /libavformat | |
parent | 6d175158e914f042d4dc3a67688456cd0b9d8c62 (diff) | |
parent | 25410c524ddee98c579c7a9ddaf0535278a6dae0 (diff) | |
download | ffmpeg-55ea31ab8981716a3ba1d2ae5521e62ff4494791.tar.gz |
Merge branch 'simple_http_server_api_implementation' of http://git.klaxa.eu/git/ffmpeg
Reviewed-by: Nicolas George <george@nsup.org>
Merged-by: Michael Niedermayer <michael@niedermayer.cc>
Diffstat (limited to 'libavformat')
-rw-r--r-- | libavformat/avio.c | 20 | ||||
-rw-r--r-- | libavformat/avio.h | 29 | ||||
-rw-r--r-- | libavformat/aviobuf.c | 17 | ||||
-rw-r--r-- | libavformat/http.c | 182 | ||||
-rw-r--r-- | libavformat/network.c | 27 | ||||
-rw-r--r-- | libavformat/network.h | 20 | ||||
-rw-r--r-- | libavformat/tcp.c | 33 | ||||
-rw-r--r-- | libavformat/url.h | 25 |
8 files changed, 318 insertions, 35 deletions
diff --git a/libavformat/avio.c b/libavformat/avio.c index c188adcce1..21713d9d5e 100644 --- a/libavformat/avio.c +++ b/libavformat/avio.c @@ -211,6 +211,26 @@ int ffurl_connect(URLContext *uc, AVDictionary **options) return 0; } +int ffurl_accept(URLContext *s, URLContext **c) +{ + av_assert0(!*c); + if (s->prot->url_accept) + return s->prot->url_accept(s, c); + return AVERROR(EBADF); +} + +int ffurl_handshake(URLContext *c) +{ + int ret; + if (c->prot->url_handshake) { + ret = c->prot->url_handshake(c); + if (ret) + return ret; + } + c->is_connected = 1; + return 0; +} + #define URL_SCHEME_CHARS \ "abcdefghijklmnopqrstuvwxyz" \ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ diff --git a/libavformat/avio.h b/libavformat/avio.h index 9b8fcce3eb..06dd7f5ef8 100644 --- a/libavformat/avio.h +++ b/libavformat/avio.h @@ -648,4 +648,33 @@ struct AVBPrint; */ int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size); +/** + * Accept and allocate a client context on a server context. + * @param s the server context + * @param c the client context, must be unallocated + * @return >= 0 on success or a negative value corresponding + * to an AVERROR on failure + */ +int avio_accept(AVIOContext *s, AVIOContext **c); + +/** + * Perform one step of the protocol handshake to accept a new client. + * This function must be called on a client returned by avio_accept() before + * using it as a read/write context. + * It is separate from avio_accept() because it may block. + * A step of the handshake is defined by places where the application may + * decide to change the proceedings. + * For example, on a protocol with a request header and a reply header, each + * one can constitute a step because the application may use the parameters + * from the request to change parameters in the reply; or each individual + * chunk of the request can constitute a step. + * If the handshake is already finished, avio_handshake() does nothing and + * returns 0 immediately. + * + * @param c the client context to perform the handshake on + * @return 0 on a complete and successful handshake + * > 0 if the handshake progressed, but is not complete + * < 0 for an AVERROR code + */ +int avio_handshake(AVIOContext *c); #endif /* AVFORMAT_AVIO_H */ diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index aceb3bfcb7..1b3d5f5b80 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1021,6 +1021,23 @@ int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size) return 0; } +int avio_accept(AVIOContext *s, AVIOContext **c) +{ + int ret; + URLContext *sc = s->opaque; + URLContext *cc = NULL; + ret = ffurl_accept(sc, &cc); + if (ret < 0) + return ret; + return ffio_fdopen(c, cc); +} + +int avio_handshake(AVIOContext *c) +{ + URLContext *cc = c->opaque; + return ffurl_handshake(cc); +} + /* output in a dynamic buffer */ typedef struct DynBuffer { diff --git a/libavformat/http.c b/libavformat/http.c index 684058147a..1eb716b107 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,14 @@ * path names). */ #define BUFFER_SIZE MAX_URL_SIZE #define MAX_REDIRECTS 8 +#define HTTP_SINGLE 1 +#define HTTP_MUTLI 2 +typedef enum { + LOWER_PROTO, + READ_HEADERS, + WRITE_REPLY_HEADERS, + FINISH +}HandshakeState; typedef struct HTTPContext { const AVClass *class; @@ -97,6 +106,11 @@ typedef struct HTTPContext { char *method; int reconnect; int listen; + char *resource; + int reply_code; + int is_multi_client; + HandshakeState handshake_step; + int is_connected_server; } HTTPContext; #define OFFSET(x) offsetof(HTTPContext, x) @@ -128,7 +142,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, 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}, { NULL } }; @@ -299,51 +315,143 @@ int ff_http_averror(int status_code, int default_averror) return default_averror; } -static void handle_http_errors(URLContext *h, int error) +static int http_write_reply(URLContext* h, int status_code) { - 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"; + int ret, body = 0, reply_code, message_len; + const char *reply_text, *content_type; 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)); + char message[BUFFER_SIZE]; + content_type = "text/plain"; + + 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 = "application/octet-stream"; + 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; + message_len = snprintf(message, sizeof(message), + "HTTP/1.1 %03d %s\r\n" + "Content-Type: %s\r\n" + "Content-Length: %zu\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; + message_len = 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, "HTTP reply header: \n%s----\n", message); + if ((ret = ffurl_write(s->hd, message, message_len)) < 0) + return ret; + return 0; +} + +static void handle_http_errors(URLContext *h, int error) +{ + av_assert0(error < 0); + http_write_reply(h, 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) > 0)) + return 2 + ret; + if ((ret < 0)) + return ret; + ch->handshake_step = READ_HEADERS; + ch->is_connected_server = 1; + return 2; + case READ_HEADERS: + av_log(c, AV_LOG_TRACE, "Read headers\n"); + if ((err = http_read_header(c, &new_location)) < 0) { + handle_http_errors(c, err); + return err; } + ch->handshake_step = WRITE_REPLY_HEADERS; + return 1; + case WRITE_REPLY_HEADERS: + 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; + ch->handshake_step = FINISH; + return 1; + case FINISH: + return 0; } + // this should never be reached. + return AVERROR(EINVAL); } 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; } @@ -389,6 +497,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; @@ -583,7 +711,7 @@ static int process_line(URLContext *h, char *line, int line_count, p = line; if (line_count == 0) { - if (s->listen) { + if (s->is_connected_server) { // HTTP method method = p; while (!av_isspace(*p)) @@ -604,6 +732,8 @@ 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)); } + if (!(s->method = av_strdup(method))) + return AVERROR(ENOMEM); } // HTTP resource @@ -614,6 +744,8 @@ 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); + if (!(s->resource = av_strdup(resource))) + return AVERROR(ENOMEM); // HTTP version while (av_isspace(*p)) @@ -1353,6 +1485,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, diff --git a/libavformat/network.c b/libavformat/network.c index 47ade8cb62..7a326d2c4e 100644 --- a/libavformat/network.c +++ b/libavformat/network.c @@ -187,12 +187,11 @@ int ff_socket(int af, int type, int proto) return fd; } -int ff_listen_bind(int fd, const struct sockaddr *addr, - socklen_t addrlen, int timeout, URLContext *h) +int ff_listen(int fd, const struct sockaddr *addr, + socklen_t addrlen) { int ret; int reuse = 1; - struct pollfd lp = { fd, POLLIN, 0 }; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) { av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n"); } @@ -203,6 +202,13 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, ret = listen(fd, 1); if (ret) return ff_neterrno(); + return ret; +} + +int ff_accept(int fd, int timeout, URLContext *h) +{ + int ret; + struct pollfd lp = { fd, POLLIN, 0 }; ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback); if (ret < 0) @@ -211,15 +217,24 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, ret = accept(fd, NULL, NULL); if (ret < 0) return ff_neterrno(); - - closesocket(fd); - if (ff_socket_nonblock(ret, 1) < 0) av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n"); return ret; } +int ff_listen_bind(int fd, const struct sockaddr *addr, + socklen_t addrlen, int timeout, URLContext *h) +{ + int ret; + if ((ret = ff_listen(fd, addr, addrlen)) < 0) + return ret; + if ((ret = ff_accept(fd, timeout, h)) < 0) + return ret; + closesocket(fd); + return ret; +} + int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next) diff --git a/libavformat/network.h b/libavformat/network.h index 86fb656164..f83c796a95 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -255,6 +255,26 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, URLContext *h); /** + * Bind to a file descriptor to an address without accepting connections. + * @param fd First argument of bind(). + * @param addr Second argument of bind(). + * @param addrlen Third argument of bind(). + * @return 0 on success or an AVERROR on failure. + */ +int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen); + +/** + * Poll for a single connection on the passed file descriptor. + * @param fd The listening socket file descriptor. + * @param timeout Polling timeout in milliseconds. + * @param h URLContext providing interrupt check + * callback and logging context. + * @return A non-blocking file descriptor on success + * or an AVERROR on failure. + */ +int ff_accept(int fd, int timeout, URLContext *h); + +/** * Connect to a file descriptor and poll for result. * * @param fd First argument of connect(), diff --git a/libavformat/tcp.c b/libavformat/tcp.c index f24cad2080..bee349ecd5 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "avformat.h" +#include "libavutil/avassert.h" #include "libavutil/parseutils.h" #include "libavutil/opt.h" #include "libavutil/time.h" @@ -44,7 +45,7 @@ typedef struct TCPContext { #define D AV_OPT_FLAG_DECODING_PARAM #define E AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { - { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E }, + { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E }, { "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { NULL } @@ -125,12 +126,17 @@ static int tcp_open(URLContext *h, const char *uri, int flags) goto fail; } - if (s->listen) { - if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, - s->listen_timeout, h)) < 0) { + if (s->listen == 2) { + // multi-client + if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0) + goto fail1; + } else if (s->listen == 1) { + // single client + if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, + s->listen_timeout, h)) < 0) { + ret = fd; goto fail1; } - fd = ret; } else { if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) { @@ -163,6 +169,22 @@ static int tcp_open(URLContext *h, const char *uri, int flags) return ret; } +static int tcp_accept(URLContext *s, URLContext **c) +{ + TCPContext *sc = s->priv_data; + TCPContext *cc; + int ret; + av_assert0(sc->listen); + if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0) + return ret; + cc = (*c)->priv_data; + ret = ff_accept(sc->fd, sc->listen_timeout, s); + if (ret < 0) + return ff_neterrno(); + cc->fd = ret; + return 0; +} + static int tcp_read(URLContext *h, uint8_t *buf, int size) { TCPContext *s = h->priv_data; @@ -223,6 +245,7 @@ static int tcp_get_file_handle(URLContext *h) URLProtocol ff_tcp_protocol = { .name = "tcp", .url_open = tcp_open, + .url_accept = tcp_accept, .url_read = tcp_read, .url_write = tcp_write, .url_close = tcp_close, diff --git a/libavformat/url.h b/libavformat/url.h index 99a3201cf6..391e3bca2a 100644 --- a/libavformat/url.h +++ b/libavformat/url.h @@ -58,6 +58,8 @@ typedef struct URLProtocol { * for those nested protocols. */ int (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options); + int (*url_accept)(URLContext *s, URLContext **c); + int (*url_handshake)(URLContext *c); /** * Read data from the protocol. @@ -140,6 +142,29 @@ int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options); /** + * Accept an URLContext c on an URLContext s + * + * @param s server context + * @param c client context, must be unallocated. + * @return >= 0 on success, ff_neterrno() on failure. + */ +int ffurl_accept(URLContext *s, URLContext **c); + +/** + * Perform one step of the protocol handshake to accept a new client. + * See avio_handshake() for details. + * Implementations should try to return decreasing values. + * If the protocol uses an underlying protocol, the underlying handshake is + * usually the first step, and the return value can be: + * (largest value for this protocol) + (return value from other protocol) + * + * @param c the client context + * @return >= 0 on success or a negative value corresponding + * to an AVERROR code on failure + */ +int ffurl_handshake(URLContext *c); + +/** * Read up to size bytes from the resource accessed by h, and store * the read bytes in buf. * |