pgproto3.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package pgproto3
  2. import (
  3. "encoding/hex"
  4. "errors"
  5. "fmt"
  6. "github.com/jackc/pgx/v5/internal/pgio"
  7. )
  8. // maxMessageBodyLen is the maximum length of a message body in bytes. See PG_LARGE_MESSAGE_LIMIT in the PostgreSQL
  9. // source. It is defined as (MaxAllocSize - 1). MaxAllocSize is defined as 0x3fffffff.
  10. const maxMessageBodyLen = (0x3fffffff - 1)
  11. // Message is the interface implemented by an object that can decode and encode
  12. // a particular PostgreSQL message.
  13. type Message interface {
  14. // Decode is allowed and expected to retain a reference to data after
  15. // returning (unlike encoding.BinaryUnmarshaler).
  16. Decode(data []byte) error
  17. // Encode appends itself to dst and returns the new buffer.
  18. Encode(dst []byte) ([]byte, error)
  19. }
  20. // FrontendMessage is a message sent by the frontend (i.e. the client).
  21. type FrontendMessage interface {
  22. Message
  23. Frontend() // no-op method to distinguish frontend from backend methods
  24. }
  25. // BackendMessage is a message sent by the backend (i.e. the server).
  26. type BackendMessage interface {
  27. Message
  28. Backend() // no-op method to distinguish frontend from backend methods
  29. }
  30. type AuthenticationResponseMessage interface {
  31. BackendMessage
  32. AuthenticationResponse() // no-op method to distinguish authentication responses
  33. }
  34. type invalidMessageLenErr struct {
  35. messageType string
  36. expectedLen int
  37. actualLen int
  38. }
  39. func (e *invalidMessageLenErr) Error() string {
  40. return fmt.Sprintf("%s body must have length of %d, but it is %d", e.messageType, e.expectedLen, e.actualLen)
  41. }
  42. type invalidMessageFormatErr struct {
  43. messageType string
  44. details string
  45. }
  46. func (e *invalidMessageFormatErr) Error() string {
  47. return fmt.Sprintf("%s body is invalid %s", e.messageType, e.details)
  48. }
  49. type writeError struct {
  50. err error
  51. safeToRetry bool
  52. }
  53. func (e *writeError) Error() string {
  54. return fmt.Sprintf("write failed: %s", e.err.Error())
  55. }
  56. func (e *writeError) SafeToRetry() bool {
  57. return e.safeToRetry
  58. }
  59. func (e *writeError) Unwrap() error {
  60. return e.err
  61. }
  62. type ExceededMaxBodyLenErr struct {
  63. MaxExpectedBodyLen int
  64. ActualBodyLen int
  65. }
  66. func (e *ExceededMaxBodyLenErr) Error() string {
  67. return fmt.Sprintf("invalid body length: expected at most %d, but got %d", e.MaxExpectedBodyLen, e.ActualBodyLen)
  68. }
  69. // getValueFromJSON gets the value from a protocol message representation in JSON.
  70. func getValueFromJSON(v map[string]string) ([]byte, error) {
  71. if v == nil {
  72. return nil, nil
  73. }
  74. if text, ok := v["text"]; ok {
  75. return []byte(text), nil
  76. }
  77. if binary, ok := v["binary"]; ok {
  78. return hex.DecodeString(binary)
  79. }
  80. return nil, errors.New("unknown protocol representation")
  81. }
  82. // beginMessage begins a new message of type t. It appends the message type and a placeholder for the message length to
  83. // dst. It returns the new buffer and the position of the message length placeholder.
  84. func beginMessage(dst []byte, t byte) ([]byte, int) {
  85. dst = append(dst, t)
  86. sp := len(dst)
  87. dst = pgio.AppendInt32(dst, -1)
  88. return dst, sp
  89. }
  90. // finishMessage finishes a message that was started with beginMessage. It computes the message length and writes it to
  91. // dst[sp]. If the message length is too large it returns an error. Otherwise it returns the final message buffer.
  92. func finishMessage(dst []byte, sp int) ([]byte, error) {
  93. messageBodyLen := len(dst[sp:])
  94. if messageBodyLen > maxMessageBodyLen {
  95. return nil, errors.New("message body too large")
  96. }
  97. pgio.SetInt32(dst[sp:], int32(messageBodyLen))
  98. return dst, nil
  99. }