Documentation ¶
Index ¶
- Constants
- Variables
- func CopyBody(b io.ReadCloser) (*bytes.Buffer, io.ReadCloser, error)
- func Delete(url, contentType string, body io.Reader) (*http.Response, error)
- func GenId(id ...string) string
- func Get(url string) (*http.Response, error)
- func Head(url string) (resp *http.Response, err error)
- func ListenAndServe(ctx context.Context, h http.Handler, opts ...Option) error
- func Log(format string, v ...any)
- func NewRequestWithContext(ctx context.Context, options Options) (*http.Request, error)
- func PUT(url, contentType string, body io.Reader) (*http.Response, error)
- func ParseBody(r io.ReadCloser) (*bytes.Buffer, error)
- func Post(url string, contentType string, body io.Reader) (*http.Response, error)
- func PostForm(url string, data url.Values) (*http.Response, error)
- func WarpHandler(next http.Handler) func(http.Handler) http.Handler
- func WarpRoundTripper(next http.RoundTripper) func(http.RoundTripper) http.RoundTripper
- type Node
- type Option
- func BasicAuth(username, password string) Option
- func Body(body any) Option
- func CertKey(cert, key string) Option
- func Cookie(cookie http.Cookie) Option
- func Cookies(cookies ...http.Cookie) Option
- func Form(form url.Values) Option
- func Gzip(body any) Option
- func Header(k, v string) Option
- func Headers(kv map[string]string) Option
- func Host(host string) Option
- func LocalAddr(addr net.Addr) Option
- func Logf(f func(ctx context.Context, stat *Stat)) Option
- func MaxConns(conn int) Option
- func Method(method string) Option
- func Param(k string, v ...string) Option
- func Params(query map[string]string) Option
- func Path(path string) Option
- func Proxy(addr string) Option
- func RoundTripper(tr http.RoundTripper) Option
- func Setup(fn ...func(tripper http.RoundTripper) http.RoundTripper) Option
- func Stream(stream func(int64, []byte) error) Option
- func Timeout(timeout time.Duration) Option
- func Trace(mLimit ...int) Option
- func URL(url string) Option
- func Use(fn ...func(http.Handler) http.Handler) Option
- func Verify(verify bool) Option
- type Options
- type Response
- type ResponseWriter
- type RoundTripperFunc
- type ServeMux
- func (mux *ServeMux) Handle(path string, h http.Handler, opts ...Option)
- func (mux *ServeMux) HandleFunc(path string, f func(http.ResponseWriter, *http.Request), opts ...Option)
- func (mux *ServeMux) Pprof()
- func (mux *ServeMux) Print()
- func (mux *ServeMux) Redirect(source, target string)
- func (mux *ServeMux) Route(path string, v any, opts ...Option)
- func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mux *ServeMux) Use(fn ...func(http.Handler) http.Handler)
- type Server
- type Session
- func (s *Session) Do(ctx context.Context, opts ...Option) (*http.Response, error)
- func (s *Session) DoRequest(ctx context.Context, opts ...Option) (*Response, error)
- func (s *Session) HTTPClient() *http.Client
- func (s *Session) RoundTrip(r *http.Request) (*http.Response, error)
- func (s *Session) RoundTripper(opts ...Option) http.RoundTripper
- type Stat
- type Transport
Constants ¶
const RequestId = "Request-Id"
Variables ¶
var ( MethodGet = Method("GET") MethodPost = Method("POST") )
Method http method
var ErrHandler = func(err string, code int) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, err, code) }) }
ErrHandler handler err
Functions ¶
func CopyBody ¶
func CopyBody(b io.ReadCloser) (*bytes.Buffer, io.ReadCloser, error)
CopyBody reads all of b to memory and then returns two equivalent ReadClosers yielding the same bytes.
It returns an error if the initial slurp of all bytes fails. It does not attempt to make the returned ReadClosers have identical error-matching behavior.
func ListenAndServe ¶
ListenAndServe listens on the TCP network address addr and then calls [Serve] with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.
The handler is typically nil, in which case [DefaultServeMux] is used.
ListenAndServe always returns a non-nil error.
func NewRequestWithContext ¶
NewRequestWithContext request
func ParseBody ¶
func ParseBody(r io.ReadCloser) (*bytes.Buffer, error)
ParseBody parse body from `Request.Body`.
func WarpHandler ¶
WarpHandler warp `http.Handler`.
func WarpRoundTripper ¶
func WarpRoundTripper(next http.RoundTripper) func(http.RoundTripper) http.RoundTripper
WarpRoundTripper warp `http.RoundTripper`.
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node trie node
type Option ¶
type Option func(*Options)
Option func
func Host ¶
Host set net/http.Request.Host. 在客户端,请求的Host字段(可选地)用来重写请求的Host头。 如过该字段为"",Request.Write方法会使用URL字段的Host。
func LocalAddr ¶
LocalAddr local ip for tcp: &net.TCPAddr{IP: ip} for unix: &net.UnixAddr{Net: "unix", Name: "xxx")}
func Proxy ¶
Proxy set proxy addr os.Setenv("HTTP_PROXY", "http://127.0.0.1:9743") os.Setenv("HTTPS_PROXY", "https://127.0.0.1:9743") https://stackoverflow.com/questions/14661511/setting-up-proxy-for-http-client
func RoundTripper ¶
func RoundTripper(tr http.RoundTripper) Option
RoundTripper set default `*http.Transport` by customer define.
func Setup ¶
func Setup(fn ...func(tripper http.RoundTripper) http.RoundTripper) Option
Setup is used for client middleware
func URL ¶
URL set client to dial connection use http transport or unix socket. IF using socket connection. you must set unix in session, and set http in request. For example, sess := requests.New(requests.URL("unix:///tmp/requests.sock")) sess.DoRequest(context.Background(), requests.URL("http://path?k=v"), requests.Body("12345")) https://old.lubui.com/2021/07/26/golang-socket-file/
type Options ¶
type Options struct { Method string URL string Path []string RawQuery url.Values Header http.Header Cookies []http.Cookie Timeout time.Duration MaxConns int Verify bool Stream func(int64, []byte) error Transport http.RoundTripper HttpRoundTripper []func(http.RoundTripper) http.RoundTripper Handler http.Handler HttpHandler []func(http.Handler) http.Handler // client session used LocalAddr net.Addr Proxy func(*http.Request) (*url.URL, error) // contains filtered or unexported fields }
Options request
type Response ¶
type Response struct { *http.Request *http.Response StartAt time.Time Cost time.Duration Content *bytes.Buffer Err error }
Response wrap `http.Response` struct.
type ResponseWriter ¶
type ResponseWriter struct { http.ResponseWriter StatusCode int Content []byte // contains filtered or unexported fields }
ResponseWriter wrap `http.ResponseWriter` interface.
func (*ResponseWriter) Flush ¶
func (w *ResponseWriter) Flush()
func (*ResponseWriter) Hijack ¶
func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
func (*ResponseWriter) Push ¶
func (w *ResponseWriter) Push(target string, opts *http.PushOptions) error
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(statusCode int)
type RoundTripperFunc ¶
RoundTripperFunc is a http.RoundTripper implementation, which is a simple function.
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux implement ServeHTTP interface.
func (*ServeMux) HandleFunc ¶
func (mux *ServeMux) HandleFunc(path string, f func(http.ResponseWriter, *http.Request), opts ...Option)
HandleFunc set func pattern path to handle path cannot override, so if your path not work, maybe it is already exists!
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server server
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address srv.Addr and then calls [Serve] or [ServeTLS] to handle requests on incoming (TLS) connections. Accepted connections are configured to enable TCP keep-alives.
If srv.Addr is blank, ":http" is used.
Filenames containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.
ListenAndServe(TLS) always returns a non-nil error. After [Server.Shutdown] or [Server.Close], the returned error is [ErrServerClosed].
func (*Server) OnShutdown ¶
OnShutdown do something after serve shutdown
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session httpclient session Clients and Transports are safe for concurrent use by multiple goroutines for efficiency should only be created once and re-used. so, session is also safe for concurrent use by multiple goroutines.
func (*Session) DoRequest ¶
DoRequest send a request and return a response, and is safely close `resp.Body`.
func (*Session) HTTPClient ¶
HTTPClient returns the http.Client that is configured to be used for HTTP requests.
func (*Session) RoundTrip ¶
RoundTrip implements the RoundTripper interface. Like the `http.RoundTripper` interface, the error types returned by RoundTrip are unspecified.
func (*Session) RoundTripper ¶
func (s *Session) RoundTripper(opts ...Option) http.RoundTripper
RoundTripper return http.RoundTripper. Setup: session.Setup -> request.Setup
type Stat ¶
type Stat struct { RequestId string `json:"RequestId"` StartAt string `json:"StartAt"` Cost int64 `json:"Cost"` Request struct { // RemoteAddr is remote addr in server side, // For client requests, it is unused. RemoteAddr string `json:"RemoteAddr"` // URL is Request.URL // For client requests, is request addr. contains schema://ip:port/path/xx // For server requests, is only path. eg: /api/v1/xxx URL string `json:"URL"` Method string `json:"Method"` Header map[string]string `json:"Header"` Body any `json:"Body"` } `json:"Request"` Response struct { // URL is server addr(http://127.0.0.1:8080). // For client requests, it is unused. URL string `json:"URL"` Header map[string]string `json:"Header"` Body any `json:"Body"` StatusCode int `json:"StatusCode"` ContentLength int64 `json:"ContentLength"` } `json:"Response"` Err string `json:"Err"` }
Stat stats
type Transport ¶
func (*Transport) RoundTrip ¶
RoundTrip implements the RoundTripper interface. Like the `http.RoundTripper` interface, the error types returned by RoundTrip are unspecified.
func (*Transport) RoundTripper ¶
func (t *Transport) RoundTripper(opts ...Option) http.RoundTripper
RoundTripper return http.RoundTripper. Setup: session.Setup -> request.Setup