libgo: update to Go 1.11
[official-gcc.git] / libgo / go / net / http / example_test.go
blob53fb0bbb4e104d920dad06d55b1e4c0ea72cd42f
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package http_test
7 import (
8 "context"
9 "fmt"
10 "io"
11 "io/ioutil"
12 "log"
13 "net/http"
14 "os"
15 "os/signal"
18 func ExampleHijacker() {
19 http.HandleFunc("/hijack", func(w http.ResponseWriter, r *http.Request) {
20 hj, ok := w.(http.Hijacker)
21 if !ok {
22 http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError)
23 return
25 conn, bufrw, err := hj.Hijack()
26 if err != nil {
27 http.Error(w, err.Error(), http.StatusInternalServerError)
28 return
30 // Don't forget to close the connection:
31 defer conn.Close()
32 bufrw.WriteString("Now we're speaking raw TCP. Say hi: ")
33 bufrw.Flush()
34 s, err := bufrw.ReadString('\n')
35 if err != nil {
36 log.Printf("error reading string: %v", err)
37 return
39 fmt.Fprintf(bufrw, "You said: %q\nBye.\n", s)
40 bufrw.Flush()
44 func ExampleGet() {
45 res, err := http.Get("http://www.google.com/robots.txt")
46 if err != nil {
47 log.Fatal(err)
49 robots, err := ioutil.ReadAll(res.Body)
50 res.Body.Close()
51 if err != nil {
52 log.Fatal(err)
54 fmt.Printf("%s", robots)
57 func ExampleFileServer() {
58 // Simple static webserver:
59 log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
62 func ExampleFileServer_stripPrefix() {
63 // To serve a directory on disk (/tmp) under an alternate URL
64 // path (/tmpfiles/), use StripPrefix to modify the request
65 // URL's path before the FileServer sees it:
66 http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
69 func ExampleStripPrefix() {
70 // To serve a directory on disk (/tmp) under an alternate URL
71 // path (/tmpfiles/), use StripPrefix to modify the request
72 // URL's path before the FileServer sees it:
73 http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
76 type apiHandler struct{}
78 func (apiHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}
80 func ExampleServeMux_Handle() {
81 mux := http.NewServeMux()
82 mux.Handle("/api/", apiHandler{})
83 mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
84 // The "/" pattern matches everything, so we need to check
85 // that we're at the root here.
86 if req.URL.Path != "/" {
87 http.NotFound(w, req)
88 return
90 fmt.Fprintf(w, "Welcome to the home page!")
94 // HTTP Trailers are a set of key/value pairs like headers that come
95 // after the HTTP response, instead of before.
96 func ExampleResponseWriter_trailers() {
97 mux := http.NewServeMux()
98 mux.HandleFunc("/sendstrailers", func(w http.ResponseWriter, req *http.Request) {
99 // Before any call to WriteHeader or Write, declare
100 // the trailers you will set during the HTTP
101 // response. These three headers are actually sent in
102 // the trailer.
103 w.Header().Set("Trailer", "AtEnd1, AtEnd2")
104 w.Header().Add("Trailer", "AtEnd3")
106 w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header
107 w.WriteHeader(http.StatusOK)
109 w.Header().Set("AtEnd1", "value 1")
110 io.WriteString(w, "This HTTP response has both headers before this text and trailers at the end.\n")
111 w.Header().Set("AtEnd2", "value 2")
112 w.Header().Set("AtEnd3", "value 3") // These will appear as trailers.
116 func ExampleServer_Shutdown() {
117 var srv http.Server
119 idleConnsClosed := make(chan struct{})
120 go func() {
121 sigint := make(chan os.Signal, 1)
122 signal.Notify(sigint, os.Interrupt)
123 <-sigint
125 // We received an interrupt signal, shut down.
126 if err := srv.Shutdown(context.Background()); err != nil {
127 // Error from closing listeners, or context timeout:
128 log.Printf("HTTP server Shutdown: %v", err)
130 close(idleConnsClosed)
133 if err := srv.ListenAndServe(); err != http.ErrServerClosed {
134 // Error starting or closing listener:
135 log.Printf("HTTP server ListenAndServe: %v", err)
138 <-idleConnsClosed
141 func ExampleListenAndServeTLS() {
142 http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
143 io.WriteString(w, "Hello, TLS!\n")
146 // One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem.
147 log.Printf("About to listen on 8443. Go to https://127.0.0.1:8443/")
148 err := http.ListenAndServeTLS(":8443", "cert.pem", "key.pem", nil)
149 log.Fatal(err)
152 func ExampleListenAndServe() {
153 // Hello world, the web server
155 helloHandler := func(w http.ResponseWriter, req *http.Request) {
156 io.WriteString(w, "Hello, world!\n")
159 http.HandleFunc("/hello", helloHandler)
160 log.Fatal(http.ListenAndServe(":8080", nil))