memcached.go 792 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package l7
  2. import (
  3. "bytes"
  4. "strings"
  5. )
  6. var (
  7. space = []byte{' '}
  8. crlf = []byte{'\r', '\n'}
  9. )
  10. func ParseMemcached(payload []byte) (string, []string) {
  11. cmd, rest, ok := bytes.Cut(payload, space)
  12. if !ok {
  13. return "", nil
  14. }
  15. command := string(cmd)
  16. switch command {
  17. case "set", "add", "cas", "append", "prepend", "replace", "delete", "incr", "decr", "touch":
  18. if key, _, ok := bytes.Cut(rest, space); ok {
  19. return command, []string{string(key)}
  20. }
  21. case "gat", "gats":
  22. _, rest, ok = bytes.Cut(rest, space)
  23. if ok {
  24. keys, _, ok := bytes.Cut(rest, crlf)
  25. if ok {
  26. return command, strings.Split(string(keys), " ")
  27. }
  28. }
  29. case "get", "gets":
  30. keys, _, ok := bytes.Cut(rest, crlf)
  31. if ok {
  32. return command, strings.Split(string(keys), " ")
  33. }
  34. }
  35. return "", nil
  36. }