github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/requests/test_request.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package requests
     4  
     5  import (
     6  	"bytes"
     7  	"io"
     8  	"net"
     9  	"net/http"
    10  )
    11  
    12  type TestRequest struct {
    13  	req      *http.Request
    14  	BodyData []byte
    15  }
    16  
    17  func NewTestRequest(raw *http.Request) *TestRequest {
    18  	return &TestRequest{
    19  		req: raw,
    20  	}
    21  }
    22  
    23  func (this *TestRequest) WAFSetCacheBody(bodyData []byte) {
    24  	this.BodyData = bodyData
    25  }
    26  
    27  func (this *TestRequest) WAFGetCacheBody() []byte {
    28  	return this.BodyData
    29  }
    30  
    31  func (this *TestRequest) WAFRaw() *http.Request {
    32  	return this.req
    33  }
    34  
    35  func (this *TestRequest) WAFRemoteAddr() string {
    36  	return this.req.RemoteAddr
    37  }
    38  
    39  func (this *TestRequest) WAFRemoteIP() string {
    40  	host, _, err := net.SplitHostPort(this.req.RemoteAddr)
    41  	if err != nil {
    42  		return this.req.RemoteAddr
    43  	} else {
    44  		return host
    45  	}
    46  }
    47  
    48  func (this *TestRequest) WAFReadBody(max int64) (data []byte, err error) {
    49  	if this.req.ContentLength > 0 {
    50  		data, err = io.ReadAll(io.LimitReader(this.req.Body, max))
    51  	}
    52  	return
    53  }
    54  
    55  func (this *TestRequest) WAFRestoreBody(data []byte) {
    56  	if len(data) > 0 {
    57  		rawReader := bytes.NewBuffer(data)
    58  		buf := make([]byte, 1024)
    59  		_, _ = io.CopyBuffer(rawReader, this.req.Body, buf)
    60  		this.req.Body = io.NopCloser(rawReader)
    61  	}
    62  }
    63  
    64  func (this *TestRequest) WAFServerId() int64 {
    65  	return 0
    66  }
    67  
    68  // WAFClose 关闭当前请求所在的连接
    69  func (this *TestRequest) WAFClose() {
    70  }
    71  
    72  func (this *TestRequest) Format(s string) string {
    73  	return s
    74  }
    75  
    76  func (this *TestRequest) WAFOnAction(action any) bool {
    77  	return true
    78  }
    79  
    80  func (this *TestRequest) WAFFingerprint() []byte {
    81  	return nil
    82  }
    83  
    84  func (this *TestRequest) DisableAccessLog() {
    85  
    86  }
    87  
    88  func (this *TestRequest) DisableStat() {
    89  
    90  }
    91  
    92  func (this *TestRequest) ProcessResponseHeaders(headers http.Header, status int) {
    93  
    94  }
    95  
    96  func (this *TestRequest) WAFMaxRequestSize() int64 {
    97  	return 1 << 20
    98  }