| | |
| | |
| | |
| |
|
| | package http_test |
| |
|
| | import ( |
| | "context" |
| | "fmt" |
| | "io" |
| | "log" |
| | "net/http" |
| | "os" |
| | "os/signal" |
| | "time" |
| | ) |
| |
|
| | func ExampleHijacker() { |
| | http.HandleFunc("/hijack", func(w http.ResponseWriter, r *http.Request) { |
| | hj, ok := w.(http.Hijacker) |
| | if !ok { |
| | http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) |
| | return |
| | } |
| | conn, bufrw, err := hj.Hijack() |
| | if err != nil { |
| | http.Error(w, err.Error(), http.StatusInternalServerError) |
| | return |
| | } |
| | |
| | defer conn.Close() |
| | bufrw.WriteString("Now we're speaking raw TCP. Say hi: ") |
| | bufrw.Flush() |
| | s, err := bufrw.ReadString('\n') |
| | if err != nil { |
| | log.Printf("error reading string: %v", err) |
| | return |
| | } |
| | fmt.Fprintf(bufrw, "You said: %q\nBye.\n", s) |
| | bufrw.Flush() |
| | }) |
| | } |
| |
|
| | func ExampleGet() { |
| | res, err := http.Get("http://www.google.com/robots.txt") |
| | if err != nil { |
| | log.Fatal(err) |
| | } |
| | body, err := io.ReadAll(res.Body) |
| | res.Body.Close() |
| | if res.StatusCode > 299 { |
| | log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body) |
| | } |
| | if err != nil { |
| | log.Fatal(err) |
| | } |
| | fmt.Printf("%s", body) |
| | } |
| |
|
| | func ExampleFileServer() { |
| | |
| | log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc")))) |
| | } |
| |
|
| | func ExampleFileServer_stripPrefix() { |
| | |
| | |
| | |
| | http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))) |
| | } |
| |
|
| | func ExampleStripPrefix() { |
| | |
| | |
| | |
| | http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))) |
| | } |
| |
|
| | type apiHandler struct{} |
| |
|
| | func (apiHandler) ServeHTTP(http.ResponseWriter, *http.Request) {} |
| |
|
| | func ExampleServeMux_Handle() { |
| | mux := http.NewServeMux() |
| | mux.Handle("/api/", apiHandler{}) |
| | mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
| | |
| | |
| | if req.URL.Path != "/" { |
| | http.NotFound(w, req) |
| | return |
| | } |
| | fmt.Fprintf(w, "Welcome to the home page!") |
| | }) |
| | } |
| |
|
| | |
| | |
| | func ExampleResponseWriter_trailers() { |
| | mux := http.NewServeMux() |
| | mux.HandleFunc("/sendstrailers", func(w http.ResponseWriter, req *http.Request) { |
| | |
| | |
| | |
| | |
| | w.Header().Set("Trailer", "AtEnd1, AtEnd2") |
| | w.Header().Add("Trailer", "AtEnd3") |
| |
|
| | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| | w.WriteHeader(http.StatusOK) |
| |
|
| | w.Header().Set("AtEnd1", "value 1") |
| | io.WriteString(w, "This HTTP response has both headers before this text and trailers at the end.\n") |
| | w.Header().Set("AtEnd2", "value 2") |
| | w.Header().Set("AtEnd3", "value 3") |
| | }) |
| | } |
| |
|
| | func ExampleServer_Shutdown() { |
| | var srv http.Server |
| |
|
| | idleConnsClosed := make(chan struct{}) |
| | go func() { |
| | sigint := make(chan os.Signal, 1) |
| | signal.Notify(sigint, os.Interrupt) |
| | <-sigint |
| |
|
| | |
| | if err := srv.Shutdown(context.Background()); err != nil { |
| | |
| | log.Printf("HTTP server Shutdown: %v", err) |
| | } |
| | close(idleConnsClosed) |
| | }() |
| |
|
| | if err := srv.ListenAndServe(); err != http.ErrServerClosed { |
| | |
| | log.Fatalf("HTTP server ListenAndServe: %v", err) |
| | } |
| |
|
| | <-idleConnsClosed |
| | } |
| |
|
| | func ExampleListenAndServeTLS() { |
| | http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
| | io.WriteString(w, "Hello, TLS!\n") |
| | }) |
| |
|
| | |
| | log.Printf("About to listen on 8443. Go to https://127.0.0.1:8443/") |
| | err := http.ListenAndServeTLS(":8443", "cert.pem", "key.pem", nil) |
| | log.Fatal(err) |
| | } |
| |
|
| | func ExampleListenAndServe() { |
| | |
| |
|
| | helloHandler := func(w http.ResponseWriter, req *http.Request) { |
| | io.WriteString(w, "Hello, world!\n") |
| | } |
| |
|
| | http.HandleFunc("/hello", helloHandler) |
| | log.Fatal(http.ListenAndServe(":8080", nil)) |
| | } |
| |
|
| | func ExampleHandleFunc() { |
| | h1 := func(w http.ResponseWriter, _ *http.Request) { |
| | io.WriteString(w, "Hello from a HandleFunc #1!\n") |
| | } |
| | h2 := func(w http.ResponseWriter, _ *http.Request) { |
| | io.WriteString(w, "Hello from a HandleFunc #2!\n") |
| | } |
| |
|
| | http.HandleFunc("/", h1) |
| | http.HandleFunc("/endpoint", h2) |
| |
|
| | log.Fatal(http.ListenAndServe(":8080", nil)) |
| | } |
| |
|
| | func newPeopleHandler() http.Handler { |
| | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| | fmt.Fprintln(w, "This is the people handler.") |
| | }) |
| | } |
| |
|
| | func ExampleNotFoundHandler() { |
| | mux := http.NewServeMux() |
| |
|
| | |
| | mux.Handle("/resources", http.NotFoundHandler()) |
| |
|
| | |
| | mux.Handle("/resources/people/", newPeopleHandler()) |
| |
|
| | log.Fatal(http.ListenAndServe(":8080", mux)) |
| | } |
| |
|
| | func ExampleProtocols_http1() { |
| | srv := http.Server{ |
| | Addr: ":8443", |
| | } |
| |
|
| | |
| | srv.Protocols = new(http.Protocols) |
| | srv.Protocols.SetHTTP1(true) |
| |
|
| | log.Fatal(srv.ListenAndServeTLS("cert.pem", "key.pem")) |
| | } |
| |
|
| | func ExampleProtocols_http1or2() { |
| | t := http.DefaultTransport.(*http.Transport).Clone() |
| |
|
| | |
| | t.Protocols = new(http.Protocols) |
| | t.Protocols.SetHTTP1(true) |
| | t.Protocols.SetHTTP2(true) |
| |
|
| | cli := &http.Client{Transport: t} |
| | res, err := cli.Get("http://www.google.com/robots.txt") |
| | if err != nil { |
| | log.Fatal(err) |
| | } |
| | res.Body.Close() |
| | } |
| |
|
| | func ExampleCrossOriginProtection() { |
| | mux := http.NewServeMux() |
| |
|
| | mux.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { |
| | io.WriteString(w, "request allowed\n") |
| | }) |
| |
|
| | srv := http.Server{ |
| | Addr: ":8080", |
| | ReadTimeout: 15 * time.Second, |
| | WriteTimeout: 15 * time.Second, |
| | |
| | |
| | Handler: http.NewCrossOriginProtection().Handler(mux), |
| | } |
| |
|
| | log.Fatal(srv.ListenAndServe()) |
| | } |
| |
|