本文整理汇总了Golang中net/url.URL.Host方法的典型用法代码示例。如果您正苦于以下问题:Golang URL.Host方法的具体用法?Golang URL.Host怎么用?Golang URL.Host使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/url.URL
的用法示例。
在下文中一共展示了URL.Host方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addTrailingSlash
func addTrailingSlash(u *url.URL) {
if l := len(u.Path); l > 0 && !strings.HasSuffix(u.Path, "/") {
u.Path += "/"
} else if l = len(u.Host); l > 0 && !strings.HasSuffix(u.Host, "/") {
u.Host += "/"
}
}
示例2: removeTrailingSlash
func removeTrailingSlash(u *url.URL) {
if l := len(u.Path); l > 0 && strings.HasSuffix(u.Path, "/") {
u.Path = u.Path[:l-1]
} else if l = len(u.Host); l > 0 && strings.HasSuffix(u.Host, "/") {
u.Host = u.Host[:l-1]
}
}
示例3: ParseConnectionString
// ParseConnectionString will parse a string to create a valid connection URL
func ParseConnectionString(path string, ssl bool) (url.URL, error) {
var host string
var port int
h, p, err := net.SplitHostPort(path)
if err != nil {
if path == "" {
host = DefaultHost
} else {
host = path
}
// If they didn't specify a port, always use the default port
port = DefaultPort
} else {
host = h
port, err = strconv.Atoi(p)
if err != nil {
return url.URL{}, fmt.Errorf("invalid port number %q: %s\n", path, err)
}
}
u := url.URL{
Scheme: "http",
}
if ssl {
u.Scheme = "https"
}
u.Host = net.JoinHostPort(host, strconv.Itoa(port))
return u, nil
}
示例4: moveBucketToHost
// moveBucketToHost moves the bucket name from the URI path to URL host.
func moveBucketToHost(u *url.URL, bucket string) {
u.Host = bucket + "." + u.Host
u.Path = strings.Replace(u.Path, "/{Bucket}", "", -1)
if u.Path == "" {
u.Path = "/"
}
}
示例5: parseURL
// parseURL parses the URL. The url.Parse function is not used here because
// url.Parse mangles the path.
func parseURL(s string) (*url.URL, error) {
// From the RFC:
//
// ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
// wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
//
// We don't use the net/url parser here because the dialer interface does
// not provide a way for applications to work around percent deocding in
// the net/url parser.
var u url.URL
switch {
case strings.HasPrefix(s, "ws://"):
u.Scheme = "ws"
s = s[len("ws://"):]
case strings.HasPrefix(s, "wss://"):
u.Scheme = "wss"
s = s[len("wss://"):]
default:
return nil, errMalformedURL
}
u.Host = s
u.Opaque = "/"
if i := strings.Index(s, "/"); i >= 0 {
u.Host = s[:i]
u.Opaque = s[i:]
}
return &u, nil
}
示例6: tcpDialer
func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
host, port, err := net.SplitHostPort(uri.Host)
if err != nil && strings.HasPrefix(err.Error(), "missing port") {
// addr is on the form "1.2.3.4"
uri.Host = net.JoinHostPort(uri.Host, "22000")
} else if err == nil && port == "" {
// addr is on the form "1.2.3.4:"
uri.Host = net.JoinHostPort(host, "22000")
}
raddr, err := net.ResolveTCPAddr("tcp", uri.Host)
if err != nil {
l.Debugln(err)
return nil, err
}
conn, err := dialer.Dial(raddr.Network(), raddr.String())
if err != nil {
l.Debugln(err)
return nil, err
}
tc := tls.Client(conn, tlsCfg)
err = tc.Handshake()
if err != nil {
tc.Close()
return nil, err
}
return tc, nil
}
示例7: NewSpawn
// create a spawn instance
func NewSpawn(sourcehost string, host string) (sp *spawn) {
sp = &spawn{}
sp.host = host
sp.sourcehost = sourcehost
// target host ( has fleetapi running on it )
u := url.URL{}
u.Scheme = "http"
u.Host = sp.host + ":" + port
u.Path = api
sp.api = u
// source host ( has astralboot + spawn running on it )
u2 := url.URL{}
u2.Scheme = "http"
u2.Host = sp.sourcehost + ":" + sourceport
u2.Path = sourceapi
sp.sourceapi = u2
// create the data maps
sp.unitText = make(map[string]string)
sp.units = make(map[string]*Unit)
return
}
示例8: connect
func (c *cli) connect(cmd string) {
var cl *client.Client
if cmd != "" {
// TODO parse out connection string
}
u := url.URL{
Scheme: "http",
}
if c.port > 0 {
u.Host = fmt.Sprintf("%s:%d", c.host, c.port)
} else {
u.Host = c.host
}
if c.username != "" {
u.User = url.UserPassword(c.username, c.password)
}
cl, err := client.NewClient(
client.Config{
URL: u,
Username: c.username,
Password: c.password,
})
if err != nil {
fmt.Printf("Could not create client %s", err)
return
}
c.client = cl
if _, v, e := c.client.Ping(); e != nil {
fmt.Printf("Failed to connect to %s\n", c.client.Addr())
} else {
c.version = v
fmt.Printf("Connected to %s version %s\n", c.client.Addr(), c.version)
}
}
示例9: URL
// URL constructs a URL appropriate for the token (i.e. for use in a
// QR code).
func (o OATH) URL(t Type, label string) string {
secret := base32.StdEncoding.EncodeToString(o.key)
u := url.URL{}
v := url.Values{}
u.Scheme = "otpauth"
switch t {
case OATH_HOTP:
u.Host = "hotp"
case OATH_TOTP:
u.Host = "totp"
}
u.Path = label
v.Add("secret", secret)
if o.Counter() != 0 && t == OATH_HOTP {
v.Add("counter", fmt.Sprintf("%d", o.Counter()))
}
if o.Size() != defaultSize {
v.Add("digits", fmt.Sprintf("%d", o.Size()))
}
switch {
case o.algo == crypto.SHA256:
v.Add("algorithm", "SHA256")
case o.algo == crypto.SHA512:
v.Add("algorithm", "SHA512")
}
if o.provider != "" {
v.Add("provider", o.provider)
}
u.RawQuery = v.Encode()
return u.String()
}
示例10: tcpDialer
func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
// Check that there is a port number in uri.Host, otherwise add one.
host, port, err := net.SplitHostPort(uri.Host)
if err != nil && strings.HasPrefix(err.Error(), "missing port") {
// addr is on the form "1.2.3.4"
uri.Host = net.JoinHostPort(uri.Host, "22000")
} else if err == nil && port == "" {
// addr is on the form "1.2.3.4:"
uri.Host = net.JoinHostPort(host, "22000")
}
// Don't try to resolve the address before dialing. The dialer may be a
// proxy, and we should let the proxy do the resolving in that case.
conn, err := dialer.Dial("tcp", uri.Host)
if err != nil {
l.Debugln(err)
return nil, err
}
tc := tls.Client(conn, tlsCfg)
err = tc.Handshake()
if err != nil {
tc.Close()
return nil, err
}
return tc, nil
}
示例11: parseURL
// parseURL parses the URL.
//
// This function is a replacement for the standard library url.Parse function.
// In Go 1.4 and earlier, url.Parse loses information from the path.
func parseURL(s string) (*url.URL, error) {
// From the RFC:
//
// ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
// wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
var u url.URL
switch {
case strings.HasPrefix(s, "ws://"):
u.Scheme = "ws"
s = s[len("ws://"):]
case strings.HasPrefix(s, "wss://"):
u.Scheme = "wss"
s = s[len("wss://"):]
default:
return nil, errMalformedURL
}
u.Host = s
u.Opaque = "/"
if i := strings.Index(s, "/"); i >= 0 {
u.Host = s[:i]
u.Opaque = s[i:]
}
if strings.Contains(u.Host, "@") {
// Don't bother parsing user information because user information is
// not allowed in websocket URIs.
return nil, errMalformedURL
}
return &u, nil
}
示例12: connect
func (c *CommandLine) connect(cmd string) {
var cl *client.Client
if cmd != "" {
// Remove the "connect" keyword if it exists
cmd = strings.TrimSpace(strings.Replace(cmd, "connect", "", -1))
if cmd == "" {
return
}
if strings.Contains(cmd, ":") {
h := strings.Split(cmd, ":")
if i, e := strconv.Atoi(h[1]); e != nil {
fmt.Printf("Connect error: Invalid port number %q: %s\n", cmd, e)
return
} else {
c.Port = i
}
if h[0] == "" {
c.Host = default_host
} else {
c.Host = h[0]
}
} else {
c.Host = cmd
// If they didn't specify a port, always use the default port
c.Port = default_port
}
}
u := url.URL{
Scheme: "http",
}
if c.Port > 0 {
u.Host = fmt.Sprintf("%s:%d", c.Host, c.Port)
} else {
u.Host = c.Host
}
if c.Username != "" {
u.User = url.UserPassword(c.Username, c.Password)
}
cl, err := client.NewClient(
client.Config{
URL: u,
Username: c.Username,
Password: c.Password,
UserAgent: "InfluxDBShell/" + version,
})
if err != nil {
fmt.Printf("Could not create client %s", err)
return
}
c.Client = cl
if _, v, e := c.Client.Ping(); e != nil {
fmt.Printf("Failed to connect to %s\n", c.Client.Addr())
} else {
c.Version = v
fmt.Printf("Connected to %s version %s\n", c.Client.Addr(), c.Version)
}
}
示例13: pathInHost
func pathInHost(u *url.URL) {
if u.Host == "" && u.Path != "" {
u.Host = u.Path
u.Path = ""
}
if u.Host[len(u.Host)-1] == ':' && u.Path != "" {
u.Host += u.Path
u.Path = ""
}
}
示例14: buildDeploymentReplicasURL
func (bc *Client) buildDeploymentReplicasURL(name string, replicas int) string {
u := new(url.URL)
u.Scheme = bc.Scheme
u.Host = net.JoinHostPort(bc.Host, strconv.Itoa(bc.Port))
if bc.Scheme == "https" && bc.Port == 443 {
u.Host = bc.Host
}
u.Path = path.Join("/deployments", name, "replicas", strconv.Itoa(replicas))
return u.String()
}
示例15: removeUnncessaryHostDots
func removeUnncessaryHostDots(u *url.URL) {
if len(u.Host) > 0 {
if matches := rxHostDots.FindStringSubmatch(u.Host); len(matches) > 1 {
// Trim the leading and trailing dots
u.Host = strings.Trim(matches[1], ".")
if len(matches) > 2 {
u.Host += matches[2]
}
}
}
}