github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/checkpoints/response_body.go (about)

     1  package checkpoints
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
     6  	"github.com/TeaOSLab/EdgeNode/internal/waf/utils"
     7  	"github.com/iwind/TeaGo/maps"
     8  	"io"
     9  )
    10  
    11  // ResponseBodyCheckpoint ${responseBody}
    12  type ResponseBodyCheckpoint struct {
    13  	Checkpoint
    14  }
    15  
    16  func (this *ResponseBodyCheckpoint) IsRequest() bool {
    17  	return false
    18  }
    19  
    20  func (this *ResponseBodyCheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
    21  	value = ""
    22  	return
    23  }
    24  
    25  func (this *ResponseBodyCheckpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
    26  	if resp.ContentLength == 0 {
    27  		value = ""
    28  		return
    29  	}
    30  
    31  	value = ""
    32  	if resp != nil && resp.Body != nil {
    33  		if len(resp.BodyData) > 0 {
    34  			value = string(resp.BodyData)
    35  			return
    36  		}
    37  		body, err := io.ReadAll(resp.Body)
    38  		if err != nil {
    39  			sysErr = err
    40  			return
    41  		}
    42  		resp.BodyData = body
    43  		_ = resp.Body.Close()
    44  		value = body
    45  		resp.Body = io.NopCloser(bytes.NewBuffer(body))
    46  	}
    47  	return
    48  }
    49  
    50  func (this *ResponseBodyCheckpoint) CacheLife() utils.CacheLife {
    51  	return utils.CacheMiddleLife
    52  }