github.com/gogf/gf/v2@v2.7.4/net/gtrace/gtrace_content.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gtrace 8 9 import ( 10 "net/http" 11 "strings" 12 13 "github.com/gogf/gf/v2/encoding/gcompress" 14 15 "github.com/gogf/gf/v2/text/gstr" 16 ) 17 18 // SafeContentForHttp cuts and returns given content by `MaxContentLogSize`. 19 // It appends string `...` to the tail of the result if the content size is greater than `MaxContentLogSize`. 20 func SafeContentForHttp(data []byte, header http.Header) (string, error) { 21 var err error 22 if gzipAccepted(header) { 23 if data, err = gcompress.UnGzip(data); err != nil { 24 return string(data), err 25 } 26 } 27 28 return SafeContent(data), nil 29 } 30 31 // SafeContent cuts and returns given content by `MaxContentLogSize`. 32 // It appends string `...` to the tail of the result if the content size is greater than `MaxContentLogSize`. 33 func SafeContent(data []byte) string { 34 content := string(data) 35 if gstr.LenRune(content) > MaxContentLogSize() { 36 content = gstr.StrLimitRune(content, MaxContentLogSize(), "...") 37 } 38 39 return content 40 } 41 42 // gzipAccepted returns whether the client will accept gzip-encoded content. 43 func gzipAccepted(header http.Header) bool { 44 a := header.Get("Content-Encoding") 45 parts := strings.Split(a, ",") 46 for _, part := range parts { 47 part = strings.TrimSpace(part) 48 if part == "gzip" || strings.HasPrefix(part, "gzip;") { 49 return true 50 } 51 } 52 53 return false 54 }