]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
config: allow to override http user agent in the base config, fixes #3964
authorJaroslav Kysela <perex@perex.cz>
Mon, 12 Mar 2018 15:07:10 +0000 (16:07 +0100)
committerJaroslav Kysela <perex@perex.cz>
Mon, 12 Mar 2018 15:10:03 +0000 (16:10 +0100)
src/config.c
src/config.h
src/http.h
src/httpc.c
src/main.c

index 4ade4756ccc7deb571892b6b6df14b168639cda4..d19521035dee396a62f2b74583138ed45d8c9282 100644 (file)
@@ -1677,7 +1677,8 @@ config_check ( void )
 static int config_newcfg = 0;
 
 void
-config_boot ( const char *path, gid_t gid, uid_t uid )
+config_boot
+  ( const char *path, gid_t gid, uid_t uid, const char *http_user_agent )
 {
   struct stat st;
   char buf[1024];
@@ -1780,6 +1781,12 @@ config_boot ( const char *path, gid_t gid, uid_t uid )
     config.realm = strdup("tvheadend");
   if (tvh_str_default(config.http_server_name, NULL) == NULL)
     config.http_server_name = strdup("HTS/tvheadend");
+  if ((config.http_user_agent &&
+       strncmp(config.http_user_agent, "TVHeadend/", 10) == 0) ||
+      tvh_str_default(config.http_user_agent, NULL) == NULL) {
+    snprintf(buf, sizeof(buf), "TVHeadend/%s", tvheadend_version);
+    tvh_str_set(&config.http_user_agent, buf);
+  }
   if (!config_scanfile_ok)
     config_muxconfpath_notify(&config.idnode, NULL);
 }
@@ -1820,6 +1827,7 @@ void config_done ( void )
   free(config.wizard);
   free(config.full_version);
   free(config.http_server_name);
+  free(config.http_user_agent);
   free(config.server_name);
   free(config.language);
   free(config.language_ui);
@@ -2391,6 +2399,15 @@ const idclass_t config_class = {
       .opts   = PO_EXPERT,
       .group  = 5
     },
+    {
+      .type   = PT_STR,
+      .id     = "http_user_agent",
+      .name   = N_("HTTP User Agent"),
+      .desc   = N_("The user agent string for the build-in HTTP client."),
+      .off    = offsetof(config_t, http_user_agent),
+      .opts   = PO_HIDDEN | PO_EXPERT,
+      .group  = 6,
+    },
     {
       .type   = PT_INT,
       .id     = "iptv_tpool",
index 85f614de968a07a7395fb59c9a017d28b6001d7b..39680475d84462f66a13daf5f0aa222b0e63756b 100644 (file)
@@ -41,6 +41,7 @@ typedef struct config {
   char *full_version;
   char *server_name;
   char *http_server_name;
+  char *http_user_agent;
   char *language;
   char *info_area;
   int chname_num;
@@ -71,9 +72,10 @@ typedef struct config {
 extern const idclass_t config_class;
 extern config_t config;
 
-void        config_boot    ( const char *path, gid_t gid, uid_t uid );
-void        config_init    ( int backup );
-void        config_done    ( void );
+void config_boot
+  ( const char *path, gid_t gid, uid_t uid, const char *http_user_agent );
+void config_init( int backup );
+void config_done( void );
 
 const char *config_get_server_name ( void );
 const char *config_get_http_server_name ( void );
index 3e5b9ad18fd8d4d091aad203dba3bea0f89c46f9..c2a44a927ac89a2e394af71cadfe2531498daaa4 100644 (file)
@@ -431,7 +431,7 @@ struct http_client {
   void    (*hc_conn_closed)  (http_client_t *hc, int err);
 };
 
-void http_client_init ( const char *user_agent );
+void http_client_init ( void );
 void http_client_done ( void );
 
 http_client_t*
index 71be497dbb8f4b314369bf10bae407fd6bf3cb25..858b9970826c2d2398245516124cc711e333a90a 100644 (file)
@@ -20,6 +20,7 @@
 #include "tvheadend.h"
 #include "http.h"
 #include "tcp.h"
+#include "config.h"
 
 #include <pthread.h>
 #include <unistd.h>
@@ -44,6 +45,8 @@
 #include <sys/socket.h>
 #endif
 
+#define HTTPCLIENT_TESTSUITE 1
+
 struct http_client_ssl {
   int      connected;
   int      shutdown;
@@ -85,7 +88,6 @@ static TAILQ_HEAD(,http_client) http_clients;
 static pthread_mutex_t          http_lock;
 static tvh_cond_t               http_cond;
 static th_pipe_t                http_pipe;
-static char                    *http_user_agent;
 
 /*
  *
@@ -1216,12 +1218,8 @@ http_client_basic_args ( http_client_t *hc, http_arg_list_t *h, const url_t *url
                                         http_port(hc, url->scheme, url->port));
     http_arg_set(h, "Host", 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);
-  }
+  assert(config.http_user_agent);
+  http_arg_set(h, "User-Agent", config.http_user_agent);
   if (!keepalive)
     http_arg_set(h, "Connection", "close");
   http_client_basic_auth(hc, h, url->user, url->pass);
@@ -1659,10 +1657,8 @@ http_client_close ( http_client_t *hc )
 pthread_t http_client_tid;
 
 void
-http_client_init ( const char *user_agent )
+http_client_init ( void )
 {
-  http_user_agent = user_agent ? strdup(user_agent) : NULL;
-
   /* Setup list */
   pthread_mutex_init(&http_lock, NULL);
   tvh_cond_init(&http_cond);
@@ -1699,7 +1695,6 @@ http_client_done ( void )
   tvhpoll_destroy(http_poll);
   http_poll = NULL;
   pthread_mutex_unlock(&http_lock);
-  free(http_user_agent);
 }
 
 /*
index 01b27e92a27fb6066f4a235d74acf2901077e8ed..9083d4208ba76b5b9c67edc1d1c598a8d50f7643 100644 (file)
@@ -1094,7 +1094,7 @@ main(int argc, char **argv)
 
   uuid_init();
   idnode_boot();
-  config_boot(opt_config, gid, uid);
+  config_boot(opt_config, gid, uid, opt_user_agent);
   tcp_server_preinit(opt_ipv6);
   http_server_init(opt_bindaddr);    // bind to ports only
   htsp_init(opt_bindaddr);          // bind to ports only
@@ -1222,7 +1222,7 @@ main(int argc, char **argv)
   tvhftrace(LS_MAIN, codec_init);
   tvhftrace(LS_MAIN, profile_init);
   tvhftrace(LS_MAIN, imagecache_init);
-  tvhftrace(LS_MAIN, http_client_init, opt_user_agent);
+  tvhftrace(LS_MAIN, http_client_init);
   tvhftrace(LS_MAIN, esfilter_init);
   tvhftrace(LS_MAIN, bouquet_init);
   tvhftrace(LS_MAIN, service_init);