File tree 2 files changed +38
-1
lines changed
2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -3434,6 +3434,39 @@ func TestOptions(t *testing.T) {
3434
3434
}
3435
3435
}
3436
3436
3437
+ func TestOptionsHandler (t * testing.T ) {
3438
+ rc := make (chan * Request , 1 )
3439
+
3440
+ ts := httptest .NewUnstartedServer (HandlerFunc (func (w ResponseWriter , r * Request ) {
3441
+ rc <- r
3442
+ }))
3443
+ ts .Config .DisableOptionsHandler = true
3444
+ ts .Start ()
3445
+ defer ts .Close ()
3446
+
3447
+ conn , err := net .Dial ("tcp" , ts .Listener .Addr ().String ())
3448
+ if err != nil {
3449
+ t .Fatal (err )
3450
+ }
3451
+ defer conn .Close ()
3452
+
3453
+ _ , err = conn .Write ([]byte ("OPTIONS * HTTP/1.1\r \n Host: foo.com\r \n \r \n " ))
3454
+ if err != nil {
3455
+ t .Fatal (err )
3456
+ }
3457
+ res , err := ReadResponse (bufio .NewReader (conn ), & Request {Method : "OPTIONS" })
3458
+ if err != nil {
3459
+ t .Fatal (err )
3460
+ }
3461
+ if res .StatusCode != 200 {
3462
+ t .Errorf ("Got non-200 response to OPTIONS *: %#v" , res )
3463
+ }
3464
+
3465
+ if got := <- rc ; got .Method != "OPTIONS" || got .RequestURI != "*" {
3466
+ t .Errorf ("Expected OPTIONS * request, got %v" , got )
3467
+ }
3468
+ }
3469
+
3437
3470
// Tests regarding the ordering of Write, WriteHeader, Header, and
3438
3471
// Flush calls. In Go 1.0, rw.WriteHeader immediately flushed the
3439
3472
// (*response).header to the wire. In Go 1.1, the actual wire flush is
Original file line number Diff line number Diff line change @@ -2570,6 +2570,10 @@ type Server struct {
2570
2570
2571
2571
Handler Handler // handler to invoke, http.DefaultServeMux if nil
2572
2572
2573
+ // DisableOptionsHandler, if true, passes "OPTIONS *" requests to the Handler,
2574
+ // otherwise responds with 200 OK and Content-Length: 0.
2575
+ DisableOptionsHandler bool
2576
+
2573
2577
// TLSConfig optionally provides a TLS configuration for use
2574
2578
// by ServeTLS and ListenAndServeTLS. Note that this value is
2575
2579
// cloned by ServeTLS and ListenAndServeTLS, so it's not
@@ -2896,7 +2900,7 @@ func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
2896
2900
if handler == nil {
2897
2901
handler = DefaultServeMux
2898
2902
}
2899
- if req .RequestURI == "*" && req .Method == "OPTIONS" {
2903
+ if ! sh . srv . DisableOptionsHandler && req .RequestURI == "*" && req .Method == "OPTIONS" {
2900
2904
handler = globalOptionsHandler {}
2901
2905
}
2902
2906
You can’t perform that action at this time.
0 commit comments