8000 Parsing connection parameters · postgres-haskell/postgres-wire@99891d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99891d1

Browse files
Parsing connection parameters
1 parent 8d2a0eb commit 99891d1

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/Database/PostgreSQL/Driver/Connection.hs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ authorize rawConn settings = do
139139
throwAuthErrorInIO $ AuthNotSupported "GSS"
140140
AuthErrorResponse desc ->
141141
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
145146

146147
performPasswordAuth password = do
147148
sendMessage rawConn $ PasswordMessage password
@@ -153,26 +154,34 @@ authorize rawConn settings = do
153154
(settingsPassword settings <> settingsUser settings) <> salt)
154155
md5Hash bs = BS.pack $ show (hash bs :: Digest MD5)
155156

156-
-- TODO right parsing
157157
-- | Parses connection parameters.
158158
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+
}
166169
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
167175
decoder = runGetIncremental decodeServerMessage
168-
go str dict | B.null str = dict
176+
go str dict | B.null str = Right dict
169177
| otherwise = case pushChunk decoder str of
170178
BG.Done rest _ v -> case v of
171179
ParameterStatus name value -> go rest $ HM.insert name value dict
180+
-- messages like `BackendData` not handled
172181
_ -> 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
176185

177186
parseServerVersion :: B.ByteString -> Either Error ServerVersion
178187
parseServerVersion bs =
@@ -181,7 +190,7 @@ parseServerVersion bs =
181190
in case numbers ++ repeat (Just 0) of
182191
(Just major : Just minor : Just rev : _) ->
183192
Right $ ServerVersion major minor rev desc
184-
_ -> Left $ undefined
193+
_ -> Left $ DecodeError "parse server version"
185194
where
186195
isDigitDot c | c == 46 = True -- dot
187196
| c >= 48 && c < 58 = True -- digits
@@ -215,7 +224,6 @@ receiverThread msgFilter rawConn dataChan allChan modeRef = receiveLoop []
215224
go :: B.ByteString -> [V.Vector B.ByteString] -> IO [V.Vector B.ByteString]
216225
go str acc = case pushChunk decoder str of
217226
BG.Done rest _ v -> do
218-
-- putStrLn $ "Received: " ++ show v
219227
when (msgFilter v) $ writeChan allChan v
220228
mode <- readIORef modeRef
221229
newAcc <- dispatch mode dataChan v acc
@@ -224,7 +232,7 @@ receiverThread msgFilter rawConn dataChan allChan modeRef = receiveLoop []
224232
else go rest newAcc
225233
-- TODO right parsing
226234
BG.Partial _ -> error "Partial"
227-
BG.Fail _ _ e -> error e
235+
BG.Fail _ _ reason -> error reason
228236

229237
dispatch :: Connect 8000 ionMode -> Dispatcher
230238
dispatch SimpleQueryMode = dispatchSimple
@@ -234,7 +242,6 @@ dispatch ExtendedQueryMode = dispatchExtended
234242
dispatchSimple :: Dispatcher
235243
dispatchSimple dataChan message acc = case message of
236244
NotificationResponse n -> pure acc
237-
-- do nothing on other messages
238245
_ -> pure acc
239246

240247
-- | Dispatcher for the ExtendedQuery mode.
@@ -302,7 +309,6 @@ defaultFilter msg = case msg of
302309
-- as result for `describe` message
303310
RowDescription{} -> True
304311

305-
306312
-- Low-level sending functions
307313

308314
sendStartMessage :: RawConnection -> StartMessage -> IO ()

src/Database/PostgreSQL/Driver/Error.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Database.PostgreSQL.Protocol.Types (ErrorDesc)
77
-- All possible errors.
88
data Error
99
= PostgresError ErrorDesc
10+
| DecodeError ByteString
1011
| AuthError AuthError
1112
| ImpossibleError ByteString
1213
deriving (Show)

0 commit comments

Comments
 (0)
0