Allow to set user defined http headers (#45).
authorDaniel Carl <danielcarl@gmx.de>
Mon, 21 Oct 2013 20:13:05 +0000 (22:13 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 21 Oct 2013 20:13:05 +0000 (22:13 +0200)
src/main.h
src/session.c
src/setting.c

index 5fe382a..865d246 100644 (file)
@@ -272,14 +272,15 @@ typedef struct {
 } State;
 
 typedef struct {
-    time_t   cookie_timeout;
-    int      scrollstep;
-    char     *home_page;
-    char     *download_dir;
-    guint    history_max;
-    char     *editor_command;
-    guint    timeoutlen;      /* timeout for ambiguous mappings */
-    gboolean strict_focus;
+    time_t     cookie_timeout;
+    int        scrollstep;
+    char       *home_page;
+    char       *download_dir;
+    guint      history_max;
+    char       *editor_command;
+    guint      timeoutlen;      /* timeout for ambiguous mappings */
+    gboolean   strict_focus;
+    GHashTable *headers;        /* holds user defined header appended to requests */
 } Config;
 
 typedef struct {
index e63e75b..7c3516c 100644 (file)
@@ -48,6 +48,7 @@ static void cookiejar_init(CookieJar *self);
 static void cookiejar_set_property(GObject *self, guint prop_id,
     const GValue *value, GParamSpec *pspec);
 #endif
+static void request_started_cb(SoupSession *session, SoupMessage *msg, gpointer data);
 
 extern VbCore vb;
 
@@ -60,6 +61,10 @@ void session_init(void)
     g_object_set(vb.session, "max-conns-per-host", SETTING_MAX_CONNS_PER_HOST, NULL);
     g_object_set(vb.session, "accept-language-auto", true, NULL);
 
+    g_signal_connect(
+        vb.session, "request-started",
+        G_CALLBACK(request_started_cb), NULL
+    );
 #ifdef FEATURE_COOKIE
     soup_session_add_feature(
         vb.session,
@@ -117,3 +122,23 @@ static void cookiejar_set_property(GObject *self, guint prop_id, const
     flock(COOKIEJAR(self)->lock, LOCK_UN);
 }
 #endif
+
+static void request_started_cb(SoupSession *session, SoupMessage *msg, gpointer data)
+{
+    GHashTableIter iter;
+    char *name, *value;
+
+    if (!vb.config.headers) {
+        return;
+    }
+
+    g_hash_table_iter_init(&iter, vb.config.headers);
+    while (g_hash_table_iter_next(&iter, (gpointer*)&name, (gpointer*)&value)) {
+        /* allow to remove header with null value */
+        if (value == NULL) {
+            soup_message_headers_remove(msg->request_headers, name);
+        } else {
+            soup_message_headers_replace(msg->request_headers, name, value);
+        }
+    }
+}
index 6ebacc4..444976d 100644 (file)
@@ -46,6 +46,7 @@ static gboolean user_style(const Setting *s, const SettingType type);
 static gboolean history_max_items(const Setting *s, const SettingType type);
 static gboolean editor_command(const Setting *s, const SettingType type);
 static gboolean timeoutlen(const Setting *s, const SettingType type);
+static gboolean headers(const Setting *s, const SettingType type);
 
 static Setting default_settings[] = {
     /* webkit settings */
@@ -109,6 +110,7 @@ static Setting default_settings[] = {
     {NULL, "download-path", TYPE_CHAR, download_path, {0}},
     {NULL, "history-max-items", TYPE_INTEGER, history_max_items, {0}},
     {NULL, "editor-command", TYPE_CHAR, editor_command, {0}},
+    {NULL, "header", TYPE_CHAR, headers, {0}},
 };
 
 void setting_init(void)
@@ -729,3 +731,47 @@ static gboolean timeoutlen(const Setting *s, const SettingType type)
 
     return true;
 }
+
+/**
+ * Allow to set user defined http headers.
+ *
+ * :set header=NAME1=VALUE!,NAME2=,NAME3
+ *
+ * Note that these headers will replace already existing headers. If there is
+ * no '=' after the header name, than the complete header will be removed from
+ * the request (NAME3), if the '=' is present means that the header value is
+ * set to empty value.
+ */
+static gboolean headers(const Setting *s, const SettingType type)
+{
+    if (type == SETTING_GET) {
+        char *key, *value;
+        GHashTableIter iter;
+        GString *str;
+
+        if (vb.config.headers) {
+            str = g_string_new("");
+            /* build a list woth the header values */
+            g_hash_table_iter_init(&iter, vb.config.headers);
+            while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&value)) {
+                g_string_append_c(str, ',');
+                soup_header_g_string_append_param(str, key, value);
+            }
+
+            /* skip the first ',' we put into the headers string */
+            print_value(s, str->str + 1);
+            g_string_free(str, true);
+        } else {
+            print_value(s, &"");
+        }
+    } else {
+        /* remove previous parsed headers */
+        if (vb.config.headers) {
+            soup_header_free_param_list(vb.config.headers);
+            vb.config.headers = NULL;
+        }
+        vb.config.headers = soup_header_parse_param_list(s->arg.s);
+    }
+
+    return true;
+}