File tree 2 files changed +43
-1
lines changed
2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -3491,6 +3491,44 @@ func TestOptions(t *testing.T) {
3491
3491
}
3492
3492
}
3493
3493
3494
+ func TestOptionsHandler (t * testing.T ) {
3495
+ rc := make (chan * Request , 1 )
3496
+
3497
+ ts := httptest .NewUnstartedServer (HandlerFunc (func (w ResponseWriter , r * Request ) {
3498
+ rc <- r
3499
+ }))
3500
+ ts .Config .DisableGeneralOptionsHandler = true
3501
+ ts .Start ()
3502
+ defer ts .Close ()
3503
+
3504
+ conn , err := net .Dial ("tcp" , ts .Listener .Addr ().String ())
3505
+ if err != nil {
3506
+ t .Fatal (err )
3507
+ }
3508
+ defer conn .Close ()
3509
+
3510
+ _ , err = conn .Write ([]byte ("OPTIONS * HTTP/1.1\r \n Host: foo.com\r \n \r \n " ))
3511
+ if err != nil {
3512
+ t .Fatal (err )
3513
+ }
3514
+ res , err := ReadResponse (bufio .NewReader (conn ), & Request {Method : "OPTIONS" })
3515
+ if err != nil {
3516
+ t .Fatal (err )
3517
+ }
3518
+ if res .StatusCode != 200 {
3519
+ t .Errorf ("Got non-200 response to OPTIONS *: %#v" , res )
3520
+ }
3521
+
3522
+ select {
3523
+ case got := <- rc :
3524
+ if got .Method != "OPTIONS" || got .RequestURI != "*" {
3525
+ t .Errorf ("Expected OPTIONS * request, got %v" , got )
3526
+ }
3527
+ case <- time .After (5 * time .Second ):
3528
+ t .Error ("timeout" )
3529
+ }
3530
+ }
3531
+
3494
3532
// Tests regarding the ordering of Write, WriteHeader, Header, and
3495
3533
// Flush calls. In Go 1.0, rw.WriteHeader immediately flushed the
3496
3534
// (*response).header to the wire. In Go 1.1, the actual wire flush is
Original file line number Diff line number Diff line change @@ -2596,6 +2596,10 @@ type Server struct {
2596
2596
2597
2597
Handler Handler // handler to invoke, http.DefaultServeMux if nil
2598
2598
2599
+ // DisableGeneralOptionsHandler, if true, passes "OPTIONS *" requests to the Handler,
2600
+ // otherwise responds with 200 OK and Content-Length: 0.
2601
+ DisableGeneralOptionsHandler bool
2602
+
2599
2603
// TLSConfig optionally provides a TLS configuration for use
2600
2604
// by ServeTLS and ListenAndServeTLS. Note that this value is
2601
2605
// cloned by ServeTLS and ListenAndServeTLS, so it's not
@@ -2922,7 +2926,7 @@ func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
2922
2926
if handler == nil {
2923
2927
handler = DefaultServeMux
2924
2928
}
2925
- if req .RequestURI == "*" && req .Method == "OPTIONS" {
2929
+ if ! sh . srv . DisableGeneralOptionsHandler && req .RequestURI == "*" && req .Method == "OPTIONS" {
2926
2930
handler = globalOptionsHandler {}
2927
2931
}
2928
2932
You can’t perform that action at this time.
0 commit comments