2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.HTTP
|
2021-09-24 01:56:37 +03:00
|
|
|
( wreqOptions,
|
|
|
|
HttpException (..),
|
|
|
|
hdrsToText,
|
|
|
|
addDefaultHeaders,
|
|
|
|
HttpResponse (..),
|
|
|
|
addHttpResponseHeaders,
|
|
|
|
getHTTPExceptionStatus,
|
|
|
|
serializeHTTPExceptionMessage,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Lens hiding ((.=))
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.CaseInsensitive (original)
|
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Conversions (UTF8 (..), convertText)
|
|
|
|
import Data.Text.Encoding qualified as TE
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.Server.Utils (redactSensitiveHeader)
|
|
|
|
import Hasura.Server.Version (HasVersion, currentVersion)
|
|
|
|
import Network.HTTP.Client qualified as HTTP
|
|
|
|
import Network.HTTP.Types qualified as HTTP
|
|
|
|
import Network.Wreq qualified as Wreq
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-03-05 15:24:47 +03:00
|
|
|
hdrsToText :: [HTTP.Header] -> [(Text, Text)]
|
|
|
|
hdrsToText hdrs =
|
|
|
|
[ (bsToTxt $ original hdrName, bsToTxt hdrVal)
|
2021-09-24 01:56:37 +03:00
|
|
|
| (hdrName, hdrVal) <- hdrs
|
2019-03-05 15:24:47 +03:00
|
|
|
]
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
wreqOptions :: HasVersion => HTTP.Manager -> [HTTP.Header] -> Wreq.Options
|
2018-11-23 16:02:46 +03:00
|
|
|
wreqOptions manager hdrs =
|
|
|
|
Wreq.defaults
|
2021-09-24 01:56:37 +03:00
|
|
|
& Wreq.headers .~ addDefaultHeaders hdrs
|
|
|
|
& Wreq.checkResponse ?~ (\_ _ -> return ())
|
|
|
|
& Wreq.manager .~ Right manager
|
2019-08-23 11:57:19 +03:00
|
|
|
|
|
|
|
-- Adds defaults headers overwriting any existing ones
|
2020-01-23 00:55:55 +03:00
|
|
|
addDefaultHeaders :: HasVersion => [HTTP.Header] -> [HTTP.Header]
|
2019-08-23 11:57:19 +03:00
|
|
|
addDefaultHeaders hdrs = defaultHeaders <> rmDefaultHeaders hdrs
|
|
|
|
where
|
|
|
|
rmDefaultHeaders = filter (not . isDefaultHeader)
|
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
isDefaultHeader :: HasVersion => HTTP.Header -> Bool
|
2020-04-24 10:55:51 +03:00
|
|
|
isDefaultHeader (hdrName, _) = hdrName `elem` map fst defaultHeaders
|
2019-08-23 11:57:19 +03:00
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
defaultHeaders :: HasVersion => [HTTP.Header]
|
2019-08-23 11:57:19 +03:00
|
|
|
defaultHeaders = [contentType, userAgent]
|
2018-11-23 16:02:46 +03:00
|
|
|
where
|
|
|
|
contentType = ("Content-Type", "application/json")
|
2021-09-24 01:56:37 +03:00
|
|
|
userAgent =
|
|
|
|
( "User-Agent",
|
|
|
|
"hasura-graphql-engine/" <> unUTF8 (convertText currentVersion)
|
|
|
|
)
|
2018-12-13 10:26:15 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype HttpException = HttpException
|
|
|
|
{unHttpException :: HTTP.HttpException}
|
2018-12-13 10:26:15 +03:00
|
|
|
deriving (Show)
|
|
|
|
|
2021-09-20 16:14:28 +03:00
|
|
|
getHTTPExceptionStatus :: HttpException -> Maybe Int
|
|
|
|
getHTTPExceptionStatus = \case
|
|
|
|
(HttpException (HTTP.HttpExceptionRequest _ httpExceptionContent)) ->
|
|
|
|
case httpExceptionContent of
|
2021-09-24 01:56:37 +03:00
|
|
|
HTTP.StatusCodeException response _ -> Just $ HTTP.statusCode $ HTTP.responseStatus response
|
2021-09-20 16:14:28 +03:00
|
|
|
HTTP.ProxyConnectException _ _ status -> Just $ HTTP.statusCode status
|
2021-09-24 01:56:37 +03:00
|
|
|
_ -> Nothing
|
2021-09-20 16:14:28 +03:00
|
|
|
(HttpException (HTTP.InvalidUrlException _ _)) -> Nothing
|
|
|
|
|
|
|
|
serializeHTTPExceptionMessage :: HttpException -> Text
|
|
|
|
serializeHTTPExceptionMessage (HttpException (HTTP.HttpExceptionRequest _ httpExceptionContent)) =
|
|
|
|
case httpExceptionContent of
|
2021-09-24 01:56:37 +03:00
|
|
|
HTTP.StatusCodeException _ _ -> "unexpected"
|
|
|
|
HTTP.TooManyRedirects _ -> "Too many redirects"
|
|
|
|
HTTP.OverlongHeaders -> "Overlong headers"
|
|
|
|
HTTP.ResponseTimeout -> "Response timeout"
|
|
|
|
HTTP.ConnectionTimeout -> "Connection timeout"
|
|
|
|
HTTP.ConnectionFailure _ -> "Connection failure"
|
|
|
|
HTTP.InvalidStatusLine _ -> "Invalid HTTP Status Line"
|
|
|
|
HTTP.InternalException _ -> "Internal Exception"
|
|
|
|
HTTP.ProxyConnectException _ _ _ -> "Proxy connection exception"
|
|
|
|
HTTP.NoResponseDataReceived -> "No response data received"
|
|
|
|
HTTP.TlsNotSupported -> "TLS not supported"
|
|
|
|
HTTP.InvalidDestinationHost _ -> "Invalid destination host"
|
|
|
|
HTTP.InvalidHeader _ -> "Invalid Header"
|
|
|
|
HTTP.InvalidRequestHeader _ -> "Invalid Request Header"
|
|
|
|
HTTP.WrongRequestBodyStreamSize _ _ -> "Wrong request body stream size"
|
|
|
|
HTTP.ResponseBodyTooShort _ _ -> "Response body too short"
|
|
|
|
HTTP.InvalidChunkHeaders -> "Invalid chunk headers"
|
|
|
|
HTTP.IncompleteHeaders -> "Incomplete headers"
|
|
|
|
_ -> "unexpected"
|
|
|
|
serializeHTTPExceptionMessage (HttpException (HTTP.InvalidUrlException url reason)) = T.pack $ "URL: " <> url <> " is invalid because " <> reason
|
2021-09-20 16:14:28 +03:00
|
|
|
|
|
|
|
encodeHTTPRequestJSON :: HTTP.Request -> J.Value
|
|
|
|
encodeHTTPRequestJSON request =
|
2021-09-24 01:56:37 +03:00
|
|
|
J.Object $
|
|
|
|
M.fromList
|
|
|
|
[ ("host", J.toJSON $ TE.decodeUtf8 $ HTTP.host request),
|
|
|
|
("port", J.toJSON $ HTTP.port request),
|
|
|
|
("secure", J.toJSON $ HTTP.secure request),
|
|
|
|
("requestHeaders", J.toJSON $ M.fromList $ hdrsToText $ map redactSensitiveHeader $ HTTP.requestHeaders request),
|
|
|
|
("path", J.toJSON $ TE.decodeUtf8 $ HTTP.path request),
|
|
|
|
("queryString", J.toJSON $ TE.decodeUtf8 $ HTTP.queryString request),
|
|
|
|
("method", J.toJSON $ TE.decodeUtf8 $ HTTP.method request),
|
|
|
|
("responseTimeout", J.String $ tshow $ HTTP.responseTimeout request)
|
|
|
|
]
|
2021-09-20 16:14:28 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
instance J.ToJSON HttpException where
|
2021-09-20 16:14:28 +03:00
|
|
|
toJSON httpException =
|
|
|
|
case httpException of
|
|
|
|
(HttpException (HTTP.InvalidUrlException _ e)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
J.object
|
|
|
|
[ "type" J..= ("invalid_url" :: Text),
|
|
|
|
"message" J..= e
|
|
|
|
]
|
2021-09-20 16:14:28 +03:00
|
|
|
(HttpException (HTTP.HttpExceptionRequest req _)) ->
|
|
|
|
let statusMaybe = getHTTPExceptionStatus httpException
|
|
|
|
exceptionContent = serializeHTTPExceptionMessage httpException
|
|
|
|
reqJSON = encodeHTTPRequestJSON req
|
2021-09-24 01:56:37 +03:00
|
|
|
in J.object $
|
|
|
|
[ "type" J..= ("http_exception" :: Text),
|
|
|
|
"message" J..= exceptionContent,
|
|
|
|
"request" J..= reqJSON
|
|
|
|
]
|
|
|
|
<> maybe mempty (\status -> ["status" J..= status]) statusMaybe
|
|
|
|
|
|
|
|
data HttpResponse a = HttpResponse
|
|
|
|
{ _hrBody :: !a,
|
|
|
|
_hrHeaders :: !HTTP.ResponseHeaders
|
|
|
|
}
|
|
|
|
deriving (Functor, Foldable, Traversable)
|
2021-08-25 04:52:38 +03:00
|
|
|
|
|
|
|
addHttpResponseHeaders :: HTTP.ResponseHeaders -> HttpResponse a -> HttpResponse a
|
|
|
|
addHttpResponseHeaders newHeaders (HttpResponse b h) = HttpResponse b (newHeaders <> h)
|