forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresBackendMessageDecoder.swift
208 lines (169 loc) · 8.16 KB
/
PostgresBackendMessageDecoder.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
207
import NIOCore
struct PostgresBackendMessageDecoder: NIOSingleStepByteToMessageDecoder {
typealias InboundOut = PostgresBackendMessage
private(set) var hasAlreadyReceivedBytes: Bool
init(hasAlreadyReceivedBytes: Bool = false) {
self.hasAlreadyReceivedBytes = hasAlreadyReceivedBytes
}
mutating func decode(buffer: inout ByteBuffer) throws -> PostgresBackendMessage? {
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"
let startReaderIndex = buffer.readerIndex
guard let firstByte = buffer.readInteger(as: UInt8.self) else {
return nil
}
switch firstByte {
case UInt8(ascii: "S"):
self.hasAlreadyReceivedBytes = true
return .sslSupported
case UInt8(ascii: "N"):
self.hasAlreadyReceivedBytes = true
return .sslUnsupported
default:
// move reader index back
buffer.moveReaderIndex(to: startReaderIndex)
self.hasAlreadyReceivedBytes = true
}
}
// all other packages start with a MessageID (UInt8) and their message length (UInt32).
// do we have enough bytes for that?
let startReaderIndex = buffer.readerIndex
guard let (idByte, length) = buffer.readMultipleIntegers(endianness: .big, as: (UInt8, UInt32).self) else {
// if this fails, the readerIndex wasn't changed
return nil
}
// 1. try to read the message
guard var message = buffer.readSlice(length: Int(length) - 4) else {
// we need to move the reader index back to its start point
buffer.moveReaderIndex(to: startReaderIndex)
return nil
}
// 2. make sure we have a known message identifier
guard let messageID = PostgresBackendMessage.ID(rawValue: idByte) else {
buffer.moveReaderIndex(to: startReaderIndex)
let completeMessage = buffer.readSlice(length: Int(length) + 1)!
throw PostgresMessageDecodingError.unknownMessageIDReceived(messageID: idByte, messageBytes: completeMessage)
}
// 3. decode the message
do {
let result = try PostgresBackendMessage.decode(from: &message, for: messageID)
if message.readableBytes > 0 {
throw PSQLPartialDecodingError.expectedExactlyNRemainingBytes(0, actual: message.readableBytes)
}
return result
} catch let error as PSQLPartialDecodingError {
buffer.moveReaderIndex(to: startReaderIndex)
let completeMessage = buffer.readSlice(length: Int(length) + 1)!
throw PostgresMessageDecodingError.withPartialError(error, messageID: messageID.rawValue, messageBytes: completeMessage)
} catch {
preconditionFailure("Expected to only see `PartialDecodingError`s here.")
}
}
mutating func decodeLast(buffer: inout ByteBuffer, seenEOF: Bool) throws -> PostgresBackendMessage? {
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 PostgresMessageDecodingError: 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 PostgresMessageDecodingError(
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 PostgresMessageDecodingError(
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 {
mutating func throwingReadInteger<I: FixedWidthInteger>(as: I.Type, file: String = #file, line: Int = #line) throws -> I {
guard let result = self.readInteger(endianness: .big, as: I.self) else {
throw PSQLPartialDecodingError.expectedAtLeastNRemainingBytes(MemoryLayout<I>.size, actual: self.readableBytes, file: file, line: line)
}
return result
}
mutating func throwingMoveReaderIndex(forwardBy offset: Int, file: String = #file, line: Int = #line) throws {
guard self.readSlice(length: offset) != nil else {
throw PSQLPartialDecodingError.expectedAtLeastNRemainingBytes(offset, actual: self.readableBytes, file: file, line: line)
}
}
}