blob: a12fdcdc63e99c362015f09e890f60809a12875f [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001# Copyright (c) 2006- Facebook
2# Distributed under the Thrift Software License
3#
4# See accompanying file LICENSE or visit the Thrift site at:
5# http://developers.facebook.com/thrift/
6
Mark Sleebd8b9912007-02-27 20:17:00 +00007from TTransport import *
8from cStringIO import StringIO
9
David Reiss2aa28902009-03-26 06:22:18 +000010import urlparse
Mark Sleebd8b9912007-02-27 20:17:00 +000011import httplib
David Reiss2aa28902009-03-26 06:22:18 +000012import warnings
Mark Sleebd8b9912007-02-27 20:17:00 +000013
14class THttpClient(TTransportBase):
15
16 """Http implementation of TTransport base."""
17
David Reiss2aa28902009-03-26 06:22:18 +000018 def __init__(self, uri_or_host, port=None, path=None):
19 """THttpClient supports two different types constructor parameters.
20
21 THttpClient(host, port, path) - deprecated
22 THttpClient(uri)
23
24 Only the second supports https."""
25
26 if port is not None:
27 warnings.warn("Please use the THttpClient('http://host:port/path') syntax", DeprecationWarning, stacklevel=2)
28 self.host = uri_or_host
29 self.port = port
30 assert path
31 self.path = path
32 self.scheme = 'http'
33 else:
34 parsed = urlparse.urlparse(uri_or_host)
35 self.scheme = parsed.scheme
36 assert self.scheme in ('http', 'https')
37 if self.scheme == 'http':
38 self.port = parsed.port or httplib.HTTP_PORT
39 elif self.scheme == 'https':
40 self.port = parsed.port or httplib.HTTPS_PORT
41 self.host = parsed.hostname
42 self.path = parsed.path
Mark Sleebd8b9912007-02-27 20:17:00 +000043 self.__wbuf = StringIO()
44 self.__http = None
45
46 def open(self):
David Reiss2aa28902009-03-26 06:22:18 +000047 if self.scheme == 'http':
48 self.__http = httplib.HTTP(self.host, self.port)
49 else:
50 self.__http = httplib.HTTPS(self.host, self.port)
Mark Sleebd8b9912007-02-27 20:17:00 +000051
52 def close(self):
53 self.__http.close()
54 self.__http = None
David Reiss0c90f6f2008-02-06 22:18:40 +000055
Mark Sleebd8b9912007-02-27 20:17:00 +000056 def isOpen(self):
57 return self.__http != None
58
59 def read(self, sz):
60 return self.__http.file.read(sz)
61
62 def write(self, buf):
63 self.__wbuf.write(buf)
64
65 def flush(self):
David Reiss7c1f6f82009-03-24 20:10:24 +000066 if self.isOpen():
67 self.close()
68 self.open();
69
Mark Sleebd8b9912007-02-27 20:17:00 +000070 # Pull data out of buffer
71 data = self.__wbuf.getvalue()
72 self.__wbuf = StringIO()
73
74 # HTTP request
David Reiss2aa28902009-03-26 06:22:18 +000075 self.__http.putrequest('POST', self.path)
Mark Sleebd8b9912007-02-27 20:17:00 +000076
77 # Write headers
78 self.__http.putheader('Host', self.host)
79 self.__http.putheader('Content-Type', 'application/x-thrift')
80 self.__http.putheader('Content-Length', str(len(data)))
81 self.__http.endheaders()
82
83 # Write payload
84 self.__http.send(data)
85
86 # Get reply to flush the request
87 self.code, self.message, self.headers = self.__http.getreply()