Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. --- a/mqtt/packet/connect_payload.go
  2. +++ b/mqtt/packet/connect_payload.go
  3. @@ -1,7 +1,9 @@
  4. package packet
  5.  
  6. import (
  7. + "bufio"
  8. "encoding/binary"
  9. + "io"
  10. "regexp"
  11.  
  12. "github.com/pkg/errors"
  13. @@ -13,17 +15,20 @@ type ConnectPayload struct {
  14.  
  15. var clientIDRegex = regexp.MustCompile("^[a-zA-Z0-9-|]*$")
  16.  
  17. -func ToConnectPayload(bs []byte) (ConnectPayload, error) {
  18. - if len(bs) < 3 {
  19. - return ConnectPayload{}, errors.New("payload length is invalid")
  20. +func ToConnectPayload(r *bufio.Reader) (ConnectPayload, error) {
  21. + lengthBytes := make([]byte, 2)
  22. + _, err := io.ReadFull(r, lengthBytes)
  23. + if err != nil {
  24. + return ConnectPayload{}, err
  25. }
  26. - length := binary.BigEndian.Uint16(bs[0:2])
  27. - var clientID string
  28. - if len(bs) < 2+int(length) {
  29. - return ConnectPayload{}, errors.New("specified length is not equals ClientID length")
  30. - } else {
  31. - clientID = string(bs[2 : 2+length])
  32. + length := binary.BigEndian.Uint16(lengthBytes)
  33. +
  34. + clientIDBytes := make([]byte, length)
  35. + _, err = io.ReadFull(r, clientIDBytes)
  36. + if err != nil {
  37. + return ConnectPayload{}, err
  38. }
  39. + clientID := string(clientIDBytes)
  40. if len(clientID) < 1 || len(clientID) > 23 {
  41. return ConnectPayload{}, errors.New("ClientID length is invalid")
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement