|
1 | 1 | """Utilities""" |
2 | | -import logging |
3 | | -import secrets |
4 | 2 | from http.cookiejar import Cookie |
5 | 3 | from http.cookiejar import http2time |
| 4 | +import logging |
| 5 | +import secrets |
6 | 6 | from urllib.parse import parse_qs |
7 | 7 | from urllib.parse import urlsplit |
8 | 8 | from urllib.parse import urlunsplit |
|
16 | 16 | from idpyoidc.defaults import BASECHR |
17 | 17 | from idpyoidc.exception import UnSupported |
18 | 18 | from idpyoidc.util import importer |
19 | | - |
20 | 19 | from .exception import TimeFormatError |
21 | 20 | from .exception import WrongContentType |
22 | 21 |
|
@@ -202,7 +201,7 @@ def verify_header(reqresp, body_type): |
202 | 201 | logger.debug("resp.txt: %s" % (sanitize(reqresp.text),)) |
203 | 202 |
|
204 | 203 | try: |
205 | | - _ctype = reqresp.headers["content-type"] |
| 204 | + _ctype = get_content_type(reqresp) |
206 | 205 | except KeyError: |
207 | 206 | if body_type: |
208 | 207 | return body_type |
@@ -249,45 +248,54 @@ def verify_header(reqresp, body_type): |
249 | 248 | return body_type |
250 | 249 |
|
251 | 250 |
|
252 | | -def get_deserialization_method(reqresp): |
253 | | - """ |
254 | | -
|
255 | | - :param reqresp: Class instance with attributes: ['status', 'text', |
256 | | - 'headers', 'url'] |
257 | | - :return: Verified body content type |
258 | | - """ |
| 251 | +def get_content_type(reqresp) -> str: |
259 | 252 | logger.debug("resp.headers: %s" % (sanitize(reqresp.headers),)) |
260 | 253 | logger.debug("resp.txt: %s" % (sanitize(reqresp.text),)) |
| 254 | + ctype = reqresp.headers.get("content-type") |
261 | 255 |
|
262 | | - _ctype = reqresp.headers.get("content-type") |
263 | | - if not _ctype: |
| 256 | + if not ctype: |
264 | 257 | # let's try to detect the format |
265 | 258 | try: |
266 | 259 | reqresp.json() |
267 | | - return "json" |
| 260 | + return "application/json" |
268 | 261 | except Exception: |
269 | 262 | try: |
270 | 263 | _jwt = factory(reqresp.txt) |
271 | | - return "jwt" |
| 264 | + return "application/jwt" |
272 | 265 | except Exception: |
273 | | - return "urlencoded" # reasonable default ?? |
274 | | - elif ';' in _ctype: |
275 | | - for _typ in _ctype.split(";"): |
| 266 | + try: |
| 267 | + _info = parse_qs(reqresp.txt) |
| 268 | + return "application/x-www-form-urlencoded" |
| 269 | + except Exception: |
| 270 | + return "text/html" # reasonable default ?? |
| 271 | + elif ';' in ctype: |
| 272 | + for _typ in ctype.split(";"): |
276 | 273 | if _typ.startswith("application") or _typ.startswith("text"): |
277 | | - _ctype = _typ |
| 274 | + ctype = _typ |
278 | 275 | break |
279 | 276 |
|
280 | | - if match_to_("application/json", _ctype) or match_to_("application/jrd+json", _ctype): |
| 277 | + return ctype |
| 278 | + |
| 279 | + |
| 280 | +def get_deserialization_method(ctype): |
| 281 | + """ |
| 282 | +
|
| 283 | + :param reqresp: Class instance with attributes: ['status', 'text', |
| 284 | + 'headers', 'url'] |
| 285 | + :return: Verified body content type |
| 286 | + """ |
| 287 | + |
| 288 | + if match_to_("application/json", ctype) or match_to_("application/jrd+json", ctype): |
281 | 289 | deser_method = "json" |
282 | | - elif match_to_("application/jwt", _ctype): |
| 290 | + elif match_to_("application/jwt", ctype): |
283 | 291 | deser_method = "jwt" |
284 | | - elif match_to_("application/jose", _ctype): |
| 292 | + elif match_to_("application/jose", ctype): |
285 | 293 | deser_method = "jose" |
286 | | - elif match_to_(URL_ENCODED, _ctype): |
| 294 | + elif match_to_(URL_ENCODED, ctype): |
287 | 295 | deser_method = "urlencoded" |
288 | | - elif match_to_("text/plain", _ctype) or match_to_("test/html", _ctype): |
| 296 | + elif match_to_("text/plain", ctype) or match_to_("test/html", ctype): |
289 | 297 | deser_method = "" |
290 | | - elif _ctype.startswith("application/") and _ctype.endswith("+jwt"): |
| 298 | + elif ctype.startswith("application/") and ctype.endswith("+jwt"): |
291 | 299 | deser_method = "jwt" |
292 | 300 | else: |
293 | 301 | deser_method = "" # reasonable default ?? |
|
0 commit comments