英文:
how to make an absolute path http redirect in golang
问题
我正在阅读源代码goto,并在goto/talk/0/main.go中找到了下面的代码:
http.Redirect(w, r, url, http.StatusFound)
根据上下文,url是一个绝对路径,并且期望进行绝对路径重定向。但是根据golang/http/redirect中的说明:
> Redirect将请求重定向到url,该url可以是相对于请求路径的路径。
这导致了一个相对路径重定向。我不知道在以前http.Redirect是否进行过绝对路径重定向,但现在不再支持。
那么如何在golang中进行绝对路径重定向呢?
我在互联网上搜索了一下,但没有找到任何信息,有人可以帮助我吗?
提前感谢。
英文:
I was reading the source code goto, and I found the code below in goto/talk/0/main.go:
http.Redirect(w, r, url, http.StatusFound)
According to the context, url was an absolute path, and an absolute path redirect was expected. But as the golang/http/redirect mentioned:
> Redirect replies to the request with a redirect to url, which may be a path relative to the request path.
It results as a relative path redirect. I don't know if http.Redirect did an absolute path redirect before, but it doesn't nowadays.
So how can I make an absolute path redirect in golang?
I searched the Internet, but found nothing,can anyone help me?
Thanks in advance.
答案1
得分: 10
当你访问golang文档中的http.Redirect时,你实际上可以点击蓝色的标题:
> func Redirect
它会带你进入源代码清单,其中有详细的说明:
// 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) {
	// ...
}
这个技巧也适用于其他函数。
英文:
When you go to golang documentation for http.Redirect, you can actually click the blue colored header:
> func Redirect
It will bring you to the source code listing which is self-explanatory:
// 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)
	}
}
This trick applies to other functions as well.
答案2
得分: 5
我最终发现,要执行绝对路径重定向,url必须是一个完整的URL,比如http://www.stackoverflow.com或https://github.com,而不是www.stackoverflow.com。
英文:
I finally found that to perform an absolute path redirect, the url must be a complete url, such as http://www.stackoverflow.com or https://github.com, but not www.stackoverflow.com.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论