nuclei/v2/pkg/requests/util.go

78 lines
1.9 KiB
Go
Raw Normal View History

2020-04-30 17:39:33 +02:00
package requests
import (
"bytes"
"compress/gzip"
2020-04-30 17:39:33 +02:00
"fmt"
"io/ioutil"
2020-04-30 17:39:33 +02:00
"strings"
)
2020-10-11 19:37:34 +02:00
const (
2020-10-16 22:07:00 +02:00
markerParenthesisOpen = "{{"
markerParenthesisClose = "}}"
markerGeneral = "§"
2020-10-11 19:37:34 +02:00
)
2020-04-30 17:39:33 +02:00
func newReplacer(values map[string]interface{}) *strings.Replacer {
var replacerItems []string
2020-10-16 22:07:00 +02:00
for key, val := range values {
replacerItems = append(
replacerItems,
fmt.Sprintf("%s%s%s", markerParenthesisOpen, key, markerParenthesisClose),
fmt.Sprintf("%s", val),
fmt.Sprintf("%s%s%s", markerGeneral, key, markerGeneral),
fmt.Sprintf("%s", val),
)
2020-04-30 17:39:33 +02:00
}
return strings.NewReplacer(replacerItems...)
}
// HandleDecompression if the user specified a custom encoding (as golang transport doesn't do this automatically)
func HandleDecompression(r *HTTPRequest, bodyOrig []byte) (bodyDec []byte, err error) {
if r.Request == nil {
return bodyOrig, nil
}
2020-11-19 01:27:06 +01:00
encodingHeader := strings.TrimSpace(strings.ToLower(r.Request.Header.Get("Accept-Encoding")))
2020-11-20 10:19:51 +01:00
if encodingHeader == "gzip" || encodingHeader == "gzip, deflate" {
gzipreader, err := gzip.NewReader(bytes.NewReader(bodyOrig))
if err != nil {
return bodyDec, err
}
defer gzipreader.Close()
bodyDec, err = ioutil.ReadAll(gzipreader)
if err != nil {
return bodyDec, err
}
return bodyDec, nil
}
return bodyOrig, nil
}
// ZipMapValues converts values from strings slices to flat string
func ZipMapValues(m map[string][]string) (m1 map[string]string) {
m1 = make(map[string]string)
for k, v := range m {
m1[k] = strings.Join(v, "")
}
return
}
// ExpandMapValues converts values from flat string to strings slice
func ExpandMapValues(m map[string]string) (m1 map[string][]string) {
m1 = make(map[string][]string)
for k, v := range m {
m1[k] = []string{v}
}
return
}
2020-11-26 11:00:25 +01:00
func hasMarker(s string) bool {
return strings.Contains(s, markerParenthesisOpen) || strings.Contains(s, markerParenthesisClose) || strings.Contains(s, markerGeneral)
}