MCPcopy Create free account
hub / github.com/ContentSquare/chproxy / getQuerySnippetFromBody

Function getQuerySnippetFromBody

utils.go:86–129  ·  view source on GitHub ↗
(req *http.Request)

Source from the content-addressed store, hash-verified

84}
85
86func getQuerySnippetFromBody(req *http.Request) string {
87 if req.Body == nil {
88 return ""
89 }
90
91 crc, ok := req.Body.(*cachedReadCloser)
92 if !ok {
93 crc = &cachedReadCloser{
94 ReadCloser: req.Body,
95 }
96 }
97
98 // 'read' request body, so it traps into to crc.
99 // Ignore any errors, since getQuerySnippet is called only
100 // during error reporting.
101 // Temporary solution: Quick and dirty way to work with the request body.
102 // TODO: Create an original copy of req.Body and work with the copy to avoid altering the original request.
103 // This current approach consumes the req.Body content with io.Copy(io.Discard, crc) to reset the internal state of crc.
104 // However, it is not the most efficient or safest method, as it modifies the original req.Body.
105 io.Copy(io.Discard, crc) // nolint
106 data := crc.String()
107
108 // Here, we attempt to restore req.Body by wrapping the string data in a ReadCloser.
109 // This is part of the temporary solution and should be replaced with a more robust method that does not consume the original req.Body.
110 req.Body = io.NopCloser(strings.NewReader(data))
111
112 u := getDecompressor(req)
113 if u == nil {
114 return data
115 }
116 bs := bytes.NewBufferString(data)
117 b, err := u.decompress(bs)
118 if err == nil {
119 return string(b)
120 }
121 // It is better to return partially decompressed data instead of an empty string.
122 if len(b) > 0 {
123 return string(b)
124 }
125
126 // The data failed to be decompressed. Return compressed data
127 // instead of an empty string.
128 return data
129}
130
131// getFullQuery returns full query from req.
132func getFullQuery(req *http.Request) ([]byte, error) {

Callers 1

getQuerySnippetFunction · 0.85

Calls 3

getDecompressorFunction · 0.85
decompressMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected