Ejemplo n.º 1
1
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 += "/"
	}
}
Ejemplo n.º 2
1
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]
	}
}
Ejemplo n.º 3
1
// 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
}
// 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 = "/"
	}
}
Ejemplo n.º 5
0
// 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
}
Ejemplo n.º 6
0
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
}
Ejemplo n.º 7
0
// 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
}
Ejemplo n.º 8
0
Archivo: main.go Proyecto: pcn/influxdb
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)
	}
}
Ejemplo n.º 9
0
// 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()

}
Ejemplo n.º 10
0
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
}
Ejemplo n.º 11
0
// 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
}
Ejemplo n.º 12
0
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)
	}
}
Ejemplo n.º 13
0
Archivo: url.go Proyecto: fcavani/net
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 = ""
	}
}
Ejemplo n.º 14
0
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()
}
Ejemplo n.º 15
0
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]
			}
		}
	}
}
Ejemplo n.º 16
0
func defaultWikiBaseUrl(wikiaName, wikiaLang string) *url.URL {
	u := new(url.URL)
	baseHost := "wikia.com"
	u.Scheme = "http"
	if wikiaLang != "en" && wikiaLang != "" {
		u.Host = wikiaLang + "." + wikiaName + "." + baseHost
	} else {
		u.Host = wikiaName + "." + baseHost
	}
	nlog.Debugf("+v%", u)
	return u
}
Ejemplo n.º 17
0
func NewChimp(apiKey string, https bool) (*ChimpAPI, error) {
	u := url.URL{}
	if https {
		u.Scheme = "https"
	} else {
		u.Scheme = "http"
	}
	u.Host = mandrill_uri
	u.Path = mandrill_version
	u.Host = fmt.Sprintf("%s.api.mailchimp.com", mailchimp_datacenter.FindString(apiKey))
	u.Path = mailchimp_version
	return &ChimpAPI{apiKey, u.String() + "?method="}, nil
}
Ejemplo n.º 18
0
func fixURL(filename string, u *url.URL) bool {
	if u.Host != "" || u.Path == "" {
		return false
	}

	target := filepath.Join(filepath.Dir(filename), u.Path)
	if fi, err := os.Stat(target); os.IsNotExist(err) {
		// We're linking to something we didn't copy over. Send
		// it through the redirector.
		rel, err := filepath.Rel(*outputDir, target)
		if err != nil {
			return false
		}

		if fileExistsInBranch(rel) {
			u.Path = filepath.Join(*branch, rel)
			u.Host = "releases.k8s.io"
			u.Scheme = "https"
			return true
		}
	} else if fi.IsDir() {
		// If there's no README.md in the directory, redirect to github
		// for the directory view.
		files, err := filepath.Glob(target + "/*")
		if err != nil {
			return false
		}

		hasReadme := false
		for _, f := range files {
			if strings.ToLower(filepath.Base(f)) == "readme.md" {
				hasReadme = true
			}
		}
		if !hasReadme {
			rel, err := filepath.Rel(*outputDir, target)
			if err != nil {
				return false
			}
			u.Path = filepath.Join(*branch, rel)
			u.Host = "releases.k8s.io"
			u.Scheme = "https"
			return true
		}
	} else if strings.HasSuffix(u.Path, ".md") {
		u.Path = u.Path[:len(u.Path)-3] + ".html"
		return true
	}

	return false
}
Ejemplo n.º 19
0
// The string representation of a Node is a URL.
// Please see ParseNode for a description of the format.
func (n *Node) String() string {
	u := url.URL{Scheme: "enode"}
	if n.Incomplete() {
		u.Host = fmt.Sprintf("%x", n.ID[:])
	} else {
		addr := net.TCPAddr{IP: n.IP, Port: int(n.TCP)}
		u.User = url.User(fmt.Sprintf("%x", n.ID[:]))
		u.Host = addr.String()
		if n.UDP != n.TCP {
			u.RawQuery = "discport=" + strconv.Itoa(int(n.UDP))
		}
	}
	return u.String()
}
Ejemplo n.º 20
0
func (bc *Client) buildDeploymentURL(name string, params map[string]string) 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)
	q := u.Query()
	for k := range params {
		q.Set(k, params[k])
	}
	u.RawQuery = q.Encode()
	return u.String()
}
Ejemplo n.º 21
0
func (bc *Client) buildDeploymentReplicasURL(name string, replicas int, cluster string, force bool) string {
	u := new(url.URL)
	u.Scheme = bc.Scheme
	host := bc.Config.Clusters[cluster].IP
	port := bc.Config.Clusters[cluster].Port
	u.Host = net.JoinHostPort(host, strconv.Itoa(port))
	if bc.Scheme == "https" && port == 443 {
		u.Host = host
	}
	q := u.Query()
	q.Set("force", strconv.FormatBool(force))
	u.RawQuery = q.Encode()
	u.Path = path.Join("/deployments", url.QueryEscape(name), "replicas", strconv.Itoa(replicas))
	return u.String()
}
Ejemplo n.º 22
0
// Set the API key used for all Get and Push API calls.
func SetAPIKey(apiKey string) {
	pUrl := url.URL{}
	pUrl.Scheme = "https"
	pUrl.User = url.UserPassword(apiKey, "")
	pUrl.Host = "api.pushbullet.com"
	pUrl.Path = "/api/pushes"
	pushUrl = pUrl.String()

	gUrl := url.URL{}
	gUrl.Scheme = "https"
	gUrl.User = url.UserPassword(apiKey, "")
	gUrl.Host = "api.pushbullet.com"
	gUrl.Path = "/api/devices"
	getUrl = gUrl.String()
}
Ejemplo n.º 23
0
// see https://mandrillapp.com/api/docs/
// currently supporting json output formats
func NewMandrill(apiKey string) (*MandrillAPI, error) {
	u := url.URL{}
	u.Scheme = "https"
	u.Host = mandrill_uri
	u.Path = mandrill_version
	return &MandrillAPI{apiKey, u.String()}, nil
}
Ejemplo n.º 24
0
func newHTTPClient(u *url.URL, tlsConfig *tls.Config, timeout time.Duration) *http.Client {

	httpTransport := &http.Transport{
		TLSClientConfig: tlsConfig,
	}

	switch u.Scheme {
	default:
		httpTransport.Dial = func(proto, addr string) (net.Conn, error) {
			return net.DialTimeout(proto, addr, timeout)
		}
	case "unix":
		socketPath := u.Path
		unixDial := func(proto, addr string) (net.Conn, error) {
			ret, err := net.DialTimeout("unix", socketPath, timeout)
			return ret, err
		}
		httpTransport.Dial = unixDial
		// Override the main URL object so the HTTP lib won't complain
		u.Scheme = "http"
		u.Host = "unix.sock"
		u.Path = ""
	}
	return &http.Client{Transport: httpTransport}
}
Ejemplo n.º 25
0
// 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

	if strings.Contains(path, ":") {
		h := strings.Split(path, ":")
		i, e := strconv.Atoi(h[1])
		if e != nil {
			return url.URL{}, fmt.Errorf("invalid port number %q: %s\n", path, e)
		}
		port = i
		if h[0] == "" {
			host = DefaultHost
		} else {
			host = h[0]
		}
	} else {
		host = path
		// If they didn't specify a port, always use the default port
		port = DefaultPort
	}

	u := url.URL{
		Scheme: "http",
	}
	if ssl {
		u.Scheme = "https"
	}
	u.Host = net.JoinHostPort(host, strconv.Itoa(port))

	return u, nil
}
Ejemplo n.º 26
0
func (c *Conn) makeHTTPRequest(endpoint string, httpMethod string, userAgent string) (req *http.Request, encReq *HTTPRequest, err error) {
	if req, err = http.NewRequest(httpMethod, "", nil); err != nil {
		return
	}
	url := new(url.URL)
	var host string
	if len(c.domain) > 0 {
		host = c.domain
	} else {
		host, _, _ = net.SplitHostPort(c.RemoteAddr().String())
	}
	url.Host = host
	req.Host = host
	req.Method = httpMethod
	req.Proto = "HTTP/1.1"
	if c.isTls {
		url.Scheme = "https"
	} else {
		url.Scheme = "http"
	}
	url.Path = endpoint
	req.URL = url

	if len(userAgent) <= 0 {
		userAgent = "Mozilla/5.0 zgrab/0.x"
	}

	req.Header.Set("User-Agent", userAgent)
	encReq = new(HTTPRequest)
	encReq.Endpoint = endpoint
	encReq.Method = httpMethod
	encReq.UserAgent = userAgent
	return req, encReq, nil
}
Ejemplo n.º 27
0
func (o *OpenTsdbOutput) Init(config interface{}) (err error) {
	o.OpenTsdbOutputConfig = config.(*OpenTsdbOutputConfig)
	//todo: check address validity
	// if o.url, err = url.Parse(o.Address); err != nil {
	//     return fmt.Errorf("Can't parse URL '%s': %s", o.Address, err.Error())
	// }
	//
	o.client = &http.Client{
		Transport: &timeoutTransport{Transport: new(http.Transport)},
		Timeout:   time.Minute,
	}

	var u *url.URL
	if u, err = url.Parse(o.Url); err == nil {
	}

	o.logMsgChan = make(chan []byte, o.LogMsgChSize)

	u, err = u.Parse("/api/put")
	if err != nil {
		return err
	}
	if strings.HasPrefix(u.Host, ":") {
		u.Host = "localhost" + u.Host
	}
	o.Url = u.String()

	if err != nil {
		log.Printf("initialize OpenTsdbOutput failed, %s", err.Error())
		return err
	}
	return
}
Ejemplo n.º 28
0
func rpmURL(cmd RpmCmd, cs RpmControls) string {
	var u url.URL

	u.Host = cmd.Collector
	u.Path = "agent_listener/invoke_raw_method"

	if cs.UseTLS {
		u.Scheme = "https"
	} else {
		u.Scheme = "http"
	}

	query := url.Values{}
	query.Set("marshal_format", "json")
	query.Set("protocol_version", procotolVersion)
	query.Set("method", cmd.Name)
	query.Set("license_key", cs.License)

	if len(cmd.RunID) > 0 {
		query.Set("run_id", cmd.RunID)
	}

	u.RawQuery = query.Encode()
	return u.String()
}
Ejemplo n.º 29
0
Archivo: vk.go Proyecto: Alezhka/vk
// URL returns redirect url for application authentication
func (a Auth) URL() string {
	u := url.URL{}
	u.Host = oauthHost
	u.Scheme = oauthScheme
	u.Path = oauthPath

	if len(a.RedirectURI) == 0 {
		a.RedirectURI = oauthRedirectURI
	}
	if len(a.ResponseType) == 0 {
		a.ResponseType = oauthResponseType
	}
	if len(a.Display) == 0 {
		a.Display = oauthDisplay
	}

	values := u.Query()
	values.Add(paramResponseType, a.ResponseType)
	values.Add(paramScope, a.Scope.String())
	values.Add(paramAppID, int64s(a.ID))
	values.Add(paramRedirectURI, a.RedirectURI)
	values.Add(paramVersion, defaultVersion)
	values.Add(paramDisplay, a.Display)
	u.RawQuery = values.Encode()

	return u.String()
}
Ejemplo n.º 30
0
func fixHost(url *url.URL) {
	if parts := strings.SplitN(url.Path, "/", 2); len(parts) > 1 {
		if len(strings.Split(parts[0], ".")) >= 2 && parts[0] != "" && parts[1] != "" {
			url.Host = parts[0]
			url.Path = parts[1]
		}
	} else {
		dotParts := strings.Split(url.Path, ".")
		if len(dotParts) == 0 {
			return
		} else if len(dotParts) > 2 || suffixlooksLikeHost(url.Path) {
			url.Host = url.Path
			url.Path = ""
		}
	}
}