You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2009/03/26 07:22:20 UTC

svn commit: r758532 - /incubator/thrift/trunk/lib/py/src/transport/THttpClient.py

Author: dreiss
Date: Thu Mar 26 06:22:18 2009
New Revision: 758532

URL: http://svn.apache.org/viewvc?rev=758532&view=rev
Log:
THRIFT-154. python: Make THttpClient take a URL in its constructor

Support https in the process.

Modified:
    incubator/thrift/trunk/lib/py/src/transport/THttpClient.py

Modified: incubator/thrift/trunk/lib/py/src/transport/THttpClient.py
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/py/src/transport/THttpClient.py?rev=758532&r1=758531&r2=758532&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/py/src/transport/THttpClient.py (original)
+++ incubator/thrift/trunk/lib/py/src/transport/THttpClient.py Thu Mar 26 06:22:18 2009
@@ -7,21 +7,47 @@
 from TTransport import *
 from cStringIO import StringIO
 
+import urlparse
 import httplib
+import warnings
 
 class THttpClient(TTransportBase):
 
   """Http implementation of TTransport base."""
 
-  def __init__(self, host, port, uri):
-    self.host = host
-    self.port = port
-    self.uri = uri
+  def __init__(self, uri_or_host, port=None, path=None):
+    """THttpClient supports two different types constructor parameters.
+
+    THttpClient(host, port, path) - deprecated
+    THttpClient(uri)
+
+    Only the second supports https."""
+
+    if port is not None:
+      warnings.warn("Please use the THttpClient('http://host:port/path') syntax", DeprecationWarning, stacklevel=2)
+      self.host = uri_or_host
+      self.port = port
+      assert path
+      self.path = path
+      self.scheme = 'http'
+    else:
+      parsed = urlparse.urlparse(uri_or_host)
+      self.scheme = parsed.scheme
+      assert self.scheme in ('http', 'https')
+      if self.scheme == 'http':
+        self.port = parsed.port or httplib.HTTP_PORT
+      elif self.scheme == 'https':
+        self.port = parsed.port or httplib.HTTPS_PORT
+      self.host = parsed.hostname
+      self.path = parsed.path
     self.__wbuf = StringIO()
     self.__http = None
 
   def open(self):
-    self.__http = httplib.HTTP(self.host, self.port)
+    if self.scheme == 'http':
+      self.__http = httplib.HTTP(self.host, self.port)
+    else:
+      self.__http = httplib.HTTPS(self.host, self.port)
 
   def close(self):
     self.__http.close()
@@ -46,7 +72,7 @@
     self.__wbuf = StringIO()
 
     # HTTP request
-    self.__http.putrequest('POST', self.uri)
+    self.__http.putrequest('POST', self.path)
 
     # Write headers
     self.__http.putheader('Host', self.host)