code.gitea.io/gitea@v1.21.7/routers/utils/utils.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package utils
     5  
     6  import (
     7  	"html"
     8  	"net/url"
     9  	"strings"
    10  
    11  	"code.gitea.io/gitea/modules/setting"
    12  )
    13  
    14  // RemoveUsernameParameterSuffix returns the username parameter without the (fullname) suffix - leaving just the username
    15  func RemoveUsernameParameterSuffix(name string) string {
    16  	if index := strings.Index(name, " ("); index >= 0 {
    17  		name = name[:index]
    18  	}
    19  	return name
    20  }
    21  
    22  // SanitizeFlashErrorString will sanitize a flash error string
    23  func SanitizeFlashErrorString(x string) string {
    24  	return strings.ReplaceAll(html.EscapeString(x), "\n", "<br>")
    25  }
    26  
    27  // IsExternalURL checks if rawURL points to an external URL like http://example.com
    28  func IsExternalURL(rawURL string) bool {
    29  	parsed, err := url.Parse(rawURL)
    30  	if err != nil {
    31  		return true
    32  	}
    33  	appURL, _ := url.Parse(setting.AppURL)
    34  	if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(appURL.Host, "www.", "", 1) {
    35  		return true
    36  	}
    37  	return false
    38  }