Skip to content

Commit

Permalink
fix http open and http_seek (redirect) authentication bug
Browse files Browse the repository at this point in the history
  • Loading branch information
debugly committed Nov 5, 2024
1 parent 5e8518c commit 4171062
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
From 471ccf6b139063fabacf243501ae0c69000bbe8e Mon Sep 17 00:00:00 2001
From: qianlongxu <qianlongxu@gmail.com>
Date: Tue, 5 Nov 2024 18:51:46 +0800
Subject: [PATCH 23] fix http open and http_seek (redirect) authentication bug

---
libavformat/http.c | 25 +++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index 06f1bdd..dc0ba67 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -78,6 +78,7 @@ typedef struct HTTPContext {
char *uri;
char *location;
HTTPAuthState auth_state;
+ int auth_type2;
HTTPAuthState proxy_auth_state;
char *http_proxy;
char *headers;
@@ -166,6 +167,7 @@ static const AVOption options[] = {
{ "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
{ "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
{ "auth_type", "HTTP authentication type", OFFSET(auth_state.auth_type), AV_OPT_TYPE_INT, { .i64 = HTTP_AUTH_NONE }, HTTP_AUTH_NONE, HTTP_AUTH_BASIC, D | E, "auth_type"},
+ { "auth_type2", "backup HTTP authentication type for seek request", OFFSET(auth_type2), AV_OPT_TYPE_INT, { .i64 = HTTP_AUTH_NONE }, HTTP_AUTH_NONE, HTTP_AUTH_BASIC, D | E, "auth_type"},
{ "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
{ "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, "auth_type"},
{ "send_expect_100", "Force sending an Expect: 100-continue header for POST", OFFSET(send_expect_100), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, E },
@@ -699,7 +701,10 @@ static int http_open(URLContext *h, const char *uri, int flags,
HTTPContext *s = h->priv_data;
int ret;
s->app_ctx = (AVApplicationContext *)av_dict_strtoptr(s->app_ctx_intptr);
-
+ if (s->auth_type2 == HTTP_AUTH_NONE) {
+ //backup the init auth_type, when not assign.
+ s->auth_type2 = s->auth_state.auth_type;
+ }
// av_log(NULL, AV_LOG_INFO, "%-*s: %-*s = %s\n", 12, "xql http_open verify", 28, "ijkapplication", s->app_ctx_intptr);
// av_log(NULL, AV_LOG_INFO, "%-*s: %-*s = %s\n", 12, "xql http_open verify", 28, "tcp_hook", s->tcp_hook);

@@ -1428,6 +1433,7 @@ static int http_connect(URLContext *h, const char *path, const char *local_path,
uint64_t off = s->off;
const char *method;
int send_expect_100 = 0;
+ int cur_auth_type = s->auth_state.auth_type;

av_bprint_init_for_buffer(&request, s->buffer, sizeof(s->buffer));

@@ -1569,9 +1575,19 @@ static int http_connect(URLContext *h, const char *path, const char *local_path,

if (s->new_location)
s->off = off;
-
err = (off == s->off) ? 0 : -1;
+
+ //in http_seek_internal func reverted to the original uri,but the s->off is not zero,so err is -1,cause can't goto the 401 authenticate logic.
+ if (err != 0 && cur_auth_type != s->auth_state.auth_type && s->http_code == 401) {
+ //reverte the off,otherwise can't seek the target position.
+ s->off = off;
+ av_log(NULL, AV_LOG_ERROR, "http 401 error,need authenticate,s->off:%llu\n", s->buffer, s->off);
+ err = 0;
+ }
done:
+ if (err != 0) {
+ av_log(NULL, AV_LOG_ERROR, "http error %s\n", s->buffer);
+ }
av_freep(&authstr);
av_freep(&proxyauthstr);
return err;
@@ -1961,6 +1977,8 @@ static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int fo
return s->off;
}

+ // http_seek use lasest redirect location, because after redirect, reset the auth_state: `memset(&s->auth_state, 0, sizeof(s->auth_state));`
+
/* if the location changed (redirect), revert to the original uri */
if (strcmp(s->uri, s->location)) {
char *new_uri;
@@ -1969,6 +1987,9 @@ static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int fo
return AVERROR(ENOMEM);
av_free(s->location);
s->location = new_uri;
+ if (s->auth_type2 != HTTP_AUTH_NONE) {
+ s->auth_state.auth_type = s->auth_type2;
+ }
}

/* we save the old context in case the seek fails */
--
2.39.5 (Apple Git-154)

This file was deleted.

0 comments on commit 4171062

Please sign in to comment.