From 5c742005fb7854dcfaa9f0efb65fd36a63ceaa2b Mon Sep 17 00:00:00 2001 From: Oka Motofumi Date: Thu, 31 May 2012 02:14:43 +0900 Subject: [PATCH 1/6] avisynth: Make sure the filename passed to avisynth is in the right code page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit avisynth is a non-unicode application and cannot accept UTF-8 characters. Therefore, the input filename should be converted to the correct code page that it expects. Signed-off-by: Martin Storsjö --- libavformat/avisynth.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c index 3b695a9a0a..c4c523dc8a 100644 --- a/libavformat/avisynth.c +++ b/libavformat/avisynth.c @@ -49,10 +49,15 @@ static int avisynth_read_header(AVFormatContext *s) DWORD id; AVStream *st; AVISynthStream *stream; + wchar_t filename_wchar[1024] = { 0 }; + char filename_char[1024] = { 0 }; AVIFileInit(); - res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL); + /* avisynth can't accept UTF-8 filename */ + MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wchar, 1024); + WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wchar, -1, filename_char, 1024, NULL, NULL); + res = AVIFileOpen(&avs->file, filename_char, OF_READ|OF_SHARE_DENY_WRITE, NULL); if (res != S_OK) { av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res); From 641f4a885f141b8350076c9293ebd971dc984347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Wed, 30 May 2012 17:35:05 +0300 Subject: [PATCH 2/6] tcp: Check the return values from bind and accept MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- libavformat/tcp.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index 37f74f6e2f..e249e4e830 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -84,8 +84,16 @@ static int tcp_open(URLContext *h, const char *uri, int flags) if (listen_socket) { int fd1; ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen); + if (ret) { + ret = ff_neterrno(); + goto fail1; + } listen(fd, 1); fd1 = accept(fd, NULL, NULL); + if (fd1 < 0) { + ret = ff_neterrno(); + goto fail1; + } closesocket(fd); fd = fd1; ff_socket_nonblock(fd, 1); From b7c3772be855f95f0e1903daf48c4c0246d680fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Wed, 30 May 2012 17:37:51 +0300 Subject: [PATCH 3/6] tcp: Try enabling SO_REUSEADDR when listening MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- libavformat/tcp.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index e249e4e830..a6eeeb0d0f 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -83,6 +83,8 @@ static int tcp_open(URLContext *h, const char *uri, int flags) if (listen_socket) { int fd1; + int reuse = 1; + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen); if (ret) { ret = ff_neterrno(); From 3cbcfa2decdbe2b223f7f878b03a5f33a41a22f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 1 Jun 2012 16:24:47 +0300 Subject: [PATCH 4/6] http: Clear the old URLContext pointer when closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes issues with opening http urls that have authentication or redirects, introduced in commit e999b641. Signed-off-by: Martin Storsjö --- libavformat/http.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libavformat/http.c b/libavformat/http.c index 22600b4e4a..61266dfb01 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -161,6 +161,7 @@ static int http_open_cnx(URLContext *h) if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) && s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) { ffurl_close(hd); + s->hd = hd = NULL; goto redo; } else goto fail; @@ -169,6 +170,7 @@ static int http_open_cnx(URLContext *h) if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) && s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) { ffurl_close(hd); + s->hd = hd = NULL; goto redo; } else goto fail; @@ -177,6 +179,7 @@ static int http_open_cnx(URLContext *h) && location_changed == 1) { /* url moved, get next */ ffurl_close(hd); + s->hd = hd = NULL; if (redirects++ >= MAX_REDIRECTS) return AVERROR(EIO); /* Restart the authentication process with the new target, which From 5952564185ecb8495bac0c8ea74e1452da6429ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 1 Jun 2012 16:30:01 +0300 Subject: [PATCH 5/6] http: Simplify code by removing a local variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- libavformat/http.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/libavformat/http.c b/libavformat/http.c index 61266dfb01..6c6ff5cf81 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -103,7 +103,6 @@ static int http_open_cnx(URLContext *h) int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0; HTTPAuthType cur_auth_type, cur_proxy_auth_type; HTTPContext *s = h->priv_data; - URLContext *hd = NULL; proxy_path = getenv("http_proxy"); use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && @@ -144,12 +143,10 @@ static int http_open_cnx(URLContext *h) ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL); if (!s->hd) { - err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE, + err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE, &h->interrupt_callback, NULL); if (err < 0) goto fail; - - s->hd = hd; } cur_auth_type = s->auth_state.auth_type; @@ -160,8 +157,8 @@ static int http_open_cnx(URLContext *h) if (s->http_code == 401) { if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) && s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) { - ffurl_close(hd); - s->hd = hd = NULL; + ffurl_close(s->hd); + s->hd = NULL; goto redo; } else goto fail; @@ -169,8 +166,8 @@ static int http_open_cnx(URLContext *h) if (s->http_code == 407) { if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) && s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) { - ffurl_close(hd); - s->hd = hd = NULL; + ffurl_close(s->hd); + s->hd = NULL; goto redo; } else goto fail; @@ -178,8 +175,8 @@ static int http_open_cnx(URLContext *h) if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307) && location_changed == 1) { /* url moved, get next */ - ffurl_close(hd); - s->hd = hd = NULL; + ffurl_close(s->hd); + s->hd = NULL; if (redirects++ >= MAX_REDIRECTS) return AVERROR(EIO); /* Restart the authentication process with the new target, which @@ -191,8 +188,8 @@ static int http_open_cnx(URLContext *h) } return 0; fail: - if (hd) - ffurl_close(hd); + if (s->hd) + ffurl_close(s->hd); s->hd = NULL; return AVERROR(EIO); } From dbaf79c9d7270eafd2479d9c650efa1433d65efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 1 Jun 2012 16:36:20 +0300 Subject: [PATCH 6/6] http: Add the url_shutdown function for https, too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- libavformat/http.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libavformat/http.c b/libavformat/http.c index 6c6ff5cf81..b2f2ea97f2 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -669,6 +669,7 @@ URLProtocol ff_https_protocol = { .url_seek = http_seek, .url_close = http_close, .url_get_file_handle = http_get_file_handle, + .url_shutdown = http_shutdown, .priv_data_size = sizeof(HTTPContext), .priv_data_class = &https_context_class, .flags = URL_PROTOCOL_FLAG_NETWORK,