1
// Copyright 2021 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
9
// The HTTP protocols are defined in terms of ASCII, not Unicode. This file
10
// contains helper functions which may use Unicode-aware functions which would
11
// otherwise be unsafe and could introduce vulnerabilities if used improperly.
13
// asciiEqualFold is strings.EqualFold, ASCII only. It reports whether s and t
14
// are equal, ASCII-case-insensitively.
15
func asciiEqualFold(s, t string) bool {
19
for i := 0; i < len(s); i++ {
20
if lower(s[i]) != lower(t[i]) {
27
// lower returns the ASCII lowercase version of b.
28
func lower(b byte) byte {
29
if 'A' <= b && b <= 'Z' {
30
return b + ('a' - 'A')
35
// isASCIIPrint returns whether s is ASCII and printable according to
36
// https://tools.ietf.org/html/rfc20#section-4.2.
37
func isASCIIPrint(s string) bool {
38
for i := 0; i < len(s); i++ {
39
if s[i] < ' ' || s[i] > '~' {
46
// asciiToLower returns the lowercase version of s if s is ASCII and printable,
47
// and whether or not it was.
48
func asciiToLower(s string) (lower string, ok bool) {
52
return strings.ToLower(s), true