HostPort use uint16 datatype now

This commit is contained in:
zam 2025-04-03 15:19:21 +02:00
parent 0a58bcc0ef
commit bcf2defd8a
5 changed files with 26 additions and 19 deletions

View file

@ -18,7 +18,7 @@ type FederationHost struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
HostFqdn string `xorm:"host_fqdn UNIQUE INDEX VARCHAR(255) NOT NULL"` HostFqdn string `xorm:"host_fqdn UNIQUE INDEX VARCHAR(255) NOT NULL"`
NodeInfo NodeInfo `xorm:"extends NOT NULL"` NodeInfo NodeInfo `xorm:"extends NOT NULL"`
HostPort string `xorm:"NOT NULL DEFAULT '443'"` HostPort uint16 `xorm:"NOT NULL DEFAULT '443'"`
HostSchema string `xorm:"NOT NULL DEFAULT 'https'"` HostSchema string `xorm:"NOT NULL DEFAULT 'https'"`
LatestActivity time.Time `xorm:"NOT NULL"` LatestActivity time.Time `xorm:"NOT NULL"`
Created timeutil.TimeStamp `xorm:"created"` Created timeutil.TimeStamp `xorm:"created"`
@ -26,7 +26,7 @@ type FederationHost struct {
} }
// Factory function for FederationHost. Created struct is asserted to be valid. // Factory function for FederationHost. Created struct is asserted to be valid.
func NewFederationHost(hostFqdn string, nodeInfo NodeInfo, port, schema string) (FederationHost, error) { func NewFederationHost(hostFqdn string, nodeInfo NodeInfo, port uint16, schema string) (FederationHost, error) {
result := FederationHost{ result := FederationHost{
HostFqdn: strings.ToLower(hostFqdn), HostFqdn: strings.ToLower(hostFqdn),
NodeInfo: nodeInfo, NodeInfo: nodeInfo,

View file

@ -6,6 +6,7 @@ package forgefed
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"strconv"
"strings" "strings"
"forgejo.org/modules/validation" "forgejo.org/modules/validation"
@ -20,7 +21,7 @@ type ActorID struct {
HostSchema string HostSchema string
Path string Path string
Host string Host string
HostPort string HostPort uint16
UnvalidatedInput string UnvalidatedInput string
IsPortSupplemented bool IsPortSupplemented bool
} }
@ -44,7 +45,7 @@ func (id ActorID) AsURI() string {
if id.IsPortSupplemented { if id.IsPortSupplemented {
result = fmt.Sprintf("%s://%s/%s/%s", id.HostSchema, id.Host, id.Path, id.ID) result = fmt.Sprintf("%s://%s/%s/%s", id.HostSchema, id.Host, id.Path, id.ID)
} else { } else {
result = fmt.Sprintf("%s://%s:%s/%s/%s", id.HostSchema, id.Host, id.HostPort, id.Path, id.ID) result = fmt.Sprintf("%s://%s:%d/%s/%s", id.HostSchema, id.Host, id.HostPort, id.Path, id.ID)
} }
return result return result
} }
@ -192,18 +193,20 @@ func newActorID(uri string) (ActorID, error) {
if validatedURI.Port() == "" && result.HostSchema == "https" { if validatedURI.Port() == "" && result.HostSchema == "https" {
result.IsPortSupplemented = true result.IsPortSupplemented = true
result.HostPort = "443" result.HostPort = 443
result.UnvalidatedInput = fmt.Sprintf("%s://%s/%s/%s", result.HostSchema, result.Host, result.Path, result.ID) result.UnvalidatedInput = fmt.Sprintf("%s://%s/%s/%s", result.HostSchema, result.Host, result.Path, result.ID)
return result, nil return result, nil
} else if validatedURI.Port() == "" && result.HostSchema == "http" { } else if validatedURI.Port() == "" && result.HostSchema == "http" {
result.IsPortSupplemented = true result.IsPortSupplemented = true
result.HostPort = "80" result.HostPort = 80
result.UnvalidatedInput = fmt.Sprintf("%s://%s/%s/%s", result.HostSchema, result.Host, result.Path, result.ID) result.UnvalidatedInput = fmt.Sprintf("%s://%s/%s/%s", result.HostSchema, result.Host, result.Path, result.ID)
return result, nil return result, nil
} }
result.HostPort = validatedURI.Port() numPort, _ := strconv.ParseUint(validatedURI.Port(), 10, 16)
result.UnvalidatedInput = fmt.Sprintf("%s://%s:%s/%s/%s", result.HostSchema, result.Host, result.HostPort, result.Path, result.ID) result.HostPort = uint16(numPort)
result.UnvalidatedInput = fmt.Sprintf("%s://%s:%d/%s/%s", result.HostSchema, result.Host, result.HostPort, result.Path, result.ID)
return result, nil return result, nil
} }

View file

@ -21,7 +21,7 @@ func TestNewPersonId(t *testing.T) {
expected.HostSchema = "https" expected.HostSchema = "https"
expected.Path = "api/v1/activitypub/user-id" expected.Path = "api/v1/activitypub/user-id"
expected.Host = "an.other.host" expected.Host = "an.other.host"
expected.HostPort = "443" expected.HostPort = 443
expected.IsPortSupplemented = true expected.IsPortSupplemented = true
expected.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1" expected.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1"
@ -36,7 +36,7 @@ func TestNewPersonId(t *testing.T) {
expected.HostSchema = "https" expected.HostSchema = "https"
expected.Path = "api/v1/activitypub/user-id" expected.Path = "api/v1/activitypub/user-id"
expected.Host = "an.other.host" expected.Host = "an.other.host"
expected.HostPort = "443" expected.HostPort = 443
expected.IsPortSupplemented = false expected.IsPortSupplemented = false
expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1" expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1"
@ -51,7 +51,7 @@ func TestNewPersonId(t *testing.T) {
expected.HostSchema = "http" expected.HostSchema = "http"
expected.Path = "api/v1/activitypub/user-id" expected.Path = "api/v1/activitypub/user-id"
expected.Host = "an.other.host" expected.Host = "an.other.host"
expected.HostPort = "80" expected.HostPort = 80
expected.IsPortSupplemented = false expected.IsPortSupplemented = false
expected.UnvalidatedInput = "http://an.other.host:80/api/v1/activitypub/user-id/1" expected.UnvalidatedInput = "http://an.other.host:80/api/v1/activitypub/user-id/1"
@ -66,7 +66,7 @@ func TestNewPersonId(t *testing.T) {
expected.HostSchema = "https" expected.HostSchema = "https"
expected.Path = "api/v1/activitypub/user-id" expected.Path = "api/v1/activitypub/user-id"
expected.Host = "an.other.host" expected.Host = "an.other.host"
expected.HostPort = "443" expected.HostPort = 443
expected.IsPortSupplemented = false expected.IsPortSupplemented = false
expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1" expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1"
@ -84,7 +84,7 @@ func TestNewRepositoryId(t *testing.T) {
expected.HostSchema = "http" expected.HostSchema = "http"
expected.Path = "api/activitypub/repository-id" expected.Path = "api/activitypub/repository-id"
expected.Host = "localhost" expected.Host = "localhost"
expected.HostPort = "3000" expected.HostPort = 3000
expected.UnvalidatedInput = "http://localhost:3000/api/activitypub/repository-id/1" expected.UnvalidatedInput = "http://localhost:3000/api/activitypub/repository-id/1"
sut, _ := NewRepositoryID("http://localhost:3000/api/activitypub/repository-id/1", "forgejo") sut, _ := NewRepositoryID("http://localhost:3000/api/activitypub/repository-id/1", "forgejo")
if sut != expected { if sut != expected {
@ -98,7 +98,7 @@ func TestActorIdValidation(t *testing.T) {
sut.HostSchema = "https" sut.HostSchema = "https"
sut.Path = "api/v1/activitypub/user-id" sut.Path = "api/v1/activitypub/user-id"
sut.Host = "an.other.host" sut.Host = "an.other.host"
sut.HostPort = "0" sut.HostPort = 0
sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/" sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/"
if sut.Validate()[0] != "userId should not be empty" { if sut.Validate()[0] != "userId should not be empty" {
t.Errorf("validation error expected but was: %v\n", sut.Validate()) t.Errorf("validation error expected but was: %v\n", sut.Validate())
@ -110,7 +110,7 @@ func TestActorIdValidation(t *testing.T) {
sut.HostSchema = "https" sut.HostSchema = "https"
sut.Path = "api/v1/activitypub/user-id" sut.Path = "api/v1/activitypub/user-id"
sut.Host = "an.other.host" sut.Host = "an.other.host"
sut.HostPort = "0" sut.HostPort = 0
sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1?illegal=action" sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1?illegal=action"
if sut.Validate()[0] != "not all input was parsed, \nUnvalidated Input:\"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" \nParsed URI: \"https://an.other.host/api/v1/activitypub/user-id/1\"" { if sut.Validate()[0] != "not all input was parsed, \nUnvalidated Input:\"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" \nParsed URI: \"https://an.other.host/api/v1/activitypub/user-id/1\"" {
t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) t.Errorf("validation error expected but was: %v\n", sut.Validate()[0])
@ -124,7 +124,7 @@ func TestPersonIdValidation(t *testing.T) {
sut.HostSchema = "https" sut.HostSchema = "https"
sut.Path = "path" sut.Path = "path"
sut.Host = "an.other.host" sut.Host = "an.other.host"
sut.HostPort = "0" sut.HostPort = 0
sut.UnvalidatedInput = "https://an.other.host/path/1" sut.UnvalidatedInput = "https://an.other.host/path/1"
_, err := validation.IsValid(sut) _, err := validation.IsValid(sut)
@ -138,7 +138,7 @@ func TestPersonIdValidation(t *testing.T) {
sut.HostSchema = "https" sut.HostSchema = "https"
sut.Path = "api/v1/activitypub/user-id" sut.Path = "api/v1/activitypub/user-id"
sut.Host = "an.other.host" sut.Host = "an.other.host"
sut.HostPort = "0" sut.HostPort = 0
sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1" sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1"
if sut.Validate()[0] != "Value forgejox is not contained in allowed values [forgejo gitea]" { if sut.Validate()[0] != "Value forgejox is not contained in allowed values [forgejo gitea]" {
t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) t.Errorf("validation error expected but was: %v\n", sut.Validate()[0])

View file

@ -10,10 +10,10 @@ import (
func (id ActorID) AsWellKnownNodeInfoURI() string { func (id ActorID) AsWellKnownNodeInfoURI() string {
wellKnownPath := ".well-known/nodeinfo" wellKnownPath := ".well-known/nodeinfo"
var result string var result string
if id.HostPort == "0" { if id.HostPort == 0 {
result = fmt.Sprintf("%s://%s/%s", id.HostSchema, id.Host, wellKnownPath) result = fmt.Sprintf("%s://%s/%s", id.HostSchema, id.Host, wellKnownPath)
} else { } else {
result = fmt.Sprintf("%s://%s:%s/%s", id.HostSchema, id.Host, id.HostPort, wellKnownPath) result = fmt.Sprintf("%s://%s:%d/%s", id.HostSchema, id.Host, id.HostPort, wellKnownPath)
} }
return result return result
} }

View file

@ -53,6 +53,10 @@ func ValidateNotEmpty(value any, name string) []string {
if v.IsZero() { if v.IsZero() {
isValid = false isValid = false
} }
case uint16:
if v == 0 {
isValid = false
}
case int64: case int64:
if v == 0 { if v == 0 {
isValid = false isValid = false