本文整理汇总了Golang中net/url.URL.ResolveReference方法的典型用法代码示例。如果您正苦于以下问题:Golang URL.ResolveReference方法的具体用法?Golang URL.ResolveReference怎么用?Golang URL.ResolveReference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/url.URL
的用法示例。
在下文中一共展示了URL.ResolveReference方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetAuthorization
func (session *session) SetAuthorization(uri *url.URL, domain []string, auth string) {
session.Lock()
defer session.Unlock()
if domain == nil || len(domain) == 0 {
root := &url.URL{
Scheme: uri.Scheme,
Opaque: uri.Opaque,
User: uri.User,
Host: uri.Host,
Path: "/",
RawQuery: "",
Fragment: "",
}
session.authcache.Set(root, auth)
return
}
for _, s := range domain {
ref, err := url.Parse(s)
if err == nil {
session.authcache.Set(uri.ResolveReference(ref), auth)
}
}
}
示例2: HandleError
// HandleError tries to resolve the given error, which should be a
// response to the given URL, by discharging any macaroon contained in
// it. That is, if the error cause is an *Error and its code is
// ErrDischargeRequired, then it will try to discharge
// err.Info.Macaroon. If the discharge succeeds, the discharged macaroon
// will be saved to the client's cookie jar and ResolveError will return
// nil.
//
// For any other kind of error, the original error will be returned.
func (c *Client) HandleError(reqURL *url.URL, err error) error {
respErr, ok := errgo.Cause(err).(*Error)
if !ok {
return err
}
if respErr.Code != ErrDischargeRequired {
return respErr
}
if respErr.Info == nil || respErr.Info.Macaroon == nil {
return errgo.New("no macaroon found in discharge-required response")
}
mac := respErr.Info.Macaroon
macaroons, err := bakery.DischargeAllWithKey(mac, c.dischargeAcquirer().AcquireDischarge, c.Key)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
var cookiePath string
if path := respErr.Info.MacaroonPath; path != "" {
relURL, err := parseURLPath(path)
if err != nil {
logger.Warningf("ignoring invalid path in discharge-required response: %v", err)
} else {
cookiePath = reqURL.ResolveReference(relURL).Path
}
}
cookie, err := NewCookie(macaroons)
if err != nil {
return errgo.Notef(err, "cannot make cookie")
}
cookie.Path = cookiePath
c.Jar.SetCookies(reqURL, []*http.Cookie{cookie})
return nil
}
示例3: encodeURL
func encodeURL(src []byte, baseHref string, urlString string, start int, end int) []byte {
relURL := string(src[start:end])
// keep anchor and javascript links intact
if strings.Index(relURL, "#") == 0 || strings.Index(relURL, "javascript") == 0 {
return src
}
// Check if url is relative and make it absolute
if strings.Index(relURL, "http") != 0 {
var basePath *url.URL
if baseHref == "" {
basePath, _ = url.Parse(urlString)
} else {
basePath, _ = url.Parse(baseHref)
}
relPath, err := url.Parse(relURL)
if err != nil {
return src
}
absURL := basePath.ResolveReference(relPath).String()
src = bytes.Replace(src, []byte(relURL), []byte(absURL), -1)
end = start + len(absURL)
}
newURL, _ := EncryptURL(string(src[start:end]))
return bytes.Replace(src, src[start:end], []byte(newURL), -1)
}
示例4: pathToUri
//Accepts several paths separated by separator and constructs the URLs
//relative to base path
func pathToUri(paths string, separator string, basePath string) (urls []url.URL, err error) {
var urlBase *url.URL
if basePath != "" {
if string(basePath[0]) != "/" {
//for windows path to build a proper url
basePath = "/" + basePath
}
urlBase, err = url.Parse("file:" + basePath)
}
if err != nil {
return nil, err
}
inputs := strings.Split(paths, ",")
for _, input := range inputs {
var urlInput *url.URL
if basePath != "" {
urlInput, err = url.Parse(filepath.ToSlash(input))
if err != nil {
return nil, err
}
urlInput = urlBase.ResolveReference(urlInput)
} else {
//TODO is opaque really apropriate?
urlInput = &url.URL{
Opaque: filepath.ToSlash(input),
}
}
urls = append(urls, *urlInput)
}
//clean
return
}
示例5: getURL
func getURL(base *url.URL, urlStr string) (*url.URL, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
return base.ResolveReference(rel), nil
}
示例6: GetAsset
func (s *Scenario) GetAsset(w *Worker, base *url.URL, node xml.Node, attr string) error {
path, err := url.Parse(node.Attr(attr))
if err != nil {
return w.Fail(nil, err)
}
requestURI := base.ResolveReference(path)
req, res, err := w.SimpleGet(requestURI.String())
if err != nil {
return w.Fail(req, err)
}
if res.StatusCode != 200 {
return w.Fail(res.Request, fmt.Errorf("Response code should be %d, got %d", 200, res.StatusCode))
}
md5sum := calcMD5(res.Body)
defer res.Body.Close()
if expectedMD5, ok := s.ExpectedAssets[requestURI.RequestURI()]; ok {
if md5sum == expectedMD5 {
w.Success(StaticFileScore)
} else {
return w.Fail(res.Request, fmt.Errorf("Expected MD5 checksum is miss match %s, got %s", expectedMD5, md5sum))
}
}
return nil
}
示例7: getFileInfo
func getFileInfo(base *url.URL, reader io.Reader) (*result, error) {
h := sha1.New()
buf := make([]byte, 1024)
tee := io.TeeReader(reader, h)
res := result{}
config, format, err := image.DecodeConfig(tee)
if err != nil {
return nil, err
}
res.Width = config.Width
res.Height = config.Height
for ; err != io.EOF; _, err = tee.Read(buf) {
if err != nil {
return nil, err
}
}
hash := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
hashURL, _ := url.Parse(hash + "." + format)
res.URI = base.ResolveReference(hashURL).String()
res.UUID = uuid.NewV5(uuid.NamespaceURL, res.URI)
return &res, nil
}
示例8: newRequest
// newRequest is a helper function to generate an http.Request and automagically
// json encode a body and resolve the given path.
func newRequest(baseURL *url.URL, method, path string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(path)
if err != nil {
return nil, err
}
uri := baseURL.ResolveReference(rel)
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, uri.String(), buf)
if err != nil {
return nil, err
}
req.Header.Add(headerAccept, acceptType)
req.Header.Add(headerContentType, contentType)
return req, nil
}
示例9: cleanHref
func cleanHref(pageUrl *url.URL, href string) string {
if href == "" {
return ""
}
href = strings.TrimSpace(href)
parsedHref, err := url.Parse(href)
if err != nil {
return ""
}
// convert www subdomain to host without www,
// to avoid duplicate path uri
if strings.Contains(parsedHref.Host, "www") {
// host is www.example.com
s := strings.Split(parsedHref.Host, "www.")
// s is [ example.com]
parsedHref.Host = strings.TrimSpace(s[1])
// host is now example.com
}
// turn relative href uri into absolute,
// using pageUrl as base
if !parsedHref.IsAbs() {
parsedHref = pageUrl.ResolveReference(parsedHref)
}
// add / to end of href path for consistency
if parsedHref.Path != "" && parsedHref.Path != "/" {
lastChar := parsedHref.Path[len(parsedHref.Path)-1:]
if lastChar != "/" {
parsedHref.Path = parsedHref.Path + "/"
}
}
return parsedHref.String()
}
示例10: Fetch
func (f *Fetcher) Fetch(u *url.URL) (urls_obj []*url.URL, err error) {
html, err := f.get(u)
if err != nil {
return nil, err
}
urls, err := f.Picker.Pick(html)
if err != nil {
return nil, err
}
//update non host url
for _, v := range urls {
u_child, err := url.Parse(v)
//skip on invalid url
if err != nil {
continue
}
urls_obj = append(urls_obj, u.ResolveReference(u_child))
}
return urls_obj, nil
}
示例11: ToAbsolute
func ToAbsolute(base *url.URL, rawUrl string) string {
url, err := url.Parse(rawUrl)
if err != nil {
return rawUrl
}
return base.ResolveReference(url).String()
}
示例12: getAbsoluteURL
func getAbsoluteURL(pageURL, targetURL *url.URL) (*url.URL, error) {
t, err := url.Parse(targetURL.String())
if err != nil {
return nil, err
}
return pageURL.ResolveReference(t), nil
}
示例13: absUrlify
func (t *AbsURL) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (err error) {
var baseURL, inURL *url.URL
if baseURL, err = url.Parse(t.BaseURL); err != nil {
return
}
replace := func(in string) string {
if inURL, err = url.Parse(in); err != nil {
return in + "?"
}
if fragmentOnly(inURL) {
return in
}
return baseURL.ResolveReference(inURL).String()
}
for _, el := range selectors {
if err = tr.Apply(htmltran.TransformAttrib(el.attr, replace), el.tag); err != nil {
return
}
}
return
}
示例14: newRequest
// newRequest creates an API request. A relative URL can be provided in urlStr,
// in which case it is resolved relative to base URL.
// Relative URLs should always be specified without a preceding slash. If
// specified, the value pointed to by body is JSON encoded and included as the
// request body.
func (c *Client) newRequest(base *url.URL, method, urlStr string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
u := base.ResolveReference(rel)
bodyReader, ok := body.(io.Reader)
if !ok && body != nil {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
bodyReader = buf
}
req, err := http.NewRequest(method, u.String(), bodyReader)
if err != nil {
return nil, err
}
// req.Header.Add("Accept", mediaTypeV3)
if c.UserAgent != "" {
req.Header.Add("User-Agent", c.UserAgent)
}
return req, nil
}
示例15: resolveUrl
func resolveUrl(base *url.URL, href string) (*url.URL, error) {
u, err := url.Parse(href)
if err != nil {
return nil, err
}
return base.ResolveReference(u), nil
}