Skip to content

Commit

Permalink
topdown: Return response headers with http.send
Browse files Browse the repository at this point in the history
Previously we left them off the returned AST object, this will now
just massage them into valid types and pass them along into the
return value.

Fixes: #2238
Signed-off-by: Patrick East <east.patrick@gmail.com>
  • Loading branch information
patrick-east authored and tsandall committed Apr 6, 2020
1 parent 29d33be commit a2ae154
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 69 deletions.
1 change: 1 addition & 0 deletions docs/content/policy-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ The `response` object parameter will contain the following fields:
| `status_code` | `number` | HTTP status code (e.g., `200`). |
| `body` | `any` | Any JSON value. If the HTTP response message body was not deserialized from JSON, this field is set to `null`. |
| `raw_body` | `string` | The entire raw HTTP response message body represented as a string. |
| `headers` | `object` | An object containing the response headers. The values will be an array of strings, repeated headers are grouped under the same keys with all values in the array. |

The table below shows examples of calling `http.send`:

Expand Down
10 changes: 10 additions & 0 deletions topdown/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,21 @@ func executeHTTPRequest(bctx BuiltinContext, obj ast.Object) (ast.Value, error)
json.NewDecoder(&buf).Decode(&resultBody)
}

respHeaders := map[string]interface{}{}
for headerName, values := range resp.Header {
var respValues []interface{}
for _, v := range values {
respValues = append(respValues, v)
}
respHeaders[headerName] = respValues
}

result := make(map[string]interface{})
result["status"] = resp.Status
result["status_code"] = resp.StatusCode
result["body"] = resultBody
result["raw_body"] = string(resultRawBody)
result["headers"] = respHeaders

resultObj, err := ast.InterfaceToValue(result)
if err != nil {
Expand Down
Loading

0 comments on commit a2ae154

Please sign in to comment.