utils.go — fiber Source File
Architecture documentation for utils.go, a go file in the fiber codebase. 1 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 5f252b75_33b5_0425_0ab1_e1b45c8382fe["utils.go"] 5722dea6_90b8_258e_fce3_6afd06b2a64b["url"] 5f252b75_33b5_0425_0ab1_e1b45c8382fe --> 5722dea6_90b8_258e_fce3_6afd06b2a64b style 5f252b75_33b5_0425_0ab1_e1b45c8382fe fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
package cors
import (
"net/url"
"strings"
"github.com/gofiber/utils/v2"
)
// matchScheme compares the scheme of the domain and pattern
func matchScheme(domain, pattern string) bool {
dScheme, _, dFound := strings.Cut(domain, ":")
pScheme, _, pFound := strings.Cut(pattern, ":")
return dFound && pFound && dScheme == pScheme
}
// normalizeDomain removes the scheme and port from the input domain
func normalizeDomain(input string) string {
// Remove scheme
if after, found := strings.CutPrefix(input, "https://"); found {
input = after
} else if after, found := strings.CutPrefix(input, "http://"); found {
input = after
}
// Find and remove port, if present
if input != "" && input[0] != '[' {
if before, _, found := strings.Cut(input, ":"); found {
input = before
}
}
return input
}
// normalizeOrigin checks if the provided origin is in a correct format
// and normalizes it by removing any path or trailing slash.
// It returns a boolean indicating whether the origin is valid
// and the normalized origin.
func normalizeOrigin(origin string) (valid bool, normalized string) { //nolint:nonamedreturns // gocritic unnamedResult prefers naming validity and normalized origin results
parsedOrigin, err := url.Parse(origin)
if err != nil {
return false, ""
}
// Don't allow a wildcard with a protocol
// wildcards cannot be used within any other value. For example, the following header is not valid:
// Access-Control-Allow-Origin: https://*
if strings.IndexByte(parsedOrigin.Host, '*') >= 0 {
return false, ""
}
// Validate there is a host present. The presence of a path, query, or fragment components
// is checked, but a trailing "/" (indicative of the root) is allowed for the path and will be normalized
if parsedOrigin.User != nil ||
parsedOrigin.Host == "" ||
(parsedOrigin.Path != "" && parsedOrigin.Path != "/") ||
parsedOrigin.RawQuery != "" ||
parsedOrigin.Fragment != "" {
return false, ""
}
// Normalize the origin by constructing it from the scheme and host.
// The path or trailing slash is not included in the normalized origin.
return true, utils.ToLower(parsedOrigin.Scheme) + "://" + utils.ToLower(parsedOrigin.Host)
}
type subdomain struct {
// The wildcard pattern
prefix string
suffix string
}
func (s subdomain) match(o string) bool {
// Not a subdomain if not long enough for a dot separator.
if len(o) < len(s.prefix)+len(s.suffix)+1 {
return false
}
if !strings.HasPrefix(o, s.prefix) || !strings.HasSuffix(o, s.suffix) {
return false
}
// Check for the dot separator and validate that there is at least one
// non-empty label between prefix and suffix. Empty labels like
// "https://.example.com" or "https://..example.com" should not match.
suffixStartIndex := len(o) - len(s.suffix)
if suffixStartIndex <= len(s.prefix) {
return false
}
if o[suffixStartIndex-1] != '.' {
return false
}
// Extract the subdomain part (without the trailing dot) and ensure it
// doesn't contain empty labels.
sub := o[len(s.prefix) : suffixStartIndex-1]
if sub == "" || strings.HasPrefix(sub, ".") || strings.Contains(sub, "..") {
return false
}
return true
}
Domain
Subdomains
Dependencies
- url
Source
Frequently Asked Questions
What does utils.go do?
utils.go is a source file in the fiber codebase, written in go. It belongs to the FiberCore domain, Adapters subdomain.
What functions are defined in utils.go?
utils.go defines 3 function(s): matchScheme, normalizeDomain, normalizeOrigin.
What does utils.go depend on?
utils.go imports 1 module(s): url.
Where is utils.go in the architecture?
utils.go is located at middleware/cors/utils.go (domain: FiberCore, subdomain: Adapters, directory: middleware/cors).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free