Blame SOURCES/0050-v2v-vCenter-Split-up-get_session_cookie-function.patch

151578
From 4965f1eae5bb56c47c7acf295822afb1108f624e Mon Sep 17 00:00:00 2001
151578
From: "Richard W.M. Jones" <rjones@redhat.com>
151578
Date: Fri, 13 Oct 2017 17:01:04 +0100
151578
Subject: [PATCH] v2v: vCenter: Split up get_session_cookie function.
151578
151578
This is a small refactoring where we split get_session_cookie into a
151578
function to fetch the URL and a function to parse the cookie.
151578
151578
(cherry picked from commit 2052fb7d1f9a173d71ab88358281def0c6b03f06)
151578
---
151578
 v2v/vCenter.ml | 106 ++++++++++++++++++++++++++++++++-------------------------
151578
 1 file changed, 59 insertions(+), 47 deletions(-)
151578
151578
diff --git a/v2v/vCenter.ml b/v2v/vCenter.ml
151578
index 341a40b25..7fc0811a4 100644
151578
--- a/v2v/vCenter.ml
151578
+++ b/v2v/vCenter.ml
151578
@@ -124,6 +124,45 @@ let rec map_source ?readahead ?password dcPath uri scheme server path =
151578
     sslverify = sslverify }
151578
 
151578
 and get_session_cookie password scheme uri sslverify https_url =
151578
+  let status, headers, dump_response =
151578
+    fetch_headers_from_url password scheme uri sslverify https_url in
151578
+
151578
+  if status = "401" then (
151578
+    dump_response stderr;
151578
+    if uri.uri_user <> None then
151578
+      error (f_"vcenter: incorrect username or password")
151578
+    else
151578
+      error (f_"vcenter: incorrect username or password.  You might need to specify the username in the URI like this: %s://USERNAME@[etc]")
151578
+            scheme
151578
+  );
151578
+
151578
+  if status = "404" then (
151578
+    dump_response stderr;
151578
+    error (f_"vcenter: URL not found: %s") https_url
151578
+  );
151578
+
151578
+  if status <> "200" then (
151578
+    dump_response stderr;
151578
+    error (f_"vcenter: invalid response from server")
151578
+  );
151578
+
151578
+  (* Get the cookie. *)
151578
+  let rec loop = function
151578
+    | [] ->
151578
+       dump_response stderr;
151578
+       warning (f_"vcenter: could not read session cookie from the vCenter Server, conversion may consume all sessions on the server and fail part way through");
151578
+       None
151578
+    | ("set-cookie", cookie) :: _ ->
151578
+       let cookie, _ = String.split ";" cookie in
151578
+       Some cookie
151578
+
151578
+    | _ :: headers ->
151578
+       loop headers
151578
+  in
151578
+  loop headers
151578
+
151578
+(* Fetch the status and reply headers from a URL. *)
151578
+and fetch_headers_from_url password scheme uri sslverify https_url =
151578
   let curl_args = ref [
151578
     "head", None;
151578
     "silent", None;
151578
@@ -153,53 +192,26 @@ and get_session_cookie password scheme uri sslverify https_url =
151578
 
151578
   if verbose () then dump_response stderr;
151578
 
151578
+  let statuses, headers =
151578
+    List.partition (
151578
+      fun line ->
151578
+        let len = String.length line in
151578
+        len >= 12 && String.sub line 0 5 = "HTTP/"
151578
+    ) lines in
151578
+
151578
   (* Look for the last HTTP/x.y NNN status code in the output. *)
151578
-  let status = ref "" in
151578
-  List.iter (
151578
-    fun line ->
151578
-      let len = String.length line in
151578
-      if len >= 12 && String.sub line 0 5 = "HTTP/" then
151578
-        status := String.sub line 9 3
151578
-  ) lines;
151578
-  let status = !status in
151578
-  if status = "" then (
151578
-    dump_response stderr;
151578
-    error (f_"vcenter: no status code in output of ‘curl’ command.  Is ‘curl’ installed?")
151578
-  );
151578
-
151578
-  if status = "401" then (
151578
-    dump_response stderr;
151578
-    if uri.uri_user <> None then
151578
-      error (f_"vcenter: incorrect username or password")
151578
-    else
151578
-      error (f_"vcenter: incorrect username or password.  You might need to specify the username in the URI like this: %s://USERNAME@[etc]")
151578
-            scheme
151578
-  );
151578
-
151578
-  if status = "404" then (
151578
-    dump_response stderr;
151578
-    error (f_"vcenter: URL not found: %s") https_url
151578
-  );
151578
-
151578
-  if status <> "200" then (
151578
-    dump_response stderr;
151578
-    error (f_"vcenter: invalid response from server")
151578
-  );
151578
-
151578
-  (* Get the cookie. *)
151578
-  let rec loop = function
151578
+  let status =
151578
+    match statuses with
151578
     | [] ->
151578
        dump_response stderr;
151578
-       warning (f_"vcenter: could not read session cookie from the vCenter Server, conversion may consume all sessions on the server and fail part way through");
151578
-       None
151578
-    | line :: lines ->
151578
-       let len = String.length line in
151578
-       if len >= 12 && String.sub line 0 12 = "Set-Cookie: " then (
151578
-         let line = String.sub line 12 (len-12) in
151578
-         let cookie, _ = String.split ";" line in
151578
-         Some cookie
151578
-       )
151578
-       else
151578
-         loop lines
151578
-  in
151578
-  loop lines
151578
+       error (f_"vcenter: no status code in output of ‘curl’ command.  Is ‘curl’ installed?")
151578
+    | ss -> String.sub (List.hd (List.rev ss)) 9 3 in
151578
+
151578
+  let headers =
151578
+    List.map (
151578
+      fun header ->
151578
+        let h, c = String.split ": " header in
151578
+        String.lowercase_ascii h, c
151578
+    ) headers in
151578
+
151578
+  status, headers, dump_response
151578
-- 
151578
2.14.3
151578