From 1daf2e05c5e73f58f4d0be79d1900f1000656739 Mon Sep 17 00:00:00 2001 From: 7415963987456321 Date: Tue, 19 Oct 2021 23:02:16 +0000 Subject: [PATCH] refactor is_fhost_url to use regex for url matching --- fhost.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/fhost.py b/fhost.py index 8644ade..21f8b8b 100755 --- a/fhost.py +++ b/fhost.py @@ -28,6 +28,7 @@ from hashlib import sha256 from magic import Magic from mimetypes import guess_extension import sys +import re import requests from short_url import UrlEncoder from validators import url as url_valid @@ -207,14 +208,12 @@ class File(db.Model): db.session.commit() return f -def fhost_url(scheme=None): - if not scheme: - return url_for(".fhost", _external=True).rstrip("/") - else: - return url_for(".fhost", _external=True, _scheme=scheme).rstrip("/") - def is_fhost_url(url): - return url.startswith(fhost_url()) or url.startswith(fhost_url("https")) + fhost_url = re.sub(r'https?://', '', url_for(".fhost", _external=True)).rstrip("/") + if re.match(rf'https?://{fhost_url}.*', url): + return True + + return False def shorten(url): if len(url) > app.config["MAX_URL_LENGTH"]: