httpc: Add --useragent command line argument to set a custom User-Agent header

This commit is contained in:
Jaroslav Kysela 2014-06-19 00:18:56 +02:00
parent b1bdbf4644
commit a78c1c7bf7
3 changed files with 17 additions and 6 deletions

View file

@ -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*

View file

@ -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);
}
/*

View file

@ -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();