本文整理汇总了Golang中net/url.Parse函数的典型用法代码示例。如果您正苦于以下问题:Golang Parse函数的具体用法?Golang Parse怎么用?Golang Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetHook
// GetHook is a heper function that retrieves a hook by
// hostname. To do this, it will retrieve a list of all hooks
// and iterate through the list.
func GetHook(client *github.Client, owner, name, rawurl string) (*github.Hook, error) {
hooks, _, err := client.Repositories.ListHooks(owner, name, nil)
if err != nil {
return nil, err
}
newurl, err := url.Parse(rawurl)
if err != nil {
fmt.Println("error parsing new hook url", rawurl, err)
return nil, err
}
for _, hook := range hooks {
hookurl, ok := hook.Config["url"].(string)
if !ok {
continue
}
oldurl, err := url.Parse(hookurl)
if err != nil {
fmt.Println("error parsing old hook url", hookurl, err)
continue
}
if newurl.Host == oldurl.Host {
return &hook, nil
}
}
return nil, nil
}
示例2: TestEncodedSlashes
func TestEncodedSlashes(t *testing.T) {
var seen string
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
seen = r.RequestURI
}))
defer backend.Close()
b, _ := url.Parse(backend.URL)
proxyHandler := NewReverseProxy(b)
setProxyDirector(proxyHandler)
frontend := httptest.NewServer(proxyHandler)
defer frontend.Close()
f, _ := url.Parse(frontend.URL)
encodedPath := "/a%2Fb/?c=1"
getReq := &http.Request{URL: &url.URL{Scheme: "http", Host: f.Host, Opaque: encodedPath}}
_, err := http.DefaultClient.Do(getReq)
if err != nil {
t.Fatalf("err %s", err)
}
if seen != encodedPath {
t.Errorf("got bad request %q expected %q", seen, encodedPath)
}
}
示例3: parseSwarm
// TODO: This could use a unit test.
func parseSwarm(hostURL string, h *host.Host) (string, error) {
swarmOptions := h.HostOptions.SwarmOptions
if !swarmOptions.Master {
return "", fmt.Errorf("%q is not a swarm master. The --swarm flag is intended for use with swarm masters", h.Name)
}
u, err := url.Parse(swarmOptions.Host)
if err != nil {
return "", fmt.Errorf("There was an error parsing the url: %s", err)
}
parts := strings.Split(u.Host, ":")
swarmPort := parts[1]
// get IP of machine to replace in case swarm host is 0.0.0.0
mURL, err := url.Parse(hostURL)
if err != nil {
return "", fmt.Errorf("There was an error parsing the url: %s", err)
}
mParts := strings.Split(mURL.Host, ":")
machineIP := mParts[0]
hostURL = fmt.Sprintf("tcp://%s:%s", machineIP, swarmPort)
return hostURL, nil
}
示例4: newRequest
// newRequest is used to create a new request
func (c *Client) newRequest(method, path string) *request {
base, _ := url.Parse(c.config.Address)
u, _ := url.Parse(path)
r := &request{
config: &c.config,
method: method,
url: &url.URL{
Scheme: base.Scheme,
Host: base.Host,
Path: u.Path,
},
params: make(map[string][]string),
}
if c.config.Region != "" {
r.params.Set("region", c.config.Region)
}
if c.config.WaitTime != 0 {
r.params.Set("wait", durToMsec(r.config.WaitTime))
}
// Add in the query parameters, if any
for key, values := range u.Query() {
for _, value := range values {
r.params.Add(key, value)
}
}
return r
}
示例5: Construct
func (h *EssentialHeader) Construct(m map[string]interface{}) error {
r := emap.Hmap(m)
if alg, err := r.GetString("alg"); err == nil {
h.Algorithm = jwa.SignatureAlgorithm(alg)
}
if h.Algorithm == "" {
h.Algorithm = jwa.NoSignature
}
h.ContentType, _ = r.GetString("cty")
h.KeyID, _ = r.GetString("kid")
h.Type, _ = r.GetString("typ")
h.X509CertThumbprint, _ = r.GetString("x5t")
h.X509CertThumbprintS256, _ = r.GetString("x5t#256")
if v, err := r.GetStringSlice("crit"); err != nil {
h.Critical = v
}
if v, err := r.GetStringSlice("x5c"); err != nil {
h.X509CertChain = v
}
if v, err := r.GetString("jku"); err == nil {
u, err := url.Parse(v)
if err == nil {
h.JwkSetURL = u
}
}
if v, err := r.GetString("x5u"); err == nil {
u, err := url.Parse(v)
if err == nil {
h.X509Url = u
}
}
return nil
}
示例6: ProxyFromEnvironment
// ProxyFromEnvironment returns the URL of the proxy to use for a
// given request, as indicated by the environment variables
// HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions
// thereof). HTTPS_PROXY takes precedence over HTTP_PROXY for https
// requests.
//
// The environment values may be either a complete URL or a
// "host[:port]", in which case the "http" scheme is assumed.
// An error is returned if the value is a different form.
//
// A nil URL and nil error are returned if no proxy is defined in the
// environment, or a proxy should not be used for the given request,
// as defined by NO_PROXY.
//
// As a special case, if req.URL.Host is "localhost" (with or without
// a port number), then a nil URL and nil error will be returned.
func ProxyFromEnvironment(req *Request) (*url.URL, error) {
var proxy string
if req.URL.Scheme == "https" {
proxy = httpsProxyEnv.Get()
}
if proxy == "" {
proxy = httpProxyEnv.Get()
}
if proxy == "" {
return nil, nil
}
if !useProxy(canonicalAddr(req.URL)) {
return nil, nil
}
proxyURL, err := url.Parse(proxy)
if err != nil || !strings.HasPrefix(proxyURL.Scheme, "http") {
// proxy was bogus. Try prepending "http://" to it and
// see if that parses correctly. If not, we fall
// through and complain about the original one.
if proxyURL, err := url.Parse("http://" + proxy); err == nil {
return proxyURL, nil
}
}
if err != nil {
return nil, fmt.Errorf("invalid proxy address %q: %v", proxy, err)
}
return proxyURL, nil
}
示例7: fetchImage
// fetchImage will take an image as either a URL or a name string and import it
// into the store if found.
func fetchImage(img string, ds *cas.Store) (string, error) {
// discover if it isn't a URL
u, err := url.Parse(img)
if err == nil && u.Scheme == "" {
app, err := discovery.NewAppFromString(img)
if globalFlags.Debug && err != nil {
fmt.Printf("discovery: %s\n", err)
}
if err == nil {
ep, err := discovery.DiscoverEndpoints(*app, true)
if err != nil {
return "", err
}
// TODO(philips): use all available mirrors
if globalFlags.Debug {
fmt.Printf("fetch: trying %v\n", ep.ACI)
}
img = ep.ACI[0]
u, err = url.Parse(img)
}
}
if err != nil { // download if it isn't a URL
return "", fmt.Errorf("%s: not a valid URL or hash", img)
}
if u.Scheme != "http" && u.Scheme != "https" {
return "", fmt.Errorf("%s: rkt only supports http or https URLs", img)
}
return fetchURL(img, ds)
}
示例8: scriptProxyConfig
// scriptProxyConfig determines a proxy configuration for downloading
// scripts from a URL. For now, it uses environment variables passed in
// the strategy's environment. There is no preference given to either lowercase
// or uppercase form of the variable.
func scriptProxyConfig(build *api.Build) (*s2iapi.ProxyConfig, error) {
httpProxy := ""
httpsProxy := ""
for _, env := range build.Spec.Strategy.SourceStrategy.Env {
switch env.Name {
case "HTTP_PROXY", "http_proxy":
httpProxy = env.Value
case "HTTPS_PROXY", "https_proxy":
httpsProxy = env.Value
}
}
if len(httpProxy) == 0 && len(httpsProxy) == 0 {
return nil, nil
}
config := &s2iapi.ProxyConfig{}
if len(httpProxy) > 0 {
proxyURL, err := url.Parse(httpProxy)
if err != nil {
return nil, err
}
config.HTTPProxy = proxyURL
}
if len(httpsProxy) > 0 {
proxyURL, err := url.Parse(httpsProxy)
if err != nil {
return nil, err
}
config.HTTPSProxy = proxyURL
}
return config, nil
}
示例9: TestSwap
func (s *ExternalSuite) TestSwap(c *check.C) {
backend1 := "b1"
backend2 := "b2"
r, err := router.Get("fake")
c.Assert(err, check.IsNil)
r.AddBackend(backend1)
addr1, _ := url.Parse("http://127.0.0.1")
r.AddRoute(backend1, addr1)
r.AddBackend(backend2)
addr2, _ := url.Parse("http://10.10.10.10")
r.AddRoute(backend2, addr2)
err = router.Swap(r, backend1, backend2)
c.Assert(err, check.IsNil)
routes1, err := r.Routes(backend1)
c.Assert(err, check.IsNil)
c.Assert(routes1, check.DeepEquals, []*url.URL{addr1})
routes2, err := r.Routes(backend2)
c.Assert(err, check.IsNil)
c.Assert(routes2, check.DeepEquals, []*url.URL{addr2})
name1, err := router.Retrieve(backend1)
c.Assert(err, check.IsNil)
c.Assert(name1, check.Equals, backend2)
name2, err := router.Retrieve(backend2)
c.Assert(err, check.IsNil)
c.Assert(name2, check.Equals, backend1)
}
示例10: fileServeHandler
func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
filePath := path.Join(Config.filesDir, fileName)
err := checkFile(fileName)
if err == NotFoundErr {
notFoundHandler(c, w, r)
return
} else if err == BadMetadata {
oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
return
}
if !Config.allowHotlink {
referer := r.Header.Get("Referer")
u, _ := url.Parse(referer)
p, _ := url.Parse(Config.siteURL)
if referer != "" && !sameOrigin(u, p) {
w.WriteHeader(403)
return
}
}
w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
http.ServeFile(w, r, filePath)
}
示例11: TestSameOrigin
// Tests domains that should (or should not) return true for a
// same-origin check.
func TestSameOrigin(t *testing.T) {
var originTests = []struct {
o1 string
o2 string
expected bool
}{
{"https://goji.io/", "https://goji.io", true},
{"http://golang.org/", "http://golang.org/pkg/net/http", true},
{"https://goji.io/", "http://goji.io", false},
{"https://goji.io:3333/", "http://goji.io:4444", false},
}
for _, origins := range originTests {
a, err := url.Parse(origins.o1)
if err != nil {
t.Fatal(err)
}
b, err := url.Parse(origins.o2)
if err != nil {
t.Fatal(err)
}
if sameOrigin(a, b) != origins.expected {
t.Fatalf("origin checking failed: %v and %v, expected %v",
origins.o1, origins.o2, origins.expected)
}
}
}
示例12: ValidateUri
func ValidateUri(baseUri string, redirectUri string) bool {
if baseUri == "" || redirectUri == "" {
log.Print("urls cannot be blank.\n")
return false
}
base, err := url.Parse(baseUri)
if err != nil {
log.Printf("Error: %s\n", err)
return false
}
redirect, err := url.Parse(redirectUri)
if err != nil {
log.Printf("Error: %s\n", err)
return false
}
// must not have fragment
if base.Fragment != "" || redirect.Fragment != "" {
log.Print("Error: url must not include fragment.\n")
return false
}
if base.Scheme == redirect.Scheme && base.Host == redirect.Host && len(redirect.Path) >= len(base.Path) && strings.HasPrefix(redirect.Path, base.Path) {
return true
}
log.Printf("urls don't validate: %s / %s\n", baseUri, redirectUri)
return false
}
示例13: prepareHandler
func prepareHandler(w http.ResponseWriter, r *http.Request) {
if h := r.Header.Get("X-Forwarded-Host"); h != "" {
baseUrl, _ = url.Parse("http://" + h)
} else {
baseUrl, _ = url.Parse("http://" + r.Host)
}
}
示例14: init
func init() {
err := envconfig.Process("HELEN", &Constants)
if err != nil {
logrus.Fatal(err)
}
if Constants.SteamDevAPIKey == "" {
logrus.Warning("Steam api key not provided, setting SteamApiMockUp to true")
}
if Constants.PublicAddress == "" {
Constants.PublicAddress = "http://" + Constants.ListenAddress
}
if Constants.MockupAuth {
logrus.Warning("Mockup authentication enabled.")
}
_, err = url.Parse(Constants.PublicAddress)
if err != nil {
logrus.Fatal("Couldn't parse HELEN_PUBLIC_ADDR - ", err)
}
_, err = url.Parse(Constants.LoginRedirectPath)
if err != nil {
logrus.Fatal("Couldn't parse HELEN_SERVER_REDIRECT_PATH - ", err)
}
if Constants.GeoIP {
logrus.Info("GeoIP support enabled")
}
}
示例15: canRedirectWithConn
func (response HTTPResponse) canRedirectWithConn(conn *Conn) bool {
targetUrl, targetUrlError := url.Parse(conn.domain)
if targetUrlError != nil {
return false
}
redirectUrl, redirectUrlError := url.Parse(response.Headers["location"].(string))
if redirectUrlError != nil {
return false
}
remoteIpAddress, _, remoteIpAddressErr := net.SplitHostPort(conn.RemoteAddr().String())
if remoteIpAddressErr != nil {
return false
}
relativePath := redirectUrl.Host == ""
matchesHost := (strings.Contains(redirectUrl.Host, targetUrl.Host)) || (redirectUrl.Host == remoteIpAddress)
matchesProto := (conn.isTls && redirectUrl.Scheme == "https") || (!conn.isTls && redirectUrl.Scheme == "http")
// Either explicit keep-alive or HTTP 1.1, which uses persistent connections by default
var keepAlive bool
if response.Headers["Connection"] != nil {
keepAlive = strings.EqualFold(response.Headers["Connection"].(string), "keep-alive")
} else {
keepAlive = response.VersionMajor == 1 && response.VersionMinor == 1
}
return (relativePath && keepAlive) || (!relativePath && keepAlive && matchesHost && matchesProto)
}