Path: blob/dev/pkg/protocols/utils/http/requtils.go
2073 views
package httputil12import (3"regexp"4"strings"56"github.com/projectdiscovery/nuclei/v3/pkg/types"7"github.com/projectdiscovery/nuclei/v3/pkg/types/scanstrategy"8"github.com/projectdiscovery/retryablehttp-go"9urlutil "github.com/projectdiscovery/utils/url"10)1112var (13// TODO: adapt regex for cases where port is updated14urlWithPortRegex = regexp.MustCompile(`^{{(BaseURL|RootURL)}}:(\d+)`)15// regex to detect trailing slash in path (not applicable to raw requests)16trailingSlashregex = regexp.MustCompile(`^\Q{{\E[a-zA-Z]+\Q}}/\E`)17// ErrNoMoreRequests is internal error to18)1920// HasTrailingSlash returns true if path(that has default variables) has trailing slash21func HasTrailingSlash(data string) bool {22return trailingSlashregex.MatchString(data)23}2425// UpdateURLPortFromPayload overrides input port if specified in payload(ex: {{BaseURL}}:8080)26func UpdateURLPortFromPayload(parsed *urlutil.URL, data string) (*urlutil.URL, string) {27matches := urlWithPortRegex.FindAllStringSubmatch(data, -1)28if len(matches) > 0 {29port := matches[0][2]30parsed.UpdatePort(port)31// remove it from dsl32data = strings.Replace(data, ":"+port, "", 1)33}34return parsed, data35}3637// SetHeader sets some headers only if the header wasn't supplied by the user38func SetHeader(req *retryablehttp.Request, name, value string) {39if _, ok := req.Header[name]; !ok {40req.Header.Set(name, value)41}42if name == "Host" {43req.Host = value44}45}4647// ShouldDisableKeepAlive depending on scan strategy48func ShouldDisableKeepAlive(options *types.Options) bool {49// with host-spray strategy keep-alive must be enabled50return options.ScanStrategy != scanstrategy.HostSpray.String()51}525354