Patchwork scripts/send-error-report: simple hack to use proxy from the enviroment

login
register
mail settings
Submitter Stanacar, StefanX
Date April 11, 2014, 3:54 p.m.
Message ID <1397231695-19060-2-git-send-email-stefanx.stanacar@intel.com>
Download mbox | patch
Permalink /patch/70511/
State New
Headers show

Comments

Stanacar, StefanX - April 11, 2014, 3:54 p.m.
People behind a proxy couldn't send an error report to an upstream server,
this should fix the issue if they use a proxy that doesn't require authentication,
and it's correctly exported in the enviroment.
There are probbaly better ways to fix this (urllib2, proxy connect,
a command line option for proxy) but those require more changes than
I feel confortable doing now.

Signed-off-by: Stefan Stanacar <stefanx.stanacar@intel.com>
---
 scripts/send-error-report | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
Stanacar, StefanX - April 11, 2014, 5:16 p.m.
Actually I'll send v2 which should work for HTTP Basic Authentication
too.


On Fri, 2014-04-11 at 18:54 +0300, Stefan Stanacar wrote:
> People behind a proxy couldn't send an error report to an upstream server,
> this should fix the issue if they use a proxy that doesn't require authentication,
> and it's correctly exported in the enviroment.
> There are probbaly better ways to fix this (urllib2, proxy connect,
> a command line option for proxy) but those require more changes than
> I feel confortable doing now.
> 
> Signed-off-by: Stefan Stanacar <stefanx.stanacar@intel.com>
> ---
>  scripts/send-error-report | 27 +++++++++++++++++++++------
>  1 file changed, 21 insertions(+), 6 deletions(-)
> 
> diff --git a/scripts/send-error-report b/scripts/send-error-report
> index 3734b61..0bce8b6 100755
> --- a/scripts/send-error-report
> +++ b/scripts/send-error-report
> @@ -8,6 +8,24 @@
>  
> 
>  import httplib, urllib, os, sys, json
> +from urllib2 import _parse_proxy as parseproxy
> +
> +
> +def handle_connection(server, data):
> +        params = urllib.urlencode({'data': data})
> +        headers = {"Content-type": "application/json"}
> +
> +        if os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY"):
> +                proxy = parseproxy(os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY"))
> +                if proxy[1] or proxy[2]:
> +                        raise Exception("http proxies that require authentication are not supported for now.")
> +                conn = httplib.HTTPConnection(proxy[3])
> +                conn.request("POST", "http://%s/ClientPost/" % server, params, headers)
> +        else:
> +                conn = httplib.HTTPConnection(server)
> +                conn.request("POST", "/ClientPost/", params, headers)
> +
> +        return conn
>  
> 
>  def sendData(json_file, server):
> @@ -45,10 +63,7 @@ def sendData(json_file, server):
>              return
>  
>          try:
> -            params = urllib.urlencode({'data': data})
> -            headers = {"Content-type": "application/json"}
> -            conn = httplib.HTTPConnection(server)
> -            conn.request("POST", "/ClientPost/", params, headers)
> +            conn = handle_connection(server, data)
>              response = conn.getresponse()
>              print response.status, response.reason
>              res = response.read()
> @@ -59,8 +74,8 @@ def sendData(json_file, server):
>                  with open("%s.response.html" % json_file, "w") as f:
>                      f.write(res)
>              conn.close()
> -        except:
> -                print("Server connection failed")
> +        except Exception as e:
> +                print("Server connection failed: %s" % e)
>  
>      else:
>          print("No data file found.")

Patch

diff --git a/scripts/send-error-report b/scripts/send-error-report
index 3734b61..0bce8b6 100755
--- a/scripts/send-error-report
+++ b/scripts/send-error-report
@@ -8,6 +8,24 @@ 
 
 
 import httplib, urllib, os, sys, json
+from urllib2 import _parse_proxy as parseproxy
+
+
+def handle_connection(server, data):
+        params = urllib.urlencode({'data': data})
+        headers = {"Content-type": "application/json"}
+
+        if os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY"):
+                proxy = parseproxy(os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY"))
+                if proxy[1] or proxy[2]:
+                        raise Exception("http proxies that require authentication are not supported for now.")
+                conn = httplib.HTTPConnection(proxy[3])
+                conn.request("POST", "http://%s/ClientPost/" % server, params, headers)
+        else:
+                conn = httplib.HTTPConnection(server)
+                conn.request("POST", "/ClientPost/", params, headers)
+
+        return conn
 
 
 def sendData(json_file, server):
@@ -45,10 +63,7 @@  def sendData(json_file, server):
             return
 
         try:
-            params = urllib.urlencode({'data': data})
-            headers = {"Content-type": "application/json"}
-            conn = httplib.HTTPConnection(server)
-            conn.request("POST", "/ClientPost/", params, headers)
+            conn = handle_connection(server, data)
             response = conn.getresponse()
             print response.status, response.reason
             res = response.read()
@@ -59,8 +74,8 @@  def sendData(json_file, server):
                 with open("%s.response.html" % json_file, "w") as f:
                     f.write(res)
             conn.close()
-        except:
-                print("Server connection failed")
+        except Exception as e:
+                print("Server connection failed: %s" % e)
 
     else:
         print("No data file found.")