Skip to content

Commit f89a7e4

Browse files
committed
解决返回内容过小也会压缩的问题
1 parent d1e8e64 commit f89a7e4

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

core/cluster.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ async def _(request: web.Request, size: int, config: web.ResponseConfiguration):
14201420
request.get_url_params().get("s") or "",
14211421
request.get_url_params().get("e") or "",
14221422
):
1423-
yield web.Response(status_code=403)
1423+
yield web.Response("Forbidden", status_code=403)
14241424
return
14251425
config.length = size * 1024 * 1024
14261426
for _ in range(size):
@@ -1444,14 +1444,14 @@ async def _(request: web.Request, hash: str, config: web.ResponseConfiguration):
14441444
size = 0,
14451445
type = FileContentType.EMPTY
14461446
), 0, request.get_ip(), request.get_user_agent(), statistics.Status.FORBIDDEN)
1447-
return web.Response(status_code=403)
1447+
return web.Response("Forbidden", status_code=403)
14481448
if not storages.available_width:
14491449
statistics.hit(None, File(
14501450
hash = hash,
14511451
size = 0,
14521452
type = FileContentType.EMPTY
14531453
), 0, request.get_ip(), request.get_user_agent(), statistics.Status.ERROR)
1454-
return web.Response(status_code=503)
1454+
return web.Response("No available storage", status_code=503)
14551455
start_bytes, end_bytes = 0, None
14561456
range_str = await request.get_headers("range", "")
14571457
range_match = re.search(r"bytes=(\d+)-(\d+)", range_str, re.S) or re.search(
@@ -1470,7 +1470,7 @@ async def _(request: web.Request, hash: str, config: web.ResponseConfiguration):
14701470
hash, start_bytes, end_bytes, request.get_ip(), request.get_user_agent()
14711471
)
14721472
if not data:
1473-
return web.Response(status_code=404)
1473+
return web.Response("Not Found", status_code=404)
14741474
config.access_log = DOWNLOAD_ACCESS_LOG
14751475
if data.is_url() and isinstance(data.get_path(), str):
14761476
return web.RedirectResponse(str(data.get_path()), response_configuration=config).set_headers(name)
@@ -1539,12 +1539,12 @@ async def _(request: web.Request):
15391539
try:
15401540
info = json.loads(base64.b64decode(auth))
15411541
except:
1542-
return web.Response(status_code=401)
1542+
return web.Response("Unauthorized", status_code=401)
15431543
if (
15441544
info["username"] != DASHBOARD_USERNAME
15451545
or info["password"] != DASHBOARD_PASSWORD
15461546
):
1547-
return web.Response(status_code=401)
1547+
return web.Response("Unauthorized", status_code=401)
15481548
token = dashboard.generate_token(request)
15491549
return web.Response(
15501550
DASHBOARD_USERNAME,

core/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
DOWNLOAD_CONFIGURATION: bool = True
6161
RESPONSE_DATE = "%a, %d %b %Y %H:%M:%S GMT"
6262
RESPONSE_COMPRESSION_IGNORE_SIZE_THRESHOLD: int = 16777216
63+
RESPONSE_COMPRESSION_MIN_THRESHOLD: int = 1024
6364
SKIP_FILE_CHECK: bool = False
6465
STATUS_CODES: dict[int, str] = {
6566
100: "Continue",

core/web.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ def _get_content_type(self, content):
769769

770770
async def _iter(self):
771771
if isinstance(self.content, (str, int, float)):
772+
self.content_type = "text/plain"
772773
yield str(self.content).encode("utf-8")
773774
elif isinstance(self.content, (dict, list, tuple, bool)):
774775
self.content_type = "application/json"
@@ -783,8 +784,10 @@ async def _iter(self):
783784
yield b""
784785

785786
async def raw(self):
786-
if isinstance(self.content, Coroutine):
787+
if isinstance(self.content, Coroutine) or asyncio.iscoroutinefunction(self.content):
787788
self.content = await self.content
789+
if asyncio.iscoroutine(self.content):
790+
self.content = await self.content()
788791
if isinstance(self.content, Response):
789792
self.status_code = self.content.status_code
790793
self.content_type = self.content.content_type
@@ -793,7 +796,7 @@ async def raw(self):
793796
self._kwargs = self.content._kwargs
794797
self._response_configuration = self.content._response_configuration
795798
self.content = self.content.content
796-
if isinstance(self.content, (Coroutine, Response)):
799+
if isinstance(self.content, (Coroutine, Response)) or asyncio.iscoroutinefunction(self.content) or asyncio.iscoroutine(self.content):
797800
await self.raw()
798801

799802
async def __call__(
@@ -822,7 +825,7 @@ async def __call__(
822825
if isinstance(content, Path):
823826
self.content_type = self.content_type or self._get_content_type(content)
824827
length = content.stat().st_size
825-
if length <= RESPONSE_COMPRESSION_IGNORE_SIZE_THRESHOLD:
828+
if RESPONSE_COMPRESSION_MIN_THRESHOLD <= length <= RESPONSE_COMPRESSION_IGNORE_SIZE_THRESHOLD:
826829
async with aiofiles.open(content, "rb") as r:
827830
content = io.BytesIO(await r.read())
828831
elif isinstance(content, io.BytesIO):
@@ -852,7 +855,7 @@ async def __call__(
852855
)
853856
self.status_code = 206 if start_bytes > 0 else 200
854857
length = length - start_bytes
855-
if isinstance(content, io.BytesIO) and len(content.getbuffer()) != 0:
858+
if isinstance(content, io.BytesIO) and len(content.getbuffer()) != 0 and RESPONSE_COMPRESSION_MIN_THRESHOLD <= len(content.getbuffer()) <= RESPONSE_COMPRESSION_IGNORE_SIZE_THRESHOLD:
856859
self.content_type = self.content_type or self._get_content_type(content)
857860
compression: Compressor = compressor(
858861
await request.get_headers("Accept-Encoding", ""),

0 commit comments

Comments
 (0)