option.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package xhttp
  2. import (
  3. "net/http"
  4. "time"
  5. )
  6. // RequestOption HTTP request option
  7. type RequestOption struct {
  8. RequestTimeOut time.Duration
  9. ResponseHeaderTimeout time.Duration
  10. Headers map[string]string
  11. Cookies []*http.Cookie
  12. username, password string
  13. CertFile string
  14. KeyFile string
  15. }
  16. // ServerOption serve option
  17. type ServerOption struct {
  18. CertFile string
  19. KeyFile string
  20. // ReadHeaderTimeout is the amount of time allowed to read request headers
  21. ReadHeaderTimeout time.Duration
  22. // ReadTimeout is the maximum duration for reading the entire
  23. // request, including the body.
  24. ReadTimeout time.Duration
  25. // WriteTimeout is the maximum duration before timing out
  26. // writes of the response.
  27. WriteTimeout time.Duration
  28. // IdleTimeout is the maximum amount of time to wait for the
  29. // next request when keep-alives are enabled.
  30. IdleTimeout time.Duration
  31. // MaxHeaderBytes controls the maximum number of bytes the
  32. // server will read parsing the request header's keys and
  33. // values, including the request line. It does not limit the
  34. // size of the request body.
  35. // If zero, DefaultMaxHeaderBytes is used.
  36. MaxHeaderBytes int
  37. ShutdownTimeout time.Duration
  38. }
  39. // DefaultRequestOption default request option
  40. func DefaultRequestOption() RequestOption {
  41. return RequestOption{
  42. Headers: map[string]string{},
  43. RequestTimeOut: 60 * time.Second,
  44. ResponseHeaderTimeout: 60 * time.Second,
  45. }
  46. }
  47. // SetTimeOut set request timeout
  48. func (r *RequestOption) SetTimeOut(v time.Duration) *RequestOption {
  49. r.RequestTimeOut = v
  50. return r
  51. }
  52. // SetResponseHeaderTimeout set response header timeout
  53. func (r *RequestOption) SetResponseHeaderTimeout(v time.Duration) *RequestOption {
  54. r.ResponseHeaderTimeout = v
  55. return r
  56. }
  57. // SetCert set request cert & key file
  58. func (r *RequestOption) SetCert(certFile, keyFile string) *RequestOption {
  59. r.CertFile = certFile
  60. r.KeyFile = keyFile
  61. return r
  62. }
  63. // SetHeader set header
  64. func (r *RequestOption) SetHeader(k, v string) *RequestOption {
  65. r.Headers[k] = v
  66. return r
  67. }
  68. // AddCookie adds a cookie to the request.
  69. func (r *RequestOption) AddCookie(c *http.Cookie) *RequestOption {
  70. r.Cookies = append(r.Cookies, c)
  71. return r
  72. }
  73. // SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
  74. func (r *RequestOption) SetBasicAuth(username, password string) *RequestOption {
  75. r.username = username
  76. r.password = password
  77. return r
  78. }
  79. // AcceptEncodingGZIP Accept-Encoding gzip
  80. func (r *RequestOption) AcceptEncodingGZIP() *RequestOption {
  81. r.Headers[AcceptEncoding] = "gzip, deflate, br"
  82. return r
  83. }
  84. // SetContentType set Content-Type
  85. func (r *RequestOption) SetContentType(v string) *RequestOption {
  86. r.Headers[ContentType] = v
  87. return r
  88. }
  89. // SetContentTypeJSON Content-Type json
  90. func (r *RequestOption) SetContentTypeJSON() *RequestOption {
  91. r.Headers[ContentType] = ContentTypeJSON
  92. return r
  93. }
  94. // SetContentTypeJSON Content-Type json
  95. func (r *RequestOption) SetContentTypeURL() *RequestOption {
  96. r.Headers[ContentType] = ContentTypeURL
  97. return r
  98. }
  99. // SetContentTypeJSON Content-Type json
  100. func (r *RequestOption) SetContentTypeXML() *RequestOption {
  101. r.Headers[ContentType] = ContentTypeXML
  102. return r
  103. }
  104. func DefaultServerOption() ServerOption {
  105. return ServerOption{
  106. ReadTimeout: readTimeout,
  107. ReadHeaderTimeout: readHeaderTimeout,
  108. WriteTimeout: writeTimeout,
  109. IdleTimeout: idleTimeout,
  110. MaxHeaderBytes: 1 << 20,
  111. ShutdownTimeout: shutTimeout,
  112. }
  113. }
  114. // SetReadTimeout set ReadTimeout
  115. func (s *ServerOption) SetReadTimeout(t time.Duration) *ServerOption {
  116. s.ReadTimeout = t
  117. return s
  118. }
  119. // SetReadHeaderTimeout set ReadHeaderTimeout
  120. func (s *ServerOption) SetReadHeaderTimeout(t time.Duration) *ServerOption {
  121. s.ReadHeaderTimeout = t
  122. return s
  123. }
  124. // SetWriteTimeout set WriteTimeout
  125. func (s *ServerOption) SetWriteTimeout(t time.Duration) *ServerOption {
  126. s.WriteTimeout = t
  127. return s
  128. }
  129. // SetIdleTimeout set IdleTimeout
  130. func (s *ServerOption) SetIdleTimeout(t time.Duration) *ServerOption {
  131. s.IdleTimeout = t
  132. return s
  133. }
  134. // SetMaxHeaderBytes set MaxHeaderBytes
  135. func (s *ServerOption) SetMaxHeaderBytes(n int) *ServerOption {
  136. s.MaxHeaderBytes = n
  137. return s
  138. }
  139. // SetShutdownTimeout set ShutdownTimeout
  140. func (s *ServerOption) SetShutdownTimeout(t time.Duration) *ServerOption {
  141. s.ShutdownTimeout = t
  142. return s
  143. }
  144. // SetCertFile set CertFile
  145. func (s *ServerOption) SetCertFile(path string) *ServerOption {
  146. s.CertFile = path
  147. return s
  148. }
  149. // SetKeyFile set KeyFile
  150. func (s *ServerOption) SetKeyFile(path string) *ServerOption {
  151. s.KeyFile = path
  152. return s
  153. }