From 010447c8f21bf6d067f97b433563c8e1cac28db5 Mon Sep 17 00:00:00 2001 From: M09Ic Date: Fri, 6 Jan 2023 01:28:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E5=8A=A0=E5=AE=89=E5=85=A8=E7=9A=84?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E6=8B=BC=E6=8E=A5,=20=E5=9C=A8=E4=B8=8D?= =?UTF-8?q?=E6=94=B9=E5=8F=98`/`=E6=95=B0=E9=87=8F=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B,=20=E5=AE=9E=E7=8E=B0=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E4=B8=94=E6=AD=A3=E7=A1=AE=E7=9A=84=E6=8B=BC=E6=8E=A5path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/option.go | 1 + internal/pool.go | 4 ++-- internal/utils.go | 12 ++++++++++++ pkg/ihttp/request.go | 13 +++++++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/internal/option.go b/internal/option.go index a5c967b..0ab274d 100644 --- a/internal/option.go +++ b/internal/option.go @@ -180,6 +180,7 @@ func (opt *Option) PrepareRunner() (*Runner, error) { if opt.Advance { r.Crawl = true r.Active = true + r.Bak = true opt.AppendRule = append(opt.AppendRule, "filebak") } else if opt.FileBak { opt.AppendRule = append(opt.AppendRule, "filebak") diff --git a/internal/pool.go b/internal/pool.go index 338e126..a4d483c 100644 --- a/internal/pool.go +++ b/internal/pool.go @@ -533,7 +533,7 @@ func (pool *Pool) doActive() { for _, u := range pkg.ActivePath { pool.wg.Add(1) pool.additionCh <- &Unit{ - path: u, + path: safePath(pool.BaseURL, u), source: ActiveSource, } } @@ -553,7 +553,7 @@ func (pool *Pool) doBak() { for w := range worder.C { pool.wg.Add(1) pool.additionCh <- &Unit{ - path: w, + path: safePath(pool.BaseURL, w), source: BakSource, } } diff --git a/internal/utils.go b/internal/utils.go index 61f0ff4..8f7cb68 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -124,3 +124,15 @@ func loadRuleWithFiles(ruleFiles []string, filter string) ([]rule.Expression, er } return rule.Compile(rules.String(), filter).Expressions, nil } + +func safePath(url, path string) string { + urlSlash := strings.HasSuffix(url, "/") + pathSlash := strings.HasPrefix(path, "/") + if !urlSlash && !pathSlash { + return "/" + path + } else if urlSlash && pathSlash { + return path[1:] + } else { + return path + } +} diff --git a/pkg/ihttp/request.go b/pkg/ihttp/request.go index 6fe89d2..8e908bc 100644 --- a/pkg/ihttp/request.go +++ b/pkg/ihttp/request.go @@ -3,15 +3,16 @@ package ihttp import ( "github.com/valyala/fasthttp" "net/http" + "strings" ) func BuildPathRequest(clientType int, base, path string) (*Request, error) { if clientType == FAST { req := fasthttp.AcquireRequest() - req.SetRequestURI(base + path) + req.SetRequestURI(safeUrlJoin(base, path)) return &Request{FastRequest: req, ClientType: FAST}, nil } else { - req, err := http.NewRequest("GET", base+path, nil) + req, err := http.NewRequest("GET", safeUrlJoin(base, path), nil) return &Request{StandardRequest: req, ClientType: STANDARD}, err } } @@ -74,3 +75,11 @@ func (r *Request) Host() string { return "" } } + +func safeUrlJoin(base, uri string) string { + if !strings.HasSuffix(base, "/") && !strings.HasPrefix(uri, "/") { + return base + "/" + uri + } else { + return base + uri + } +}