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