bpoole16 commented on a change in pull request #2491: Limit length of HTTP body
displayed when debugging.
URL:
https://github.com/apache/incubator-openwhisk/pull/2491#discussion_r130422528
##########
File path: tools/cli/go-whisk/whisk/client.go
##########
@@ -222,14 +222,76 @@ func (c *Client) addAuthHeader(req *http.Request,
authRequired bool) error {
}
return nil
}
+// findAndLimitString() takes in a []byte(str) which is used to reprsent a
string.
+// It then finds a substring that is specified by a start(startAt) and
end(endAt) index.
+// This substring is then limited if it is greater than a specified
length(limit) and a format buffer is added to the end.
+// A new []byte(newStr) is created containing the original []byte(str) and the
newly created limited substring.
+// It then returns the newly constructed []byte(newStr).
+func findAndLimitString(str []byte, startAt, endAt, limit int, buffer string)
[]byte {
+ var newStr []byte
+
+ limitAt := limit + startAt // Calculates correct index to end the
limited substring
+ newStr = append(newStr, str[:startAt]...)
+ if (len(str[startAt:endAt]) > limit) { // Checks if substring exceeds
limit
+ Verbose("Substring exceeds %d bytes and will be truncated\n", limit)
+ newStr = append(newStr, str[startAt:limitAt]...) // Appends the
limited substring
+ newStr = append(newStr, buffer...) // Adds a buffer to keep
consistent formating
+ } else {
+ newStr = append(newStr, str[startAt:endAt]...) // If substring does
not exceed the limit use original substring
+ }
+ newStr = append(newStr, str[endAt:]...)
+
+ return newStr
+}
+
+// respBodyLimiter limits the size of the "code" field in Resp Body for
--verbose ONLY.
+// It returns truncated Resp Body, reloaded io.ReadCloser and any errors.
+func respBodyLimiter(body io.ReadCloser) ([]byte, io.ReadCloser, error) {
+ limit := 1000 // 1000 byte limit, anything over is truncated
+ buffer := "\"\n " // Appended to the end of newData to keep correct
formating
+ data, err := ioutil.ReadAll(body)
+ if err != nil {
+ Verbose("ioutil.ReadAll(req.Body) error: %s\n", err)
+ werr := MakeWskError(err, EXITCODE_ERR_NETWORK, DISPLAY_MSG,
NO_DISPLAY_USAGE)
+ return nil, body, werr
+ }
+ reload := ioutil.NopCloser(bytes.NewBuffer(data))
+ if bytes.Contains(data, []byte("\"code\":")) && bytes.Contains(data,
[]byte("\"binary\":")) {
Review comment:
The goal is to limit the fields that are known to run long, this really only
being the code field. I'm trying to figure out the best way to extract the code
field and limit it and then reconstruct the output with the newly limited code
field. The current implementation is not the best way to do it and has issues
displaying correctly with other commands like `wsk action get largeFile -v`.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services