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.
18 func ExampleHijacker() {
19 http
.HandleFunc("/hijack", func(w http
.ResponseWriter
, r
*http
.Request
) {
20 hj
, ok
:= w
.(http
.Hijacker
)
22 http
.Error(w
, "webserver doesn't support hijacking", http
.StatusInternalServerError
)
25 conn
, bufrw
, err
:= hj
.Hijack()
27 http
.Error(w
, err
.Error(), http
.StatusInternalServerError
)
30 // Don't forget to close the connection:
32 bufrw
.WriteString("Now we're speaking raw TCP. Say hi: ")
34 s
, err
:= bufrw
.ReadString('\n')
36 log
.Printf("error reading string: %v", err
)
39 fmt
.Fprintf(bufrw
, "You said: %q\nBye.\n", s
)
45 res
, err
:= http
.Get("http://www.google.com/robots.txt")
49 robots
, err
:= ioutil
.ReadAll(res
.Body
)
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
!= "/" {
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
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() {
119 idleConnsClosed
:= make(chan struct{})
121 sigint
:= make(chan os
.Signal
, 1)
122 signal
.Notify(sigint
, os
.Interrupt
)
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
)
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)
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))