Skip to content

Commit

Permalink
refactor: move manually created tchar table to bytesconv_table_gen (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
nickajacks1 authored and Max-Cheng committed Feb 12, 2024
1 parent ddb49f9 commit a19671a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
5 changes: 3 additions & 2 deletions bytesconv_table.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions bytesconv_table_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,34 @@ func main() {
return a
}()

tcharTable := func() [128]byte {
// Should match net/textproto's validHeaderFieldByte(c byte) bool
// Defined by RFC 9110 5.6.2
// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
// "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
var a [128]byte
for _, v := range "!#$%&'*+-.^_`|~" {
a[v] = 1
}
for i := 'a'; i <= 'z'; i++ {
a[i] = 1
}
for i := 'A'; i <= 'Z'; i++ {
a[i] = 1
}
for i := '0'; i <= '9'; i++ {
a[i] = 1
}
return a
}()

w := bytes.NewBufferString(pre)
fmt.Fprintf(w, "const hex2intTable = %q\n", hex2intTable)
fmt.Fprintf(w, "const toLowerTable = %q\n", toLowerTable)
fmt.Fprintf(w, "const toUpperTable = %q\n", toUpperTable)
fmt.Fprintf(w, "const quotedArgShouldEscapeTable = %q\n", quotedArgShouldEscapeTable)
fmt.Fprintf(w, "const quotedPathShouldEscapeTable = %q\n", quotedPathShouldEscapeTable)
fmt.Fprintf(w, "const tcharTable = %q\n", tcharTable)

if err := os.WriteFile("bytesconv_table.go", w.Bytes(), 0o660); err != nil {
log.Fatal(err)
Expand Down
21 changes: 1 addition & 20 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,26 +548,7 @@ func (h *ResponseHeader) AddTrailerBytes(trailer []byte) error {
// validHeaderFieldByte returns true if c is a valid tchar as defined
// by section 5.6.2 of [RFC9110].
func validHeaderFieldByte(c byte) bool {
return c < 128 && tcharTable[c]
}

// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
// / DIGIT / ALPHA
//
// See: https://www.rfc-editor.org/rfc/rfc9110#tokens
var tcharTable = [128]bool{
'!': true, '#': true, '$': true, '%': true, '&': true, '\'': true, '*': true, '+': true,
'-': true, '.': true, '^': true, '_': true, '`': true, '|': true, '~': true,
'0': true, '1': true, '2': true, '3': true, '4': true, '5': true, '6': true, '7': true, '8': true, '9': true,
'A': true, 'B': true, 'C': true, 'D': true, 'E': true, 'F': true, 'G': true,
'H': true, 'I': true, 'J': true, 'K': true, 'L': true, 'M': true, 'N': true,
'O': true, 'P': true, 'Q': true, 'R': true, 'S': true, 'T': true, 'U': true,
'V': true, 'W': true, 'X': true, 'Y': true, 'Z': true,
'a': true, 'b': true, 'c': true, 'd': true, 'e': true, 'f': true, 'g': true,
'h': true, 'i': true, 'j': true, 'k': true, 'l': true, 'm': true, 'n': true,
'o': true, 'p': true, 'q': true, 'r': true, 's': true, 't': true, 'u': true,
'v': true, 'w': true, 'x': true, 'y': true, 'z': true,
return c < 128 && tcharTable[c] == 1
}

// VisitHeaderParams calls f for each parameter in the given header bytes.
Expand Down

0 comments on commit a19671a

Please sign in to comment.