http.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package l7
  2. import (
  3. "bytes"
  4. "strconv"
  5. "strings"
  6. )
  7. func ParseHttp(payload []byte) (string, string) {
  8. method, rest, ok := bytes.Cut(payload, space)
  9. if !ok {
  10. return "", ""
  11. }
  12. if !isHttpMethod(string(method)) {
  13. return "", ""
  14. }
  15. uri, _, ok := bytes.Cut(rest, space)
  16. if !ok {
  17. uri = append(uri, []byte("...")...)
  18. }
  19. return string(method), string(uri)
  20. }
  21. func ParseHttpHost(payload []byte) (string, string, string, uint16) {
  22. method, rest, ok := bytes.Cut(payload, space)
  23. if !ok {
  24. return "", "", "", 0
  25. }
  26. if !isHttpMethod(string(method)) {
  27. return "", "", "", 0
  28. }
  29. uri, rest, ok := bytes.Cut(rest, space)
  30. if !ok {
  31. uri = append(uri, []byte("...")...)
  32. }
  33. hostStart := bytes.Index(rest, []byte("Host:")) + len("Host:")
  34. hostEnd := bytes.Index(rest[hostStart:], []byte("\r\n"))
  35. hostPort := string(bytes.TrimSpace(rest[hostStart : hostStart+hostEnd]))
  36. hostParts := strings.Split(hostPort, ":")
  37. host := hostParts[0]
  38. port := uint16(80) // Default port
  39. if len(hostParts) > 1 {
  40. port64, err := strconv.ParseUint(hostParts[1], 10, 16)
  41. if err == nil {
  42. port = uint16(port64)
  43. }
  44. }
  45. return string(method), string(uri), host, port
  46. }