github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/cookies.go (about)

     1  package requests
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/gospider007/gson"
    10  )
    11  
    12  // cookies
    13  type Cookies []*http.Cookie
    14  
    15  // return cookies with string,join with '; '
    16  func (obj Cookies) String() string {
    17  	cooks := []string{}
    18  	for _, cook := range obj {
    19  		cooks = append(cooks, fmt.Sprintf("%s=%s", cook.Name, cook.Value))
    20  	}
    21  	return strings.Join(cooks, "; ")
    22  }
    23  
    24  // get cookies by name
    25  func (obj Cookies) Gets(name string) Cookies {
    26  	var result Cookies
    27  	for _, cook := range obj {
    28  		if cook.Name == name {
    29  			result = append(result, cook)
    30  		}
    31  	}
    32  	return result
    33  }
    34  
    35  // get cookie by name
    36  func (obj Cookies) Get(name string) *http.Cookie {
    37  	vals := obj.Gets(name)
    38  	if i := len(vals); i == 0 {
    39  		return nil
    40  	} else {
    41  		return vals[i-1]
    42  	}
    43  }
    44  
    45  // get cookie values by name, return []string
    46  func (obj Cookies) GetVals(name string) []string {
    47  	var result []string
    48  	for _, cook := range obj {
    49  		if cook.Name == name {
    50  			result = append(result, cook.Value)
    51  		}
    52  	}
    53  	return result
    54  }
    55  
    56  // get cookie value by name,return string
    57  func (obj Cookies) GetVal(name string) string {
    58  	vals := obj.GetVals(name)
    59  	if i := len(vals); i == 0 {
    60  		return ""
    61  	} else {
    62  		return vals[i-1]
    63  	}
    64  }
    65  func (obj Cookies) append(cook *http.Cookie) Cookies {
    66  	return append(obj, cook)
    67  }
    68  
    69  // read cookies or parse cookies,support json,map,[]string,http.Header,string
    70  func ReadCookies(val any) (Cookies, error) {
    71  	switch cook := val.(type) {
    72  	case *http.Cookie:
    73  		return Cookies{
    74  			cook,
    75  		}, nil
    76  	case http.Cookie:
    77  		return Cookies{
    78  			&cook,
    79  		}, nil
    80  	case Cookies:
    81  		return cook, nil
    82  	case []*http.Cookie:
    83  		return Cookies(cook), nil
    84  	case string:
    85  		return readCookies(http.Header{"Cookie": []string{cook}}, ""), nil
    86  	case http.Header:
    87  		return readCookies(cook, ""), nil
    88  	case []string:
    89  		return readCookies(http.Header{"Cookie": cook}, ""), nil
    90  	default:
    91  		return any2Cookies(cook)
    92  	}
    93  }
    94  
    95  // read set cookies or parse set cookies,support json,map,[]string,http.Header,string
    96  func ReadSetCookies(val any) (Cookies, error) {
    97  	switch cook := val.(type) {
    98  	case Cookies:
    99  		return cook, nil
   100  	case []*http.Cookie:
   101  		return Cookies(cook), nil
   102  	case string:
   103  		return readSetCookies(http.Header{"Set-Cookie": []string{cook}}), nil
   104  	case http.Header:
   105  		return readSetCookies(cook), nil
   106  	case []string:
   107  		return readSetCookies(http.Header{"Set-Cookie": cook}), nil
   108  	default:
   109  		return any2Cookies(cook)
   110  	}
   111  }
   112  func any2Cookies(val any) (Cookies, error) {
   113  	switch cooks := val.(type) {
   114  	case map[string]string:
   115  		cookies := Cookies{}
   116  		for kk, vv := range cooks {
   117  			cookies = append(cookies, &http.Cookie{
   118  				Name:  kk,
   119  				Value: vv,
   120  			})
   121  		}
   122  		return cookies, nil
   123  	case map[string][]string:
   124  		cookies := Cookies{}
   125  		for kk, vvs := range cooks {
   126  			for _, vv := range vvs {
   127  				cookies = append(cookies, &http.Cookie{
   128  					Name:  kk,
   129  					Value: vv,
   130  				})
   131  			}
   132  		}
   133  		return cookies, nil
   134  	case *gson.Client:
   135  		if !cooks.IsObject() {
   136  			return nil, errors.New("cookies not support type")
   137  		}
   138  		cookies := Cookies{}
   139  		for kk, vvs := range cooks.Map() {
   140  			if vvs.IsArray() {
   141  				for _, vv := range vvs.Array() {
   142  					cookies = append(cookies, &http.Cookie{
   143  						Name:  kk,
   144  						Value: vv.String(),
   145  					})
   146  				}
   147  			} else {
   148  				cookies = append(cookies, &http.Cookie{
   149  					Name:  kk,
   150  					Value: vvs.String(),
   151  				})
   152  			}
   153  		}
   154  		return cookies, nil
   155  	default:
   156  		jsonData, err := gson.Decode(cooks)
   157  		if err != nil {
   158  			return nil, err
   159  		}
   160  		cookies := Cookies{}
   161  		for kk, vvs := range jsonData.Map() {
   162  			if vvs.IsArray() {
   163  				for _, vv := range vvs.Array() {
   164  					cookies = append(cookies, &http.Cookie{
   165  						Name:  kk,
   166  						Value: vv.String(),
   167  					})
   168  				}
   169  			} else {
   170  				cookies = append(cookies, &http.Cookie{
   171  					Name:  kk,
   172  					Value: vvs.String(),
   173  				})
   174  			}
   175  		}
   176  		return cookies, nil
   177  	}
   178  }
   179  func (obj *RequestOption) initCookies() (Cookies, error) {
   180  	if obj.Cookies == nil {
   181  		return nil, nil
   182  	}
   183  	return ReadCookies(obj.Cookies)
   184  }