Blob Blame History Raw
From 5917bdc15f3b2e0c96ba5a9860cd36f968a28ec2 Mon Sep 17 00:00:00 2001
From: Nir Soffer <nirsof@gmail.com>
Date: Thu, 14 Jun 2018 21:16:01 +0300
Subject: [PATCH] v2v: -o rhv-upload: Optimize http request sending

When sending request with small or no payload, it is simpler and
possibly more efficient to use the high level HTTPSConnection.request(),
instead of the lower level APIs.

The only reason to use the lower level APIs is to avoid copying the
payload, or on python 2, to use a bigger buffer size when streaming a
file-like object.

(cherry picked from commit 77a412c0a1cd0e303a072fc5088c8f3bfed36583)
---
 v2v/rhv-upload-plugin.py | 35 ++++++++++++++++-------------------
 1 file changed, 16 insertions(+), 19 deletions(-)

diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
index b4557b83c..9ad354b84 100644
--- a/v2v/rhv-upload-plugin.py
+++ b/v2v/rhv-upload-plugin.py
@@ -237,12 +237,12 @@ def pread(h, count, offset):
     transfer = h['transfer']
     transfer_service = h['transfer_service']
 
-    http.putrequest("GET", h['path'])
+    headers = {"Range", "bytes=%d-%d" % (offset, offset+count-1)}
     # Authorization is only needed for old imageio.
     if h['needs_auth']:
-        http.putheader("Authorization", transfer.signed_ticket)
-    http.putheader("Range", "bytes=%d-%d" % (offset, offset+count-1))
-    http.endheaders()
+        headers["Authorization"] = transfer.signed_ticket
+
+    http.request("GET", h['path'], headers=headers)
 
     r = http.getresponse()
     # 206 = HTTP Partial Content.
@@ -297,11 +297,10 @@ def zero(h, count, offset, may_trim):
                       'size': count,
                       'flush': False}).encode()
 
-    http.putrequest("PATCH", h['path'])
-    http.putheader("Content-Type", "application/json")
-    http.putheader("Content-Length", len(buf))
-    http.endheaders()
-    http.send(buf)
+    headers = {"Content-Type": "application/json",
+               "Content-Length": str(len(buf))}
+
+    http.request("PATCH", h['path'], body=buf, headers=headers)
 
     r = http.getresponse()
     if r.status != 200:
@@ -349,11 +348,10 @@ def trim(h, count, offset):
                       'size': count,
                       'flush': False}).encode()
 
-    http.putrequest("PATCH", h['path'])
-    http.putheader("Content-Type", "application/json")
-    http.putheader("Content-Length", len(buf))
-    http.endheaders()
-    http.send(buf)
+    headers = {"Content-Type": "application/json",
+               "Content-Length": str(len(buf))}
+
+    http.request("PATCH", h['path'], body=buf, headers=headers)
 
     r = http.getresponse()
     if r.status != 200:
@@ -370,11 +368,10 @@ def flush(h):
     # Construct the JSON request for flushing.
     buf = json.dumps({'op': "flush"}).encode()
 
-    http.putrequest("PATCH", h['path'])
-    http.putheader("Content-Type", "application/json")
-    http.putheader("Content-Length", len(buf))
-    http.endheaders()
-    http.send(buf)
+    headers = {"Content-Type": "application/json",
+               "Content-Length": str(len(buf))}
+
+    http.request("PATCH", h['path'], body=buf, headers=headers)
 
     r = http.getresponse()
     if r.status != 200:
-- 
2.21.0