2023-07-28 19:28:20 +05:30
|
|
|
package httputil
|
2023-02-01 17:23:28 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
|
|
|
|
urlutil "github.com/projectdiscovery/utils/url"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
// TODO: adapt regex for cases where port is updated
|
|
|
|
|
urlWithPortRegex = regexp.MustCompile(`^{{(BaseURL|RootURL)}}:(\d+)`)
|
2023-08-01 14:33:43 -04:00
|
|
|
// regex to detect trailing slash in path (not applicable to raw requests)
|
2023-02-01 17:23:28 +05:30
|
|
|
trailingSlashregex = regexp.MustCompile(`^\Q{{\E[a-zA-Z]+\Q}}/\E`)
|
2023-07-13 00:51:06 +05:30
|
|
|
// ErrNoMoreRequests is internal error to
|
2023-02-01 17:23:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// HasTrailingSlash returns true if path(that has default variables) has trailing slash
|
|
|
|
|
func HasTrailingSlash(data string) bool {
|
|
|
|
|
return trailingSlashregex.MatchString(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateURLPortFromPayload overrides input port if specified in payload(ex: {{BaseURL}}:8080)
|
|
|
|
|
func UpdateURLPortFromPayload(parsed *urlutil.URL, data string) (*urlutil.URL, string) {
|
|
|
|
|
matches := urlWithPortRegex.FindAllStringSubmatch(data, -1)
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
|
port := matches[0][2]
|
|
|
|
|
parsed.UpdatePort(port)
|
|
|
|
|
// remove it from dsl
|
|
|
|
|
data = strings.Replace(data, ":"+port, "", 1)
|
|
|
|
|
}
|
|
|
|
|
return parsed, data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setHeader sets some headers only if the header wasn't supplied by the user
|
|
|
|
|
func SetHeader(req *retryablehttp.Request, name, value string) {
|
|
|
|
|
if _, ok := req.Header[name]; !ok {
|
|
|
|
|
req.Header.Set(name, value)
|
|
|
|
|
}
|
|
|
|
|
if name == "Host" {
|
|
|
|
|
req.Host = value
|
|
|
|
|
}
|
|
|
|
|
}
|