Blame 0001-curl-Allow-a-cookie-or-cookies-to-be-sent-with-http-.patch

f2a088
From a94f83d94fdf907680f068f1be7ad13d1f697067 Mon Sep 17 00:00:00 2001
f2a088
From: "Richard W.M. Jones" <rjones@redhat.com>
f2a088
Date: Fri, 29 Aug 2014 16:03:12 +0100
f2a088
Subject: [PATCH] curl: Allow a cookie or cookies to be sent with http/https
f2a088
 requests.
f2a088
f2a088
In order to access VMware ESX efficiently, we need to send a session
f2a088
cookie.  This patch is very simple and just allows you to send that
f2a088
session cookie.  It punts on the question of how you get the session
f2a088
cookie in the first place, but in practice you can just run a `curl'
f2a088
command against the server and extract the cookie that way.
f2a088
f2a088
To use it, add file.cookie to the curl URL.  For example:
f2a088
f2a088
$ qemu-img info 'json: {
f2a088
    "file.driver":"https",
f2a088
    "file.url":"https://vcenter/folder/Windows%202003/Windows%202003-flat.vmdk?dcPath=Datacenter&dsName=datastore1",
f2a088
    "file.sslverify":"off",
f2a088
    "file.cookie":"vmware_soap_session=\"52a01262-bf93-ccce-d379-8dabb3e55560\""}'
f2a088
image: [...]
f2a088
file format: raw
f2a088
virtual size: 8.0G (8589934592 bytes)
f2a088
disk size: unavailable
f2a088
f2a088
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
f2a088
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
f2a088
---
f2a088
 block/curl.c    | 16 ++++++++++++++++
f2a088
 qemu-options.hx |  5 +++++
f2a088
 2 files changed, 21 insertions(+)
f2a088
f2a088
diff --git a/block/curl.c b/block/curl.c
f2a088
index 2698ae3..9051bc0 100644
f2a088
--- a/block/curl.c
f2a088
+++ b/block/curl.c
f2a088
@@ -73,6 +73,7 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
f2a088
 #define CURL_BLOCK_OPT_READAHEAD "readahead"
f2a088
 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
f2a088
 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
f2a088
+#define CURL_BLOCK_OPT_COOKIE    "cookie"
f2a088
 
f2a088
 struct BDRVCURLState;
f2a088
 
f2a088
@@ -112,6 +113,7 @@ typedef struct BDRVCURLState {
f2a088
     size_t readahead_size;
f2a088
     bool sslverify;
f2a088
     int timeout;
f2a088
+    char *cookie;
f2a088
     bool accept_range;
f2a088
     AioContext *aio_context;
f2a088
 } BDRVCURLState;
f2a088
@@ -385,6 +387,9 @@ static CURLState *curl_init_state(BDRVCURLState *s)
f2a088
         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
f2a088
         curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
f2a088
                          (long) s->sslverify);
f2a088
+        if (s->cookie) {
f2a088
+            curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
f2a088
+        }
f2a088
         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, s->timeout);
f2a088
         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
f2a088
                          (void *)curl_read_cb);
f2a088
@@ -497,6 +502,11 @@ static QemuOptsList runtime_opts = {
f2a088
             .type = QEMU_OPT_NUMBER,
f2a088
             .help = "Curl timeout"
f2a088
         },
f2a088
+        {
f2a088
+            .name = CURL_BLOCK_OPT_COOKIE,
f2a088
+            .type = QEMU_OPT_STRING,
f2a088
+            .help = "Pass the cookie or list of cookies with each request"
f2a088
+        },
f2a088
         { /* end of list */ }
f2a088
     },
f2a088
 };
f2a088
@@ -509,6 +519,7 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,
f2a088
     QemuOpts *opts;
f2a088
     Error *local_err = NULL;
f2a088
     const char *file;
f2a088
+    const char *cookie;
f2a088
     double d;
f2a088
 
f2a088
     static int inited = 0;
f2a088
@@ -538,6 +549,9 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,
f2a088
 
f2a088
     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
f2a088
 
f2a088
+    cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
f2a088
+    s->cookie = g_strdup(cookie);
f2a088
+
f2a088
     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
f2a088
     if (file == NULL) {
f2a088
         error_setg(errp, "curl block driver requires an 'url' option");
f2a088
@@ -593,6 +607,7 @@ out:
f2a088
     curl_easy_cleanup(state->curl);
f2a088
     state->curl = NULL;
f2a088
 out_noclean:
f2a088
+    g_free(s->cookie);
f2a088
     g_free(s->url);
f2a088
     qemu_opts_del(opts);
f2a088
     return -EINVAL;
f2a088
@@ -695,6 +710,7 @@ static void curl_close(BlockDriverState *bs)
f2a088
     DPRINTF("CURL: Close\n");
f2a088
     curl_detach_aio_context(bs);
f2a088
 
f2a088
+    g_free(s->cookie);
f2a088
     g_free(s->url);
f2a088
 }
f2a088
 
f2a088
diff --git a/qemu-options.hx b/qemu-options.hx
f2a088
index 52d56f4..5479cf5 100644
f2a088
--- a/qemu-options.hx
f2a088
+++ b/qemu-options.hx
f2a088
@@ -2352,6 +2352,11 @@ multiple of 512 bytes. It defaults to 256k.
f2a088
 Whether to verify the remote server's certificate when connecting over SSL. It
f2a088
 can have the value 'on' or 'off'. It defaults to 'on'.
f2a088
 
f2a088
+@item cookie
f2a088
+Send this cookie (it can also be a list of cookies separated by ';') with
f2a088
+each outgoing request.  Only supported when using protocols such as HTTP
f2a088
+which support cookies, otherwise ignored.
f2a088
+
f2a088
 @item timeout
f2a088
 Set the timeout in seconds of the CURL connection. This timeout is the time
f2a088
 that CURL waits for a response from the remote server to get the size of the
f2a088
-- 
f2a088
2.0.4
f2a088