如何在golang中创建绝对路径http重定向

car*_*000 4 go

我正在阅读源代码goto,我在goto/talk/0/main.go中找到了以下代码:

http.Redirect(w, r, url, http.StatusFound)
Run Code Online (Sandbox Code Playgroud)

根据上下文,url是一个绝对路径,并且预期绝对路径重定向.但正如golang/http/redirect所提到的那样:

Redirect 使用重定向到url回复请求,该URL可能是相对于请求路径的路径.

它导致相对路径重定向.我不知道之前是否http.Redirect进行过绝对路径重定向,但现在还没有.

那么如何在golang中进行绝对路径重定向呢?我搜索了互联网,但没有发现任何人,有人可以帮助我吗?提前致谢.

Afr*_*ief 8

当你去golang文档时http.Redirect,你可以实际点击蓝色标题:

func 重定向

它将带您进入源代码清单,这是不言自明的:

// Redirect replies to the request with a redirect to url,
// which may be a path relative to the request path.
func Redirect(w ResponseWriter, r *Request, urlStr string, code int) {
    if u, err := url.Parse(urlStr); err == nil {
        // If url was relative, make absolute by
        // combining with request path.
        // The browser would probably do this for us,
        // but doing it ourselves is more reliable.

        // NOTE(rsc): RFC 2616 says that the Location
        // line must be an absolute URI, like
        // "http://www.google.com/redirect/",
        // not a path like "/redirect/".
        // Unfortunately, we don't know what to
        // put in the host name section to get the
        // client to connect to us again, so we can't
        // know the right absolute URI to send back.
        // Because of this problem, no one pays attention
        // to the RFC; they all send back just a new path.
        // So do we.
        oldpath := r.URL.Path
        if oldpath == "" { // should not happen, but avoid a crash if it does
            oldpath = "/"
        }
        if u.Scheme == "" {
            // no leading http://server
            if urlStr == "" || urlStr[0] != '/' {
                // make relative path absolute
                olddir, _ := path.Split(oldpath)
                urlStr = olddir + urlStr
            }

            var query string
            if i := strings.Index(urlStr, "?"); i != -1 {
                urlStr, query = urlStr[:i], urlStr[i:]
            }

            // clean up but preserve trailing slash
            trailing := strings.HasSuffix(urlStr, "/")
            urlStr = path.Clean(urlStr)
            if trailing && !strings.HasSuffix(urlStr, "/") {
                urlStr += "/"
            }
            urlStr += query
        }
    }

    w.Header().Set("Location", urlStr)
    w.WriteHeader(code)

    // RFC2616 recommends that a short note "SHOULD" be included in the
    // response because older user agents may not understand 301/307.
    // Shouldn't send the response for POST or HEAD; that leaves GET.
    if r.Method == "GET" {
        note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] +
                "</a>.\n"
        fmt.Fprintln(w, note)
    }
}
Run Code Online (Sandbox Code Playgroud)

这个技巧也适用于其他功能.


car*_*000 5

我最终发现要执行绝对路径重定向,必须url是完整的url,例如http://www.stackoverflow.comor https://github.com,但不是www.stackoverflow.com

  • @orian [link]http://golang.org/pkg/net/http/#Redirect[link],这里是 http.Redirect 的文档,但它神经提到前缀“http://”将执行绝对路径重定向。 (3认同)