|
19 | 19 | from apiserver import utils |
20 | 20 | from apiserver.report.handler.report_handler_interface import IReportHandler |
21 | 21 | from apiserver.report.report_handler_factory import ReportHandler |
22 | | - |
| 22 | +import gzip |
23 | 23 | logger = logging.getLogger('dongtai.openapi') |
24 | 24 |
|
25 | 25 |
|
@@ -151,7 +151,8 @@ def save_method_call(self, pool_sign, current_version_agents): |
151 | 151 | query_params=self.http_query_string, |
152 | 152 | http_protocol=self.http_protocol) |
153 | 153 | method_pool.res_header = utils.base64_decode(self.http_res_header) |
154 | | - method_pool.res_body = self.http_res_body |
| 154 | + method_pool.res_body = decode_content( |
| 155 | + self.http_res_body, get_content_encoding(self.http_req_header)) |
155 | 156 | method_pool.uri_sha1 = self.sha1(self.http_uri) |
156 | 157 | method_pool.save(update_fields=[ |
157 | 158 | 'update_time', |
@@ -225,3 +226,25 @@ def sha1(raw): |
225 | 226 | h = sha1() |
226 | 227 | h.update(raw.encode('utf-8')) |
227 | 228 | return h.hexdigest() |
| 229 | + |
| 230 | + |
| 231 | +def decode_content(body, content_type): |
| 232 | + if content_type == 'gzip': |
| 233 | + try: |
| 234 | + return gzip.decompress(bytes(body, encoding='utf-8')) |
| 235 | + except: |
| 236 | + logger.error('not gzip type but using gzip as content_encoding') |
| 237 | + return body |
| 238 | + return body |
| 239 | + |
| 240 | + |
| 241 | +def get_content_encoding(header): |
| 242 | + headers = SaasMethodPoolHandler.parse_headers(header) |
| 243 | + for header in headers: |
| 244 | + try: |
| 245 | + k, v = header.strip().split(':') |
| 246 | + if k.lower() == 'content-encoding': |
| 247 | + return v |
| 248 | + except: |
| 249 | + pass |
| 250 | + return '' |
0 commit comments