|
| 1 | +import os |
| 2 | +import logging |
| 3 | + |
| 4 | +import tornado.web |
| 5 | +import tornado.httpclient |
| 6 | + |
| 7 | +from urllib.parse import urlparse |
| 8 | + |
| 9 | +logger = logging.getLogger('subidy-web') |
| 10 | + |
| 11 | + |
| 12 | +def image_proxy_factory(config, core): |
| 13 | + return [ |
| 14 | + (r"/coverart/(?P<id>.+)", ImageProxyHandler, {"core": core, "config": config}) |
| 15 | + ] |
| 16 | + |
| 17 | +class ImageProxyHandler(tornado.web.RequestHandler): |
| 18 | + def initialize(self, core, config): |
| 19 | + self.core = core |
| 20 | + |
| 21 | + subidy_config = config["subidy"] |
| 22 | + self._subsonic_url=subidy_config["url"] |
| 23 | + self._subsonic_username=subidy_config["username"] |
| 24 | + self._subsonic_password=subidy_config["password"] |
| 25 | + |
| 26 | + async def get(self, **kwargs): |
| 27 | + id = kwargs.get('id') |
| 28 | + |
| 29 | + logger.debug('Handle coverart %s request', id) |
| 30 | + |
| 31 | + def handle_response(response): |
| 32 | + if (response.error and not |
| 33 | + isinstance(response.error, tornado.httpclient.HTTPError)): |
| 34 | + self.set_status(500) |
| 35 | + self.write('Internal server error:\n' + str(response.error)) |
| 36 | + else: |
| 37 | + self.set_status(response.code, response.reason) |
| 38 | + self._headers = tornado.httputil.HTTPHeaders() # clear tornado default header |
| 39 | + |
| 40 | + for header, v in response.headers.get_all(): |
| 41 | + if header not in ('Content-Length', 'Transfer-Encoding', 'Content-Encoding', 'Connection'): |
| 42 | + self.add_header(header, v) # some header appear multiple times, eg 'Set-Cookie' |
| 43 | + |
| 44 | + if response.body: |
| 45 | + self.set_header('Content-Length', len(response.body)) |
| 46 | + self.write(response.body) |
| 47 | + self.finish() |
| 48 | + |
| 49 | + try: |
| 50 | + if 'Proxy-Connection' in self.request.headers: |
| 51 | + del self.request.headers['Proxy-Connection'] |
| 52 | + resp = await fetch_request( |
| 53 | + f"{self._subsonic_url}/rest/getCoverArt?id={id}", |
| 54 | + method=self.request.method, headers=self.request.headers, |
| 55 | + follow_redirects=False, allow_nonstandard_methods=False) |
| 56 | + handle_response(resp) |
| 57 | + except tornado.httpclient.HTTPError as e: |
| 58 | + if hasattr(e, 'response') and e.response: |
| 59 | + handle_response(e.response) |
| 60 | + else: |
| 61 | + self.set_status(500) |
| 62 | + self.write('Internal server error:\n' + str(e)) |
| 63 | + self.finish() |
| 64 | + |
| 65 | +def get_proxy(url): |
| 66 | + url_parsed = urlparse(url, scheme='http') |
| 67 | + proxy_key = '%s_proxy' % url_parsed.scheme |
| 68 | + return os.environ.get(proxy_key) |
| 69 | + |
| 70 | + |
| 71 | +def parse_proxy(proxy): |
| 72 | + proxy_parsed = urlparse(proxy, scheme='http') |
| 73 | + return proxy_parsed.hostname, proxy_parsed.port |
| 74 | + |
| 75 | + |
| 76 | +def fetch_request(url, **kwargs): |
| 77 | + proxy = get_proxy(url) |
| 78 | + if proxy: |
| 79 | + logger.debug('Forward request via upstream proxy %s', proxy) |
| 80 | + tornado.httpclient.AsyncHTTPClient.configure( |
| 81 | + 'tornado.curl_httpclient.CurlAsyncHTTPClient') |
| 82 | + host, port = parse_proxy(proxy) |
| 83 | + kwargs['proxy_host'] = host |
| 84 | + kwargs['proxy_port'] = port |
| 85 | + |
| 86 | + req = tornado.httpclient.HTTPRequest(url, **kwargs) |
| 87 | + client = tornado.httpclient.AsyncHTTPClient() |
| 88 | + return client.fetch(req, raise_error=False) |
0 commit comments