forked from streadway/amqp
/
uri.go
145 lines (122 loc) · 3.25 KB
/
uri.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2012, Sean Treadway, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/streadway/amqp
package amqp
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
)
var errURIScheme = errors.New("AMQP scheme must be either 'amqp://' or 'amqps://'")
var schemePorts = map[string]int{
"amqp": 5672,
"amqps": 5671,
}
var defaultURI = URI{
Scheme: "amqp",
Host: "localhost",
Port: 5672,
Username: "guest",
Password: "guest",
Vhost: "/",
}
// URI represents a parsed AMQP URI string.
type URI struct {
Scheme string
Host string
Port int
Username string
Password string
Vhost string
}
// ParseURI attempts to parse the given AMQP URI according to the spec.
// See http://www.rabbitmq.com/uri-spec.html.
//
// Default values for the fields are:
//
// Scheme: amqp
// Host: localhost
// Port: 5672
// Username: guest
// Password: guest
// Vhost: /
//
func ParseURI(uri string) (URI, error) {
me := defaultURI
paten := `^amqp(s)?://[^:\/\/]+:[^:\/\/]+@[^:\/\/]+:[0-9]+\/[^:\/\/]*`
reg, _ := regexp.Compile(paten)
if !reg.Match([]byte(uri)) {
return defaultURI, errors.New("unvalid")
}
piceInfo := strings.Split(uri, ":")
scheme := piceInfo[0]
userName := piceInfo[1][2:]
passwordHostNameInfo := strings.Split(piceInfo[2], "@")
password := passwordHostNameInfo[0]
hostName := passwordHostNameInfo[1]
portVhostInfo := strings.Split(piceInfo[3], "/")
port, _ := strconv.Atoi(portVhostInfo[0])
var vhost string
if portVhostInfo[1] == "" {
vhost = "/"
} else {
vhost = portVhostInfo[1]
}
me.Scheme = scheme
me.Host = hostName
me.Port = port
me.Username = userName
me.Password = password
me.Vhost = vhost
return me, nil
}
// Splits host:port, host, [ho:st]:port, or [ho:st]. Unlike net.SplitHostPort
// which splits :port, host:port or [host]:port
//
// Handles hosts that have colons that are in brackets like [::1]:http
func splitHostPort(addr string) (host, port string) {
i := strings.LastIndex(addr, ":")
if i >= 0 {
host, port = addr[:i], addr[i+1:]
if len(port) > 0 && port[len(port)-1] == ']' && addr[0] == '[' {
// we've split on an inner colon, the port was missing outside of the
// brackets so use the full addr. We could assert that host should not
// contain any colons here
host, port = addr, ""
}
} else {
host = addr
}
return
}
// PlainAuth returns a PlainAuth structure based on the parsed URI's
// Username and Password fields.
func (uri URI) PlainAuth() *PlainAuth {
return &PlainAuth{
Username: uri.Username,
Password: uri.Password,
}
}
func (uri URI) String() string {
var authority string
if uri.Username != defaultURI.Username || uri.Password != defaultURI.Password {
authority += uri.Username
if uri.Password != defaultURI.Password {
authority += ":" + uri.Password
}
authority += "@"
}
authority += uri.Host
if defaultPort, found := schemePorts[uri.Scheme]; !found || defaultPort != uri.Port {
authority += ":" + strconv.FormatInt(int64(uri.Port), 10)
}
var vhost string
if uri.Vhost != defaultURI.Vhost {
vhost = uri.Vhost
}
return fmt.Sprintf("%s://%s/%s", uri.Scheme, authority, url.QueryEscape(vhost))
}