crystal-lee commented on issue #6916:
URL: https://github.com/apache/apisix/issues/6916#issuecomment-1133561098
This is the code。The request do not be forwarded to the upstream, but I must
set a upstream?
```
func (p CBFTransfer) Filter(conf interface{}, w http.ResponseWriter, r
pkgHTTP.Request) {
//跳转
Redirect(w, r, "http://www.baidu.com", http.StatusMovedPermanently)
}
```
```
func Redirect(w http.ResponseWriter, r pkgHTTP.Request, url string, code
int) {
log.Infof("redirect to %s", url)
if u, err := urlpkg.Parse(url); err == nil {
// If url was relative, make its path absolute by
// combining with request path.
// The client would probably do this for us,
// but doing it ourselves is more reliable.
// See RFC 7231, section 7.1.2
if u.Scheme == "" && u.Host == "" {
oldpath := gconv.String(r.Path())
if oldpath == "" { // should not happen, but avoid a
crash if it does
oldpath = "/"
}
// no leading http://server
if url == "" || url[0] != '/' {
// make relative path absolute
olddir, _ := path.Split(oldpath)
url = olddir + url
}
var query string
if i := strings.Index(url, "?"); i != -1 {
url, query = url[:i], url[i:]
}
// clean up but preserve trailing slash
trailing := strings.HasSuffix(url, "/")
url = path.Clean(url)
if trailing && !strings.HasSuffix(url, "/") {
url += "/"
}
url += query
}
}
h := w.Header()
// RFC 7231 notes that a short HTML body is usually included in
// the response because older user agents may not understand 301/307.
// Do it only if the request didn't already have a Content-Type header.
_, hadCT := h["Content-Type"]
h.Set("Location", hexEscapeNonASCII(url))
if !hadCT && (r.Method() == "GET" || r.Method() == "HEAD") {
h.Set("Content-Type", "text/html; charset=utf-8")
}
w.WriteHeader(code)
// Shouldn't send the body for POST or HEAD; that leaves GET.
if !hadCT && r.Method() == "GET" {
body := "<a href=\"" + htmlEscape(url) + "\">" +
http.StatusText(code) + "</a>.\n"
fmt.Fprintln(w, body)
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]