@@ -139,9 +139,10 @@ authorize rawConn settings = do
139
139
throwAuthErrorInIO $ AuthNotSupported " GSS"
140
140
AuthErrorResponse desc ->
141
141
throwErrorInIO $ PostgresError desc
142
- -- TODO handle this case
143
- -- data receiving error
144
- f -> error " athorize"
142
+ -- this case is near impossible and ignored
143
+ BG. Partial _ -> throwErrorInIO $
144
+ DecodeError " partial auth response"
145
+ BG. Fail _ _ reason -> throwErrorInIO . DecodeError $ BS. pack reason
145
146
146
147
performPasswordAuth password = do
147
148
sendMessage rawConn $ PasswordMessage password
@@ -153,26 +154,34 @@ authorize rawConn settings = do
153
154
(settingsPassword settings <> settingsUser settings) <> salt)
154
155
md5Hash bs = BS. pack $ show (hash bs :: Digest MD5 )
155
156
156
- -- TODO right parsing
157
157
-- | Parses connection parameters.
158
158
parseParameters :: B. ByteString -> Either Error ConnectionParameters
159
- parseParameters str =
160
- let dict = go str HM. empty
161
- in Right ConnectionParameters
162
- { paramServerVersion = ServerVersion 1 1 1 " "
163
- , paramIntegerDatetimes = False
164
- , paramServerEncoding = " "
165
- }
159
+ parseParameters str = do
160
+ dict <- go str HM. empty
161
+ serverVersion <- parseServerVersion =<< lookupKey " server_version" dict
162
+ serverEncoding <- lookupKey " server_encoding" dict
163
+ integerDatetimes <- parseBool <$> lookupKey " integer_datetimes" dict
164
+ pure ConnectionParameters
165
+ { paramServerVersion = serverVersion
166
+ , paramIntegerDatetimes = integerDatetimes
167
+ , paramServerEncoding = serverEncoding
168
+ }
166
169
where
170
+ lookupKey key = maybe
171
+ (Left . DecodeError $ " Missing connection parameter " <> key ) Right
172
+ . HM. lookup key
173
+ parseBool bs | bs == " on" || bs == "
8000
yes" || bs == " 1" = True
174
+ | otherwise = False
167
175
decoder = runGetIncremental decodeServerMessage
168
- go str dict | B. null str = dict
176
+ go str dict | B. null str = Right dict
169
177
| otherwise = case pushChunk decoder str of
170
178
BG. Done rest _ v -> case v of
171
179
ParameterStatus name value -> go rest $ HM. insert name value dict
180
+ -- messages like `BackendData` not handled
172
181
_ -> go rest dict
173
- -- TODO right parsing
174
- BG. Partial _ -> error " Partial "
175
- BG. Fail _ _ e -> error e
182
+ -- this case is near impossible and ignored
183
+ BG. Partial _ -> Left $ DecodeError " partial auth response "
184
+ BG. Fail _ _ reason -> Left . DecodeError $ BS. pack reason
176
185
177
186
parseServerVersion :: B. ByteString -> Either Error ServerVersion
178
187
parseServerVersion bs =
@@ -181,7 +190,7 @@ parseServerVersion bs =
181
190
in case numbers ++ repeat (Just 0 ) of
182
191
(Just major : Just minor : Just rev : _) ->
183
192
Right $ ServerVersion major minor rev desc
184
- _ -> Left $ undefined
193
+ _ -> Left $ DecodeError " parse server version "
185
194
where
186
195
isDigitDot c | c == 46 = True -- dot
187
196
| c >= 48 && c < 58 = True -- digits
@@ -215,7 +224,6 @@ receiverThread msgFilter rawConn dataChan allChan modeRef = receiveLoop []
215
224
go :: B. ByteString -> [V. Vector B. ByteString ] -> IO [V. Vector B. ByteString ]
216
225
go str acc = case pushChunk decoder str of
217
226
BG. Done rest _ v -> do
218
- -- putStrLn $ "Received: " ++ show v
219
227
when (msgFilter v) $ writeChan allChan v
220
228
mode <- readIORef modeRef
221
229
newAcc <- dispatch mode dataChan v acc
@@ -224,7 +232,7 @@ receiverThread msgFilter rawConn dataChan allChan modeRef = receiveLoop []
224
232
else go rest newAcc
225
233
-- TODO right parsing
226
234
BG. Partial _ -> error " Partial"
227
- BG. Fail _ _ e -> error e
235
+ BG. Fail _ _ reason -> error reason
228
236
229
237
dispatch :: Connect
8000
ionMode -> Dispatcher
230
238
dispatch SimpleQueryMode = dispatchSimple
@@ -234,7 +242,6 @@ dispatch ExtendedQueryMode = dispatchExtended
234
242
dispatchSimple :: Dispatcher
235
243
dispatchSimple dataChan message acc = case message of
236
244
NotificationResponse n -> pure acc
237
- -- do nothing on other messages
238
245
_ -> pure acc
239
246
240
247
-- | Dispatcher for the ExtendedQuery mode.
@@ -302,7 +309,6 @@ defaultFilter msg = case msg of
302
309
-- as result for `describe` message
303
310
RowDescription {} -> True
304
311
305
-
306
312
-- Low-level sending functions
307
313
308
314
sendStartMessage :: RawConnection -> StartMessage -> IO ()
0 commit comments