2025-04-16 08:07:51 +00:00
|
|
|
// Copyright 2023, 2024, 2025 The Forgejo Authors. All rights reserved.
|
2024-05-07 17:58:13 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package forgefed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2025-04-16 08:07:51 +00:00
|
|
|
"strconv"
|
2024-05-07 17:58:13 +02:00
|
|
|
"strings"
|
|
|
|
|
2025-03-27 19:40:14 +00:00
|
|
|
"forgejo.org/modules/validation"
|
2024-05-07 17:58:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------- ActorID --------------------------------------------
|
|
|
|
type ActorID struct {
|
2025-04-16 08:07:51 +00:00
|
|
|
ID string
|
|
|
|
Source string
|
|
|
|
HostSchema string
|
|
|
|
Path string
|
|
|
|
Host string
|
|
|
|
HostPort uint16
|
|
|
|
UnvalidatedInput string
|
|
|
|
IsPortSupplemented bool
|
2024-05-07 17:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Factory function for ActorID. Created struct is asserted to be valid
|
|
|
|
func NewActorID(uri string) (ActorID, error) {
|
|
|
|
result, err := newActorID(uri)
|
|
|
|
if err != nil {
|
|
|
|
return ActorID{}, err
|
|
|
|
}
|
|
|
|
|
2024-05-14 08:24:31 +02:00
|
|
|
if valid, err := validation.IsValid(result); !valid {
|
|
|
|
return ActorID{}, err
|
2024-05-07 17:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id ActorID) AsURI() string {
|
2025-06-02 22:29:10 +02:00
|
|
|
var result, path string
|
|
|
|
|
|
|
|
if id.Path == "" {
|
|
|
|
path = id.ID
|
|
|
|
} else {
|
|
|
|
path = fmt.Sprintf("%s/%s", id.Path, id.ID)
|
|
|
|
}
|
2025-04-16 08:07:51 +00:00
|
|
|
|
|
|
|
if id.IsPortSupplemented {
|
2025-06-02 22:29:10 +02:00
|
|
|
result = fmt.Sprintf("%s://%s/%s", id.HostSchema, id.Host, path)
|
2024-05-07 17:58:13 +02:00
|
|
|
} else {
|
2025-06-02 22:29:10 +02:00
|
|
|
result = fmt.Sprintf("%s://%s:%d/%s", id.HostSchema, id.Host, id.HostPort, path)
|
2024-05-07 17:58:13 +02:00
|
|
|
}
|
2025-04-16 08:07:51 +00:00
|
|
|
|
2024-05-07 17:58:13 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id ActorID) Validate() []string {
|
|
|
|
var result []string
|
2025-06-02 22:29:10 +02:00
|
|
|
result = append(result, validation.ValidateNotEmpty(id.ID, "ID")...)
|
2024-05-07 17:58:13 +02:00
|
|
|
result = append(result, validation.ValidateNotEmpty(id.Host, "host")...)
|
2025-04-16 08:07:51 +00:00
|
|
|
result = append(result, validation.ValidateNotEmpty(id.HostPort, "hostPort")...)
|
|
|
|
result = append(result, validation.ValidateNotEmpty(id.HostSchema, "hostSchema")...)
|
2024-05-07 17:58:13 +02:00
|
|
|
result = append(result, validation.ValidateNotEmpty(id.UnvalidatedInput, "unvalidatedInput")...)
|
|
|
|
|
|
|
|
if id.UnvalidatedInput != id.AsURI() {
|
|
|
|
result = append(result, fmt.Sprintf("not all input was parsed, \nUnvalidated Input:%q \nParsed URI: %q", id.UnvalidatedInput, id.AsURI()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func newActorID(uri string) (ActorID, error) {
|
|
|
|
validatedURI, err := url.ParseRequestURI(uri)
|
|
|
|
if err != nil {
|
|
|
|
return ActorID{}, err
|
|
|
|
}
|
|
|
|
pathWithActorID := strings.Split(validatedURI.Path, "/")
|
|
|
|
if containsEmptyString(pathWithActorID) {
|
|
|
|
pathWithActorID = removeEmptyStrings(pathWithActorID)
|
|
|
|
}
|
|
|
|
length := len(pathWithActorID)
|
|
|
|
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
|
2025-04-16 08:07:51 +00:00
|
|
|
id := strings.ToLower(pathWithActorID[length-1])
|
2024-05-07 17:58:13 +02:00
|
|
|
|
|
|
|
result := ActorID{}
|
|
|
|
result.ID = id
|
2025-04-16 08:07:51 +00:00
|
|
|
result.HostSchema = strings.ToLower(validatedURI.Scheme)
|
|
|
|
result.Host = strings.ToLower(validatedURI.Hostname())
|
|
|
|
result.Path = strings.ToLower(pathWithoutActorID)
|
|
|
|
|
|
|
|
if validatedURI.Port() == "" && result.HostSchema == "https" {
|
|
|
|
result.IsPortSupplemented = true
|
|
|
|
result.HostPort = 443
|
|
|
|
} else if validatedURI.Port() == "" && result.HostSchema == "http" {
|
|
|
|
result.IsPortSupplemented = true
|
|
|
|
result.HostPort = 80
|
|
|
|
} else {
|
|
|
|
numPort, _ := strconv.ParseUint(validatedURI.Port(), 10, 16)
|
|
|
|
result.HostPort = uint16(numPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
result.UnvalidatedInput = strings.ToLower(uri)
|
|
|
|
|
2024-05-07 17:58:13 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2025-06-02 22:29:10 +02:00
|
|
|
func containsEmptyString(ar []string) bool {
|
|
|
|
for _, elem := range ar {
|
|
|
|
if elem == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2024-05-07 17:58:13 +02:00
|
|
|
}
|
|
|
|
|
2025-06-02 22:29:10 +02:00
|
|
|
func removeEmptyStrings(ls []string) []string {
|
|
|
|
var rs []string
|
|
|
|
for _, str := range ls {
|
|
|
|
if str != "" {
|
|
|
|
rs = append(rs, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rs
|
2024-05-07 17:58:13 +02:00
|
|
|
}
|