]> 127.0.0.1 Git - part/.git/commitdiff
cookie func v0.6.0
authorqydysky <qydysky@foxmail.com>
Sat, 3 Jul 2021 17:42:48 +0000 (01:42 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 3 Jul 2021 17:42:48 +0000 (01:42 +0800)
reqf/Reqf.go
reqf/cookie.go [new file with mode: 0644]

index 7b8784994c4649685a71d9a96db9f720aa69d9ba..cbb9c729bdf7057708675c526e3900d2b4b0c064 100644 (file)
@@ -35,6 +35,7 @@ type Rval struct {
     Retry int
     SleepTime int
     JustResponseCode bool
+    Cookies []*http.Cookie
 
     SaveToPath string
     SaveToChan chan[]byte
@@ -146,7 +147,8 @@ func (this *Req) Reqf_1(val Rval) (err error) {
     if Timeout > 0 {
         cx, _ = context.WithTimeout(cx,time.Duration(Timeout)*time.Millisecond)
     }
-    req,_ := http.NewRequest(Method, Url, body)
+    req,e := http.NewRequest(Method, Url, body)
+    if e != nil {panic(e)}
     req = req.WithContext(cx)
 
     var done = make(chan struct{})
@@ -158,6 +160,10 @@ func (this *Req) Reqf_1(val Rval) (err error) {
         }
     }()
     
+    for _,v := range val.Cookies {
+        req.AddCookie(v)
+    }
+
     if _,ok := Header["Accept"];!ok {Header["Accept"] = `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8`}
     if _,ok := Header["Connection"];!ok {Header["Connection"] = "keep-alive"}
     if _,ok := Header["Accept-Encoding"];!ok {Header["Accept-Encoding"] = "gzip, deflate, br"}
@@ -325,35 +331,6 @@ func (t *Req) Id() uintptr {
     return t.id.Id
 }
 
-func Cookies_String_2_Map(Cookies string) (o map[string]string) {
-    o = make(map[string]string)
-    list := strings.Split(Cookies, `; `)
-    for _,v := range list {
-        s := strings.SplitN(v, "=", 2)
-        if len(s) != 2 {continue}
-        o[s[0]] = s[1]
-    }
-    return
-}
-
-func Map_2_Cookies_String(Cookies map[string]string) (o string) {
-    if len(Cookies) == 0 {return ""}
-    for k,v := range Cookies {
-        o += k +`=`+ v + `; `
-    }
-    t := []rune(o)
-    o = string(t[:len(t)-2])
-    return
-}
-
-func Cookies_List_2_Map(Cookies []*http.Cookie) (o map[string]string) {
-    o = make(map[string]string)
-    for _,v := range Cookies {
-               o[v.Name] = v.Value
-    }
-    return
-}
-
 func IsTimeout(e error) bool {
     if errors.Is(e, context.DeadlineExceeded) || errors.Is(e, ConnectTimeoutErr) || errors.Is(e, ReadTimeoutErr) {return true}
     if net_err,ok := e.(net.Error);ok && net_err.Timeout() {return true}
diff --git a/reqf/cookie.go b/reqf/cookie.go
new file mode 100644 (file)
index 0000000..00c82b3
--- /dev/null
@@ -0,0 +1,107 @@
+package part
+
+import (
+       "time"
+       "strings"
+       "net/http"
+       "encoding/json"
+)
+
+type CookieJson struct {
+    Name  string `json:"Name"`
+    Value string `json:"Value"`
+
+    Path       string `json:"Path"`    // optional
+    Domain     string `json:"Domain"`    // optional
+    Expires    string `json:"Expires"` // time.Time in RFC1123
+    RawExpires string `json:"RawExpires"`    // for reading cookies only
+
+    // MaxAge=0 means no 'Max-Age' attribute specified.
+    // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
+    // MaxAge>0 means Max-Age attribute present and given in seconds
+    MaxAge   int `json:"MaxAge"`
+    Secure   bool `json:"Secure"`
+    HttpOnly bool `json:"HttpOnly"`
+    SameSite http.SameSite `json:"SameSite"` // Go 1.11
+    Raw      string `json:"Raw"`
+    Unparsed []string `json:"Unparsed"` // Raw text of unparsed attribute-value pairs
+}
+
+func (t *CookieJson) ToJson() ([]byte,error) {
+       return json.Marshal(t)
+}
+
+func (t *CookieJson) FromJson(cookie_in_json []byte) (error) {
+       return json.Unmarshal(cookie_in_json, t)
+}
+
+func (t *CookieJson) ToCookie() (http.Cookie,error) {
+       exp,e := time.Parse(time.RFC1123, t.Expires)
+       if exp.IsZero() {
+               exp = time.Now().Add(time.Duration(t.MaxAge)*time.Second)
+       }
+       return http.Cookie{
+               Name:t.Name,
+               Value:t.Value,
+               Path:t.Path,
+               Domain:t.Domain,
+               Expires:exp,
+               RawExpires:t.RawExpires,
+               MaxAge:t.MaxAge,
+               Secure:t.Secure,
+               HttpOnly:t.HttpOnly,
+               SameSite:t.SameSite,
+               Raw:t.Raw,
+               Unparsed:t.Unparsed,
+       },e
+}
+
+func (t *CookieJson) FromCookie(cookie *http.Cookie) {
+       exp_t := cookie.Expires
+       if exp_t.IsZero() {
+               exp_t = time.Now().Add(time.Duration(cookie.MaxAge)*time.Second)
+       }
+       exp := exp_t.Format(time.RFC1123)
+
+       t.Name = cookie.Name
+       t.Value = cookie.Value
+       t.Path = cookie.Path
+       t.Domain = cookie.Domain
+       t.Expires = exp
+       t.RawExpires = cookie.RawExpires
+       t.MaxAge = cookie.MaxAge
+       t.Secure = cookie.Secure
+       t.HttpOnly = cookie.HttpOnly
+       t.SameSite = cookie.SameSite
+       t.Raw = cookie.Raw
+       t.Unparsed = cookie.Unparsed
+}
+
+func Cookies_String_2_Map(Cookies string) (o map[string]string) {
+    o = make(map[string]string)
+    list := strings.Split(Cookies, `; `)
+    for _,v := range list {
+        s := strings.SplitN(v, "=", 2)
+        if len(s) != 2 {continue}
+        o[s[0]] = s[1]
+    }
+    return
+}
+
+func Map_2_Cookies_String(Cookies map[string]string) (o string) {
+    if len(Cookies) == 0 {return ""}
+    for k,v := range Cookies {
+        o += k +`=`+ v + `; `
+    }
+    t := []rune(o)
+    o = string(t[:len(t)-2])
+    return
+}
+
+func Cookies_List_2_Map(Cookies []*http.Cookie) (o map[string]string) {
+    o = make(map[string]string)
+    for _,v := range Cookies {
+               o[v.Name] = v.Value
+    }
+    return
+}
\ No newline at end of file