]> 127.0.0.1 Git - front/.git/commitdiff
1 v0.1.20250417040517
authorqydysky <qydysky@foxmail.com>
Thu, 17 Apr 2025 04:03:39 +0000 (04:03 +0000)
committerGitHub <noreply@github.com>
Thu, 17 Apr 2025 04:03:39 +0000 (04:03 +0000)
config_test.go
http.go
main.go

index 06c7c0b29847b01055908ad5204bf43436007972..9d98a002bffee9a5878d58738e2a7d955be743a4 100644 (file)
@@ -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 29fec9ddea68fdcb7f842ba79e2c0aa2b22922f0..a7c5f71754ed1c38e2130fe576da0330530134b2 100644 (file)
--- 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 ccbf967277c788f44029b6d1eeaa8ccafd6a3d54..3db55ebabc26dd13f027e26bc1b8a683b430a9c7 100755 (executable)
--- 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`: