Skip to content

Commit 633118d

Browse files
authored
Merge pull request #114 from jschlyter/check_content_type
Correctly check content type
2 parents 97bff84 + 8ef4566 commit 633118d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/cryptojwt/key_bundle.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .jwk.rsa import RSAKey
3131
from .jwk.rsa import new_rsa_key
3232
from .utils import as_unicode
33+
from .utils import check_content_type
3334
from .utils import httpc_params_loader
3435

3536
__author__ = "Roland Hedberg"
@@ -513,8 +514,8 @@ def _parse_remote_response(self, response):
513514
"""
514515
# Check if the content type is the right one.
515516
try:
516-
if response.headers["Content-Type"] != "application/json":
517-
LOGGER.warning("Wrong Content_type (%s)", response.headers["Content-Type"])
517+
if not check_content_type(response.headers["Content-Type"], "application/json"):
518+
LOGGER.warning("Wrong Content_type (%s)", respeonse.headers["Content-Type"])
518519
except KeyError:
519520
pass
520521

src/cryptojwt/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import cgi
23
import functools
34
import importlib
45
import json
@@ -264,3 +265,9 @@ def httpc_params_loader(httpc_params):
264265
if "timeout" not in httpc_params:
265266
httpc_params["timeout"] = DEFAULT_HTTPC_TIMEOUT
266267
return httpc_params
268+
269+
270+
def check_content_type(content_type, mime_type):
271+
"""Return True if the content type contains the MIME type"""
272+
mt, _ = cgi.parse_header(content_type)
273+
return mime_type == mt

tests/test_31_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from cryptojwt.utils import check_content_type
2+
3+
4+
def test_check_content_type():
5+
assert check_content_type(content_type="application/json", mime_type="application/json") == True
6+
assert (
7+
check_content_type(
8+
content_type="application/json; charset=utf-8", mime_type="application/json"
9+
)
10+
== True
11+
)
12+
assert (
13+
check_content_type(
14+
content_type="application/html; charset=utf-8", mime_type="application/json"
15+
)
16+
== False
17+
)

0 commit comments

Comments
 (0)