forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSQLBackendMessageDecoder.swift
207 lines (168 loc) · 8.01 KB
/
PSQLBackendMessageDecoder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
struct PSQLBackendMessageDecoder: NIOSingleStepByteToMessageDecoder {
typealias InboundOut = PSQLBackendMessage
private(set) var hasAlreadyReceivedBytes: Bool
init(hasAlreadyReceivedBytes: Bool = false) {
self.hasAlreadyReceivedBytes = hasAlreadyReceivedBytes
}
mutating func decode(buffer: inout ByteBuffer) throws -> PSQLBackendMessage? {
// make sure we have at least one byte to read
guard buffer.readableBytes > 0 else {
return nil
}
if !self.hasAlreadyReceivedBytes {
// We have not received any bytes yet! Let's peek at the first message id. If it
// is a "S" or "N" we assume that it is connected to an SSL upgrade request. All
// other messages that we expect now, don't start with either "S" or "N"
// we made sure, we have at least one byte available, above, thus force unwrap is okay
let firstByte = buffer.getInteger(at: buffer.readerIndex, as: UInt8.self)!
switch firstByte {
case UInt8(ascii: "S"):
// mark byte as read
buffer.moveReaderIndex(forwardBy: 1)
self.hasAlreadyReceivedBytes = true
return .sslSupported
case UInt8(ascii: "N"):
// mark byte as read
buffer.moveReaderIndex(forwardBy: 1)
self.hasAlreadyReceivedBytes = true
return .sslUnsupported
default:
self.hasAlreadyReceivedBytes = true
}
}
// all other packages have an Int32 after the identifier that determines their length.
// do we have enough bytes for that?
guard buffer.readableBytes >= 5 else {
return nil
}
let idByte = buffer.getInteger(at: buffer.readerIndex, as: UInt8.self)!
let length = buffer.getInteger(at: buffer.readerIndex + 1, as: Int32.self)!
guard length + 1 <= buffer.readableBytes else {
return nil
}
// At this point we are sure, that we have enough bytes to decode the next message.
// 1. Create a byteBuffer that represents exactly the next message. This can be force
// unwrapped, since it was verified that enough bytes are available.
let completeMessageBuffer = buffer.readSlice(length: 1 + Int(length))!
// 2. make sure we have a known message identifier
guard let messageID = PSQLBackendMessage.ID(rawValue: idByte) else {
throw PSQLDecodingError.unknownMessageIDReceived(messageID: idByte, messageBytes: completeMessageBuffer)
}
// 3. decode the message
do {
// get a mutable byteBuffer copy
var slice = completeMessageBuffer
// move reader index forward by five bytes
slice.moveReaderIndex(forwardBy: 5)
return try PSQLBackendMessage.decode(from: &slice, for: messageID)
} catch let error as PSQLPartialDecodingError {
throw PSQLDecodingError.withPartialError(error, messageID: messageID.rawValue, messageBytes: completeMessageBuffer)
} catch {
preconditionFailure("Expected to only see `PartialDecodingError`s here.")
}
}
mutating func decodeLast(buffer: inout ByteBuffer, seenEOF: Bool) throws -> PSQLBackendMessage? {
try self.decode(buffer: &buffer)
}
}
/// An error representing a failure to decode [a Postgres wire message](https://www.postgresql.org/docs/13/protocol-message-formats.html)
/// to the Swift structure `PSQLBackendMessage`.
///
/// If you encounter a `DecodingError` when using a trusted Postgres server please make to file an issue at:
/// [https://github.com/vapor/postgres-nio/issues](https://github.com/vapor/postgres-nio/issues)
struct PSQLDecodingError: Error {
/// The backend message ID bytes
let messageID: UInt8
/// The backend message's payload encoded in base64
let payload: String
/// A textual description of the error
let description: String
/// The file this error was thrown in
let file: String
/// The line in `file` this error was thrown
let line: Int
static func withPartialError(
_ partialError: PSQLPartialDecodingError,
messageID: UInt8,
messageBytes: ByteBuffer) -> Self
{
var byteBuffer = messageBytes
let data = byteBuffer.readData(length: byteBuffer.readableBytes)!
return PSQLDecodingError(
messageID: messageID,
payload: data.base64EncodedString(),
description: partialError.description,
file: partialError.file,
line: partialError.line)
}
static func unknownMessageIDReceived(
messageID: UInt8,
messageBytes: ByteBuffer,
file: String = #file,
line: Int = #line) -> Self
{
var byteBuffer = messageBytes
let data = byteBuffer.readData(length: byteBuffer.readableBytes)!
return PSQLDecodingError(
messageID: messageID,
payload: data.base64EncodedString(),
description: "Received a message with messageID '\(Character(UnicodeScalar(messageID)))'. There is no message type associated with this message identifier.",
file: file,
line: line)
}
}
struct PSQLPartialDecodingError: Error {
/// A textual description of the error
let description: String
/// The file this error was thrown in
let file: String
/// The line in `file` this error was thrown
let line: Int
static func valueNotRawRepresentable<Target: RawRepresentable>(
value: Target.RawValue,
asType: Target.Type,
file: String = #file,
line: Int = #line) -> Self
{
return PSQLPartialDecodingError(
description: "Can not represent '\(value)' with type '\(asType)'.",
file: file, line: line)
}
static func unexpectedValue(value: Any, file: String = #file, line: Int = #line) -> Self {
return PSQLPartialDecodingError(
description: "Value '\(value)' is not expected.",
file: file, line: line)
}
static func expectedAtLeastNRemainingBytes(_ expected: Int, actual: Int, file: String = #file, line: Int = #line) -> Self {
return PSQLPartialDecodingError(
description: "Expected at least '\(expected)' remaining bytes. But only found \(actual).",
file: file, line: line)
}
static func expectedExactlyNRemainingBytes(_ expected: Int, actual: Int, file: String = #file, line: Int = #line) -> Self {
return PSQLPartialDecodingError(
description: "Expected exactly '\(expected)' remaining bytes. But found \(actual).",
file: file, line: line)
}
static func fieldNotDecodable(type: Any.Type, file: String = #file, line: Int = #line) -> Self {
return PSQLPartialDecodingError(
description: "Could not read '\(type)' from ByteBuffer.",
file: file, line: line)
}
static func integerMustBePositiveOrNull<Number: FixedWidthInteger>(_ actual: Number, file: String = #file, line: Int = #line) -> Self {
return PSQLPartialDecodingError(
description: "Expected the integer to be positive or null, but got \(actual).",
file: file, line: line)
}
}
extension ByteBuffer {
func ensureAtLeastNBytesRemaining(_ n: Int, file: String = #file, line: Int = #line) throws {
guard self.readableBytes >= n else {
throw PSQLPartialDecodingError.expectedAtLeastNRemainingBytes(2, actual: self.readableBytes, file: file, line: line)
}
}
func ensureExactNBytesRemaining(_ n: Int, file: String = #file, line: Int = #line) throws {
guard self.readableBytes == n else {
throw PSQLPartialDecodingError.expectedExactlyNRemainingBytes(n, actual: self.readableBytes, file: file, line: line)
}
}
}