Skip to content

Commit

Permalink
use methods for buffer options check
Browse files Browse the repository at this point in the history
  • Loading branch information
malud committed May 16, 2023
1 parent c964c6e commit a596359
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
20 changes: 16 additions & 4 deletions eval/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ const (
JSONParseResponse BufferOption = 8
)

func (i BufferOption) Request() bool {
return i&BufferRequest == BufferRequest
}

func (i BufferOption) JSONRequest() bool {
return i&JSONParseRequest == JSONParseRequest
}

func (i BufferOption) Response() bool {
return i&BufferResponse == BufferResponse
}

func (i BufferOption) JSONResponse() bool {
return i&JSONParseResponse == JSONParseResponse
}

func (i BufferOption) GoString() string {
var result []string
for _, o := range []BufferOption{BufferRequest, BufferResponse, JSONParseRequest, JSONParseResponse} {
Expand All @@ -35,10 +51,6 @@ func (i BufferOption) GoString() string {
return strings.Join(result, "|")
}

func (i BufferOption) Response() bool {
return i&BufferResponse == BufferResponse
}

// MustBuffer determines if any of the hcl.bodies makes use of 'body', 'form_body' or 'json_body' or
// of known attributes and variables which require a parsed client-request or backend-response body.
func MustBuffer(bodies ...hcl.Body) BufferOption {
Expand Down
13 changes: 7 additions & 6 deletions eval/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,12 @@ func newBerespValues(ctx context.Context, readBody bool, beresp *http.Response)
port, _ := strconv.ParseInt(p, 10, 64)

bufferOption, bOk := bereq.Context().Value(request.BufferOptions).(BufferOption)
var body, jsonBody cty.Value

if bOk && (bufferOption&BufferRequest) == BufferRequest {
body, jsonBody = parseReqBody(bereq, (bufferOption&JSONParseRequest) == JSONParseRequest)
var body, jsonBody cty.Value
if bOk && bufferOption.Request() {
body, jsonBody = parseReqBody(bereq, bufferOption.JSONRequest())
}

bereqVal = cty.ObjectVal(ContextMap{
Method: cty.StringVal(bereq.Method),
URL: cty.StringVal(bereq.URL.String()),
Expand All @@ -277,10 +278,10 @@ func newBerespValues(ctx context.Context, readBody bool, beresp *http.Response)

var respBody, respJSONBody cty.Value
if readBody && !IsUpgradeResponse(bereq, beresp) {
if bOk && (bufferOption&BufferResponse) == BufferResponse {
respBody, respJSONBody = parseRespBody(beresp, (bufferOption&JSONParseResponse) == JSONParseResponse)
if bOk && bufferOption.Response() {
respBody, respJSONBody = parseRespBody(beresp, bufferOption.JSONResponse())
}
} else if bOk && (bufferOption&BufferResponse) != BufferResponse {
} else if bOk && !bufferOption.Response() {
hasBlock, _ := bereq.Context().Value(request.ResponseBlock).(bool)
ws, _ := bereq.Context().Value(request.WebsocketsAllowed).(bool)
if name != "default" || (name == "default" && hasBlock) {
Expand Down
2 changes: 1 addition & 1 deletion eval/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func SetGetBody(req *http.Request, bufferOpts BufferOption, bodyLimit int64) err
return nil
}

if (bufferOpts & BufferRequest) != BufferRequest {
if !bufferOpts.Request() {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion handler/transport/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (b *Backend) RoundTrip(req *http.Request) (*http.Response, error) {
// from this result.
evalCtx := eval.ContextFromRequest(req)
// has own body variable reference?
readBody := eval.MustBuffer(b.context)&eval.BufferResponse == eval.BufferResponse
readBody := eval.MustBuffer(b.context).Response()
evalCtx = evalCtx.WithBeresp(beresp, backendVal, readBody)

clfValue, err := eval.EvalCustomLogFields(evalCtx.HCLContext(), ctxBody)
Expand Down

0 comments on commit a596359

Please sign in to comment.