validate.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tag
  15. import "errors"
  16. const (
  17. maxKeyLength = 255
  18. // valid are restricted to US-ASCII subset (range 0x20 (' ') to 0x7e ('~')).
  19. validKeyValueMin = 32
  20. validKeyValueMax = 126
  21. )
  22. var (
  23. errInvalidKeyName = errors.New("invalid key name: only ASCII characters accepted; max length must be 255 characters")
  24. errInvalidValue = errors.New("invalid value: only ASCII characters accepted; max length must be 255 characters")
  25. )
  26. func checkKeyName(name string) bool {
  27. if len(name) == 0 {
  28. return false
  29. }
  30. if len(name) > maxKeyLength {
  31. return false
  32. }
  33. return isASCII(name)
  34. }
  35. func isASCII(s string) bool {
  36. for _, c := range s {
  37. if (c < validKeyValueMin) || (c > validKeyValueMax) {
  38. return false
  39. }
  40. }
  41. return true
  42. }
  43. func checkValue(v string) bool {
  44. if len(v) > maxKeyLength {
  45. return false
  46. }
  47. return isASCII(v)
  48. }