@@ -20,13 +20,14 @@ type HandlerFunc func(w http.ResponseWriter, r *http.Request, pathParams map[str
2020// It matches http requests to patterns and invokes the corresponding handler.
2121type ServeMux struct {
2222 // handlers maps HTTP method to a list of handlers.
23- handlers map [string ][]handler
24- forwardResponseOptions []func (context.Context , http.ResponseWriter , proto.Message ) error
25- marshalers marshalerRegistry
26- incomingHeaderMatcher HeaderMatcherFunc
27- outgoingHeaderMatcher HeaderMatcherFunc
28- metadataAnnotators []func (context.Context , * http.Request ) metadata.MD
29- protoErrorHandler ProtoErrorHandlerFunc
23+ handlers map [string ][]handler
24+ forwardResponseOptions []func (context.Context , http.ResponseWriter , proto.Message ) error
25+ marshalers marshalerRegistry
26+ incomingHeaderMatcher HeaderMatcherFunc
27+ outgoingHeaderMatcher HeaderMatcherFunc
28+ metadataAnnotators []func (context.Context , * http.Request ) metadata.MD
29+ protoErrorHandler ProtoErrorHandlerFunc
30+ disablePathLengthFallback bool
3031}
3132
3233// ServeMuxOption is an option that can be given to a ServeMux on construction.
@@ -102,6 +103,13 @@ func WithProtoErrorHandler(fn ProtoErrorHandlerFunc) ServeMuxOption {
102103 }
103104}
104105
106+ // WithDisablePathLengthFallback returns a ServeMuxOption for disable path length fallback.
107+ func WithDisablePathLengthFallback () ServeMuxOption {
108+ return func (serveMux * ServeMux ) {
109+ serveMux .disablePathLengthFallback = true
110+ }
111+ }
112+
105113// NewServeMux returns a new ServeMux whose internal mapping is empty.
106114func NewServeMux (opts ... ServeMuxOption ) * ServeMux {
107115 serveMux := & ServeMux {
@@ -177,7 +185,7 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
177185 components [l - 1 ], verb = c [:idx ], c [idx + 1 :]
178186 }
179187
180- if override := r .Header .Get ("X-HTTP-Method-Override" ); override != "" && isPathLengthFallback (r ) {
188+ if override := r .Header .Get ("X-HTTP-Method-Override" ); override != "" && s . isPathLengthFallback (r ) {
181189 r .Method = strings .ToUpper (override )
182190 if err := r .ParseForm (); err != nil {
183191 if s .protoErrorHandler != nil {
@@ -211,7 +219,7 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
211219 continue
212220 }
213221 // X-HTTP-Method-Override is optional. Always allow fallback to POST.
214- if isPathLengthFallback (r ) {
222+ if s . isPathLengthFallback (r ) {
215223 if err := r .ParseForm (); err != nil {
216224 if s .protoErrorHandler != nil {
217225 _ , outboundMarshaler := MarshalerForRequest (s , r )
@@ -250,8 +258,8 @@ func (s *ServeMux) GetForwardResponseOptions() []func(context.Context, http.Resp
250258 return s .forwardResponseOptions
251259}
252260
253- func isPathLengthFallback (r * http.Request ) bool {
254- return r .Method == "POST" && r .Header .Get ("Content-Type" ) == "application/x-www-form-urlencoded"
261+ func ( s * ServeMux ) isPathLengthFallback (r * http.Request ) bool {
262+ return ! s . disablePathLengthFallback && r .Method == "POST" && r .Header .Get ("Content-Type" ) == "application/x-www-form-urlencoded"
255263}
256264
257265type handler struct {
0 commit comments