Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch/match exactly #165

Merged
merged 3 commits into from Jul 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions flask_cors/core.py
Expand Up @@ -260,15 +260,14 @@ def try_match_any(inst, patterns):
return any(try_match(inst, pattern) for pattern in patterns)


def try_match(request_origin, pattern):
def try_match(request_origin, maybe_regex):
"""Safely attempts to match a pattern or string to a request origin."""
try:
if isinstance(pattern, RegexObject):
return re.match(pattern, request_origin)
else:
return re.match(pattern, request_origin, flags=re.IGNORECASE)
except:
return request_origin == pattern
if isinstance(maybe_regex, RegexObject):
return re.match(maybe_regex, request_origin)
elif probably_regex(maybe_regex):
return re.match(maybe_regex, request_origin, flags=re.IGNORECASE)
else:
return request_origin == maybe_regex


def get_cors_options(appInstance, *dicts):
Expand Down
3 changes: 2 additions & 1 deletion tests/core/helper_tests.py
Expand Up @@ -21,7 +21,8 @@

class InternalsTestCase(unittest.TestCase):
def test_try_match(self):
self.assertTrue(try_match('www.com/foo+', 'www.com/foo'))
self.assertFalse(try_match('www.com/foo', 'www.com/fo'))
self.assertTrue(try_match('www.com/foo', 'www.com/fo*'))

def test_flexible_str_str(self):
self.assertEquals(flexible_str('Bar, Foo, Qux'), 'Bar, Foo, Qux')
Expand Down