From: qydysky Date: Thu, 17 Apr 2025 04:03:39 +0000 (+0000) Subject: 1 X-Git-Tag: v0.1.20250417040517 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=c3bfbb056f63b82d96854f83ba4fdba4a36a4720;p=front%2F.git 1 --- diff --git a/config_test.go b/config_test.go index 06c7c0b..9d98a00 100644 --- a/config_test.go +++ b/config_test.go @@ -252,3 +252,62 @@ func Test_Res(t *testing.T) { t.Fatal(resb) } } + +func Test_Cookie(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + c1 := "login_locale=zh_CN; Max-Age=31536000; Expires=Wed, 15 Apr 2026 02:29:42; Path=/" + c2 := "login_locale=zh_CN; Max-Age=31536000; Expires=Wed, 15 Apr 2026 02:29:43; Path=/" + c3 := "ts=11111111111" + + pweb.New(&http.Server{ + Addr: "127.0.0.1:19001", + }).Handle(map[string]func(http.ResponseWriter, *http.Request){ + `/`: func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Set-Cookie", c1) + w.Header().Add("Set-Cookie", c2) + w.Header().Add("Set-Cookie", c3) + io.Copy(w, r.Body) + }, + }) + + conf := &Config{ + RetryBlocks: RetryBlocks{ + Num: 10, + Size: "3B", + }, + Addr: "127.0.0.1:19000", + Routes: []Route{ + { + Path: []string{"/"}, + PathAdd: true, + Backs: []Back{ + { + Name: "1", + To: "://127.0.0.1:19001", + Weight: 1, + }, + }, + }, + }, + } + + go conf.Run(ctx, logger) + + time.Sleep(time.Second) + + r := reqf.New() + if e := r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:19000/", + }); e != nil { + t.Fatal() + } + + if v, ok := map[string][]string(r.Response.Header)["Set-Cookie"]; !ok { + t.Fail() + } else if v[0] != c1 || v[1] != c2 || v[2] != c3 { + t.Fail() + } + // t.Log(r.Response.Header) +} diff --git a/http.go b/http.go index 29fec9d..a7c5f71 100644 --- a/http.go +++ b/http.go @@ -140,10 +140,10 @@ func (httpDealer) Deal(ctx context.Context, reqId uint32, w http.ResponseWriter, if utils.ValidCookieDomain(r.Host) { cookie.Domain = r.Host } - w.Header().Set("Set-Cookie", (cookie).String()) + w.Header().Add("Set-Cookie", (cookie).String()) } - w.Header().Set(header+"Info", cookie+";"+chosenBack.Name) + w.Header().Add(header+"Info", cookie+";"+chosenBack.Name) // if e := copyHeader(resp.Header, w.Header(), chosenBack.getDealerResHeader()) diff --git a/main.go b/main.go index ccbf967..3db55eb 100755 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ import ( "time" _ "unsafe" + "slices" + "github.com/qydysky/front/dealer" utils "github.com/qydysky/front/utils" pctx "github.com/qydysky/part/ctx" @@ -147,18 +149,20 @@ func copyHeader(s, t http.Header, app []dealer.HeaderDealer) { continue } if strings.ToLower(k) == "set-cookie" { - cookies := strings.Split(v[0], ";") - for k, v := range cookies { - if strings.Contains(strings.ToLower(v), "domain=") { - cookies = append(cookies[:k], cookies[k+1:]...) - break + for _, cookie := range v { + cookieSlice := strings.Split(cookie, ";") + for cookieK, cookieV := range cookieSlice { + if strings.Contains(strings.ToLower(cookieV), "domain=") { + cookieSlice = slices.Delete(cookieSlice, cookieK, cookieK+1) + } } + tm[k] = append(tm[k], strings.Join(cookieSlice, ";")) } - tm[k] = []string{strings.Join(cookies, ";")} } else { - tm[k] = v + tm[k] = append(tm[k], v...) } } + for _, v := range app { switch v.Action { case `replace`: