Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 19 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 20 | import httplib |
| 21 | import socket |
| 22 | import urlparse |
| 23 | import warnings |
| 24 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 25 | from cStringIO import StringIO |
| 26 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 27 | from TTransport import * |
| 28 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 29 | |
| 30 | class THttpClient(TTransportBase): |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 31 | """Http implementation of TTransport base.""" |
| 32 | |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 33 | def __init__(self, uri_or_host, port=None, path=None): |
| 34 | """THttpClient supports two different types constructor parameters. |
| 35 | |
| 36 | THttpClient(host, port, path) - deprecated |
| 37 | THttpClient(uri) |
| 38 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 39 | Only the second supports https. |
| 40 | """ |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 41 | if port is not None: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 42 | warnings.warn( |
| 43 | "Please use the THttpClient('http://host:port/path') syntax", |
| 44 | DeprecationWarning, |
| 45 | stacklevel=2) |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 46 | self.host = uri_or_host |
| 47 | self.port = port |
| 48 | assert path |
| 49 | self.path = path |
| 50 | self.scheme = 'http' |
| 51 | else: |
| 52 | parsed = urlparse.urlparse(uri_or_host) |
| 53 | self.scheme = parsed.scheme |
| 54 | assert self.scheme in ('http', 'https') |
| 55 | if self.scheme == 'http': |
| 56 | self.port = parsed.port or httplib.HTTP_PORT |
| 57 | elif self.scheme == 'https': |
| 58 | self.port = parsed.port or httplib.HTTPS_PORT |
| 59 | self.host = parsed.hostname |
| 60 | self.path = parsed.path |
Bryan Duxbury | 727d67d | 2010-09-02 01:00:19 +0000 | [diff] [blame] | 61 | if parsed.query: |
| 62 | self.path += '?%s' % parsed.query |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 63 | self.__wbuf = StringIO() |
| 64 | self.__http = None |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 65 | self.__timeout = None |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame^] | 66 | self.__custom_headers = None |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 67 | |
| 68 | def open(self): |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 69 | if self.scheme == 'http': |
| 70 | self.__http = httplib.HTTP(self.host, self.port) |
| 71 | else: |
| 72 | self.__http = httplib.HTTPS(self.host, self.port) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 73 | |
| 74 | def close(self): |
| 75 | self.__http.close() |
| 76 | self.__http = None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 77 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 78 | def isOpen(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 79 | return self.__http is not None |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 80 | |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 81 | def setTimeout(self, ms): |
| 82 | if not hasattr(socket, 'getdefaulttimeout'): |
| 83 | raise NotImplementedError |
| 84 | |
| 85 | if ms is None: |
| 86 | self.__timeout = None |
| 87 | else: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 88 | self.__timeout = ms / 1000.0 |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 89 | |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame^] | 90 | def setCustomHeaders(self, headers): |
| 91 | self.__custom_headers = headers |
| 92 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 93 | def read(self, sz): |
| 94 | return self.__http.file.read(sz) |
| 95 | |
| 96 | def write(self, buf): |
| 97 | self.__wbuf.write(buf) |
| 98 | |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 99 | def __withTimeout(f): |
| 100 | def _f(*args, **kwargs): |
| 101 | orig_timeout = socket.getdefaulttimeout() |
| 102 | socket.setdefaulttimeout(args[0].__timeout) |
| 103 | result = f(*args, **kwargs) |
| 104 | socket.setdefaulttimeout(orig_timeout) |
| 105 | return result |
| 106 | return _f |
| 107 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 108 | def flush(self): |
David Reiss | 7c1f6f8 | 2009-03-24 20:10:24 +0000 | [diff] [blame] | 109 | if self.isOpen(): |
| 110 | self.close() |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 111 | self.open() |
David Reiss | 7c1f6f8 | 2009-03-24 20:10:24 +0000 | [diff] [blame] | 112 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 113 | # Pull data out of buffer |
| 114 | data = self.__wbuf.getvalue() |
| 115 | self.__wbuf = StringIO() |
| 116 | |
| 117 | # HTTP request |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 118 | self.__http.putrequest('POST', self.path) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 119 | |
| 120 | # Write headers |
| 121 | self.__http.putheader('Host', self.host) |
| 122 | self.__http.putheader('Content-Type', 'application/x-thrift') |
| 123 | self.__http.putheader('Content-Length', str(len(data))) |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame^] | 124 | |
| 125 | if self.__custom_headers: |
| 126 | for key, val in self.__custom_headers.iteritems(): |
| 127 | self.__http.putheader(key, val) |
| 128 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 129 | self.__http.endheaders() |
| 130 | |
| 131 | # Write payload |
| 132 | self.__http.send(data) |
| 133 | |
| 134 | # Get reply to flush the request |
| 135 | self.code, self.message, self.headers = self.__http.getreply() |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 136 | |
| 137 | # Decorate if we know how to timeout |
| 138 | if hasattr(socket, 'getdefaulttimeout'): |
| 139 | flush = __withTimeout(flush) |