...

Source file src/github.com/linkerd/linkerd2/pkg/util/http.go

Documentation: github.com/linkerd/linkerd2/pkg/util

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	httpPb "github.com/linkerd/linkerd2-proxy-api/go/http_types"
     9  )
    10  
    11  // KB = Kilobyte
    12  const KB = 1024
    13  
    14  // MB = Megabyte
    15  const MB = KB * 1024
    16  
    17  // ParseScheme converts a scheme string to protobuf
    18  // TODO: validate scheme
    19  func ParseScheme(scheme string) *httpPb.Scheme {
    20  	value, ok := httpPb.Scheme_Registered_value[strings.ToUpper(scheme)]
    21  	if ok {
    22  		return &httpPb.Scheme{
    23  			Type: &httpPb.Scheme_Registered_{
    24  				Registered: httpPb.Scheme_Registered(value),
    25  			},
    26  		}
    27  	}
    28  	return &httpPb.Scheme{
    29  		Type: &httpPb.Scheme_Unregistered{
    30  			Unregistered: strings.ToUpper(scheme),
    31  		},
    32  	}
    33  }
    34  
    35  // ParseMethod converts a method string to protobuf
    36  // TODO: validate method
    37  func ParseMethod(method string) *httpPb.HttpMethod {
    38  	value, ok := httpPb.HttpMethod_Registered_value[strings.ToUpper(method)]
    39  	if ok {
    40  		return &httpPb.HttpMethod{
    41  			Type: &httpPb.HttpMethod_Registered_{
    42  				Registered: httpPb.HttpMethod_Registered(value),
    43  			},
    44  		}
    45  	}
    46  	return &httpPb.HttpMethod{
    47  		Type: &httpPb.HttpMethod_Unregistered{
    48  			Unregistered: strings.ToUpper(method),
    49  		},
    50  	}
    51  }
    52  
    53  // ReadAllLimit reads from r until EOF or until limit bytes are read. If EOF is
    54  // reached, the full bytes are returned. If the limit is reached, an error is
    55  // returned.
    56  func ReadAllLimit(r io.Reader, limit int) ([]byte, error) {
    57  	bytes, err := io.ReadAll(io.LimitReader(r, int64(limit)))
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	if len(bytes) == limit {
    62  		return nil, fmt.Errorf("limit reached while reading: %d", limit)
    63  	}
    64  	return bytes, nil
    65  }
    66  

View as plain text