mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

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

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