github.com/google/martian/v3@v3.3.3/header/framing_modifier.go (about)

     1  // Copyright 2015 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package header
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"strings"
    21  
    22  	"github.com/google/martian/v3"
    23  )
    24  
    25  // NewBadFramingModifier makes a best effort to fix inconsistencies in the
    26  // request such as multiple Content-Lengths or the lack of Content-Length and
    27  // improper Transfer-Encoding. If it is unable to determine a proper resolution
    28  // it returns an error.
    29  //
    30  // http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-3.3
    31  func NewBadFramingModifier() martian.RequestModifier {
    32  	return martian.RequestModifierFunc(
    33  		func(req *http.Request) error {
    34  			cls := req.Header["Content-Length"]
    35  			if len(cls) > 0 {
    36  				var length string
    37  
    38  				// Iterate over all Content-Length headers, splitting any we find with
    39  				// commas, and check that all Content-Lengths are equal.
    40  				for _, ls := range cls {
    41  					for _, l := range strings.Split(ls, ",") {
    42  						// First length, set it as the canonical Content-Length.
    43  						if length == "" {
    44  							length = strings.TrimSpace(l)
    45  							continue
    46  						}
    47  
    48  						// Mismatched Content-Lengths.
    49  						if length != strings.TrimSpace(l) {
    50  							return fmt.Errorf(`bad request framing: multiple mismatched "Content-Length" headers: %v`, cls)
    51  						}
    52  					}
    53  				}
    54  
    55  				// All Content-Lengths are equal, remove extras and set it to the
    56  				// canonical value.
    57  				req.Header.Set("Content-Length", length)
    58  			}
    59  
    60  			tes := req.Header["Transfer-Encoding"]
    61  			if len(tes) > 0 {
    62  				// Extract the last Transfer-Encoding value, and split on commas.
    63  				last := strings.Split(tes[len(tes)-1], ",")
    64  
    65  				// Check that the last, potentially comma-delimited, value is
    66  				// "chunked", else we have no way to determine when the request is
    67  				// finished.
    68  				if strings.TrimSpace(last[len(last)-1]) != "chunked" {
    69  					return fmt.Errorf(`bad request framing: "Transfer-Encoding" header is present, but does not end in "chunked"`)
    70  				}
    71  
    72  				// Transfer-Encoding "chunked" takes precedence over
    73  				// Content-Length.
    74  				req.Header.Del("Content-Length")
    75  			}
    76  
    77  			return nil
    78  		})
    79  }