Better error handling, many fixes all around
This commit is contained in:
@@ -3,8 +3,11 @@ package gemini
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"gemini-grc/logging"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type URL struct {
|
||||
@@ -38,7 +41,13 @@ func (u URL) String() string {
|
||||
return u.Full
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface
|
||||
func (u URL) StringNoDefaultPort() string {
|
||||
if u.Port == 1965 {
|
||||
return fmt.Sprintf("%s://%s%s", u.Protocol, u.Hostname, u.Path)
|
||||
}
|
||||
return u.Full
|
||||
}
|
||||
|
||||
func (u URL) Value() (driver.Value, error) {
|
||||
if u.Full == "" {
|
||||
return nil, nil
|
||||
@@ -65,3 +74,24 @@ func ParseURL(input string, descr string) (*URL, error) {
|
||||
full := fmt.Sprintf("%s://%s:%d%s", protocol, hostname, port, path)
|
||||
return &URL{Protocol: protocol, Hostname: hostname, Port: port, Path: path, Descr: descr, Full: full}, nil
|
||||
}
|
||||
|
||||
func DeriveAbsoluteURL(currentURL URL, input string) (*URL, error) {
|
||||
logging.LogDebug("Calculating redirect URL. Current %s header string %s", currentURL, input)
|
||||
// If URL is absolute, return just it
|
||||
if strings.Contains(input, "://") {
|
||||
return ParseURL(input, "")
|
||||
}
|
||||
// input is a path. Clean it and construct
|
||||
// new path
|
||||
var newPath string
|
||||
// Handle weird cases found in the wild
|
||||
if strings.HasPrefix(input, "/") {
|
||||
newPath = path.Clean(input)
|
||||
} else if input == "./" || input == "." {
|
||||
newPath = path.Join(currentURL.Path, "/")
|
||||
} else {
|
||||
newPath = path.Join(currentURL.Path, path.Clean(input))
|
||||
}
|
||||
strURL := fmt.Sprintf("%s://%s:%d%s", currentURL.Protocol, currentURL.Hostname, currentURL.Port, newPath)
|
||||
return ParseURL(strURL, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user