package xhttp import ( "net/http" "time" ) // RequestOption HTTP request option type RequestOption struct { RequestTimeOut time.Duration ResponseHeaderTimeout time.Duration Headers map[string]string Cookies []*http.Cookie username, password string CertFile string KeyFile string } // ServerOption serve option type 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 option func DefaultRequestOption() RequestOption { return RequestOption{ Headers: map[string]string{}, RequestTimeOut: 60 * time.Second, ResponseHeaderTimeout: 60 * time.Second, } } // SetTimeOut set request timeout func (r *RequestOption) SetTimeOut(v time.Duration) *RequestOption { r.RequestTimeOut = v return r } // SetResponseHeaderTimeout set response header timeout func (r *RequestOption) SetResponseHeaderTimeout(v time.Duration) *RequestOption { r.ResponseHeaderTimeout = v return r } // SetCert set request cert & key file func (r *RequestOption) SetCert(certFile, keyFile string) *RequestOption { r.CertFile = certFile r.KeyFile = keyFile return r } // SetHeader set header func (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 gzip func (r *RequestOption) AcceptEncodingGZIP() *RequestOption { r.Headers[AcceptEncoding] = "gzip, deflate, br" return r } // SetContentType set Content-Type func (r *RequestOption) SetContentType(v string) *RequestOption { r.Headers[ContentType] = v return r } // SetContentTypeJSON Content-Type json func (r *RequestOption) SetContentTypeJSON() *RequestOption { r.Headers[ContentType] = ContentTypeJSON return r } // SetContentTypeJSON Content-Type json func (r *RequestOption) SetContentTypeURL() *RequestOption { r.Headers[ContentType] = ContentTypeURL return r } // SetContentTypeJSON Content-Type json func (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 ReadTimeout func (s *ServerOption) SetReadTimeout(t time.Duration) *ServerOption { s.ReadTimeout = t return s } // SetReadHeaderTimeout set ReadHeaderTimeout func (s *ServerOption) SetReadHeaderTimeout(t time.Duration) *ServerOption { s.ReadHeaderTimeout = t return s } // SetWriteTimeout set WriteTimeout func (s *ServerOption) SetWriteTimeout(t time.Duration) *ServerOption { s.WriteTimeout = t return s } // SetIdleTimeout set IdleTimeout func (s *ServerOption) SetIdleTimeout(t time.Duration) *ServerOption { s.IdleTimeout = t return s } // SetMaxHeaderBytes set MaxHeaderBytes func (s *ServerOption) SetMaxHeaderBytes(n int) *ServerOption { s.MaxHeaderBytes = n return s } // SetShutdownTimeout set ShutdownTimeout func (s *ServerOption) SetShutdownTimeout(t time.Duration) *ServerOption { s.ShutdownTimeout = t return s } // SetCertFile set CertFile func (s *ServerOption) SetCertFile(path string) *ServerOption { s.CertFile = path return s } // SetKeyFile set KeyFile func (s *ServerOption) SetKeyFile(path string) *ServerOption { s.KeyFile = path return s }