[4/8] lavf/http: add support for setting custom User-Agent

Message ID 1394118691-19478-5-git-send-email-alessandro@ghedini.me
State New
Headers show

Commit Message

Alessandro Ghedini March 6, 2014, 3:11 p.m.
Original ffmpeg commit 2bb1c71 by Clément Bœsch.
---
 doc/protocols.texi | 4 ++++
 libavformat/http.c | 7 +++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

Comments

Luca Barbato March 6, 2014, 3:21 p.m. | #1
On 06/03/14 16:11, Alessandro Ghedini wrote:
> Original ffmpeg commit 2bb1c71 by Clément Bœsch.
> ---
>  doc/protocols.texi | 4 ++++
>  libavformat/http.c | 7 +++++--
>  2 files changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/doc/protocols.texi b/doc/protocols.texi
> index 9c32e9b..c0785d9 100644
> --- a/doc/protocols.texi
> +++ b/doc/protocols.texi
> @@ -92,6 +92,10 @@ HTTP (Hyper Text Transfer Protocol).
>  This protocol accepts the following options:
>  
>  @table @option
> +@item user-agent
> +Override the User-Agent header. If not specified the protocol will use a
> +string of the form "Lavf/<version>".
> +
>  @item mime_type
>  Set MIME type.
>  
> diff --git a/libavformat/http.c b/libavformat/http.c
> index 01a9041..af5ba9a 100644
> --- a/libavformat/http.c
> +++ b/libavformat/http.c
> @@ -51,6 +51,7 @@ typedef struct {
>      int http_code;
>      int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
>      int64_t off, filesize;
> +    char *user_agent;
>      int icy_data_read;      ///< how much data was read since last ICY metadata packet
>      int icy_metaint;        ///< after how many bytes of read data a new metadata packet will be found
>      char *location;
> @@ -80,9 +81,11 @@ typedef struct {
>  #define OFFSET(x) offsetof(HTTPContext, x)
>  #define D AV_OPT_FLAG_DECODING_PARAM
>  #define E AV_OPT_FLAG_ENCODING_PARAM
> +#define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
>  static const AVOption options[] = {
>  {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
>  {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
> +{"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D },

traditionally we use _ as separator for the options. Probably we could
maintain an alias to be compatible.

lu
Alessandro Ghedini March 6, 2014, 4:59 p.m. | #2
On gio, mar 06, 2014 at 04:21:04 +0100, Luca Barbato wrote:
> On 06/03/14 16:11, Alessandro Ghedini wrote:
> > Original ffmpeg commit 2bb1c71 by Clément Bœsch.
> > ---
> >  doc/protocols.texi | 4 ++++
> >  libavformat/http.c | 7 +++++--
> >  2 files changed, 9 insertions(+), 2 deletions(-)
> > 
> > diff --git a/doc/protocols.texi b/doc/protocols.texi
> > index 9c32e9b..c0785d9 100644
> > --- a/doc/protocols.texi
> > +++ b/doc/protocols.texi
> > @@ -92,6 +92,10 @@ HTTP (Hyper Text Transfer Protocol).
> >  This protocol accepts the following options:
> >  
> >  @table @option
> > +@item user-agent
> > +Override the User-Agent header. If not specified the protocol will use a
> > +string of the form "Lavf/<version>".
> > +
> >  @item mime_type
> >  Set MIME type.
> >  
> > diff --git a/libavformat/http.c b/libavformat/http.c
> > index 01a9041..af5ba9a 100644
> > --- a/libavformat/http.c
> > +++ b/libavformat/http.c
> > @@ -51,6 +51,7 @@ typedef struct {
> >      int http_code;
> >      int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
> >      int64_t off, filesize;
> > +    char *user_agent;
> >      int icy_data_read;      ///< how much data was read since last ICY metadata packet
> >      int icy_metaint;        ///< after how many bytes of read data a new metadata packet will be found
> >      char *location;
> > @@ -80,9 +81,11 @@ typedef struct {
> >  #define OFFSET(x) offsetof(HTTPContext, x)
> >  #define D AV_OPT_FLAG_DECODING_PARAM
> >  #define E AV_OPT_FLAG_ENCODING_PARAM
> > +#define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
> >  static const AVOption options[] = {
> >  {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
> >  {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
> > +{"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D },
> 
> traditionally we use _ as separator for the options. Probably we could
> maintain an alias to be compatible.

Ok. I added a new user_agent option and added "for compatibility with ffmpeg"
to the description of user-agent. Is this ok, or is there a better way to do
this?
Luca Barbato March 6, 2014, 5:02 p.m. | #3
On 06/03/14 17:59, Alessandro Ghedini wrote:
> On gio, mar 06, 2014 at 04:21:04 +0100, Luca Barbato wrote:
>> On 06/03/14 16:11, Alessandro Ghedini wrote:
>>> Original ffmpeg commit 2bb1c71 by Clément Bœsch.
>>> ---
>>>  doc/protocols.texi | 4 ++++
>>>  libavformat/http.c | 7 +++++--
>>>  2 files changed, 9 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/doc/protocols.texi b/doc/protocols.texi
>>> index 9c32e9b..c0785d9 100644
>>> --- a/doc/protocols.texi
>>> +++ b/doc/protocols.texi
>>> @@ -92,6 +92,10 @@ HTTP (Hyper Text Transfer Protocol).
>>>  This protocol accepts the following options:
>>>  
>>>  @table @option
>>> +@item user-agent
>>> +Override the User-Agent header. If not specified the protocol will use a
>>> +string of the form "Lavf/<version>".
>>> +
>>>  @item mime_type
>>>  Set MIME type.
>>>  
>>> diff --git a/libavformat/http.c b/libavformat/http.c
>>> index 01a9041..af5ba9a 100644
>>> --- a/libavformat/http.c
>>> +++ b/libavformat/http.c
>>> @@ -51,6 +51,7 @@ typedef struct {
>>>      int http_code;
>>>      int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
>>>      int64_t off, filesize;
>>> +    char *user_agent;
>>>      int icy_data_read;      ///< how much data was read since last ICY metadata packet
>>>      int icy_metaint;        ///< after how many bytes of read data a new metadata packet will be found
>>>      char *location;
>>> @@ -80,9 +81,11 @@ typedef struct {
>>>  #define OFFSET(x) offsetof(HTTPContext, x)
>>>  #define D AV_OPT_FLAG_DECODING_PARAM
>>>  #define E AV_OPT_FLAG_ENCODING_PARAM
>>> +#define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
>>>  static const AVOption options[] = {
>>>  {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
>>>  {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
>>> +{"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D },
>>
>> traditionally we use _ as separator for the options. Probably we could
>> maintain an alias to be compatible.
> 
> Ok. I added a new user_agent option and added "for compatibility with ffmpeg"
> to the description of user-agent. Is this ok, or is there a better way to do
> this?

I guess it is the best we can do, thanks a lot again.

lu

Patch

diff --git a/doc/protocols.texi b/doc/protocols.texi
index 9c32e9b..c0785d9 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -92,6 +92,10 @@  HTTP (Hyper Text Transfer Protocol).
 This protocol accepts the following options:
 
 @table @option
+@item user-agent
+Override the User-Agent header. If not specified the protocol will use a
+string of the form "Lavf/<version>".
+
 @item mime_type
 Set MIME type.
 
diff --git a/libavformat/http.c b/libavformat/http.c
index 01a9041..af5ba9a 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -51,6 +51,7 @@  typedef struct {
     int http_code;
     int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
     int64_t off, filesize;
+    char *user_agent;
     int icy_data_read;      ///< how much data was read since last ICY metadata packet
     int icy_metaint;        ///< after how many bytes of read data a new metadata packet will be found
     char *location;
@@ -80,9 +81,11 @@  typedef struct {
 #define OFFSET(x) offsetof(HTTPContext, x)
 #define D AV_OPT_FLAG_DECODING_PARAM
 #define E AV_OPT_FLAG_ENCODING_PARAM
+#define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
 static const AVOption options[] = {
 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
 {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
+{"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D },
 {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
 {"post_data", "custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E },
 {"mime_type", "set MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
@@ -505,8 +508,8 @@  static int http_connect(URLContext *h, const char *path, const char *local_path,
 
     /* 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);
     if (!has_header(s->headers, "\r\nAccept: "))
         len += av_strlcpy(headers + len, "Accept: */*\r\n",
                           sizeof(headers) - len);