github.com/cloudwego/hertz@v0.9.3/internal/bytesconv/bytesconv_table_gen.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  /*
     5   * Copyright 2022 CloudWeGo Authors
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   *
    19   * The MIT License (MIT)
    20   *
    21   * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors
    22   *
    23   * Permission is hereby granted, free of charge, to any person obtaining a copy
    24   * of this software and associated documentation files (the "Software"), to deal
    25   * in the Software without restriction, including without limitation the rights
    26   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    27   * copies of the Software, and to permit persons to whom the Software is
    28   * furnished to do so, subject to the following conditions:
    29   *
    30   * The above copyright notice and this permission notice shall be included in
    31   * all copies or substantial portions of the Software.
    32   *
    33   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    34   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    35   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    36   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    37   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    38   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    39   * THE SOFTWARE.
    40   *
    41   * This file may have been modified by CloudWeGo authors. All CloudWeGo
    42   * Modifications are Copyright 2022 CloudWeGo Authors.
    43   */
    44  
    45  package main
    46  
    47  import (
    48  	"bytes"
    49  	"fmt"
    50  	"io/ioutil"
    51  	"log"
    52  )
    53  
    54  const (
    55  	toLower = 'a' - 'A'
    56  )
    57  
    58  func main() {
    59  	hex2intTable := func() [256]byte {
    60  		var b [256]byte
    61  		for i := 0; i < 256; i++ {
    62  			c := byte(16)
    63  			if i >= '0' && i <= '9' {
    64  				c = byte(i) - '0'
    65  			} else if i >= 'a' && i <= 'f' {
    66  				c = byte(i) - 'a' + 10
    67  			} else if i >= 'A' && i <= 'F' {
    68  				c = byte(i) - 'A' + 10
    69  			}
    70  			b[i] = c
    71  		}
    72  		return b
    73  	}()
    74  
    75  	toLowerTable := func() [256]byte {
    76  		var a [256]byte
    77  		for i := 0; i < 256; i++ {
    78  			c := byte(i)
    79  			if c >= 'A' && c <= 'Z' {
    80  				c += toLower
    81  			}
    82  			a[i] = c
    83  		}
    84  		return a
    85  	}()
    86  
    87  	toUpperTable := func() [256]byte {
    88  		var a [256]byte
    89  		for i := 0; i < 256; i++ {
    90  			c := byte(i)
    91  			if c >= 'a' && c <= 'z' {
    92  				c -= toLower
    93  			}
    94  			a[i] = c
    95  		}
    96  		return a
    97  	}()
    98  
    99  	quotedArgShouldEscapeTable := func() [256]byte {
   100  		// According to RFC 3986 ยง2.3
   101  		var a [256]byte
   102  		for i := 0; i < 256; i++ {
   103  			a[i] = 1
   104  		}
   105  
   106  		// ALPHA
   107  		for i := int('a'); i <= int('z'); i++ {
   108  			a[i] = 0
   109  		}
   110  		for i := int('A'); i <= int('Z'); i++ {
   111  			a[i] = 0
   112  		}
   113  
   114  		// DIGIT
   115  		for i := int('0'); i <= int('9'); i++ {
   116  			a[i] = 0
   117  		}
   118  
   119  		// Unreserved characters
   120  		for _, v := range `-_.~` {
   121  			a[v] = 0
   122  		}
   123  
   124  		return a
   125  	}()
   126  
   127  	quotedPathShouldEscapeTable := func() [256]byte {
   128  		// The implementation here equal to net/url shouldEscape(s, encodePath)
   129  		//
   130  		// The RFC allows : @ & = + $ but saves / ; , for assigning
   131  		// meaning to individual path segments. This package
   132  		// only manipulates the path as a whole, so we allow those
   133  		// last three as well. That leaves only ? to escape.
   134  		a := quotedArgShouldEscapeTable
   135  
   136  		for _, v := range `$&+,/:;=@` {
   137  			a[v] = 0
   138  		}
   139  
   140  		return a
   141  	}()
   142  
   143  	validCookieValueTable := func() [256]byte {
   144  		// The implementation here is equal to net/http validCookieValueByte(b byte)
   145  		// see https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1
   146  		var a [256]byte
   147  		for i := 0; i < 256; i++ {
   148  			a[i] = 0
   149  		}
   150  
   151  		for i := 0x20; i < 0x7f; i++ {
   152  			a[i] = 1
   153  		}
   154  
   155  		a['"'] = 0
   156  		a[';'] = 0
   157  		a['\\'] = 0
   158  
   159  		return a
   160  	}()
   161  
   162  	validHeaderFieldValueTable := func() [256]byte {
   163  		// The implementation here is equal to httpguts.ValidHeaderFieldValue
   164  		var a [256]byte
   165  		for i := 0; i < 256; i++ {
   166  			a[i] = 1
   167  		}
   168  
   169  		for i := 0; i < ' '; i++ {
   170  			a[i] = 0
   171  		}
   172  
   173  		// del CTL
   174  		a[0x7f] = 0
   175  		// tab
   176  		a['\t'] = 1
   177  
   178  		return a
   179  	}()
   180  
   181  	newlineToSpaceTable := func() [256]byte {
   182  		var a [256]byte
   183  		for i := 0; i < 256; i++ {
   184  			c := byte(i)
   185  			if c == '\r' || c == '\n' {
   186  				c = ' '
   187  			}
   188  			a[i] = c
   189  		}
   190  		return a
   191  	}()
   192  
   193  	validHeaderFieldNameTable := func() [256]byte {
   194  		// The implementation here is equal to httpguts ValidHeaderFieldName(string)
   195  		// see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
   196  		//
   197  		//  RFC 7230 says:
   198  		//   header-field   = field-name ":" OWS field-value OWS
   199  		//   field-name     = token
   200  		//   token          = 1*tchar
   201  		//   tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
   202  		//           "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
   203  		var a [256]byte
   204  		for i := 0; i < 256; i++ {
   205  			a[i] = 0
   206  		}
   207  
   208  		a['!'] = 1
   209  		a['#'] = 1
   210  		a['$'] = 1
   211  		a['%'] = 1
   212  		a['&'] = 1
   213  		a['\''] = 1
   214  		a['*'] = 1
   215  		a['+'] = 1
   216  		a['-'] = 1
   217  		a['.'] = 1
   218  		a['^'] = 1
   219  		a['_'] = 1
   220  		a['`'] = 1
   221  		a['|'] = 1
   222  		a['~'] = 1
   223  
   224  		// ALPHA
   225  		for i := int('a'); i <= int('z'); i++ {
   226  			a[i] = 1
   227  		}
   228  		for i := int('A'); i <= int('Z'); i++ {
   229  			a[i] = 1
   230  		}
   231  
   232  		// DIGIT
   233  		for i := int('0'); i <= int('9'); i++ {
   234  			a[i] = 1
   235  		}
   236  
   237  		return a
   238  	}()
   239  
   240  	w := new(bytes.Buffer)
   241  	w.WriteString(pre)
   242  	fmt.Fprintf(w, "const (\n")
   243  	fmt.Fprintf(w, "\tHex2intTable = %q\n", hex2intTable)
   244  	fmt.Fprintf(w, "\tToLowerTable = %q\n", toLowerTable)
   245  	fmt.Fprintf(w, "\tToUpperTable = %q\n", toUpperTable)
   246  	fmt.Fprintf(w, "\tQuotedArgShouldEscapeTable = %q\n", quotedArgShouldEscapeTable)
   247  	fmt.Fprintf(w, "\tQuotedPathShouldEscapeTable = %q\n", quotedPathShouldEscapeTable)
   248  	fmt.Fprintf(w, "\tValidCookieValueTable = %q\n", validCookieValueTable)
   249  	fmt.Fprintf(w, "\tValidHeaderFieldValueTable = %q\n", validHeaderFieldValueTable)
   250  	fmt.Fprintf(w, "\tNewlineToSpaceTable = %q\n", newlineToSpaceTable)
   251  	fmt.Fprintf(w, "\tValidHeaderFieldNameTable = %q\n", validHeaderFieldNameTable)
   252  	fmt.Fprintf(w, ")\n")
   253  
   254  	if err := ioutil.WriteFile("bytesconv_table.go", w.Bytes(), 0o660); err != nil {
   255  		log.Fatal(err)
   256  	}
   257  }
   258  
   259  const pre = `/*
   260   * Copyright 2022 CloudWeGo Authors
   261   *
   262   * Licensed under the Apache License, Version 2.0 (the "License");
   263   * you may not use this file except in compliance with the License.
   264   * You may obtain a copy of the License at
   265   *
   266   *     http://www.apache.org/licenses/LICENSE-2.0
   267   *
   268   * Unless required by applicable law or agreed to in writing, software
   269   * distributed under the License is distributed on an "AS IS" BASIS,
   270   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   271   * See the License for the specific language governing permissions and
   272   * limitations under the License.
   273   *
   274   * The MIT License (MIT)
   275   *
   276   * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors
   277   *
   278   * Permission is hereby granted, free of charge, to any person obtaining a copy
   279   * of this software and associated documentation files (the "Software"), to deal
   280   * in the Software without restriction, including without limitation the rights
   281   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   282   * copies of the Software, and to permit persons to whom the Software is
   283   * furnished to do so, subject to the following conditions:
   284   *
   285   * The above copyright notice and this permission notice shall be included in
   286   * all copies or substantial portions of the Software.
   287   *
   288   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   289   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   290   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   291   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   292   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   293   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   294   * THE SOFTWARE.
   295   *
   296   * This file may have been modified by CloudWeGo authors. All CloudWeGo
   297   * Modifications are Copyright 2022 CloudWeGo Authors.
   298   */
   299  
   300  package bytesconv
   301  
   302  // Code generated by go run bytesconv_table_gen.go; DO NOT EDIT.
   303  // See bytesconv_table_gen.go for more information about these tables.
   304  
   305  `