Skip to content

Commit

Permalink
Merge pull request #737 from wklken/fix_20221018
Browse files Browse the repository at this point in the history
fix(responseandproxy): force_ee_response & proxy response headers bug
  • Loading branch information
wklken authored Oct 18, 2022
2 parents f9ae411 + c8d4e5b commit 717a224
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/api/bkuser_core/common/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,23 @@ def _should_use_raw_response(self, req: "HttpRequest", resp: "Response") -> bool

return False

def _is_response_ee_format(self, resp: "Response") -> bool:
"""返回值是否符合企业版格式"""
if getattr(resp, "data", None) is None:
return False

required_keys = ["result", "message", "code", "data"]
for key in required_keys:
if key not in resp.data:
return False

return True

def _force_ee_response(self, resp: "Response") -> "Response":
"""强制刷返回值"""
if self._is_response_ee_format(resp):
return resp

# 来自缓存的 response 没有对应属性
# 由于 response 已经被渲染,并不需要 callback
# FIXME: 这里204的处理有问题, 强制渲染会导致204卡主(一直不结束)
Expand Down
21 changes: 19 additions & 2 deletions src/saas/bkuser_shell/proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get_api_path(request) -> str:
if settings.SITE_URL == "/":
return request.path

return "/" + request.path.replace(settings.SITE_URL, "")
return "/" + request.path.replace(settings.SITE_URL, "").lstrip("/")

def do_proxy(self, request, rewrite_path=None):
try:
Expand Down Expand Up @@ -123,13 +123,30 @@ def _do_call(self, request, rewrite_path=None):

content = resp.content
status_code = resp.status_code

# 无权限, 改状态码为403
if b"auth_infos" in content and b"callback_url" in content:
status_code = status.HTTP_403_FORBIDDEN

resp_headers = resp.headers
if "Content-Encoding" in resp_headers:
resp_headers.pop("Content-Encoding")

logger.debug(
"proxy request[method=%s,url=%s,params=%s,data=%s,headers=%s] response[status=%s,headers=%s,content=%s]",
method,
url,
params,
data,
headers,
status_code,
content,
resp_headers,
)

# DONT'T set the content_type here!
return HttpResponse(
content,
status=status_code,
headers=resp.headers,
headers=resp_headers,
)

0 comments on commit 717a224

Please sign in to comment.