mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-21 08:31:27 -05:00
Extract url string validation and parsing from ActorID parsing
This commit is contained in:
parent
4d3ab4dda2
commit
fa1acd1ebb
1 changed files with 19 additions and 15 deletions
|
@ -123,20 +123,24 @@ func removeEmptyStrings(ls []string) []string {
|
||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This parsing is very Person-Specific. We should adjust the name & move to a better location (maybe forgefed package?)
|
func ValidateAndParseIRI(unvalidatedIRI string) (url.URL, error) {
|
||||||
func ParseActorID(unvalidatedIRI, source string) (ActorID, error) {
|
err := validate_is_not_empty(unvalidatedIRI) // url.Parse seems to accept empty strings?
|
||||||
if unvalidatedIRI == "" {
|
|
||||||
return ActorID{}, fmt.Errorf("the given IRI was empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := url.Parse(unvalidatedIRI)
|
|
||||||
|
|
||||||
// check if userID IRI is well formed url
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ActorID{}, fmt.Errorf("the actor ID was not a valid IRI: %v", err)
|
return url.URL{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pathWithUserID := strings.Split(u.Path, "/")
|
validatedURL, err := url.Parse(unvalidatedIRI)
|
||||||
|
if err != nil {
|
||||||
|
return url.URL{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return *validatedURL, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: This parsing is very Person-Specific. We should adjust the name & move to a better location (maybe forgefed package?)
|
||||||
|
func ParseActorID(validatedURL url.URL, source string) ActorID {
|
||||||
|
|
||||||
|
pathWithUserID := strings.Split(validatedURL.Path, "/")
|
||||||
|
|
||||||
if containsEmptyString(pathWithUserID) {
|
if containsEmptyString(pathWithUserID) {
|
||||||
pathWithUserID = removeEmptyStrings(pathWithUserID)
|
pathWithUserID = removeEmptyStrings(pathWithUserID)
|
||||||
|
@ -153,9 +157,9 @@ func ParseActorID(unvalidatedIRI, source string) (ActorID, error) {
|
||||||
return ActorID{ // ToDo: maybe keep original input to validate against (maybe extra method)
|
return ActorID{ // ToDo: maybe keep original input to validate against (maybe extra method)
|
||||||
userId: userId,
|
userId: userId,
|
||||||
source: source,
|
source: source,
|
||||||
schema: u.Scheme,
|
schema: validatedURL.Scheme,
|
||||||
host: u.Hostname(), // u.Host returns hostname:port
|
host: validatedURL.Hostname(), // u.Host returns hostname:port
|
||||||
path: pathWithoutUserID,
|
path: pathWithoutUserID,
|
||||||
port: u.Port(),
|
port: validatedURL.Port(),
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue