修复不走jsdelivr时分支文件出现的问题

修改python版替换最多一次
pull/30/head
hunshnet 2020-05-14 20:37:10 +08:00
parent 8fbe0e87e1
commit 9963eab51c
2 changed files with 14 additions and 7 deletions

View File

@ -34,23 +34,25 @@ def proxy(u):
u = u if u.startswith('http') else 'https://' + u u = u if u.startswith('http') else 'https://' + u
u = u.replace(':/g', '://g', 1) # uwsgi会将//传递为/ u = u.replace(':/g', '://g', 1) # uwsgi会将//传递为/
if jsdelivr and exp2.match(u): if jsdelivr and exp2.match(u):
u = u.replace('/blob/', '@').replace('github.com', 'cdn.jsdelivr.net/gh') u = u.replace('/blob/', '@', 1).replace('github.com', 'cdn.jsdelivr.net/gh', 1)
return redirect(u) return redirect(u)
elif cnpmjs and exp3.match(u): elif cnpmjs and exp3.match(u):
u = u.replace('github.com', 'github.com.cnpmjs.org') + request.url.replace(request.base_url, '') u = u.replace('github.com', 'github.com.cnpmjs.org', 1) + request.url.replace(request.base_url, '', 1)
return redirect(u) return redirect(u)
elif jsdelivr and exp4.match(u): elif jsdelivr and exp4.match(u):
u = re.sub(r'\.com/.*?/.+?/(.+?/)', '@$1', u, 1) u = re.sub(r'\.com/.*?/.+?/(.+?/)', '@$1', u, 1)
u = u.replace('raw.githubusercontent.com', 'cdn.jsdelivr.net/gh') u = u.replace('raw.githubusercontent.com', 'cdn.jsdelivr.net/gh', 1)
return redirect(u) return redirect(u)
else: else:
if exp2.match(u):
u = u.replace('/blob/', '/raw/', 1)
headers = {} headers = {}
r_headers = {} r_headers = {}
for i in ['Range', 'User-Agent']: for i in ['Range', 'User-Agent']:
if i in request.headers: if i in request.headers:
r_headers[i] = request.headers.get(i) r_headers[i] = request.headers.get(i)
try: try:
r = requests.request(method=request.method, url=u + request.url.replace(request.base_url, ''), data=request.data, headers=r_headers, stream=True) r = requests.request(method=request.method, url=u + request.url.replace(request.base_url, '', 1), data=request.data, headers=r_headers, stream=True)
for i in ['Content-Type']: for i in ['Content-Type']:
if i in r.headers: if i in r.headers:
headers[i] = r.headers.get(i) headers[i] = r.headers.get(i)

View File

@ -69,11 +69,16 @@ async function fetchHandler(e) {
const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/i const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/i
const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$/i const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$/i
const exp4 = /^(?:https?:\/\/)?raw\.githubusercontent\.com\/.+?\/.+?\/.+?\/.+$/i const exp4 = /^(?:https?:\/\/)?raw\.githubusercontent\.com\/.+?\/.+?\/.+?\/.+$/i
if (path.search(exp1) === 0 || !Config.jsdelivr && path.search(exp2) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4))) { if (path.search(exp1) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4))) {
return httpHandler(req, path) return httpHandler(req, path)
} else if (path.search(exp2) === 0) { } else if (path.search(exp2) === 0) {
if (Config.jsdelivr){
const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh') const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
return Response.redirect(newUrl, 302) return Response.redirect(newUrl, 302)
}else{
path = path.replace('/blob/', '/raw/')
return httpHandler(req, path)
}
} else if (path.search(exp3) === 0) { } else if (path.search(exp3) === 0) {
const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org') const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org')
return Response.redirect(newUrl, 302) return Response.redirect(newUrl, 302)