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 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 20 | from io import BytesIO |
Roger Meier | 3f5a264 | 2012-04-13 14:20:08 +0000 | [diff] [blame] | 21 | import os |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 22 | import socket |
Roger Meier | 3f5a264 | 2012-04-13 14:20:08 +0000 | [diff] [blame] | 23 | import sys |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 24 | import warnings |
| 25 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 26 | from six.moves import urllib |
| 27 | from six.moves import http_client |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 28 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 29 | from .TTransport import * |
| 30 | import six |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 31 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 32 | |
| 33 | class THttpClient(TTransportBase): |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 34 | """Http implementation of TTransport base.""" |
| 35 | |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 36 | def __init__(self, uri_or_host, port=None, path=None): |
| 37 | """THttpClient supports two different types constructor parameters. |
| 38 | |
| 39 | THttpClient(host, port, path) - deprecated |
| 40 | THttpClient(uri) |
| 41 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 42 | Only the second supports https. |
| 43 | """ |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 44 | if port is not None: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 45 | warnings.warn( |
| 46 | "Please use the THttpClient('http://host:port/path') syntax", |
| 47 | DeprecationWarning, |
| 48 | stacklevel=2) |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 49 | self.host = uri_or_host |
| 50 | self.port = port |
| 51 | assert path |
| 52 | self.path = path |
| 53 | self.scheme = 'http' |
| 54 | else: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 55 | parsed = urllib.parse.urlparse(uri_or_host) |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 56 | self.scheme = parsed.scheme |
| 57 | assert self.scheme in ('http', 'https') |
| 58 | if self.scheme == 'http': |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 59 | self.port = parsed.port or http_client.HTTP_PORT |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 60 | elif self.scheme == 'https': |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 61 | self.port = parsed.port or http_client.HTTPS_PORT |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 62 | self.host = parsed.hostname |
| 63 | self.path = parsed.path |
Bryan Duxbury | 727d67d | 2010-09-02 01:00:19 +0000 | [diff] [blame] | 64 | if parsed.query: |
| 65 | self.path += '?%s' % parsed.query |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 66 | self.__wbuf = BytesIO() |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 67 | self.__http = None |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 68 | self.__http_response = None |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 69 | self.__timeout = None |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 70 | self.__custom_headers = None |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 71 | |
| 72 | def open(self): |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 73 | if self.scheme == 'http': |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 74 | self.__http = http_client.HTTPConnection(self.host, self.port) |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 75 | else: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 76 | self.__http = http_client.HTTPSConnection(self.host, self.port) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 77 | |
| 78 | def close(self): |
| 79 | self.__http.close() |
| 80 | self.__http = None |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 81 | self.__http_response = None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 82 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 83 | def isOpen(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 84 | return self.__http is not None |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 85 | |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 86 | def setTimeout(self, ms): |
| 87 | if not hasattr(socket, 'getdefaulttimeout'): |
| 88 | raise NotImplementedError |
| 89 | |
| 90 | if ms is None: |
| 91 | self.__timeout = None |
| 92 | else: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 93 | self.__timeout = ms / 1000.0 |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 94 | |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 95 | def setCustomHeaders(self, headers): |
| 96 | self.__custom_headers = headers |
| 97 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 98 | def read(self, sz): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 99 | return self.__http_response.read(sz) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 100 | |
| 101 | def write(self, buf): |
| 102 | self.__wbuf.write(buf) |
| 103 | |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 104 | def __withTimeout(f): |
| 105 | def _f(*args, **kwargs): |
| 106 | orig_timeout = socket.getdefaulttimeout() |
| 107 | socket.setdefaulttimeout(args[0].__timeout) |
Jens Geyer | 4f2c0a0 | 2015-01-05 21:17:06 +0100 | [diff] [blame] | 108 | try: |
| 109 | result = f(*args, **kwargs) |
| 110 | finally: |
| 111 | socket.setdefaulttimeout(orig_timeout) |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 112 | return result |
| 113 | return _f |
| 114 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 115 | def flush(self): |
David Reiss | 7c1f6f8 | 2009-03-24 20:10:24 +0000 | [diff] [blame] | 116 | if self.isOpen(): |
| 117 | self.close() |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 118 | self.open() |
David Reiss | 7c1f6f8 | 2009-03-24 20:10:24 +0000 | [diff] [blame] | 119 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 120 | # Pull data out of buffer |
| 121 | data = self.__wbuf.getvalue() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 122 | self.__wbuf = BytesIO() |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 123 | |
| 124 | # HTTP request |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 125 | self.__http.putrequest('POST', self.path) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 126 | |
| 127 | # Write headers |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 128 | self.__http.putheader('Content-Type', 'application/x-thrift') |
| 129 | self.__http.putheader('Content-Length', str(len(data))) |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 130 | |
Roger Meier | 3f5a264 | 2012-04-13 14:20:08 +0000 | [diff] [blame] | 131 | if not self.__custom_headers or 'User-Agent' not in self.__custom_headers: |
| 132 | user_agent = 'Python/THttpClient' |
| 133 | script = os.path.basename(sys.argv[0]) |
| 134 | if script: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 135 | user_agent = '%s (%s)' % (user_agent, urllib.parse.quote(script)) |
Roger Meier | 3f5a264 | 2012-04-13 14:20:08 +0000 | [diff] [blame] | 136 | self.__http.putheader('User-Agent', user_agent) |
| 137 | |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 138 | if self.__custom_headers: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 139 | for key, val in six.iteritems(self.__custom_headers): |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 140 | self.__http.putheader(key, val) |
| 141 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 142 | self.__http.endheaders() |
| 143 | |
| 144 | # Write payload |
| 145 | self.__http.send(data) |
| 146 | |
| 147 | # Get reply to flush the request |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 148 | self.__http_response = self.__http.getresponse() |
| 149 | self.code = self.__http_response.status |
| 150 | self.message = self.__http_response.reason |
| 151 | self.headers = self.__http_response.msg |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 152 | |
| 153 | # Decorate if we know how to timeout |
| 154 | if hasattr(socket, 'getdefaulttimeout'): |
| 155 | flush = __withTimeout(flush) |