From 4c46a14ba3fcd2e5dd6f67c2c711abb464fb275e Mon Sep 17 00:00:00 2001 From: Drol Date: Fri, 18 Aug 2023 12:48:54 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=9B=B4=E6=96=B0=E5=8F=8D=E4=BB=A3?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=EF=BC=8C=E8=A7=A3=E5=86=B3=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E6=9C=89=E6=97=B6=E7=9B=B8?= =?UTF-8?q?=E5=BA=94400=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 有时刷新会出现400错误,文件列表无法加载。在本地使用curl和fetch对原接口发起访问都没有问题,确定官方接口参数没有变更。最后调试发现在cloudflare的worker脚本发起请求有机率会响应400。而且只有带filters参数的文件列表接口才有问题,filters参数是一个在url查询参数中的json对象。猜测cloudflare运行脚本的各个服务器的fetch版本不一致,对url中的encode后的json处理方式有区别。原先脚本是直接将请求前端请求url字符串作为fetch第一个参数。现在改成先new URL对象,再将对象作为fetch参数,这个时候console.log观察URL对象的searchParams解析正常。而fetch对URL对象的解析在各版本中应该比较稳定一致,所以没有出现400的问题了 --- cf-worker/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cf-worker/index.js b/cf-worker/index.js index 24054e7b8..adb3c30d9 100644 --- a/cf-worker/index.js +++ b/cf-worker/index.js @@ -71,7 +71,7 @@ addEventListener('fetch', event => { } } // 发起 fetch - let fr = (await fetch(url, fp)); + let fr = (await fetch(new URL(url), fp)); outCt = fr.headers.get('content-type'); if(outCt && (outCt.includes('application/text') || outCt.includes('text/html'))) { try { @@ -132,4 +132,4 @@ addEventListener('fetch', event => { let len = blocker.keys.filter(x => url.includes(x)).length; return len != 0; } - } \ No newline at end of file + }