github.com/blend/go-sdk@v1.20220411.3/webutil/is_valid_method.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package webutil
     9  
    10  import (
    11  	"strings"
    12  )
    13  
    14  // IsValidMethod returns if a http method is valid.
    15  /*
    16  	Method         = "OPTIONS"                ; Section 9.2
    17  					| "GET"                    ; Section 9.3
    18  					| "HEAD"                   ; Section 9.4
    19  					| "POST"                   ; Section 9.5
    20  					| "PUT"                    ; Section 9.6
    21  					| "DELETE"                 ; Section 9.7
    22  					| "TRACE"                  ; Section 9.8
    23  					| "CONNECT"                ; Section 9.9
    24  					| extension-method
    25  	extension-method = token
    26  		token          = 1*<any CHAR except CTLs or separators>
    27  */
    28  func IsValidMethod(method string) bool {
    29  	return len(method) > 0 && strings.IndexFunc(method, isNotMethodToken) == -1
    30  }
    31  
    32  func isNotMethodToken(r rune) bool {
    33  	return !IsHTTPTokenRune(r)
    34  }
    35  
    36  // IsHTTPTokenRune returns if a rune is in the http valid rune table.
    37  func IsHTTPTokenRune(r rune) bool {
    38  	i := int(r)
    39  	return i < len(isTokenTable) && isTokenTable[i]
    40  }
    41  
    42  var isTokenTable = [127]bool{
    43  	'!':  true,
    44  	'#':  true,
    45  	'$':  true,
    46  	'%':  true,
    47  	'&':  true,
    48  	'\'': true,
    49  	'*':  true,
    50  	'+':  true,
    51  	'-':  true,
    52  	'.':  true,
    53  	'0':  true,
    54  	'1':  true,
    55  	'2':  true,
    56  	'3':  true,
    57  	'4':  true,
    58  	'5':  true,
    59  	'6':  true,
    60  	'7':  true,
    61  	'8':  true,
    62  	'9':  true,
    63  	'A':  true,
    64  	'B':  true,
    65  	'C':  true,
    66  	'D':  true,
    67  	'E':  true,
    68  	'F':  true,
    69  	'G':  true,
    70  	'H':  true,
    71  	'I':  true,
    72  	'J':  true,
    73  	'K':  true,
    74  	'L':  true,
    75  	'M':  true,
    76  	'N':  true,
    77  	'O':  true,
    78  	'P':  true,
    79  	'Q':  true,
    80  	'R':  true,
    81  	'S':  true,
    82  	'T':  true,
    83  	'U':  true,
    84  	'W':  true,
    85  	'V':  true,
    86  	'X':  true,
    87  	'Y':  true,
    88  	'Z':  true,
    89  	'^':  true,
    90  	'_':  true,
    91  	'`':  true,
    92  	'a':  true,
    93  	'b':  true,
    94  	'c':  true,
    95  	'd':  true,
    96  	'e':  true,
    97  	'f':  true,
    98  	'g':  true,
    99  	'h':  true,
   100  	'i':  true,
   101  	'j':  true,
   102  	'k':  true,
   103  	'l':  true,
   104  	'm':  true,
   105  	'n':  true,
   106  	'o':  true,
   107  	'p':  true,
   108  	'q':  true,
   109  	'r':  true,
   110  	's':  true,
   111  	't':  true,
   112  	'u':  true,
   113  	'v':  true,
   114  	'w':  true,
   115  	'x':  true,
   116  	'y':  true,
   117  	'z':  true,
   118  	'|':  true,
   119  	'~':  true,
   120  }