优化结构

This commit is contained in:
Yvan
2026-08-28 15:07:16 +08:00
parent ae5345de14
commit a4ed3a02df
15 changed files with 92 additions and 60 deletions
+3 -28
View File
@@ -43,8 +43,7 @@ func OperationAudit(runtime *config.Store, recorder *systemservice.AuditRecorder
}
if c.Request.Method == http.MethodGet {
requestBody = operationQueryBody(c.Request.URL.RawQuery)
} else if value, ok := c.Get(ctxReqBodyKey); ok {
requestBody = []byte(stringValue(value))
} else if _, ok := c.Get(ctxReqBodyKey); ok {
} else if c.Request.Body != nil {
// Fallback for tests or custom middleware chains that omit AccessLog.
if strings.Contains(c.GetHeader("Content-Type"), "multipart/form-data") {
@@ -79,13 +78,13 @@ func OperationAudit(runtime *config.Store, recorder *systemservice.AuditRecorder
responseBody = redactJSON(body.Bytes(), c.Writer.Header().Get("Content-Type"), maxBytes)
}
}
if isDownloadResponse(c) && len(responseBody) > maxBytes {
if truncated, _ := c.Get(ctxRespTruncatedKey); truncated == true {
responseBody = "[超出记录长度]"
}
errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String()
operationBody := ""
capturedBody := false
if c.Request.Method != http.MethodGet {
if c.Request.Method != "GET" {
if value, ok := c.Get(ctxReqBodyKey); ok {
// AccessLog has already captured, redacted, summarized, and bounded the
// request body. Treat that value as final so audit does not process it a
@@ -162,27 +161,3 @@ func maskOperationBody(value any) {
}
}
}
// operationDownloadHeaders lists the response headers whose presence marks a
// download: the value is the substring that identifies it.
var operationDownloadHeaders = [...]struct{ header, marker string }{
{"Pragma", "public"},
{"Expires", "0"},
{"Cache-Control", "must-revalidate, post-check=0, pre-check=0"},
{"Content-Type", "application/force-download"},
{"Content-Type", "application/octet-stream"},
{"Content-Type", "application/vnd.ms-excel"},
{"Content-Type", "application/download"},
{"Content-Disposition", "attachment"},
{"Content-Transfer-Encoding", "binary"},
}
func isDownloadResponse(c *gin.Context) bool {
header := c.Writer.Header()
for _, candidate := range operationDownloadHeaders {
if strings.Contains(header.Get(candidate.header), candidate.marker) {
return true
}
}
return false
}