Added separate Path and File variables for HTTP requests

dev
Ice3man543 2021-08-30 13:35:11 +05:30
parent 3430ca2567
commit 482fea58a8
2 changed files with 14 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/url"
"path"
"regexp"
"strings"
"time"
@ -272,13 +273,23 @@ func generateVariables(parsed *url.URL, trailingSlash bool) map[string]interface
parsed.Path = strings.TrimSuffix(parsed.Path, "/")
}
escapedPath := parsed.EscapedPath()
directory := path.Dir(escapedPath)
if directory == "." {
directory = ""
}
base := path.Base(escapedPath)
if base == "." {
base = ""
}
return map[string]interface{}{
"BaseURL": parsed.String(),
"RootURL": fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host),
"Hostname": parsed.Host,
"Host": domain,
"Port": port,
"Path": parsed.EscapedPath(),
"Path": directory,
"File": base,
"Scheme": parsed.Scheme,
}
}

View File

@ -28,7 +28,8 @@ func TestVariables(t *testing.T) {
require.Equal(t, values["BaseURL"], parsed.String(), "incorrect baseurl")
require.Equal(t, values["RootURL"], "http://localhost:9001", "incorrect rootURL")
require.Equal(t, values["Host"], "localhost", "incorrect domain name")
require.Equal(t, values["Path"], "/test/123", "incorrect path")
require.Equal(t, values["Path"], "/test", "incorrect path")
require.Equal(t, values["File"], "123", "incorrect file")
require.Equal(t, values["Port"], "9001", "incorrect port number")
require.Equal(t, values["Scheme"], "http", "incorrect scheme")
require.Equal(t, values["Hostname"], "localhost:9001", "incorrect hostname")