File: util.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (86 lines) | stat: -rw-r--r-- 2,063 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package util

import (
	"fmt"
	"net"
	"net/url"
)

const (
	hkpPort = "11371"
)

const (
	httpScheme  = "http"
	httpsScheme = "https"
	hkpScheme   = "hkp"
	hkpsScheme  = "hkps"
)

// NormalizeKeyserverURI is normalizing a URI string by converting
// protocol scheme hkp:// and hkps:// to their corresponding http://
// and https:// protocol scheme and returns the parsed URI with the
// corresponding scheme.
func NormalizeKeyserverURI(uri string) (*url.URL, error) {
	u, err := url.Parse(uri)
	if err != nil {
		return nil, err
	}

	switch u.Scheme {
	case httpScheme, httpsScheme:
	case hkpScheme:
		u.Scheme = httpScheme
		if u.Port() == "" {
			u.Host = net.JoinHostPort(u.Hostname(), hkpPort)
		}
	case hkpsScheme:
		u.Scheme = httpsScheme
	default:
		return nil, fmt.Errorf("unsupported keyserver protocol scheme %q", u.Scheme)
	}

	return u, nil
}

// SameKeyserver returns if two URIs point to the same keyserver or not, URI are
// also normalized meaning by example that hkp://localhost is equivalent to
// http://localhost:11371.
func SameKeyserver(u1, u2 string) bool {
	uri1, err := NormalizeKeyserverURI(u1)
	if err != nil {
		return false
	}
	uri2, err := NormalizeKeyserverURI(u2)
	if err != nil {
		return false
	}
	return Equal(uri1, uri2)
}

// SameURI returns if two URIs point to the same service or not.
func SameURI(u1, u2 string) bool {
	uri1, err := url.Parse(u1)
	if err != nil {
		return false
	}
	uri2, err := url.Parse(u2)
	if err != nil {
		return false
	}
	return Equal(uri1, uri2)
}

// Equal returns if both URLs have the same hostname, port and scheme or not.
func Equal(u1, u2 *url.URL) bool {
	if u1.Host == "" || u2.Host == "" {
		return false
	}
	return u1.Host == u2.Host && u1.Scheme == u2.Scheme
}