Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(responseandproxy): force_ee_response & proxy response headers bug #737

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)