From 09df0e72a1b3f72b61f53f9871b505a608efe81c Mon Sep 17 00:00:00 2001 From: Elias Gabriel Date: Tue, 27 Jul 2021 10:57:54 -0700 Subject: [PATCH] [CAT-103] FIX: Support gzip deserialization (#116) * ADD: gzip as a valid MIME type * REMOVE: gzip decompression check by extension --- indico/http/client.py | 21 ++++++++---------- indico/http/serialization.py | 15 +++++++------ tests/unit/http/mocked_data/application_gzip_ | Bin 0 -> 1183 bytes tests/unit/http/test_unit_serialization.py | 15 +++++++++---- 4 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 tests/unit/http/mocked_data/application_gzip_ diff --git a/indico/http/client.py b/indico/http/client.py index 08c6dbb3..900b7ef0 100644 --- a/indico/http/client.py +++ b/indico/http/client.py @@ -1,16 +1,17 @@ -import logging -import requests import http.cookiejar -from pathlib import Path -from contextlib import contextmanager +import logging from collections import defaultdict +from contextlib import contextmanager +from copy import deepcopy +from pathlib import Path from typing import Union + +import requests +from indico.client.request import HTTPRequest from indico.config import IndicoConfig +from indico.errors import IndicoAuthenticationFailed, IndicoRequestError from indico.http.serialization import deserialize -from indico.errors import IndicoRequestError, IndicoAuthenticationFailed -from indico.client.request import HTTPRequest from requests import Response -from copy import deepcopy logger = logging.getLogger(__file__) @@ -128,14 +129,10 @@ def _make_request( # code, api_response = url_parts = path.split(".") json = False - gzip = False if len(url_parts) > 1 and (url_parts[-1] == "json" or url_parts[-2] == "json"): json = True - if url_parts[-1] == "gz": - gzip = True - - content = deserialize(response, force_json=json, gzip=gzip) + content = deserialize(response, force_json=json) # If auth expired refresh if response.status_code == 401 and not _refreshed: diff --git a/indico/http/serialization.py b/indico/http/serialization.py index 23f8046e..a89f90e7 100644 --- a/indico/http/serialization.py +++ b/indico/http/serialization.py @@ -1,15 +1,15 @@ """ Handles deserialization / decoding of responses """ -from collections import defaultdict import cgi -import json -import io import gzip +import io +import json import logging -import msgpack import traceback +from collections import defaultdict +import msgpack from indico.errors import IndicoDecodingError logger = logging.getLogger(__name__) @@ -25,11 +25,10 @@ def raw_bytes(content, *args, **kwargs): return content -def deserialize(response, gzip=False, force_json=False): +def deserialize(response, force_json=False): content_type, params = cgi.parse_header(response.headers.get("Content-Type")) - content = None - if gzip: + if content_type in ["application/x-gzip", "application/gzip"]: content = decompress(response) else: content = response.content @@ -85,5 +84,7 @@ def zip_serialization(content, charset=None): "image/png": image_serialization, "image/jpg": image_serialization, "application/zip": zip_serialization, + "application/x-gzip": raw_bytes, + "application/gzip": raw_bytes, }, ) diff --git a/tests/unit/http/mocked_data/application_gzip_ b/tests/unit/http/mocked_data/application_gzip_ new file mode 100644 index 0000000000000000000000000000000000000000..3e403e4d13ff501f5573b30dff476babd882f5e8 GIT binary patch literal 1183 zcmV;Q1Yr9giwFP!000001MOGqZre5#&Tj#FhXVrv5+I={>gu2 zGHol6C6A<%v_+qBKlTROi|kN}?bLQtXG_u!)d#~O56N?=Lw+AAJ{o0d!gX5YZ!Z=8 ztvo&`A`E@=bfbXYeO6%1UvYivhaQ%xw?bXd_vs4ICz#6|N~O63SP@yu)jj>b)c!xk zhgMQ_MQaOu>TUp5axHR=^$#DhDx@sr+a%*kp{aE$T1bZICLEx9y+{;7Ni#Ko5+)D77H1k6$|{-xK&O|4ZH5j2}0~ z!};%fXlnTX3~2cOed8|wN8R329RFY9e-O=||14k)|DOY^$9o5xF7a$@^{>DG_|vv* z%LNL>#ieb%cwt*k2bm4dF4sIO`dB|Ib(ZEr0mE)5XgEfCEAZ;t%a^t#@@_TUw%7ts z@) zs_Oyj;BCo;cICbH?HtP@ITae>=?goDqMP!a;s(wrT0@r+v#%j)*M;+H;+3!}2o@zO zn9dWNS0eB00k|~s!E{qv3O+R1{K>X}t4cF36sIz4m}5f5nv5o3hK7{AlC ztu@$&({wn>1mH@;L5hWS=wPV*PVKGs;nK=S*d{E%pO`etVi2Ez-njfhLAg_!yEnD~Rl0NFpXW zndHAGi($P2E`i@L6OXDXQAolde0ELwRHkbOM}rL*6xVQ3i3#Afzj~PCLFuyBLzyWx z3{?(U+xK<7T_sinPYaL(?NgdcRREfu+$I`E2rg!uIk3>qTw|knpRWX z;Yw6Ib$$@pmC$&#N9%r$A9*JI&aY{{ovUL)=v19fH!Lctw&pq zbA5eHa&aY#oFfN@j>OhC>+7`;bjw8Gh}U&H>01i|<7z!0i!u0raLe7gJTW