Blame SOURCES/0053-v2v-o-rhv-upload-Optimize-http-request-sending.patch

97ae69
From dc6afe583853bc2e18a689aa06685e69d65ede84 Mon Sep 17 00:00:00 2001
97ae69
From: Nir Soffer <nirsof@gmail.com>
97ae69
Date: Thu, 14 Jun 2018 21:16:01 +0300
97ae69
Subject: [PATCH] v2v: -o rhv-upload: Optimize http request sending
97ae69
97ae69
When sending request with small or no payload, it is simpler and
97ae69
possibly more efficient to use the high level HTTPSConnection.request(),
97ae69
instead of the lower level APIs.
97ae69
97ae69
The only reason to use the lower level APIs is to avoid copying the
97ae69
payload, or on python 2, to use a bigger buffer size when streaming a
97ae69
file-like object.
97ae69
97ae69
(cherry picked from commit 77a412c0a1cd0e303a072fc5088c8f3bfed36583)
97ae69
---
97ae69
 v2v/rhv-upload-plugin.py | 35 ++++++++++++++++-------------------
97ae69
 1 file changed, 16 insertions(+), 19 deletions(-)
97ae69
97ae69
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
97ae69
index b4557b83c..9ad354b84 100644
97ae69
--- a/v2v/rhv-upload-plugin.py
97ae69
+++ b/v2v/rhv-upload-plugin.py
97ae69
@@ -237,12 +237,12 @@ def pread(h, count, offset):
97ae69
     transfer = h['transfer']
97ae69
     transfer_service = h['transfer_service']
97ae69
 
97ae69
-    http.putrequest("GET", h['path'])
97ae69
+    headers = {"Range", "bytes=%d-%d" % (offset, offset+count-1)}
97ae69
     # Authorization is only needed for old imageio.
97ae69
     if h['needs_auth']:
97ae69
-        http.putheader("Authorization", transfer.signed_ticket)
97ae69
-    http.putheader("Range", "bytes=%d-%d" % (offset, offset+count-1))
97ae69
-    http.endheaders()
97ae69
+        headers["Authorization"] = transfer.signed_ticket
97ae69
+
97ae69
+    http.request("GET", h['path'], headers=headers)
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     # 206 = HTTP Partial Content.
97ae69
@@ -297,11 +297,10 @@ def zero(h, count, offset, may_trim):
97ae69
                       'size': count,
97ae69
                       'flush': False}).encode()
97ae69
 
97ae69
-    http.putrequest("PATCH", h['path'])
97ae69
-    http.putheader("Content-Type", "application/json")
97ae69
-    http.putheader("Content-Length", len(buf))
97ae69
-    http.endheaders()
97ae69
-    http.send(buf)
97ae69
+    headers = {"Content-Type": "application/json",
97ae69
+               "Content-Length": str(len(buf))}
97ae69
+
97ae69
+    http.request("PATCH", h['path'], body=buf, headers=headers)
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     if r.status != 200:
97ae69
@@ -349,11 +348,10 @@ def trim(h, count, offset):
97ae69
                       'size': count,
97ae69
                       'flush': False}).encode()
97ae69
 
97ae69
-    http.putrequest("PATCH", h['path'])
97ae69
-    http.putheader("Content-Type", "application/json")
97ae69
-    http.putheader("Content-Length", len(buf))
97ae69
-    http.endheaders()
97ae69
-    http.send(buf)
97ae69
+    headers = {"Content-Type": "application/json",
97ae69
+               "Content-Length": str(len(buf))}
97ae69
+
97ae69
+    http.request("PATCH", h['path'], body=buf, headers=headers)
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     if r.status != 200:
97ae69
@@ -370,11 +368,10 @@ def flush(h):
97ae69
     # Construct the JSON request for flushing.
97ae69
     buf = json.dumps({'op': "flush"}).encode()
97ae69
 
97ae69
-    http.putrequest("PATCH", h['path'])
97ae69
-    http.putheader("Content-Type", "application/json")
97ae69
-    http.putheader("Content-Length", len(buf))
97ae69
-    http.endheaders()
97ae69
-    http.send(buf)
97ae69
+    headers = {"Content-Type": "application/json",
97ae69
+               "Content-Length": str(len(buf))}
97ae69
+
97ae69
+    http.request("PATCH", h['path'], body=buf, headers=headers)
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     if r.status != 200:
97ae69
-- 
97ae69
2.17.1
97ae69