Exemple #1
0
func (s *S) SetUpSuite(c *gocheck.C) {
	go func() {
		http.Handle("/html.html", http.HandlerFunc(htmlHandler))
		http.Handle("/bin.bin", http.HandlerFunc(binHandler))
		http.Handle("/post", http.HandlerFunc(postHandler))
		http.Handle("/404.html", http.NotFoundHandler())
		http.Handle("/cookie.html", http.HandlerFunc(cookieHandler))
		http.Handle("/redirect/", http.HandlerFunc(redirectHandler))
		fmt.Printf("\nRunning test server on http://localhost:54123\n")
		if err := http.ListenAndServe(":54123", nil); err != nil {
			c.Fatalf("Cannot run test server on port: %s", err.String())
		}
	}()
	time.Sleep(2e8)
	c.Succeed()
}
Exemple #2
0
// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Clean path to canonical form and redirect.
	if p := cleanPath(r.URL.Path); p != r.URL.Path {
		w.Header().Set("Location", p)
		w.WriteHeader(http.StatusMovedPermanently)
		return
	}
	// Host-specific pattern takes precedence over generic ones
	h := mux.match(r.Host + r.URL.Path)
	if h == nil {
		h = mux.match(r.URL.Path)
	}
	if h == nil {
		h = http.NotFoundHandler()
	}
	h.ServeHTTP(w, r)
}
Exemple #3
0
// ServeHTTP dispatches the handler registered in the matched route.
//
// When there is a match, the route variables can be retrieved calling
// mux.Vars(request).
func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	// Clean path to canonical form and redirect.
	// (this comes from the http package)
	if p := cleanPath(request.URL.Path); p != request.URL.Path {
		writer.Header().Set("Location", p)
		writer.WriteHeader(http.StatusMovedPermanently)
		return
	}
	var handler http.Handler
	if match, ok := r.Match(request); ok {
		handler = match.Handler
	}
	if handler == nil {
		if r.NotFoundHandler == nil {
			r.NotFoundHandler = http.NotFoundHandler()
		}
		handler = r.NotFoundHandler
	}
	defer context.DefaultContext.Clear(request)
	handler.ServeHTTP(writer, request)
}