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