// NewMPSClient constructs a MPSClient from the provided baseURL and // TLS config. // If TLS config is nil, the default configuration is used. func NewMPSClient(baseURL string, tlsConfig *tls.Config) (*MPSClient, error) { u, err := url.Parse(baseURL) if err != nil { return nil, fmt.Errorf("could not parse url: %v", err) } t := &http.Transport{TLSClientConfig: tlsConfig} client := &http.Client{Transport: t} if (u.Scheme != "http") && (u.Scheme != "https") { return nil, fmt.Errorf("base url must have scheme 'http' or 'https'") } if u.Host == "" { return nil, fmt.Errorf("no host in url") } var wsURL url.URL wsURL = *u wsURL.Scheme = "ws" if u.Scheme == "https" { wsURL.Scheme = "wss" } httpProxy := httputil.NewSingleHostReverseProxy(u) wsProxy := wsutil.NewSingleHostReverseProxy(&wsURL) httpProxy.Transport = t wsProxy.TLSClientConfig = t.TLSClientConfig return &MPSClient{baseURL, client, httpProxy, wsProxy, t}, nil }
// 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 }
// 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 }
// LTMVirtualServerNameList show local traffic manager specific virtual server func LTMVirtualServerNameList(c *gin.Context) { lbpair := c.Params.ByName("lbpair") vservername := c.Params.ByName("virtual") f5url, err := ltm.Loadbalancer(lbpair, conf.Ltmdevicenames) if err != nil { respondWithStatus(err.Status, vservername, nil, err.Message, conf.Documentation["ltmvirtualdocumentationuri"], c) return } res, virtualservernamelist, err := ltm.ShowLTMVirtualServerName(f5url, vservername) if err != nil { respondWithStatus(err.Status, vservername, nil, err.Message, conf.Documentation["ltmvirtualdocumentationuri"], c) return } json.Unmarshal([]byte(res.Body), &returnerror) u1 := new(url.URL) u1.Scheme = common.Protocol u1.Path = path.Join(c.Request.Host, c.Request.RequestURI, common.ProfilesURI) u2 := new(url.URL) u2.Scheme = common.Protocol u2.Path = path.Join(c.Request.Host, c.Request.RequestURI, common.FwURI) virtualservernamelist.ProfilesReference = u1.String() virtualservernamelist.FwRulesReference = u2.String() if len(virtualservernamelist.Pool) > 0 { u := new(url.URL) u.Scheme = common.Protocol u.Path = path.Join(c.Request.Host, "/api/ltms/", lbpair, common.PoolsURI, util.ReplaceCommon(virtualservernamelist.Pool)) virtualservernamelist.PoolsReference = u.String() } respondWithStatus(res.Status, "", virtualservernamelist, returnerror.ErrorMessage(), conf.Documentation["ltmvirtualdocumentationuri"], c) }
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 }
// 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 }
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() }
// 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 }
func normalizeSchemeFromURL(target *url.URL, source *url.URL) { if target.Scheme == "" { if source != nil && source.Scheme != "" && strings.HasPrefix(target.Path, "/") { target.Scheme = source.Scheme } else if target.Host != "" { target.Scheme = "http" } } }
// Combine a URL and Request to make the URL absolute func makeURLAbs(url *url.URL, request *http.Request) { if !url.IsAbs() { url.Host = request.Host if request.TLS != nil || request.Header.Get("X-Forwarded-Proto") == "https" { url.Scheme = "https" } else { url.Scheme = "http" } } }
// Combine a URL and Request to make the URL absolute func makeURLAbs(url *url.URL, request *http.Request) { if !url.IsAbs() { url.Host = request.Host if strings.HasPrefix(request.Proto, "HTTP/") { url.Scheme = "http" } else { url.Scheme = "https" } } }
// Validate that the LTI request was signed with the provided consumer secret. // // TODO Implement nonce checking and better timestamp checking. func (tp *LTIToolProvider) ValidateRequest(consumerSecret string, checkTimestamp, checkNonce bool) (bool, error) { var err error defer func() { if err != nil { tp.LTIResponse.LTIErrorMessage = errInvalidRequest.Error() tp.LTIResponse.LTIErrorLog = err.Error() } }() // First check OAuth Signature req := tp.ltiConsumerRequest // Create fully qualified URL var requestUrl *url.URL if tp.requestProxyPath != "" { req.URL.Path = fmt.Sprintf("%s%s", tp.requestProxyPath, req.URL.Path) } if !req.URL.IsAbs() { requestUrl = req.URL if tp.requestProxyScheme != "" { requestUrl.Scheme = tp.requestProxyScheme } else if req.TLS == nil { requestUrl.Scheme = "http" } else { requestUrl.Scheme = "https" } requestUrl.Host = req.Host } else { requestUrl = req.URL } reqStr := hmacsha1.RequestSignatureBaseString(req.Method, requestUrl.String(), req.Form) if !hmacsha1.CheckMAC(reqStr, consumerSecret, "", tp.LTIHeaders.OAuthSignature) { err = errLogInvalidSignature return false, err } // Second verify that timestamp is withing acceptable range if checkTimestamp { tstamp := tp.LTIHeaders.OAuthTimestamp if !acceptTimestamp(tstamp) { err = errLogInvalidTimestamp return false, err } } // Third, make sure unique nonce if checkNonce { // TODO: Nonce verification } return true, nil }
func replaceSchemeWithWS(u *url.URL) error { switch u.Scheme { case "http": u.Scheme = "ws" case "https": u.Scheme = "wss" default: return fmt.Errorf("invalid scheme in url: %s", u.Scheme) } return nil }
func New(apikey string, https ...bool) (*API, error) { u := url.URL{} if https[0] { u.Scheme = "https" } else { u.Scheme = "http" } u.Host = fmt.Sprintf("%s.api.mailchimp.com", datacenter.FindString(apikey)) u.Path = "/1.3/" return &API{apikey, u.String() + "?method="}, nil }
func (e *endpoint) translateURL(r *http.Request) string { newURL := url.URL{} newURL.Host = e.OriginServers[0].Host newURL.Path = r.URL.Path newURL.Scheme = "http" if e.OriginServers[0].HTTPS { newURL.Scheme = "https" } return newURL.String() }
func NewChimp(apiKey string, https bool) *ChimpAPI { u := url.URL{} if https { u.Scheme = "https" } else { u.Scheme = "http" } u.Host = fmt.Sprintf("%s.api.mailchimp.com", mailchimp_datacenter.FindString(apiKey)) u.Path = mailchimp_version return &ChimpAPI{Key: apiKey, endpoint: u.String()} }
// Fixes the missing data from the url and substitutes the data from the // BaseUrl func (w *Walker) fixMissingURLData(u *url.URL) { // Check if host is defined if u.Host == "" { // If there isn't a host, then it's a relative link to the base domain u.Host = w.RootNode.URL.Host u.Scheme = w.RootNode.URL.Scheme } if u.Scheme == "" { u.Scheme = w.RootNode.URL.Scheme } }
func responseURL(r *http.Request, paths ...string) *url.URL { responseURL := url.URL{} if r.TLS == nil { responseURL.Scheme = "http" } else { responseURL.Scheme = "https" } responseURL.Host = r.Host responseURL.Path = fmt.Sprintf("/%s", strings.Join(paths, "/")) return &responseURL }
func fileUri(bucket string, key string) *url.URL { uri := new(url.URL) uri.Host = hostname if secure { uri.Scheme = "https" } else { uri.Scheme = "http" } uri.Path = fmt.Sprintf("%s/%s", bucket, key) return uri }
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 }
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 }
func (fm FeedManager) discoverSecureParserFeeds(u *url.URL) (feeds []content.Feed, err error) { if u.Scheme == "http" { fm.logger.Debugln("Testing secure link of", u) u.Scheme = "https" feeds, err = fm.discoverParserFeeds(u.String()) u.Scheme = "http" } if u.Scheme != "http" || err != nil { feeds, err = fm.discoverParserFeeds(u.String()) } return }
// 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() }
// confirmURL generates a url.URL for a confirmation link that the user will // get via and must click to confirm a request. func (h *Handler) confirmURL(action string, listName string, email string, key string) url.URL { u := url.URL{} u.Scheme = "http" u.Host = h.cfg.Subscribegun.Listen u.Path = path.Join("/", action, listName, "confirm", email, key) return u }
// NewRemoteContext returns a context that gives access to the production // APIs for the application at the given host. All communication will be // performed over SSL unless the host is localhost. func NewRemoteContext(host string, client *http.Client) (appengine.Context, error) { // Add an appcfg header to outgoing requests. t := client.Transport if t == nil { t = http.DefaultTransport } client.Transport = &headerAddingRoundTripper{t} url := url.URL{ Scheme: "https", Host: host, Path: "/_ah/remote_api", } if host == "localhost" || strings.HasPrefix(host, "localhost:") { url.Scheme = "http" } u := url.String() appID, err := getAppID(client, u) if err != nil { return nil, fmt.Errorf("unable to contact server: %v", err) } return &context{ client: client, url: u, appID: appID, }, nil }
// 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 }
// 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() }
// 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 }
// probe calls the supplied vcs function to probe a variety of url constructions. // If vcs returns non nil, it is assumed that the url is not a valid repo. func probe(vcs func(*url.URL) error, url *url.URL, insecure bool, schemes ...string) (string, error) { var unsuccessful []string for _, scheme := range schemes { // make copy of url and apply scheme url := *url url.Scheme = scheme switch url.Scheme { case "https", "ssh": if err := vcs(&url); err == nil { return url.String(), nil } case "http", "git": if !insecure { log.Printf("skipping insecure protocol: %s", url.String()) continue } if err := vcs(&url); err == nil { return url.String(), nil } default: return "", fmt.Errorf("unsupported scheme: %v", url.Scheme) } unsuccessful = append(unsuccessful, url.String()) } return "", fmt.Errorf("vcs probe failed, tried: %s", strings.Join(unsuccessful, ",")) }
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} }