Skip to content

Commit 925149d

Browse files
brackendawsongopherbot
authored andcommitted
net/http: add example for CrossOriginProtection
It's not immediately appaerent that a method must be used to wrap the handler, so add a basic example to guide users to the right API. Fixes golang#74121 Change-Id: I23fc3dff6fff9bf4eb29c099bc77da8c99620671 Reviewed-on: https://go-review.googlesource.com/c/go/+/681256 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sean Liao <[email protected]> Auto-Submit: Sean Liao <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent cf4af0b commit 925149d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/net/http/example_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"os"
1414
"os/signal"
15+
"time"
1516
)
1617

1718
func ExampleHijacker() {
@@ -221,3 +222,22 @@ func ExampleProtocols_http1or2() {
221222
}
222223
res.Body.Close()
223224
}
225+
226+
func ExampleCrossOriginProtection() {
227+
mux := http.NewServeMux()
228+
229+
mux.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
230+
io.WriteString(w, "request allowed\n")
231+
})
232+
233+
srv := http.Server{
234+
Addr: ":8080",
235+
ReadTimeout: 15 * time.Second,
236+
WriteTimeout: 15 * time.Second,
237+
// Use CrossOriginProtection.Handler to block all non-safe cross-origin
238+
// browser requests to mux.
239+
Handler: http.NewCrossOriginProtection().Handler(mux),
240+
}
241+
242+
log.Fatal(srv.ListenAndServe())
243+
}

0 commit comments

Comments
 (0)