forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSQLFrontendMessageDecoder.swift
245 lines (203 loc) · 9.63 KB
/
PSQLFrontendMessageDecoder.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
@testable import PostgresNIO
import NIOCore
struct PSQLFrontendMessageDecoder: NIOSingleStepByteToMessageDecoder {
typealias InboundOut = PostgresFrontendMessage
private(set) var isInStartup: Bool
init() {
self.isInStartup = true
}
mutating func decode(buffer: inout ByteBuffer) throws -> PostgresFrontendMessage? {
// make sure we have at least one byte to read
guard buffer.readableBytes > 0 else {
return nil
}
if self.isInStartup {
guard let length = buffer.getInteger(at: buffer.readerIndex, as: UInt32.self) else {
return nil
}
guard var messageSlice = buffer.getSlice(at: buffer.readerIndex + 4, length: Int(length) - 4) else {
return nil
}
buffer.moveReaderIndex(to: Int(length))
let finalIndex = buffer.readerIndex
guard let code = messageSlice.readInteger(as: UInt32.self) else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: UInt32.self)
}
switch code {
case 80877103:
self.isInStartup = true
return .sslRequest
case 196608:
var user: String?
var database: String?
var options = [(String, String)]()
while let name = messageSlice.readNullTerminatedString(), messageSlice.readerIndex < finalIndex {
let value = messageSlice.readNullTerminatedString()
switch name {
case "user":
user = value
case "database":
database = value
default:
if let value = value {
options.append((name, value))
}
}
}
let parameters = PostgresFrontendMessage.Startup.Parameters(
user: user!,
database: database,
options: options,
replication: .false
)
let startup = PostgresFrontendMessage.Startup(
protocolVersion: 0x00_03_00_00,
parameters: parameters
)
precondition(buffer.readerIndex == finalIndex)
self.isInStartup = false
return .startup(startup)
default:
throw PostgresMessageDecodingError.unknownStartupCodeReceived(code: code, messageBytes: messageSlice)
}
}
// all other packages have an Int32 after the identifier that determines their length.
// do we have enough bytes for that?
guard let idByte = buffer.getInteger(at: buffer.readerIndex, as: UInt8.self),
let length = buffer.getInteger(at: buffer.readerIndex + 1, as: Int32.self) 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.
guard let completeMessageBuffer = buffer.readSlice(length: 1 + Int(length)) else {
return nil
}
// 2. make sure we have a known message identifier
guard let messageID = PostgresFrontendMessage.ID(rawValue: idByte) else {
throw PostgresMessageDecodingError.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 PostgresFrontendMessage.decode(from: &slice, for: messageID)
} catch let error as PSQLPartialDecodingError {
throw PostgresMessageDecodingError.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 -> PostgresFrontendMessage? {
try self.decode(buffer: &buffer)
}
}
extension PostgresFrontendMessage {
static func decode(from buffer: inout ByteBuffer, for messageID: ID) throws -> PostgresFrontendMessage {
switch messageID {
case .bind:
guard let portalName = buffer.readNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
guard let preparedStatementName = buffer.readNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
guard let parameterFormatCount = buffer.readInteger(as: UInt16.self) else {
preconditionFailure("TODO: Unimplemented")
}
let parameterFormats = (0..<parameterFormatCount).map({ _ in PostgresFormat(rawValue: buffer.readInteger(as: Int16.self)!)! })
guard let parameterCount = buffer.readInteger(as: UInt16.self) else {
preconditionFailure("TODO: Unimplemented")
}
let parameters = (0..<parameterCount).map { _ -> ByteBuffer? in
let length = buffer.readInteger(as: UInt32.self)
switch length {
case .some(..<0):
return nil
case .some(0...):
return buffer.readSlice(length: Int(length!))
default:
preconditionFailure("TODO: Unimplemented")
}
}
guard let resultColumnFormatCount = buffer.readInteger(as: UInt16.self) else {
preconditionFailure("TODO: Unimplemented")
}
let resultColumnFormats = (0..<resultColumnFormatCount).map({ _ in PostgresFormat(rawValue: buffer.readInteger(as: Int16.self)!)! })
return .bind(
Bind(
portalName: portalName,
preparedStatementName: preparedStatementName,
parameterFormats: parameterFormats,
parameters: parameters,
resultColumnFormats: resultColumnFormats
)
)
case .close:
preconditionFailure("TODO: Unimplemented")
case .describe:
switch buffer.readInteger(as: UInt8.self) {
case UInt8(ascii: "S"):
return .describe(.preparedStatement(buffer.readNullTerminatedString()!))
case UInt8(ascii: "P"):
return .describe(.portal(buffer.readNullTerminatedString()!))
default:
preconditionFailure("TODO: Unimplemented")
}
case .execute:
guard let portalName = buffer.readNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
guard let maxNumberOfRows = buffer.readInteger(as: Int32.self) else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: Int.self)
}
return .execute(.init(portalName: portalName, maxNumberOfRows: maxNumberOfRows))
case .flush:
return .flush
case .parse:
guard let preparedStatementName = buffer.readNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
guard let query = buffer.readNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
guard let parameterCount = buffer.readInteger(as: UInt16.self) else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
let parameters = (0..<parameterCount).map({ _ in PostgresDataType(buffer.readInteger(as: UInt32.self)!) })
return .parse(.init(preparedStatementName: preparedStatementName, query: query, parameters: parameters))
case .password:
guard let password = buffer.readNullTerminatedString() else {
throw PSQLPartialDecodingError.fieldNotDecodable(type: String.self)
}
return .password(.init(value: password))
case .saslInitialResponse:
preconditionFailure("TODO: Unimplemented")
case .saslResponse:
preconditionFailure("TODO: Unimplemented")
case .sync:
return .sync
case .terminate:
return .terminate
}
}
}
extension PostgresMessageDecodingError {
static func unknownStartupCodeReceived(
code: UInt32,
messageBytes: ByteBuffer,
file: String = #fileID,
line: Int = #line) -> Self
{
var byteBuffer = messageBytes
let data = byteBuffer.readData(length: byteBuffer.readableBytes)!
return PostgresMessageDecodingError(
messageID: 0,
payload: data.base64EncodedString(),
description: "Received a startup code '\(code)'. There is no message associated with this code.",
file: file,
line: line)
}
}