2020-05-13 15:33:16 +03:00
|
|
|
{-|
|
|
|
|
= Hasura.Eventing.HTTP
|
|
|
|
|
|
|
|
This module is an utility module providing HTTP utilities for
|
|
|
|
"Hasura.Eventing.EventTriggers" and "Hasura.Eventing.ScheduledTriggers".
|
|
|
|
|
|
|
|
The event triggers and scheduled triggers share the event delivery
|
|
|
|
mechanism using the 'tryWebhook' function defined in this module.
|
|
|
|
-}
|
|
|
|
module Hasura.Eventing.HTTP
|
|
|
|
( HTTPErr(..)
|
|
|
|
, HTTPResp(..)
|
|
|
|
, tryWebhook
|
|
|
|
, runHTTP
|
|
|
|
, isNetworkError
|
|
|
|
, isNetworkErrorHC
|
|
|
|
, logHTTPForET
|
|
|
|
, logHTTPForST
|
|
|
|
, ExtraLogContext(..)
|
2020-07-28 20:52:44 +03:00
|
|
|
, RequestDetails (..)
|
2020-05-13 15:33:16 +03:00
|
|
|
, EventId
|
|
|
|
, Invocation(..)
|
|
|
|
, InvocationVersion
|
|
|
|
, Response(..)
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
, ResponseLogBehavior(..)
|
2020-05-13 15:33:16 +03:00
|
|
|
, WebhookRequest(..)
|
|
|
|
, WebhookResponse(..)
|
|
|
|
, ClientError(..)
|
|
|
|
, isClientError
|
|
|
|
, mkClientErr
|
|
|
|
, mkWebhookReq
|
|
|
|
, mkResp
|
|
|
|
, LogEnvHeaders
|
|
|
|
, encodeHeader
|
|
|
|
, decodeHeader
|
|
|
|
, getRetryAfterHeaderFromHTTPErr
|
|
|
|
, getRetryAfterHeaderFromResp
|
|
|
|
, parseRetryHeaderValue
|
|
|
|
, TriggerTypes(..)
|
|
|
|
, invocationVersionET
|
|
|
|
, invocationVersionST
|
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.ByteString.Lazy as LBS
|
|
|
|
import qualified Data.CaseInsensitive as CI
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
import qualified Data.HashMap.Lazy as HML
|
2020-05-13 15:33:16 +03:00
|
|
|
import qualified Data.TByteString as TBS
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as TE
|
|
|
|
import qualified Data.Text.Encoding.Error as TE
|
2020-07-28 20:52:44 +03:00
|
|
|
import qualified Data.Time.Clock as Time
|
2020-05-13 15:33:16 +03:00
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
|
|
|
|
|
|
|
import Control.Exception (try)
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Aeson.TH
|
|
|
|
import Data.Either
|
|
|
|
import Data.Has
|
2020-07-28 20:52:44 +03:00
|
|
|
import Data.Int (Int64)
|
2020-05-13 15:33:16 +03:00
|
|
|
import Hasura.Logging
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Headers
|
|
|
|
import Hasura.RQL.Types.EventTrigger
|
2020-07-15 13:40:48 +03:00
|
|
|
import Hasura.Tracing
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
type LogEnvHeaders = Bool
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
retryAfterHeader :: CI.CI Text
|
2020-05-13 15:33:16 +03:00
|
|
|
retryAfterHeader = "Retry-After"
|
|
|
|
|
|
|
|
data WebhookRequest
|
|
|
|
= WebhookRequest
|
|
|
|
{ _rqPayload :: Value
|
|
|
|
, _rqHeaders :: [HeaderConf]
|
2020-10-27 16:53:49 +03:00
|
|
|
, _rqVersion :: Text
|
2020-05-13 15:33:16 +03:00
|
|
|
}
|
2021-01-19 22:14:42 +03:00
|
|
|
$(deriveToJSON hasuraJSON{omitNothingFields=True} ''WebhookRequest)
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
data WebhookResponse
|
|
|
|
= WebhookResponse
|
|
|
|
{ _wrsBody :: TBS.TByteString
|
|
|
|
, _wrsHeaders :: [HeaderConf]
|
|
|
|
, _wrsStatus :: Int
|
|
|
|
}
|
2021-01-19 22:14:42 +03:00
|
|
|
$(deriveToJSON hasuraJSON{omitNothingFields=True} ''WebhookResponse)
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
newtype ClientError = ClientError { _ceMessage :: TBS.TByteString}
|
2021-01-19 22:14:42 +03:00
|
|
|
$(deriveToJSON hasuraJSON{omitNothingFields=True} ''ClientError)
|
2020-05-13 15:33:16 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
type InvocationVersion = Text
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
invocationVersionET :: InvocationVersion
|
|
|
|
invocationVersionET = "2"
|
|
|
|
|
|
|
|
invocationVersionST :: InvocationVersion
|
|
|
|
invocationVersionST = "1"
|
|
|
|
|
|
|
|
-- | There are two types of events: EventType (for event triggers) and ScheduledType (for scheduled triggers)
|
|
|
|
data TriggerTypes = EventType | ScheduledType
|
|
|
|
|
|
|
|
data Response (a :: TriggerTypes) =
|
|
|
|
ResponseHTTP WebhookResponse | ResponseError ClientError
|
|
|
|
|
|
|
|
instance ToJSON (Response 'EventType) where
|
|
|
|
toJSON (ResponseHTTP resp) = object
|
|
|
|
[ "type" .= String "webhook_response"
|
|
|
|
, "data" .= toJSON resp
|
|
|
|
, "version" .= invocationVersionET
|
|
|
|
]
|
|
|
|
toJSON (ResponseError err) = object
|
|
|
|
[ "type" .= String "client_error"
|
|
|
|
, "data" .= toJSON err
|
|
|
|
, "version" .= invocationVersionET
|
|
|
|
]
|
|
|
|
|
|
|
|
instance ToJSON (Response 'ScheduledType) where
|
|
|
|
toJSON (ResponseHTTP resp) = object
|
|
|
|
[ "type" .= String "webhook_response"
|
|
|
|
, "data" .= toJSON resp
|
|
|
|
, "version" .= invocationVersionST
|
|
|
|
]
|
|
|
|
toJSON (ResponseError err) = object
|
|
|
|
[ "type" .= String "client_error"
|
|
|
|
, "data" .= toJSON err
|
|
|
|
, "version" .= invocationVersionST
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
data Invocation (a :: TriggerTypes)
|
|
|
|
= Invocation
|
|
|
|
{ iEventId :: EventId
|
|
|
|
, iStatus :: Int
|
|
|
|
, iRequest :: WebhookRequest
|
2020-10-28 19:40:33 +03:00
|
|
|
, iResponse :: Response a
|
2020-05-13 15:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data ExtraLogContext
|
|
|
|
= ExtraLogContext
|
|
|
|
{ elEventCreatedAt :: Maybe Time.UTCTime
|
|
|
|
, elEventId :: EventId
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
data HTTPResp (a :: TriggerTypes)
|
|
|
|
= HTTPResp
|
|
|
|
{ hrsStatus :: !Int
|
|
|
|
, hrsHeaders :: ![HeaderConf]
|
|
|
|
, hrsBody :: !TBS.TByteString
|
2020-07-28 20:52:44 +03:00
|
|
|
, hrsSize :: !Int64
|
2020-05-13 15:33:16 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2021-01-19 22:14:42 +03:00
|
|
|
$(deriveToJSON hasuraJSON{omitNothingFields=True} ''HTTPResp)
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
instance ToEngineLog (HTTPResp 'EventType) Hasura where
|
|
|
|
toEngineLog resp = (LevelInfo, eventTriggerLogType, toJSON resp)
|
|
|
|
|
|
|
|
instance ToEngineLog (HTTPResp 'ScheduledType) Hasura where
|
|
|
|
toEngineLog resp = (LevelInfo, scheduledTriggerLogType, toJSON resp)
|
|
|
|
|
|
|
|
data HTTPErr (a :: TriggerTypes)
|
|
|
|
= HClient !HTTP.HttpException
|
|
|
|
| HParse !HTTP.Status !String
|
|
|
|
| HStatus !(HTTPResp a)
|
|
|
|
| HOther !String
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance ToJSON (HTTPErr a) where
|
|
|
|
toJSON err = toObj $ case err of
|
|
|
|
(HClient e) -> ("client", toJSON $ show e)
|
|
|
|
(HParse st e) ->
|
|
|
|
( "parse"
|
|
|
|
, toJSON (HTTP.statusCode st, show e)
|
|
|
|
)
|
|
|
|
(HStatus resp) ->
|
|
|
|
("status", toJSON resp)
|
|
|
|
(HOther e) -> ("internal", toJSON $ show e)
|
|
|
|
where
|
2020-10-27 16:53:49 +03:00
|
|
|
toObj :: (Text, Value) -> Value
|
2020-05-13 15:33:16 +03:00
|
|
|
toObj (k, v) = object [ "type" .= k
|
|
|
|
, "detail" .= v]
|
|
|
|
|
|
|
|
instance ToEngineLog (HTTPErr 'EventType) Hasura where
|
|
|
|
toEngineLog err = (LevelError, eventTriggerLogType, toJSON err)
|
|
|
|
|
|
|
|
instance ToEngineLog (HTTPErr 'ScheduledType) Hasura where
|
|
|
|
toEngineLog err = (LevelError, scheduledTriggerLogType, toJSON err)
|
|
|
|
|
|
|
|
mkHTTPResp :: HTTP.Response LBS.ByteString -> HTTPResp a
|
|
|
|
mkHTTPResp resp =
|
|
|
|
HTTPResp
|
|
|
|
{ hrsStatus = HTTP.statusCode $ HTTP.responseStatus resp
|
|
|
|
, hrsHeaders = map decodeHeader $ HTTP.responseHeaders resp
|
2020-07-28 20:52:44 +03:00
|
|
|
, hrsBody = TBS.fromLBS respBody
|
|
|
|
, hrsSize = LBS.length respBody
|
2020-05-13 15:33:16 +03:00
|
|
|
}
|
|
|
|
where
|
2020-07-28 20:52:44 +03:00
|
|
|
respBody = HTTP.responseBody resp
|
2020-05-13 15:33:16 +03:00
|
|
|
decodeBS = TE.decodeUtf8With TE.lenientDecode
|
|
|
|
decodeHeader (hdrName, hdrVal)
|
|
|
|
= HeaderConf (decodeBS $ CI.original hdrName) (HVValue (decodeBS hdrVal))
|
|
|
|
|
2020-07-28 20:52:44 +03:00
|
|
|
newtype RequestDetails
|
|
|
|
= RequestDetails { _rdSize :: Int64 }
|
2021-01-19 22:14:42 +03:00
|
|
|
$(deriveToJSON hasuraJSON ''RequestDetails)
|
2020-07-28 20:52:44 +03:00
|
|
|
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
-- TODO(swann): move elsewhere? it could be useful more generally
|
|
|
|
data ResponseLogBehavior = LogSanitisedResponse | LogEntireResponse
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2020-05-13 15:33:16 +03:00
|
|
|
data HTTPRespExtra (a :: TriggerTypes)
|
|
|
|
= HTTPRespExtra
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
{ _hreResponse :: !(Either (HTTPErr a) (HTTPResp a))
|
|
|
|
, _hreContext :: !ExtraLogContext
|
|
|
|
, _hreRequest :: !RequestDetails
|
|
|
|
, _hreLogResponse :: !ResponseLogBehavior
|
|
|
|
-- ^ Whether to log the entire response, including the body and the headers,
|
|
|
|
-- which may contain sensitive information.
|
2020-05-13 15:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
instance ToJSON (HTTPRespExtra a) where
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
toJSON (HTTPRespExtra resp ctxt req logResp) =
|
2020-05-13 15:33:16 +03:00
|
|
|
case resp of
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
Left errResp -> object
|
|
|
|
[ "response" .= toJSON errResp
|
|
|
|
, "request" .= toJSON req
|
|
|
|
, "event_id" .= elEventId ctxt
|
|
|
|
]
|
|
|
|
Right okResp -> object
|
|
|
|
[ "response" .= case logResp of
|
|
|
|
LogEntireResponse -> toJSON okResp
|
|
|
|
LogSanitisedResponse -> sanitisedRespJSON okResp
|
|
|
|
, "request" .= toJSON req
|
|
|
|
, "event_id" .= elEventId ctxt
|
|
|
|
]
|
|
|
|
where
|
|
|
|
sanitisedRespJSON v
|
|
|
|
= Object $ HML.fromList
|
|
|
|
[ "size" .= hrsSize v
|
|
|
|
, "status" .= hrsStatus v
|
|
|
|
]
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
instance ToEngineLog (HTTPRespExtra 'EventType) Hasura where
|
|
|
|
toEngineLog resp = (LevelInfo, eventTriggerLogType, toJSON resp)
|
|
|
|
|
|
|
|
instance ToEngineLog (HTTPRespExtra 'ScheduledType) Hasura where
|
|
|
|
toEngineLog resp = (LevelInfo, scheduledTriggerLogType, toJSON resp)
|
|
|
|
|
|
|
|
isNetworkError :: HTTPErr a -> Bool
|
|
|
|
isNetworkError = \case
|
|
|
|
HClient he -> isNetworkErrorHC he
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
isNetworkErrorHC :: HTTP.HttpException -> Bool
|
|
|
|
isNetworkErrorHC = \case
|
|
|
|
HTTP.HttpExceptionRequest _ (HTTP.ConnectionFailure _) -> True
|
2021-01-19 22:14:42 +03:00
|
|
|
HTTP.HttpExceptionRequest _ HTTP.ConnectionTimeout -> True
|
|
|
|
HTTP.HttpExceptionRequest _ HTTP.ResponseTimeout -> True
|
|
|
|
_ -> False
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
anyBodyParser :: HTTP.Response LBS.ByteString -> Either (HTTPErr a) (HTTPResp a)
|
|
|
|
anyBodyParser resp = do
|
|
|
|
let httpResp = mkHTTPResp resp
|
|
|
|
if respCode >= HTTP.status200 && respCode < HTTP.status300
|
|
|
|
then return httpResp
|
|
|
|
else throwError $ HStatus httpResp
|
|
|
|
where
|
|
|
|
respCode = HTTP.responseStatus resp
|
|
|
|
|
|
|
|
data HTTPReq
|
|
|
|
= HTTPReq
|
|
|
|
{ _hrqMethod :: !String
|
|
|
|
, _hrqUrl :: !String
|
|
|
|
, _hrqPayload :: !(Maybe Value)
|
|
|
|
, _hrqTry :: !Int
|
|
|
|
, _hrqDelay :: !(Maybe Int)
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2021-01-19 22:14:42 +03:00
|
|
|
$(deriveJSON hasuraJSON{omitNothingFields=True} ''HTTPReq)
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
instance ToEngineLog HTTPReq Hasura where
|
|
|
|
toEngineLog req = (LevelInfo, eventTriggerLogType, toJSON req)
|
|
|
|
|
|
|
|
logHTTPForET
|
|
|
|
:: ( MonadReader r m
|
|
|
|
, Has (Logger Hasura) r
|
|
|
|
, MonadIO m
|
|
|
|
)
|
2020-07-28 20:52:44 +03:00
|
|
|
=> Either (HTTPErr 'EventType) (HTTPResp 'EventType)
|
|
|
|
-> ExtraLogContext
|
|
|
|
-> RequestDetails
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
-> ResponseLogBehavior
|
2020-07-28 20:52:44 +03:00
|
|
|
-> m ()
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
logHTTPForET eitherResp extraLogCtx reqDetails logResp = do
|
2020-05-13 15:33:16 +03:00
|
|
|
logger :: Logger Hasura <- asks getter
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
unLogger logger $ HTTPRespExtra eitherResp extraLogCtx reqDetails logResp
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
logHTTPForST
|
|
|
|
:: ( MonadReader r m
|
|
|
|
, Has (Logger Hasura) r
|
|
|
|
, MonadIO m
|
|
|
|
)
|
2020-07-28 20:52:44 +03:00
|
|
|
=> Either (HTTPErr 'ScheduledType) (HTTPResp 'ScheduledType)
|
|
|
|
-> ExtraLogContext
|
|
|
|
-> RequestDetails
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
-> ResponseLogBehavior
|
2020-07-28 20:52:44 +03:00
|
|
|
-> m ()
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
logHTTPForST eitherResp extraLogCtx reqDetails logResp = do
|
2020-05-13 15:33:16 +03:00
|
|
|
logger :: Logger Hasura <- asks getter
|
server: sanitise event trigger logs (fix #1233)
- sanitise the response field in event trigger and scheduled trigger logs, removing the body and the headers
- flatten the log structure to include the event id at `$.detail.event_id` instead of `$.detail.context.event_id`
the log format changes as follows:
```diff
diff --git a/log b/log
index 96127e0..1fb909a 100644
--- a/log
+++ b/log
@@ -1,48 +1,15 @@
{
"detail": {
- "context": {
- "event_id": "b9d4e627-6029-43f2-9d46-31c532b07070"
- },
+ "event_id": "adcc8dcd-2f32-4554-bd55-90c787aee137",
"request": {
"size": 416
},
"response": {
- "body": "{\n \"args\": {}, \n \"data\": \"{\\\"event\\\":{\\\"session_variables\\\":{\\\"x-hasura-role\\\":\\\"admin\\\"},\\\"op\\\":\\\"INSERT\\\",\\\"data\\\":{\\\"old\\\":null,\\\"new\\\":{\\\"name\\\":\\\"someotheranimal\\\",\\\"id\\\":3}},\\\"trace_context\\\":{\\\"trace_id\\\":\\\"e8237c03de151634\\\",\\\"span_id\\\":\\\"8c5f8952e9e06da8\\\"}},\\\"created_at\\\":\\\"2021-05-06T07:52:58.796611Z\\\",\\\"id>
- "headers": [
- {
- "name": "Date",
- "value": "Thu, 06 May 2021 07:53:00 GMT"
- },
- {
- "name": "Content-Type",
- "value": "application/json"
- },
- {
- "name": "Content-Length",
- "value": "1692"
- },
- {
- "name": "Connection",
- "value": "keep-alive"
- },
- {
- "name": "Server",
- "value": "gunicorn/19.9.0"
- },
- {
- "name": "Access-Control-Allow-Origin",
- "value": "*"
- },
- {
- "name": "Access-Control-Allow-Credentials",
- "value": "true"
- }
- ],
- "size": 1692,
+ "size": 1719,
"status": 200
}
},
"level": "info",
- "timestamp": "2021-05-06T13:23:00.376+0530",
+ "timestamp": "2021-05-06T13:25:14.481+0530",
"type": "event-trigger"
}
```
GitOrigin-RevId: d9622de366737da04dc6d9ff73238be16ec9305e
2021-05-12 15:09:51 +03:00
|
|
|
unLogger logger $ HTTPRespExtra eitherResp extraLogCtx reqDetails logResp
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
runHTTP :: (MonadIO m) => HTTP.Manager -> HTTP.Request -> m (Either (HTTPErr a) (HTTPResp a))
|
|
|
|
runHTTP manager req = do
|
|
|
|
res <- liftIO $ try $ HTTP.httpLbs req manager
|
|
|
|
return $ either (Left . HClient) anyBodyParser res
|
|
|
|
|
|
|
|
tryWebhook ::
|
|
|
|
( MonadReader r m
|
|
|
|
, Has HTTP.Manager r
|
|
|
|
, MonadIO m
|
|
|
|
, MonadError (HTTPErr a) m
|
2020-07-15 13:40:48 +03:00
|
|
|
, MonadTrace m
|
2020-05-13 15:33:16 +03:00
|
|
|
)
|
|
|
|
=> [HTTP.Header]
|
|
|
|
-> HTTP.ResponseTimeout
|
2020-07-28 20:52:44 +03:00
|
|
|
-> LBS.ByteString
|
|
|
|
-- ^ the request body. It is passed as a 'BL.Bytestring' because we need to
|
|
|
|
-- log the request size. As the logging happens outside the function, we pass
|
|
|
|
-- it the final request body, instead of 'Value'
|
2020-05-13 15:33:16 +03:00
|
|
|
-> String
|
|
|
|
-> m (HTTPResp a)
|
2020-07-28 21:51:56 +03:00
|
|
|
tryWebhook headers timeout payload webhook = do
|
2020-05-13 15:33:16 +03:00
|
|
|
initReqE <- liftIO $ try $ HTTP.parseRequest webhook
|
|
|
|
manager <- asks getter
|
|
|
|
case initReqE of
|
|
|
|
Left excp -> throwError $ HClient excp
|
|
|
|
Right initReq -> do
|
|
|
|
let req =
|
|
|
|
initReq
|
|
|
|
{ HTTP.method = "POST"
|
|
|
|
, HTTP.requestHeaders = headers
|
2020-07-28 20:52:44 +03:00
|
|
|
, HTTP.requestBody = HTTP.RequestBodyLBS payload
|
2020-05-13 15:33:16 +03:00
|
|
|
, HTTP.responseTimeout = timeout
|
|
|
|
}
|
2020-07-28 21:51:56 +03:00
|
|
|
tracedHttpRequest req $ \req' -> do
|
2020-07-15 13:40:48 +03:00
|
|
|
eitherResp <- runHTTP manager req'
|
|
|
|
onLeft eitherResp throwError
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
mkResp :: Int -> TBS.TByteString -> [HeaderConf] -> Response a
|
|
|
|
mkResp status payload headers =
|
|
|
|
let wr = WebhookResponse payload headers status
|
|
|
|
in ResponseHTTP wr
|
|
|
|
|
|
|
|
mkClientErr :: TBS.TByteString -> Response a
|
|
|
|
mkClientErr message =
|
|
|
|
let cerr = ClientError message
|
|
|
|
in ResponseError cerr
|
|
|
|
|
|
|
|
mkWebhookReq :: Value -> [HeaderConf] -> InvocationVersion -> WebhookRequest
|
|
|
|
mkWebhookReq payload headers = WebhookRequest payload headers
|
|
|
|
|
|
|
|
isClientError :: Int -> Bool
|
|
|
|
isClientError status = status >= 1000
|
|
|
|
|
|
|
|
encodeHeader :: EventHeaderInfo -> HTTP.Header
|
|
|
|
encodeHeader (EventHeaderInfo hconf cache) =
|
|
|
|
let (HeaderConf name _) = hconf
|
|
|
|
ciname = CI.mk $ TE.encodeUtf8 name
|
|
|
|
value = TE.encodeUtf8 cache
|
|
|
|
in (ciname, value)
|
|
|
|
|
|
|
|
decodeHeader
|
|
|
|
:: LogEnvHeaders -> [EventHeaderInfo] -> (HTTP.HeaderName, BS.ByteString)
|
|
|
|
-> HeaderConf
|
|
|
|
decodeHeader logenv headerInfos (hdrName, hdrVal)
|
|
|
|
= let name = decodeBS $ CI.original hdrName
|
|
|
|
getName ehi = let (HeaderConf name' _) = ehiHeaderConf ehi
|
|
|
|
in name'
|
|
|
|
mehi = find (\hi -> getName hi == name) headerInfos
|
|
|
|
in case mehi of
|
|
|
|
Nothing -> HeaderConf name (HVValue (decodeBS hdrVal))
|
|
|
|
Just ehi -> if logenv
|
|
|
|
then HeaderConf name (HVValue (ehiCachedValue ehi))
|
|
|
|
else ehiHeaderConf ehi
|
|
|
|
where
|
|
|
|
decodeBS = TE.decodeUtf8With TE.lenientDecode
|
|
|
|
|
|
|
|
getRetryAfterHeaderFromHTTPErr :: HTTPErr a -> Maybe Text
|
|
|
|
getRetryAfterHeaderFromHTTPErr (HStatus resp) = getRetryAfterHeaderFromResp resp
|
|
|
|
getRetryAfterHeaderFromHTTPErr _ = Nothing
|
|
|
|
|
|
|
|
getRetryAfterHeaderFromResp :: HTTPResp a -> Maybe Text
|
|
|
|
getRetryAfterHeaderFromResp resp =
|
|
|
|
let mHeader =
|
|
|
|
find
|
|
|
|
(\(HeaderConf name _) -> CI.mk name == retryAfterHeader)
|
|
|
|
(hrsHeaders resp)
|
|
|
|
in case mHeader of
|
|
|
|
Just (HeaderConf _ (HVValue value)) -> Just value
|
|
|
|
_ -> Nothing
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
parseRetryHeaderValue :: Text -> Maybe Int
|
2020-05-13 15:33:16 +03:00
|
|
|
parseRetryHeaderValue hValue =
|
|
|
|
let seconds = readMaybe $ T.unpack hValue
|
|
|
|
in case seconds of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just sec ->
|
|
|
|
if sec > 0
|
|
|
|
then Just sec
|
|
|
|
else Nothing
|