MCPcopy
hub / github.com/labstack/echo / proxyHTTP

Function proxyHTTP

middleware/proxy.go:422–449  ·  view source on GitHub ↗
(c *echo.Context, tgt *ProxyTarget, config ProxyConfig)

Source from the content-addressed store, hash-verified

420const StatusCodeContextCanceled = 499
421
422func proxyHTTP(c *echo.Context, tgt *ProxyTarget, config ProxyConfig) http.Handler {
423 proxy := httputil.NewSingleHostReverseProxy(tgt.URL)
424 proxy.ErrorHandler = func(resp http.ResponseWriter, req *http.Request, err error) {
425 desc := tgt.URL.String()
426 if tgt.Name != "" {
427 desc = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
428 }
429 // If the client canceled the request (usually by closing the connection), we can report a
430 // client error (4xx) instead of a server error (5xx) to correctly identify the situation.
431 // The Go standard library (at of late 2020) wraps the exported, standard
432 // context. Canceled error with unexported garbage value requiring a substring check, see
433 // https://github.com/golang/go/blob/6965b01ea248cabb70c3749fd218b36089a21efb/src/net/net.go#L416-L430
434 // From Caddy https://github.com/caddyserver/caddy/blob/afa778ae05503f563af0d1015cdf7e5e78b1eeec/modules/caddyhttp/reverseproxy/reverseproxy.go#L1352
435 if errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation was canceled") {
436 httpError := echo.NewHTTPError(StatusCodeContextCanceled, "client closed connection").Wrap(err)
437 c.Set("_error", httpError)
438 } else {
439 httpError := echo.NewHTTPError(
440 http.StatusBadGateway,
441 "remote server unreachable, could not proxy request",
442 ).Wrap(fmt.Errorf("server: %s, err: %w", desc, err))
443 c.Set("_error", httpError)
444 }
445 }
446 proxy.Transport = config.Transport
447 proxy.ModifyResponse = config.ModifyResponse
448 return proxy
449}

Callers 1

ToMiddlewareMethod · 0.85

Calls 4

SetMethod · 0.80
StringMethod · 0.45
ErrorMethod · 0.45
WrapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…