[FFmpeg-devel] [PATCH] http: add user_agent option.

Clément Bœsch ubitux at gmail.com
Wed Sep 28 21:07:12 CEST 2011


---
Hi :)

I'm not able to actually test this patch; the option parsing looks broken (or
I'm doing something wrong):

./ffplay -user-agent
→ missing argument, ok

./ffplay -user-agent foobar
→ Unrecognized option 'user-agent'
  Failed to set value 'foo' for option 'user-agent'

  Why unrecognized here?

./ffplay -user-agent foobar http://example.org
→ Unrecognized option 'user-agent'
  Failed to set value 'foobar' for option 'user-agent'

  Doesn't seem to help.

Setting -user-agent foobar after the URL doesn't raise an error but doesn't
actually set the option.

Any hint?
---
 libavformat/http.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index 751a202..2e6505d 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -45,6 +45,7 @@ typedef struct {
     int line_count;
     int http_code;
     int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
+    char *user_agent;
     int64_t off, filesize;
     char location[MAX_URL_SIZE];
     HTTPAuthState auth_state;
@@ -53,8 +54,10 @@ typedef struct {
 } HTTPContext;
 
 #define OFFSET(x) offsetof(HTTPContext, x)
+#define DEC AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
 {"chunksize", "use chunked transfer-encoding for posts, -1 disables it, 0 enables it", OFFSET(chunksize), FF_OPT_TYPE_INT64, {.dbl = 0}, -1, 0 }, /* Default to 0, for chunked POSTs */
+{"user-agent", "override User-Agent header", OFFSET(user_agent), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
 {NULL}
 };
 static const AVClass httpcontext_class = {
@@ -307,8 +310,9 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
 
     /* set default headers if needed */
     if (!has_header(s->headers, "\r\nUser-Agent: "))
-       len += av_strlcatf(headers + len, sizeof(headers) - len,
-                          "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
+        len += av_strlcatf(headers + len, sizeof(headers) - len,
+                           "User-Agent: %s\r\n",
+                           s->user_agent ? s->user_agent : LIBAVFORMAT_IDENT);
     if (!has_header(s->headers, "\r\nAccept: "))
         len += av_strlcpy(headers + len, "Accept: */*\r\n",
                           sizeof(headers) - len);
-- 
1.7.6.4



More information about the ffmpeg-devel mailing list