Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # 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 Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 7 | from TTransport import * |
| 8 | from cStringIO import StringIO |
| 9 | |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame^] | 10 | import urlparse |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 11 | import httplib |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame^] | 12 | import warnings |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 13 | |
| 14 | class THttpClient(TTransportBase): |
| 15 | |
| 16 | """Http implementation of TTransport base.""" |
| 17 | |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame^] | 18 | 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 Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 43 | self.__wbuf = StringIO() |
| 44 | self.__http = None |
| 45 | |
| 46 | def open(self): |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame^] | 47 | 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 Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 51 | |
| 52 | def close(self): |
| 53 | self.__http.close() |
| 54 | self.__http = None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 56 | 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 Reiss | 7c1f6f8 | 2009-03-24 20:10:24 +0000 | [diff] [blame] | 66 | if self.isOpen(): |
| 67 | self.close() |
| 68 | self.open(); |
| 69 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 70 | # Pull data out of buffer |
| 71 | data = self.__wbuf.getvalue() |
| 72 | self.__wbuf = StringIO() |
| 73 | |
| 74 | # HTTP request |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame^] | 75 | self.__http.putrequest('POST', self.path) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 76 | |
| 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() |