PNG  IHDR  8] PLTE S =tRNS   PNG  IHDR  8] PLTE S =tRNS   REDROOM
PHP 7.4.33
Preview: example_test.go Size: 5.38 KB
/proc/thread-self/root/opt/golang/1.22.0/src/net/http/example_test.go

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package http_test

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"os/signal"
)

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
		}
		// Don't forget to close the connection:
		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() {
	// Simple static webserver:
	log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}

func ExampleFileServer_stripPrefix() {
	// To serve a directory on disk (/tmp) under an alternate URL
	// path (/tmpfiles/), use StripPrefix to modify the request
	// URL's path before the FileServer sees it:
	http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
}

func ExampleStripPrefix() {
	// To serve a directory on disk (/tmp) under an alternate URL
	// path (/tmpfiles/), use StripPrefix to modify the request
	// URL's path before the FileServer sees it:
	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) {
		// The "/" pattern matches everything, so we need to check
		// that we're at the root here.
		if req.URL.Path != "/" {
			http.NotFound(w, req)
			return
		}
		fmt.Fprintf(w, "Welcome to the home page!")
	})
}

// HTTP Trailers are a set of key/value pairs like headers that come
// after the HTTP response, instead of before.
func ExampleResponseWriter_trailers() {
	mux := http.NewServeMux()
	mux.HandleFunc("/sendstrailers", func(w http.ResponseWriter, req *http.Request) {
		// Before any call to WriteHeader or Write, declare
		// the trailers you will set during the HTTP
		// response. These three headers are actually sent in
		// the trailer.
		w.Header().Set("Trailer", "AtEnd1, AtEnd2")
		w.Header().Add("Trailer", "AtEnd3")

		w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header
		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") // These will appear as trailers.
	})
}

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

		// We received an interrupt signal, shut down.
		if err := srv.Shutdown(context.Background()); err != nil {
			// Error from closing listeners, or context timeout:
			log.Printf("HTTP server Shutdown: %v", err)
		}
		close(idleConnsClosed)
	}()

	if err := srv.ListenAndServe(); err != http.ErrServerClosed {
		// Error starting or closing listener:
		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")
	})

	// One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem.
	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() {
	// Hello world, the web server

	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()

	// Create sample handler to returns 404
	mux.Handle("/resources", http.NotFoundHandler())

	// Create sample handler that returns 200
	mux.Handle("/resources/people/", newPeopleHandler())

	log.Fatal(http.ListenAndServe(":8080", mux))
}

Directory Contents

Dirs: 9 × Files: 64

Name Size Perms Modified Actions
cgi DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
cookiejar DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
fcgi DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
httptest DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
httptrace DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
httputil DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
internal DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
pprof DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
testdata DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
3.01 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
33.40 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
46.07 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
63.24 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.56 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
11.53 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
19.26 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.42 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.04 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
560 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.38 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
8.43 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.52 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.62 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
30.36 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
46.70 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
357.28 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
812 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.04 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.90 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.05 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.13 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.22 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
900 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
4.94 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.68 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.95 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
517 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.91 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
15.17 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
14.50 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.16 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.38 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.70 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
48.34 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
23.30 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
42.39 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
11.10 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
4.17 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.81 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.89 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
23.63 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
566 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
11.80 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.95 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
4.02 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.48 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.89 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.60 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
120.67 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.95 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
192.54 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.90 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.56 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
12.90 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.45 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
30.89 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.13 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
87.81 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
362 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
364 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.05 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
186.06 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.22 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).
 !"#$%&'(()*+,-./00123456789 t\ wIDATx ]ys  47Y ƒ -  "  Rv  < f{Ɛ $k l L > L  ~h^ 1  [  r G t& h  l F z3O Y ! p A(_g̷ E8 )S 8 c  Kb"z ~ 5 J xAL WU <  *  5 m;W a pB h ~P J 2 3 6 ҙ .Ƹ P i  4g F R L P ΪK/D  M v (a3 k J Œ4N5* SH ` SdJ z  O J Xՠ V>u ߱ BE&L b2 ?2` tX+  c CB A$ i b C ĀMB E : /  # Dx &l =q Ty  0 \p I ( L Ǎ { e 4k ;`u^ヲ eP!( d {  )T A 8 O;Ě n >;s6 !  :Nx `[S D HU ~ q›J F} a g*D 49 / pn k h (t 8NxƐF _!r չ7 ZR R׷ q/5") Ӎ NY 0 x sZ!   o  fu  ,  K"$ ? pg  㕣=  1» {h " fh7    y  } € +7  $ y " X —ą - G P u 4 m >J 5 L =V ' ^@I p ?MS xЌ XV P ! h "C NS9B8̢ ]!K  e   zA , ӏkbY  !< XQ ٿyS| *" f { w  4@[S <  # 0 ! js [m  =,~ o "ݎ DHf Wo $ g ! Vԅ t mB /y Wf V4񺍸 c+@x?  B ~u " xUN e 0 BĂ) ~J pz! 7y6]l Ԥ@ P a< O /DHC `≻  N m"$  0ObB }{ x AO FCG D R ^ "B  { WDH  UR l@ T #  +"d T ; 0 i  D}. 7 ` ' ] w rE &S i ƕiTD EL P _ u h $ Ա FG wVD G L R Zf ' .!] J /ZR oGЍs Mr Ĥ ʬ 3 Q [3 cL ` ^ p + ( F;# B 5 '  2Y f [  ϶R0e }  E 7 6M aۮ H <& n % L] E}Up x紉, Uw' Q  Ǯշo k ވۙ 0N94 VX5 xEDE l D #֤ } C o )W :  ^ s 9  bRf iX5u ཱི 4 :[  T 1. | [E 2ؽ Iy\ : o x K G 5 ylP ' uK E ftb/i[3 .g _  [3M n G, #NwQ5~  ؚ) | n =Ц"x qg gB ` 듘 ~ x w ? ? R~  _ u. &VQ K˻   H C ( TN˄+ `C dA nB׭ D 3"Z G ê ^k H_ /- ~ " R_  .8 Z_ 6@ o  xg  uP ? 3լ @7AM!  E7^ - =V L  x  g-D0  CtmW 7  O  G _ WD0 g C  w1 r d w : a | \  *" f nֳ ^ H# f L ` Z ۽hV  }S F r0Ù Bć5r] @! NL iQ]{s^=4 d  WD  "  "   M; t" 8 5 e dL| "-*st" ) SWD ?R[S e ooF  20.D ? bo =) A i o d ģ ZҰaO @E =) i D a &ܟa CϞ y6 ,<%{^x%{f8? `iw^ ?/ M * IEND B`