MCPcopy Create free account
hub / github.com/coder/coder / handleResponse

Method handleResponse

enterprise/aibridgeproxyd/aibridgeproxyd.go:1047–1104  ·  view source on GitHub ↗

handleResponse handles responses received from aibridged. This is called for every MITM'd request, including the pass-through path where handleRequest re-validated the CONNECT-time provider and forwarded the request to the original upstream instead of aibridged. Pass-through responses are identified

(resp *http.Response, ctx *goproxy.ProxyCtx)

Source from the content-addressed store, hash-verified

1045// to avoid mislabeled logs and corrupting MITM metrics.
1046// Tunneled requests (non-provider-host domains) bypass this handler entirely.
1047func (s *Server) handleResponse(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
1048 if resp == nil {
1049 return nil
1050 }
1051
1052 reqCtx, _ := ctx.UserData.(*requestContext)
1053 connectSessionID := uuid.Nil
1054 requestID := uuid.Nil
1055 provider := ""
1056 if reqCtx != nil {
1057 connectSessionID = reqCtx.ConnectSessionID
1058 requestID = reqCtx.RequestID
1059 provider = reqCtx.Provider
1060 }
1061
1062 logger := s.logger.With(
1063 slog.F("connect_id", connectSessionID.String()),
1064 slog.F("request_id", requestID.String()),
1065 slog.F("provider", provider),
1066 slog.F("status", resp.StatusCode),
1067 )
1068
1069 // Pass-through responses (handleRequest returned without routing to
1070 // aibridged) come from the real upstream. The aibridged-specific log
1071 // and metrics do not apply; the pass-through itself is already logged
1072 // in handleRequest.
1073 if requestID == uuid.Nil {
1074 return resp
1075 }
1076
1077 switch {
1078 case resp.StatusCode >= http.StatusInternalServerError:
1079 logger.Error(s.ctx, "received error response from aibridged",
1080 slog.F("response_body", s.readErrorBodyForLog(resp, logger)))
1081 case resp.StatusCode >= http.StatusBadRequest:
1082 logger.Warn(s.ctx, "received error response from aibridged",
1083 slog.F("response_body", s.readErrorBodyForLog(resp, logger)))
1084 default:
1085 logger.Debug(s.ctx, "received response from aibridged")
1086 }
1087
1088 if s.metrics != nil && provider != "" {
1089 // Decrement inflight requests gauge now that the request is complete.
1090 s.metrics.InflightMITMRequests.WithLabelValues(provider).Dec()
1091
1092 // Record response by status code.
1093 s.metrics.MITMResponsesTotal.WithLabelValues(strconv.Itoa(resp.StatusCode), provider).Inc()
1094 }
1095
1096 // Dump the response to disk when a dumper was created for this request.
1097 if reqCtx != nil && reqCtx.Dumper != nil {
1098 if err := reqCtx.Dumper.DumpResponse(resp); err != nil {
1099 logger.Warn(s.ctx, "failed to dump response", slog.Error(err))
1100 }
1101 }
1102
1103 return resp
1104}

Callers

nothing calls this directly

Calls 5

readErrorBodyForLogMethod · 0.95
WithLabelValuesMethod · 0.80
DumpResponseMethod · 0.65
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected