| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 | package xhttpimport (	"net/http"	"time")// RequestOption HTTP request optiontype RequestOption struct {	RequestTimeOut        time.Duration	ResponseHeaderTimeout time.Duration	Headers map[string]string	Cookies []*http.Cookie	username, password string	CertFile string	KeyFile  string}// ServerOption serve optiontype ServerOption struct {	CertFile string	KeyFile  string	// ReadHeaderTimeout is the amount of time allowed to read request headers	ReadHeaderTimeout time.Duration	// ReadTimeout is the maximum duration for reading the entire	// request, including the body.	ReadTimeout time.Duration	// WriteTimeout is the maximum duration before timing out	// writes of the response.	WriteTimeout time.Duration	// IdleTimeout is the maximum amount of time to wait for the	// next request when keep-alives are enabled.	IdleTimeout time.Duration	// MaxHeaderBytes controls the maximum number of bytes the	// server will read parsing the request header's keys and	// values, including the request line. It does not limit the	// size of the request body.	// If zero, DefaultMaxHeaderBytes is used.	MaxHeaderBytes int	ShutdownTimeout time.Duration}// DefaultRequestOption default request optionfunc DefaultRequestOption() RequestOption {	return RequestOption{		Headers:               map[string]string{},		RequestTimeOut:        60 * time.Second,		ResponseHeaderTimeout: 60 * time.Second,	}}// SetTimeOut set request timeoutfunc (r *RequestOption) SetTimeOut(v time.Duration) *RequestOption {	r.RequestTimeOut = v	return r}// SetResponseHeaderTimeout set response header timeoutfunc (r *RequestOption) SetResponseHeaderTimeout(v time.Duration) *RequestOption {	r.ResponseHeaderTimeout = v	return r}// SetCert set request cert & key filefunc (r *RequestOption) SetCert(certFile, keyFile string) *RequestOption {	r.CertFile = certFile	r.KeyFile = keyFile	return r}// SetHeader set headerfunc (r *RequestOption) SetHeader(k, v string) *RequestOption {	r.Headers[k] = v	return r}// AddCookie adds a cookie to the request.func (r *RequestOption) AddCookie(c *http.Cookie) *RequestOption {	r.Cookies = append(r.Cookies, c)	return r}// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.func (r *RequestOption) SetBasicAuth(username, password string) *RequestOption {	r.username = username	r.password = password	return r}// AcceptEncodingGZIP Accept-Encoding gzipfunc (r *RequestOption) AcceptEncodingGZIP() *RequestOption {	r.Headers[AcceptEncoding] = "gzip, deflate, br"	return r}// SetContentType set Content-Typefunc (r *RequestOption) SetContentType(v string) *RequestOption {	r.Headers[ContentType] = v	return r}// SetContentTypeJSON Content-Type jsonfunc (r *RequestOption) SetContentTypeJSON() *RequestOption {	r.Headers[ContentType] = ContentTypeJSON	return r}// SetContentTypeJSON Content-Type jsonfunc (r *RequestOption) SetContentTypeURL() *RequestOption {	r.Headers[ContentType] = ContentTypeURL	return r}// SetContentTypeJSON Content-Type jsonfunc (r *RequestOption) SetContentTypeXML() *RequestOption {	r.Headers[ContentType] = ContentTypeXML	return r}func DefaultServerOption() ServerOption {	return ServerOption{		ReadTimeout:       readTimeout,		ReadHeaderTimeout: readHeaderTimeout,		WriteTimeout:      writeTimeout,		IdleTimeout:       idleTimeout,		MaxHeaderBytes: 1 << 20,		ShutdownTimeout: shutTimeout,	}}// SetReadTimeout set ReadTimeoutfunc (s *ServerOption) SetReadTimeout(t time.Duration) *ServerOption {	s.ReadTimeout = t	return s}// SetReadHeaderTimeout set ReadHeaderTimeoutfunc (s *ServerOption) SetReadHeaderTimeout(t time.Duration) *ServerOption {	s.ReadHeaderTimeout = t	return s}// SetWriteTimeout set WriteTimeoutfunc (s *ServerOption) SetWriteTimeout(t time.Duration) *ServerOption {	s.WriteTimeout = t	return s}// SetIdleTimeout set IdleTimeoutfunc (s *ServerOption) SetIdleTimeout(t time.Duration) *ServerOption {	s.IdleTimeout = t	return s}// SetMaxHeaderBytes set MaxHeaderBytesfunc (s *ServerOption) SetMaxHeaderBytes(n int) *ServerOption {	s.MaxHeaderBytes = n	return s}// SetShutdownTimeout set ShutdownTimeoutfunc (s *ServerOption) SetShutdownTimeout(t time.Duration) *ServerOption {	s.ShutdownTimeout = t	return s}// SetCertFile set CertFilefunc (s *ServerOption) SetCertFile(path string) *ServerOption {	s.CertFile = path	return s}// SetKeyFile set KeyFilefunc (s *ServerOption) SetKeyFile(path string) *ServerOption {	s.KeyFile = path	return s}
 |