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 |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 23 | import ssl |
Roger Meier | 3f5a264 | 2012-04-13 14:20:08 +0000 | [diff] [blame] | 24 | import sys |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 25 | import warnings |
Martin Wilck | 1ac0a80 | 2016-04-27 09:41:03 +0200 | [diff] [blame] | 26 | import base64 |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 27 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 28 | from six.moves import urllib |
| 29 | from six.moves import http_client |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 30 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 31 | from .TTransport import TTransportBase |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 32 | import six |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 33 | |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 34 | |
| 35 | class THttpClient(TTransportBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 36 | """Http implementation of TTransport base.""" |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 37 | |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 38 | def __init__(self, uri_or_host, port=None, path=None, cafile=None, cert_file=None, key_file=None, ssl_context=None): |
| 39 | """THttpClient supports two different types of construction: |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 40 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | THttpClient(host, port, path) - deprecated |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 42 | THttpClient(uri, [port=<n>, path=<s>, cafile=<filename>, cert_file=<filename>, key_file=<filename>, ssl_context=<context>]) |
David Reiss | 2aa2890 | 2009-03-26 06:22:18 +0000 | [diff] [blame] | 43 | |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 44 | Only the second supports https. To properly authenticate against the server, |
| 45 | provide the client's identity by specifying cert_file and key_file. To properly |
| 46 | authenticate the server, specify either cafile or ssl_context with a CA defined. |
| 47 | NOTE: if both cafile and ssl_context are defined, ssl_context will override cafile. |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 48 | """ |
| 49 | if port is not None: |
| 50 | warnings.warn( |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 51 | "Please use the THttpClient('http{s}://host:port/path') constructor", |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 52 | DeprecationWarning, |
| 53 | stacklevel=2) |
| 54 | self.host = uri_or_host |
| 55 | self.port = port |
| 56 | assert path |
| 57 | self.path = path |
| 58 | self.scheme = 'http' |
| 59 | else: |
| 60 | parsed = urllib.parse.urlparse(uri_or_host) |
| 61 | self.scheme = parsed.scheme |
| 62 | assert self.scheme in ('http', 'https') |
| 63 | if self.scheme == 'http': |
| 64 | self.port = parsed.port or http_client.HTTP_PORT |
| 65 | elif self.scheme == 'https': |
| 66 | self.port = parsed.port or http_client.HTTPS_PORT |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 67 | self.certfile = cert_file |
| 68 | self.keyfile = key_file |
| 69 | self.context = ssl.create_default_context(cafile=cafile) if (cafile and not ssl_context) else ssl_context |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | self.host = parsed.hostname |
| 71 | self.path = parsed.path |
| 72 | if parsed.query: |
| 73 | self.path += '?%s' % parsed.query |
Martin Wilck | 1ac0a80 | 2016-04-27 09:41:03 +0200 | [diff] [blame] | 74 | try: |
| 75 | proxy = urllib.request.getproxies()[self.scheme] |
| 76 | except KeyError: |
| 77 | proxy = None |
| 78 | else: |
| 79 | if urllib.request.proxy_bypass(self.host): |
| 80 | proxy = None |
| 81 | if proxy: |
| 82 | parsed = urllib.parse.urlparse(proxy) |
| 83 | self.realhost = self.host |
| 84 | self.realport = self.port |
| 85 | self.host = parsed.hostname |
| 86 | self.port = parsed.port |
| 87 | self.proxy_auth = self.basic_proxy_auth_header(parsed) |
| 88 | else: |
| 89 | self.realhost = self.realport = self.proxy_auth = None |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 90 | self.__wbuf = BytesIO() |
| 91 | self.__http = None |
| 92 | self.__http_response = None |
| 93 | self.__timeout = None |
| 94 | self.__custom_headers = None |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 95 | |
Martin Wilck | 1ac0a80 | 2016-04-27 09:41:03 +0200 | [diff] [blame] | 96 | @staticmethod |
| 97 | def basic_proxy_auth_header(proxy): |
| 98 | if proxy is None or not proxy.username: |
| 99 | return None |
| 100 | ap = "%s:%s" % (urllib.parse.unquote(proxy.username), |
| 101 | urllib.parse.unquote(proxy.password)) |
| 102 | cr = base64.b64encode(ap).strip() |
| 103 | return "Basic " + cr |
| 104 | |
| 105 | def using_proxy(self): |
| 106 | return self.realhost is not None |
| 107 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 108 | def open(self): |
| 109 | if self.scheme == 'http': |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 110 | self.__http = http_client.HTTPConnection(self.host, self.port, |
| 111 | timeout=self.__timeout) |
Martin Wilck | 1ac0a80 | 2016-04-27 09:41:03 +0200 | [diff] [blame] | 112 | elif self.scheme == 'https': |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame^] | 113 | self.__http = http_client.HTTPSConnection(self.host, self.port, |
| 114 | key_file=self.keyfile, |
| 115 | cert_file=self.certfile, |
| 116 | timeout=self.__timeout, |
| 117 | context=self.context) |
| 118 | if self.using_proxy(): |
| 119 | self.__http.set_tunnel(self.realhost, self.realport, |
| 120 | {"Proxy-Authorization": self.proxy_auth}) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 121 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 122 | def close(self): |
| 123 | self.__http.close() |
| 124 | self.__http = None |
| 125 | self.__http_response = None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 126 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 127 | def isOpen(self): |
| 128 | return self.__http is not None |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 129 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 130 | def setTimeout(self, ms): |
| 131 | if not hasattr(socket, 'getdefaulttimeout'): |
| 132 | raise NotImplementedError |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 133 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 134 | if ms is None: |
| 135 | self.__timeout = None |
| 136 | else: |
| 137 | self.__timeout = ms / 1000.0 |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 138 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 139 | def setCustomHeaders(self, headers): |
| 140 | self.__custom_headers = headers |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 141 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 142 | def read(self, sz): |
| 143 | return self.__http_response.read(sz) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 144 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 145 | def write(self, buf): |
| 146 | self.__wbuf.write(buf) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 147 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 148 | def __withTimeout(f): |
| 149 | def _f(*args, **kwargs): |
| 150 | orig_timeout = socket.getdefaulttimeout() |
| 151 | socket.setdefaulttimeout(args[0].__timeout) |
| 152 | try: |
| 153 | result = f(*args, **kwargs) |
| 154 | finally: |
| 155 | socket.setdefaulttimeout(orig_timeout) |
| 156 | return result |
| 157 | return _f |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 158 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 159 | def flush(self): |
| 160 | if self.isOpen(): |
| 161 | self.close() |
| 162 | self.open() |
David Reiss | 7c1f6f8 | 2009-03-24 20:10:24 +0000 | [diff] [blame] | 163 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 164 | # Pull data out of buffer |
| 165 | data = self.__wbuf.getvalue() |
| 166 | self.__wbuf = BytesIO() |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 167 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 168 | # HTTP request |
Martin Wilck | 1ac0a80 | 2016-04-27 09:41:03 +0200 | [diff] [blame] | 169 | if self.using_proxy() and self.scheme == "http": |
| 170 | # need full URL of real host for HTTP proxy here (HTTPS uses CONNECT tunnel) |
| 171 | self.__http.putrequest('POST', "http://%s:%s%s" % |
| 172 | (self.realhost, self.realport, self.path)) |
| 173 | else: |
| 174 | self.__http.putrequest('POST', self.path) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 175 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 176 | # Write headers |
| 177 | self.__http.putheader('Content-Type', 'application/x-thrift') |
| 178 | self.__http.putheader('Content-Length', str(len(data))) |
Martin Wilck | 1ac0a80 | 2016-04-27 09:41:03 +0200 | [diff] [blame] | 179 | if self.using_proxy() and self.scheme == "http" and self.proxy_auth is not None: |
| 180 | self.__http.putheader("Proxy-Authorization", self.proxy_auth) |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 181 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 182 | if not self.__custom_headers or 'User-Agent' not in self.__custom_headers: |
| 183 | user_agent = 'Python/THttpClient' |
| 184 | script = os.path.basename(sys.argv[0]) |
| 185 | if script: |
| 186 | user_agent = '%s (%s)' % (user_agent, urllib.parse.quote(script)) |
| 187 | self.__http.putheader('User-Agent', user_agent) |
Roger Meier | 3f5a264 | 2012-04-13 14:20:08 +0000 | [diff] [blame] | 188 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 189 | if self.__custom_headers: |
| 190 | for key, val in six.iteritems(self.__custom_headers): |
| 191 | self.__http.putheader(key, val) |
Roger Meier | fa392e9 | 2012-04-11 22:15:15 +0000 | [diff] [blame] | 192 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 193 | self.__http.endheaders() |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 194 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 195 | # Write payload |
| 196 | self.__http.send(data) |
Mark Slee | bd8b991 | 2007-02-27 20:17:00 +0000 | [diff] [blame] | 197 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 198 | # Get reply to flush the request |
| 199 | self.__http_response = self.__http.getresponse() |
| 200 | self.code = self.__http_response.status |
| 201 | self.message = self.__http_response.reason |
| 202 | self.headers = self.__http_response.msg |
David Reiss | ff3d249 | 2010-03-09 05:19:16 +0000 | [diff] [blame] | 203 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 204 | # Decorate if we know how to timeout |
| 205 | if hasattr(socket, 'getdefaulttimeout'): |
| 206 | flush = __withTimeout(flush) |