Documentation
¶
Overview ¶
Package httputil provides a small junk drawer of http client and server helpers.
Index ¶
- func HTTPError(res *http.Response) error
- func HTTPErrorf(res *http.Response, format string, args ...interface{}) error
- func IsHTTPStatus(err error, status int) bool
- type HTTPTracker
- func (t *HTTPTracker) CancelRequest(req *http.Request)
- func (t *HTTPTracker) Close() error
- func (t *HTTPTracker) MarshalJSON() ([]byte, error)
- func (t *HTTPTracker) Report(w io.Writer)
- func (t *HTTPTracker) ReportLoop(w io.Writer, ch <-chan os.Signal)
- func (t *HTTPTracker) RoundTrip(req *http.Request) (*http.Response, error)
- func (t *HTTPTracker) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTTPError ¶
HTTPError converts an http response into an error.
Note that this reads the body, so only use it when the response exists and you don't believe it's valid for your needs.
func HTTPErrorf ¶
HTTPErrorf converts an http response into an error.
This allows for standard printf-style formatting with the addition of %S for the http status (e.g. "404 Not Found") and %B for the body that was returned along with the error.
Note that this reads the body, so only use it when the response exists and you don't believe it's valid for your needs.
func IsHTTPStatus ¶
IsHTTPStatus returns true if the given error is caused by an HTTP response with the given HTTP status.
Types ¶
type HTTPTracker ¶
type HTTPTracker struct {
// Next is the RoundTripper being wrapped
Next http.RoundTripper
// TrackStacks will record user stacks if true.
TrackStacks bool
// contains filtered or unexported fields
}
HTTPTracker is a http.RoundTripper wrapper that tracks usage of clients.
The easiest way to use http tracker for a commandline tool is to just call InitHTTPTracker:
httputil.InitHTTPTracker(false)
This wraps the current http.DefaultTransport with a tracking transport and installs a SIGINFO handler to report the current state on demand.
If you have a web server that is also an HTTP client and uses expvar, you can publish an expvar version of the data with the following, similar invocation:
expvar.Publish("httpclients", httputil.InitHTTPTracker(false))
The boolean parameter in the above examples determines whether stacks are also tracked. See the docs for InitHTTPTracker for more details.
func InitHTTPTracker ¶
func InitHTTPTracker(trackStacks bool) *HTTPTracker
InitHTTPTracker wraps http.DefaultTransport with a tracking DefaultTransport and installs a SIGINFO handler to report progress.
If trackStacks is true, the call stack will be included with tracking information and reports.
func InitHTTPTrackerOnly ¶
func InitHTTPTrackerOnly(trackStacks bool) *HTTPTracker
InitHTTPTracker wraps http.DefaultTransport with a tracking DefaultTransport and does not install a SIGINFO wrapper.
If trackStacks is true, the call stack will be included with tracking information and reports.
func (*HTTPTracker) CancelRequest ¶
func (t *HTTPTracker) CancelRequest(req *http.Request)
func (*HTTPTracker) MarshalJSON ¶
func (t *HTTPTracker) MarshalJSON() ([]byte, error)
MarshalJSON provides a JSON representation of the state of tracker.
func (*HTTPTracker) Report ¶
func (t *HTTPTracker) Report(w io.Writer)
Report writes a textual report of the current state of HTTP clients to the given writer.
func (*HTTPTracker) ReportLoop ¶
func (t *HTTPTracker) ReportLoop(w io.Writer, ch <-chan os.Signal)
ReportLoop will issue a report on the given Writer whenever it receives a signal on the given channel.
This primarily exists for signal handlers.
func (*HTTPTracker) String ¶
func (t *HTTPTracker) String() string
String produces a JSON formatted representation of the tracker state. This is directly useful to expvar.