Skip to content

Commit afd0011

Browse files
committed
Parse webpush http2 response status
1 parent 8129eb6 commit afd0011

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

  • src/Simplex/Messaging/Notifications/Server/Push

src/Simplex/Messaging/Notifications/Server/Push/WebPush.hs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfRegCode (.
4242
import Simplex.Messaging.Notifications.Server.Push
4343
import Simplex.Messaging.Notifications.Server.Store.Types
4444
import Simplex.Messaging.Util (liftError', safeDecodeUtf8, tshow)
45-
import Simplex.Messaging.Transport.HTTP2.Client (HTTP2Client, getHTTP2Client, defaultHTTP2ClientConfig, HTTP2ClientError, sendRequest)
45+
import Simplex.Messaging.Transport.HTTP2.Client (HTTP2Client, getHTTP2Client, defaultHTTP2ClientConfig, HTTP2ClientError, sendRequest, HTTP2Response (..))
4646
import Network.Socket (ServiceName, HostName)
4747
import System.X509.Unix
48-
import qualified Network.HTTP2.Client as H
48+
import qualified Network.HTTP2.Client as H2
4949
import Data.ByteString.Builder (lazyByteString)
5050
import Simplex.Messaging.Encoding.String (StrEncoding(..))
5151
import Data.Bifunctor (first)
@@ -157,8 +157,8 @@ wpHeaders vapidH = [
157157
-- TODO: topic for pings and interval
158158
]
159159

160-
wpHTTP2Req :: B.ByteString -> [(N.HeaderName, B.ByteString)] -> LB.ByteString -> H.Request
161-
wpHTTP2Req path headers s = H.requestBuilder N.methodPost path headers (lazyByteString s)
160+
wpHTTP2Req :: B.ByteString -> [(N.HeaderName, B.ByteString)] -> LB.ByteString -> H2.Request
161+
wpHTTP2Req path headers s = H2.requestBuilder N.methodPost path headers (lazyByteString s)
162162

163163
wpPushProviderClientH2 :: WebPushClient -> HTTP2Client -> PushProviderClient
164164
wpPushProviderClientH2 _ _ NtfTknRec {token = APNSDeviceToken _ _} _ = throwE PPInvalidPusher
@@ -169,7 +169,11 @@ wpPushProviderClientH2 c@WebPushClient {wpConfig, cache} http2 tkn@NtfTknRec {to
169169
vapidH <- liftError' toPPWPError $ try $ getVapidHeader (vapidKey wpConfig) cache $ wpAud pp
170170
let req = wpHTTP2Req (wpPath params) (wpHeaders vapidH) $ LB.fromStrict encBody
171171
logDebug $ "HTTP/2 Request to " <> tshow (strEncode p)
172-
void $ liftHTTPS2 $ sendRequest http2 req Nothing
172+
HTTP2Response {response} <- liftHTTPS2 $ sendRequest http2 req Nothing
173+
let status = H2.responseStatus response
174+
if status >= Just N.ok200 && status < Just N.status300
175+
then pure ()
176+
else throwError $ fromStatusCode status
173177
where
174178
body :: ExceptT PushProviderError IO B.ByteString
175179
body = withExceptT PPCryptoError $ wpEncrypt c tkn params pn
@@ -249,13 +253,14 @@ wpEncrypt' WPKey {wpAuth, wpP256dh = WPP256dh uaPubK} asPrivK salt clearT = do
249253
toPPWPError :: SomeException -> PushProviderError
250254
toPPWPError e = case fromException e of
251255
Just (InvalidUrlException _ _) -> PPWPInvalidUrl
252-
Just (HttpExceptionRequest _ (StatusCodeException resp _)) -> fromStatusCode (responseStatus resp) ("" :: String)
256+
Just (HttpExceptionRequest _ (StatusCodeException resp _)) -> fromStatusCode (Just $ responseStatus resp)
253257
_ -> PPWPOtherError e
254-
where
255-
fromStatusCode status reason
256-
| status == N.status200 = PPWPRemovedEndpoint
257-
| status == N.status410 = PPWPRemovedEndpoint
258-
| status == N.status413 = PPWPRequestTooLong
259-
| status == N.status429 = PPRetryLater
260-
| status >= N.status500 = PPRetryLater
261-
| otherwise = PPResponseError (Just status) (tshow reason)
258+
259+
fromStatusCode :: Maybe N.Status -> PushProviderError
260+
fromStatusCode status
261+
| status == Just N.status404 = PPWPRemovedEndpoint
262+
| status == Just N.status410 = PPWPRemovedEndpoint
263+
| status == Just N.status413 = PPWPRequestTooLong
264+
| status == Just N.status429 = PPRetryLater
265+
| status >= Just N.status500 = PPRetryLater
266+
| otherwise = PPResponseError status "Invalid response"

0 commit comments

Comments
 (0)