This is a creation in Article, where the information may have evolved or changed.
Keywords:handlerfunc, Redirecthandler, Redirect
Objective
In the Go-http-handlerfunc () function, the detailed description of the Handlerfunc, so this article in another way: from the outside to a layer of stripping, that is, the user level of the API or the use of the method, and then introduce the internal implementation details involved.
Redirecthandler Implementation Code
func Redirect(w ResponseWriter, r *Request, urlStr string, code int) { // Skipped now ...}// Redirect to a fixed URLtype redirectHandler struct { url string code int}func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) { Redirect(w, r, rh.url, rh.code)}// RedirectHandler returns a request handler that redirects// each request it receives to the given url using the given// status code.//// The provided code should be in the 3xx range and is usually// StatusMovedPermanently, StatusFound or StatusSeeOther.func RedirectHandler(url string, code int) Handler { return &redirectHandler{url, code}}
Customary method
The interface that the user can see is:
func RedirectHandler(url string, code int) Handler
Example
Todo to add
Type Redirecthandler
In the above source code, the definition of type Redirecthandler is given. --redirecthandler is also an implementation of the handler interface.
Redirect ()
Redirecthandler specific class uses the redirect () function, the source code is as follows:
Redirect replies to the request with a Redirect to url,//which may is a path relative to the request path.////the Pro vided code should be in the 3xx range and is usually//statusmovedpermanently, statusfound or Statusseeother.func Redirect (w Responsewriter, R *request, urlstr string, code int) {if u, err: = URL. Parse (URLSTR); Err = = Nil {//If URL is relative, make absolute by//combining with request path. The browser would probably does this for us,//But doing it ourselves are more reliable. Note (RSC): RFC 2616 says, the location//line must is an absolute URI, like//"http://www.google.co m/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 U s again, so we can ' t//know the right absolute URIs to send back. Because of this problem, no one pays attention//to theRFC; They all send back just a new path. So do we. if U.scheme = = "" && u.host = = "" {oldpath: = R.url. Path if OldPath = = "" {//should not happen, but avoid a crash if it does OldPath = "/" }//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)//RFC 2616 recommends that a short note "should" being included in the//response because older user agent s 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)}}var Htmlreplacer = strings. Newreplacer ("&", "&", "<", "<", ">", ">",//"& #34;" is shorter than "&am P;quot; ". ' "'," & #34; ",//" & #39; "is shorter than" ' "and apos be not in HTML until HTML5. "'", "& #39;",) func Htmlescape (s string) string {return Htmlreplacer.replace (s)}
To be explained in detail