github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/utils/http/requtils.go (about)

     1  package httputil
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/projectdiscovery/nuclei/v2/pkg/types"
     8  	"github.com/projectdiscovery/nuclei/v2/pkg/types/scanstrategy"
     9  	"github.com/projectdiscovery/retryablehttp-go"
    10  	urlutil "github.com/projectdiscovery/utils/url"
    11  )
    12  
    13  var (
    14  	// TODO: adapt regex for cases where port is updated
    15  	urlWithPortRegex = regexp.MustCompile(`^{{(BaseURL|RootURL)}}:(\d+)`)
    16  	// regex to detect trailing slash in path (not applicable to raw requests)
    17  	trailingSlashregex = regexp.MustCompile(`^\Q{{\E[a-zA-Z]+\Q}}/\E`)
    18  	// ErrNoMoreRequests is internal error to
    19  )
    20  
    21  // HasTrailingSlash returns true if path(that has default variables) has trailing slash
    22  func HasTrailingSlash(data string) bool {
    23  	return trailingSlashregex.MatchString(data)
    24  }
    25  
    26  // UpdateURLPortFromPayload overrides input port if specified in payload(ex: {{BaseURL}}:8080)
    27  func UpdateURLPortFromPayload(parsed *urlutil.URL, data string) (*urlutil.URL, string) {
    28  	matches := urlWithPortRegex.FindAllStringSubmatch(data, -1)
    29  	if len(matches) > 0 {
    30  		port := matches[0][2]
    31  		parsed.UpdatePort(port)
    32  		// remove it from dsl
    33  		data = strings.Replace(data, ":"+port, "", 1)
    34  	}
    35  	return parsed, data
    36  }
    37  
    38  // setHeader sets some headers only if the header wasn't supplied by the user
    39  func SetHeader(req *retryablehttp.Request, name, value string) {
    40  	if _, ok := req.Header[name]; !ok {
    41  		req.Header.Set(name, value)
    42  	}
    43  	if name == "Host" {
    44  		req.Host = value
    45  	}
    46  }
    47  
    48  // ShouldDisableKeepAlive depending on scan strategy
    49  func ShouldDisableKeepAlive(options *types.Options) bool {
    50  	// with host-spray strategy keep-alive must be enabled
    51  	return options.ScanStrategy != scanstrategy.HostSpray.String()
    52  }