// 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 = "/" } }
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] } }
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 += "/" } }
func (c *client) purge(host string, f gfind.File, k int, id int) { var Url *url.URL Url, err := url.Parse(host) chkErr(err) Url.Path += "/purge" Url.Path += f.Relpath var u string u = Url.String() if security != "" { u += "?" + ParseNgxSecurityLink(security, host, f) } req, err := http.NewRequest("HEAD", u, nil) req.Close = true if vhost != "client.com" { req.Host = vhost } resp, err := c.c.Do(req) chkErr(err) if resp.StatusCode == 200 { color.Printf("%6v-%-2d @{g}%-17v@{|} %v %v\n", k, id, "PURGE:SUCCESS", Url.String(), f.Size()) } else if resp.StatusCode == 404 { color.Printf("%6v-%-2d @{y}%-17v@{|} %v %v\n", k, id, "PURGE:NOFILE", Url.String(), f.Size()) } }
// 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 generateDownloadPlist(baseURL *url.URL, ipaPath string, plinfo *plistBundle) ([]byte, error) { dp := new(downloadPlist) item := new(plItem) baseURL.Path = ipaPath ipaUrl := baseURL.String() item.Assets = append(item.Assets, &plAsset{ Kind: "software-package", URL: ipaUrl, }) iconFiles := plinfo.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles if iconFiles != nil && len(iconFiles) > 0 { baseURL.Path = "/-/unzip/" + ipaPath + "/-/**/" + iconFiles[0] + ".png" imgUrl := baseURL.String() item.Assets = append(item.Assets, &plAsset{ Kind: "display-image", URL: imgUrl, }) } item.Metadata.Kind = "software" item.Metadata.BundleIdentifier = plinfo.CFBundleIdentifier item.Metadata.BundleVersion = plinfo.CFBundleVersion item.Metadata.Title = plinfo.CFBundleName if item.Metadata.Title == "" { item.Metadata.Title = filepath.Base(ipaUrl) } dp.Items = append(dp.Items, item) data, err := goplist.MarshalIndent(dp, goplist.XMLFormat, " ") return data, err }
func (req *PubnubRequest) Url(publishKey string, subscribeKey string, secretKey string) string { if req.url != "" { return req.url } var Url *url.URL if req.ssl { Url, _ = url.Parse("https://" + OriginHost) } else { Url, _ = url.Parse("http://" + OriginHost) } switch req.operation { case "time": Url.Path += "/" + req.operation + "/0" case "here_now": Url.Path += "/v2/presence/sub-key/" + subscribeKey + "/channel/" + req.Channel case "publish": messageBytes, err := json.Marshal(req.Message) if err != nil { panic(err) } Url.Path += "/" + req.operation + "/" + publishKey + "/" + subscribeKey + "/" + secretKey + "/" + req.Channel + "/0/" + string(messageBytes) case "subscribe": timetoken := "0" if req.Timetoken != "" { timetoken = req.Timetoken } Url.Path += "/" + req.operation + "/" + subscribeKey + "/" + req.Channel + "/0/" + timetoken + "?uuid=" + req.UUID } req.url = Url.String() return req.url }
func removeDotSegments(u *url.URL) { if len(u.Path) > 0 { var dotFree []string var lastIsDot bool sections := strings.Split(u.Path, "/") for _, s := range sections { if s == ".." { if len(dotFree) > 0 { dotFree = dotFree[:len(dotFree)-1] } } else if s != "." { dotFree = append(dotFree, s) } lastIsDot = (s == "." || s == "..") } // Special case if host does not end with / and new path does not begin with / u.Path = strings.Join(dotFree, "/") if !strings.HasSuffix(u.Host, "/") && !strings.HasPrefix(u.Path, "/") { u.Path = "/" + u.Path } // Special case if the last segment was a dot, make sure the path ends with a slash if lastIsDot && !strings.HasSuffix(u.Path, "/") { u.Path += "/" } } }
// 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 }
// CreateAndConnect connects and creates the requested DB (dropping // if exists) then returns a new connection to the created DB. func CreateAndConnect(pgURL url.URL, name string) (*gosql.DB, error) { { pgURL.Path = "" db, err := gosql.Open("postgres", pgURL.String()) if err != nil { return nil, err } defer db.Close() if _, err := db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name)); err != nil { return nil, err } if _, err := db.Exec(fmt.Sprintf(`CREATE DATABASE %s`, name)); err != nil { return nil, err } } pgURL.Path = name db, err := gosql.Open("postgres", pgURL.String()) if err != nil { return nil, err } return db, nil }
func v2AuthURL(ep url.URL, action string, name string) *url.URL { if name != "" { ep.Path = path.Join(ep.Path, defaultV2AuthPrefix, action, name) return &ep } ep.Path = path.Join(ep.Path, defaultV2AuthPrefix, action) return &ep }
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 = "" } }
/* setPath builds a JSON url.Path for a given resource type. */ func setPath(url *url.URL, resource string) { // ensure that path is "/" terminated before concatting resource if url.Path != "" && !strings.HasSuffix(url.Path, "/") { url.Path = url.Path + "/" } // don't pluralize resource automagically, JSON API spec is agnostic url.Path = fmt.Sprintf("%s%s", url.Path, resource) }
func cleanPath(u *url.URL) { hasSlash := strings.HasSuffix(u.Path, "/") // clean up path, removing duplicate `/` u.Path = path.Clean(u.Path) u.RawPath = path.Clean(u.RawPath) if hasSlash && !strings.HasSuffix(u.Path, "/") { u.Path += "/" u.RawPath += "/" } }
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 replaceRawPath(u *url.URL, rp string) { var p, q string fs := strings.Split(rp, "?") p = fs[0] if len(fs) > 1 { q = fs[1] } if p[0] == '/' { u.Path = p } else { u.Path += p } u.RawQuery = q }
// 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() }
func compatMatcher(u *url.URL) (r *semver.Repo, err error) { // Special case just for glfw.v3.1 -- we made a bad mistake here and this // is purely for backwards compatability so you can still write the (bad) // import: // // "azul3d.org/native/glfw.v3.1" // // All imports like this should be updated to just ".v3" instead. if u.Path == "/native/glfw.v3.1" { u.Path = "/native/glfw.v3" } // Special case just for .dev paths -- previously we suggested importing // in-development packages under the .dev extension (which violates semver // specification): // // "azul3d.org/tmx.dev" // // Now we redirect those to .v0 -- which has the *same effect*, but only a // different meaning under semver specification. In-development packages // today are imported under either .v0 (no stable release yet) or .vN-dev: // // "azul3d.org/tmx.v2-dev" // if strings.HasSuffix(u.Path, ".dev") { u.Path = strings.TrimSuffix(u.Path, ".dev") + ".v0" } // Legacy packages are in a seperate GitHub repository, let the legacy // matcher handle them. for _, pkg := range legacyPackages { if u.Path == pkg { return legacyMatcher.Match(u) } } // Special case for /website, we want: // // go get azul3d.org/website // // to simply download the latest (v0). if u.Path == "/website" { u.Path = "/website.v0" } // Now let the github matcher perform the matching. return githubMatcher.Match(u) }
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} }
// 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 }
func createRequest() *http.Request { r := new(http.Request) url := new(url.URL) url.Path = "/station/9530" r.URL = url return r }
// GetFile for Hg doesn't support updating at this time. It will download // the file every time. func (g *HgGetter) GetFile(dst string, u *url.URL) error { td, err := ioutil.TempDir("", "getter-hg") if err != nil { return err } if err := os.RemoveAll(td); err != nil { return err } // Get the filename, and strip the filename from the URL so we can // just get the repository directly. filename := filepath.Base(u.Path) u.Path = filepath.Dir(u.Path) // Get the full repository if err := g.Get(td, u); err != nil { return err } // Copy the single file u, err = urlhelper.Parse(fmtFileURL(filepath.Join(td, filename))) if err != nil { return err } fg := &FileGetter{Copy: true} return fg.GetFile(dst, u) }
func (cfg *OIDCConnectorConfig) Connector(ns url.URL, lf oidc.LoginFunc, tpls *template.Template) (Connector, error) { ns.Path = path.Join(ns.Path, httpPathCallback) ccfg := oidc.ClientConfig{ RedirectURL: ns.String(), Credentials: oidc.ClientCredentials{ ID: cfg.ClientID, Secret: cfg.ClientSecret, }, } cl, err := oidc.NewClient(ccfg) if err != nil { return nil, err } idpc := &OIDCConnector{ id: cfg.ID, issuerURL: cfg.IssuerURL, cbURL: ns, loginFunc: lf, client: cl, trustedEmailProvider: cfg.TrustedEmailProvider, emailClaim: cfg.EmailClaim, } return idpc, nil }
func getGameList(scheme, host string) (gameList []*Game, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("%v", r) } }() u := url.URL{Scheme: scheme, Host: host} u.Path = "games/" resp, err := client(*useTLS).Get(u.String()) if err != nil { return gameList, err } defer resp.Body.Close() contents := make([]byte, 131072) // should be enough, but will fail poorly if game list is longer n, err := resp.Body.Read(contents) if err != nil && err != io.EOF { return gameList, err } gameList = make([]*Game, 0) err = json.Unmarshal(contents[:n], &gameList) if err != nil { return make([]*Game, 0), err } return gameList, err }
func Detect_language(q string) string { var Url *url.URL Url, err := url.Parse("https://www.googleapis.com") if err != nil { fmt.Println(err) } Url.Path += "/language/translate/v2/detect" parameters := url.Values{} parameters.Add("q", q) parameters.Add("key", Google_key()) Url.RawQuery = parameters.Encode() resp, err := http.Get(Url.String()) if err != nil { fmt.Println(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) var data gtresponse json.Unmarshal(body, &data) lang := data.Data.Detections[0][0].Language return lang }
// Expand subsitutes any {encoded} strings in the URL passed in using // the map supplied. // // This calls SetOpaque to avoid encoding of the parameters in the URL path. func Expand(u *url.URL, expansions map[string]string) { escaped, unescaped, err := uritemplates.Expand(u.Path, expansions) if err == nil { u.Path = unescaped u.RawPath = escaped } }
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 = "" } } }
// Expand subsitutes any {encoded} strings in the URL passed in using // the map supplied. // // This calls SetOpaque to avoid encoding of the parameters in the URL path. func Expand(u *url.URL, expansions map[string]string) { expanded, err := uritemplates.Expand(u.Path, expansions) if err == nil { u.Path = expanded SetOpaque(u) } }
// ParseURL is wrapper around url.Parse, where Scheme defaults to "https" and Path defaults to "/sdk" func ParseURL(s string) (*url.URL, error) { var err error var u *url.URL if s != "" { // Default the scheme to https if !schemeMatch.MatchString(s) { s = "https://" + s } u, err = url.Parse(s) if err != nil { return nil, err } // Default the path to /sdk if u.Path == "" { u.Path = "/sdk" } if u.User == nil { u.User = url.UserPassword("", "") } } return u, nil }