From a78c1c7bf7a8a23a73799d4513a0cc09fdf598b0 Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Thu, 19 Jun 2014 00:18:56 +0200 Subject: [PATCH] httpc: Add --useragent command line argument to set a custom User-Agent header --- src/http.h | 2 +- src/httpc.c | 14 +++++++++++--- src/main.c | 7 +++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/http.h b/src/http.h index b67cb9a5..d80901ec 100644 --- a/src/http.h +++ b/src/http.h @@ -289,7 +289,7 @@ struct http_client { void (*hc_conn_closed) (http_client_t *hc, int err); }; -void http_client_init ( void ); +void http_client_init ( const char *user_agent ); void http_client_done ( void ); http_client_t* diff --git a/src/httpc.c b/src/httpc.c index 2c6d34ac..864c7a64 100644 --- a/src/httpc.c +++ b/src/httpc.c @@ -78,6 +78,7 @@ static TAILQ_HEAD(,http_client) http_clients; static pthread_mutex_t http_lock; static pthread_cond_t http_cond; static th_pipe_t http_pipe; +static char *http_user_agent; /* * @@ -998,8 +999,12 @@ http_client_basic_args ( http_arg_list_t *h, const url_t *url, int keepalive ) snprintf(buf, sizeof(buf), "%s:%u", url->host, http_port(url->scheme, url->port)); http_arg_set(h, "Host", buf); - snprintf(buf, sizeof(buf), "TVHeadend/%s", tvheadend_version); - http_arg_set(h, "User-Agent", buf); + if (http_user_agent) { + http_arg_set(h, "User-Agent", http_user_agent); + } else { + snprintf(buf, sizeof(buf), "TVHeadend/%s", tvheadend_version); + http_arg_set(h, "User-Agent", buf); + } if (!keepalive) http_arg_set(h, "Connection", "close"); if (url->user && url->user[0] && url->pass && url->pass[0]) { @@ -1345,10 +1350,12 @@ http_client_close ( http_client_t *hc ) pthread_t http_client_tid; void -http_client_init ( void ) +http_client_init ( const char *user_agent ) { tvhpoll_event_t ev; + http_user_agent = user_agent ? strdup(user_agent) : NULL; + /* Setup list */ pthread_mutex_init(&http_lock, NULL); pthread_cond_init(&http_cond, NULL); @@ -1382,6 +1389,7 @@ http_client_done ( void ) assert(TAILQ_FIRST(&http_clients) == NULL); tvh_pipe_close(&http_pipe); tvhpoll_destroy(http_poll); + free(http_user_agent); } /* diff --git a/src/main.c b/src/main.c index 7b73323a..caa22411 100644 --- a/src/main.c +++ b/src/main.c @@ -481,7 +481,8 @@ main(int argc, char **argv) *opt_dvb_adapters = NULL, #endif *opt_bindaddr = NULL, - *opt_subscribe = NULL; + *opt_subscribe = NULL, + *opt_user_agent = NULL; str_list_t opt_satip_xml = { .max = 10, .num = 0, .str = calloc(10, sizeof(char*)) }; str_list_t opt_tsfile = { .max = 10, .num = 0, .str = calloc(10, sizeof(char*)) }; cmdline_opt_t cmdline_opts[] = { @@ -520,6 +521,8 @@ main(int argc, char **argv) OPT_INT, &tvheadend_htsp_port }, { 0, "htsp_port2", "Specify extra htsp port", OPT_INT, &tvheadend_htsp_port_extra }, + { 0, "useragent", "Specify User-Agent header for the http client", + OPT_STR, &opt_user_agent }, { 0, NULL, "Debug Options", OPT_BOOL, NULL }, { 'd', "stderr", "Enable debug on stderr", OPT_BOOL, &opt_stderr }, @@ -774,7 +777,7 @@ main(int argc, char **argv) imagecache_init(); - http_client_init(); + http_client_init(opt_user_agent); esfilter_init(); service_init();