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_r130643696
 
 

 ##########
 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\":")) {
+        newData := findAndLimitString(data, bytes.Index(data, 
[]byte("\"code\":")), bytes.Index(data, []byte("\"binary\":")), limit, buffer)
+        return newData, reload, nil
+    }
+
+    return data, reload, nil
+}
+
+// reqBodyLimiter limits the size of Req Body for --verbose ONLY.
+// It returns truncated Req Body, reloaded io.ReadCloser and any errors.
+func reqBodyLimiter(body io.ReadCloser) ([]byte, io.ReadCloser, error) {
+    limit := 1000    // 1000 byte limit, anything over is truncated
+    buffer := "\n"    // Added to 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 len(data) > limit {
+        Verbose("Req Body excedes %d bytes and will be truncated\n", limit)
+        newData := string(data[:limit]) + buffer    // Must convert to string 
to add buffer otherwise malformed error will be thrown
+        return []byte(newData), reload, nil
 
 Review comment:
   Ah, just remembered why I did that. If you just return a string then you 
would have to wrap data in string. It seems to be a pick your poison, wrap 
newData in a []byte or wrap data in a string.
 
----------------------------------------------------------------
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

Reply via email to