octet.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package httphead
  2. // OctetType desribes character type.
  3. //
  4. // From the "Basic Rules" chapter of RFC2616
  5. // See https://tools.ietf.org/html/rfc2616#section-2.2
  6. //
  7. // OCTET = <any 8-bit sequence of data>
  8. // CHAR = <any US-ASCII character (octets 0 - 127)>
  9. // UPALPHA = <any US-ASCII uppercase letter "A".."Z">
  10. // LOALPHA = <any US-ASCII lowercase letter "a".."z">
  11. // ALPHA = UPALPHA | LOALPHA
  12. // DIGIT = <any US-ASCII digit "0".."9">
  13. // CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
  14. // CR = <US-ASCII CR, carriage return (13)>
  15. // LF = <US-ASCII LF, linefeed (10)>
  16. // SP = <US-ASCII SP, space (32)>
  17. // HT = <US-ASCII HT, horizontal-tab (9)>
  18. // <"> = <US-ASCII double-quote mark (34)>
  19. // CRLF = CR LF
  20. // LWS = [CRLF] 1*( SP | HT )
  21. //
  22. // Many HTTP/1.1 header field values consist of words separated by LWS
  23. // or special characters. These special characters MUST be in a quoted
  24. // string to be used within a parameter value (as defined in section
  25. // 3.6).
  26. //
  27. // token = 1*<any CHAR except CTLs or separators>
  28. // separators = "(" | ")" | "<" | ">" | "@"
  29. // | "," | ";" | ":" | "\" | <">
  30. // | "/" | "[" | "]" | "?" | "="
  31. // | "{" | "}" | SP | HT
  32. type OctetType byte
  33. // IsChar reports whether octet is CHAR.
  34. func (t OctetType) IsChar() bool { return t&octetChar != 0 }
  35. // IsControl reports whether octet is CTL.
  36. func (t OctetType) IsControl() bool { return t&octetControl != 0 }
  37. // IsSeparator reports whether octet is separator.
  38. func (t OctetType) IsSeparator() bool { return t&octetSeparator != 0 }
  39. // IsSpace reports whether octet is space (SP or HT).
  40. func (t OctetType) IsSpace() bool { return t&octetSpace != 0 }
  41. // IsToken reports whether octet is token.
  42. func (t OctetType) IsToken() bool { return t&octetToken != 0 }
  43. const (
  44. octetChar OctetType = 1 << iota
  45. octetControl
  46. octetSpace
  47. octetSeparator
  48. octetToken
  49. )
  50. // OctetTypes is a table of octets.
  51. var OctetTypes [256]OctetType
  52. func init() {
  53. for c := 32; c < 256; c++ {
  54. var t OctetType
  55. if c <= 127 {
  56. t |= octetChar
  57. }
  58. if 0 <= c && c <= 31 || c == 127 {
  59. t |= octetControl
  60. }
  61. switch c {
  62. case '(', ')', '<', '>', '@', ',', ';', ':', '"', '/', '[', ']', '?', '=', '{', '}', '\\':
  63. t |= octetSeparator
  64. case ' ', '\t':
  65. t |= octetSpace | octetSeparator
  66. }
  67. if t.IsChar() && !t.IsControl() && !t.IsSeparator() && !t.IsSpace() {
  68. t |= octetToken
  69. }
  70. OctetTypes[c] = t
  71. }
  72. }