github.com/yaling888/clash@v1.53.0/mitm/util.go (about)

     1  package mitm
     2  
     3  import (
     4  	"strings"
     5  
     6  	regexp "github.com/dlclark/regexp2"
     7  )
     8  
     9  var allowContentType = []string{
    10  	"text/",
    11  	"application/xhtml",
    12  	"application/xml",
    13  	"application/atom+xml",
    14  	"application/json",
    15  	"application/x-www-form-urlencoded",
    16  }
    17  
    18  func CanRewriteBody(contentLength int64, contentEncoding, contentType string) bool {
    19  	if contentLength <= 0 && contentEncoding == "" {
    20  		return false
    21  	}
    22  
    23  	for _, v := range allowContentType {
    24  		if strings.HasPrefix(contentType, v) {
    25  			return true
    26  		}
    27  	}
    28  
    29  	return false
    30  }
    31  
    32  func findStringSubmatch(re *regexp.Regexp, s string) []string {
    33  	var sub []string
    34  	m, _ := re.FindStringMatch(s)
    35  	for m != nil {
    36  		for _, g := range m.Groups() {
    37  			for _, c := range g.Captures {
    38  				sub = append(sub, c.String())
    39  			}
    40  		}
    41  		m, _ = re.FindNextMatch(m)
    42  	}
    43  	return sub
    44  }