fix(metrics): non ws response being encoded twice; simplified response handling

This commit is contained in:
yusing
2025-08-29 20:46:47 +08:00
parent 0a5438b18b
commit a8beb2d92f

View File

@@ -1,6 +1,8 @@
package metrics package metrics
import ( import (
"io"
"maps"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -52,25 +54,22 @@ func SystemInfo(c *gin.Context) {
isWS := httpheaders.IsWebsocket(c.Request.Header) isWS := httpheaders.IsWebsocket(c.Request.Header)
if !isWS { if !isWS {
respData, status, err := agent.Forward(c.Request, agentPkg.EndpointSystemInfo) resp, err := agent.Forward(c.Request, agentPkg.EndpointSystemInfo)
if err != nil { if err != nil {
c.Error(apitypes.InternalServerError(err, "failed to forward request to agent")) c.Error(apitypes.InternalServerError(err, "failed to forward request to agent"))
return return
} }
if status != http.StatusOK { maps.Copy(c.Writer.Header(), resp.Header)
c.JSON(status, apitypes.Error(string(respData))) c.Status(resp.StatusCode)
return io.Copy(c.Writer, resp.Body)
}
c.JSON(status, respData)
} else { } else {
rp := reverseproxy.NewReverseProxy("agent", nettypes.NewURL(agentPkg.AgentURL), agent.Transport()) rp := reverseproxy.NewReverseProxy("agent", nettypes.NewURL(agentPkg.AgentURL), agent.Transport())
header := c.Request.Header.Clone() r, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, agentPkg.EndpointSystemInfo+"?"+query.Encode(), c.Request.Body)
r, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, agentPkg.EndpointSystemInfo+"?"+query.Encode(), nil)
if err != nil { if err != nil {
c.Error(apitypes.InternalServerError(err, "failed to create request")) c.Error(apitypes.InternalServerError(err, "failed to create request"))
return return
} }
r.Header = header r.Header = c.Request.Header
rp.ServeHTTP(c.Writer, r) rp.ServeHTTP(c.Writer, r)
} }
} }