From 486206bd32a9186a0020e45734137bd29f8b30fb Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Sun, 16 Aug 2020 16:06:34 -0400 Subject: [PATCH 1/2] #24 make static paths relative --- cellxgene_gateway/cache_entry.py | 25 ++++++++++++++++--------- cellxgene_gateway/cache_key.py | 7 ------- tests/test_cache_entry.py | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 tests/test_cache_entry.py diff --git a/cellxgene_gateway/cache_entry.py b/cellxgene_gateway/cache_entry.py index c48461c..cab230d 100644 --- a/cellxgene_gateway/cache_entry.py +++ b/cellxgene_gateway/cache_entry.py @@ -12,13 +12,13 @@ import datetime from flask import make_response, request, render_template from requests import get, post, put +import re from cellxgene_gateway import env from cellxgene_gateway.cellxgene_exception import CellxgeneException from cellxgene_gateway.util import current_time_stamp from cellxgene_gateway.flask_util import querystring - class CacheEntry: def __init__( self, @@ -97,8 +97,20 @@ class CacheEntry: ) self.status = "terminated" + def rewrite_text_content(self, cellxgene_content): + # for v0.16.0 compatibility, see issue #24 + gateway_content = re.sub('(="|\()/static/', r'\1static/', cellxgene_content).replace( + "http://fonts.gstatic.com", "https://fonts.gstatic.com" + ).replace(self.cellxgene_basepath(), self.gateway_basepath()) + return gateway_content + + def gateway_basepath(self): + return f"{env.external_protocol}://{env.external_host}/view/{self.key.pathpart}/" + def cellxgene_basepath(self): + return f"http://127.0.0.1:{self.port}" + def serve_content(self, path): - gateway_basepath = f"{env.external_protocol}://{env.external_host}/view/{self.key.pathpart}/" + gateway_basepath = self.gateway_basepath() subpath = path[len(self.key.pathpart) :] # noqa: E203 if len(subpath) == 0: @@ -113,8 +125,6 @@ class CacheEntry: all_output=self.all_output, ) - port = self.port - cellxgene_basepath = f"http://127.0.0.1:{port}" headers = {} copy_headers = [ "accept", @@ -137,7 +147,7 @@ class CacheEntry: if h in request.headers: headers[h] = request.headers[h] - full_path = cellxgene_basepath + subpath + querystring() + full_path = self.cellxgene_basepath() + subpath + querystring() if request.method in ["GET", "HEAD", "OPTIONS"]: cellxgene_response = get(full_path, headers=headers) @@ -155,10 +165,7 @@ class CacheEntry: ) content_type = cellxgene_response.headers["content-type"] if "text" in content_type: - cellxgene_content = cellxgene_response.content.decode() - gateway_content = cellxgene_content.replace( - "http://fonts.gstatic.com", "https://fonts.gstatic.com" - ).replace(cellxgene_basepath, gateway_basepath) + gateway_content = self.rewrite_text_content(cellxgene_response.content.decode()) else: gateway_content = cellxgene_response.content diff --git a/cellxgene_gateway/cache_key.py b/cellxgene_gateway/cache_key.py index 2f9a3ae..7b76a36 100644 --- a/cellxgene_gateway/cache_key.py +++ b/cellxgene_gateway/cache_key.py @@ -7,13 +7,6 @@ # OR CONDITIONS OF ANY KIND, either express or implied. See the License for # the specific language governing permissions and limitations under the License. -import os - -from flask_api import status - -from cellxgene_gateway import env -from cellxgene_gateway.cellxgene_exception import CellxgeneException - # There are three kinds of CacheKey: # 1) somedir/dataset.h5ad: a dataset # in this case, pathpart == dataset == 'somedir/dataset.h5ad' diff --git a/tests/test_cache_entry.py b/tests/test_cache_entry.py new file mode 100644 index 0000000..8afab85 --- /dev/null +++ b/tests/test_cache_entry.py @@ -0,0 +1,16 @@ +import unittest +from cellxgene_gateway.cache_entry import CacheEntry +from cellxgene_gateway.cache_key import CacheKey + +key = CacheKey('path/part', 'dataset', 'tmp.csv') + +class TestRenderEntry(unittest.TestCase): + def test_GIVEN_absolute_static_url_THEN_return_relative(self): + actual = CacheEntry.for_key(key, 8000).rewrite_text_content("src:url(/static/assets/") + expected = "src:url(static/assets/" + self.assertEqual(actual, expected) + def test_GIVEN_absolute_src_THEN_return_relative(self): + actual = CacheEntry.for_key(key, 8000).rewrite_text_content('') + expected = '' + self.assertEqual(actual, expected) + From 8cdd23a57b4e5ae1cbd29421c9e8b103329127b6 Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Sun, 16 Aug 2020 16:17:28 -0400 Subject: [PATCH 2/2] #24 switched to absolute paths relative paths won't necessarily work in css files --- cellxgene_gateway/cache_entry.py | 2 +- tests/test_cache_entry.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cellxgene_gateway/cache_entry.py b/cellxgene_gateway/cache_entry.py index cab230d..60563a1 100644 --- a/cellxgene_gateway/cache_entry.py +++ b/cellxgene_gateway/cache_entry.py @@ -99,7 +99,7 @@ class CacheEntry: def rewrite_text_content(self, cellxgene_content): # for v0.16.0 compatibility, see issue #24 - gateway_content = re.sub('(="|\()/static/', r'\1static/', cellxgene_content).replace( + gateway_content = re.sub('(="|\()/static/', f'\\1{self.gateway_basepath()}static/', cellxgene_content).replace( "http://fonts.gstatic.com", "https://fonts.gstatic.com" ).replace(self.cellxgene_basepath(), self.gateway_basepath()) return gateway_content diff --git a/tests/test_cache_entry.py b/tests/test_cache_entry.py index 8afab85..c5ec12e 100644 --- a/tests/test_cache_entry.py +++ b/tests/test_cache_entry.py @@ -2,15 +2,15 @@ import unittest from cellxgene_gateway.cache_entry import CacheEntry from cellxgene_gateway.cache_key import CacheKey -key = CacheKey('path/part', 'dataset', 'tmp.csv') +key = CacheKey('czi/pbmc3k.h5ad', 'pbmc3k.h5ad', 'tmp.csv') class TestRenderEntry(unittest.TestCase): - def test_GIVEN_absolute_static_url_THEN_return_relative(self): + def test_GIVEN_absolute_static_url_THEN_include_path(self): actual = CacheEntry.for_key(key, 8000).rewrite_text_content("src:url(/static/assets/") - expected = "src:url(static/assets/" + expected = "src:url(http://localhost:5005/view/czi/pbmc3k.h5ad/static/assets/" self.assertEqual(actual, expected) - def test_GIVEN_absolute_src_THEN_return_relative(self): + def test_GIVEN_absolute_src_THEN_include_path(self): actual = CacheEntry.for_key(key, 8000).rewrite_text_content('') - expected = '' + expected = '' self.assertEqual(actual, expected)