This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 629
[CO-370] Make Content-Type parsing more lenient #3596
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2bcf426
[CBR-179] Add requst throttling for new wallet layer
akegalj 05a16a3
[CBR-179] Move middlewares to dedicated module
akegalj 52b7e06
[CBR-179] Remove wai-cors dependency
KtorZ ad61962
[CO-370] Make Content-Type parsing more lenient
KtorZ 49b06f0
[CO-370] Update CHANGELOG entry
KtorZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{- | A collection of middlewares used by this edge node. | ||
@Middleware@ is a component that sits between the server and application. | ||
It can do such tasks as GZIP encoding or response caching. | ||
-} | ||
|
||
module Cardano.Wallet.Server.Middlewares | ||
( withMiddlewares | ||
, throttleMiddleware | ||
, withDefaultHeader | ||
) where | ||
|
||
import Universum | ||
|
||
import Data.Aeson (encode) | ||
import qualified Data.List as List | ||
import Network.HTTP.Types.Header (Header) | ||
import Network.HTTP.Types.Method (methodPatch, methodPost, methodPut) | ||
import Network.Wai (Application, Middleware, ifRequest, | ||
requestHeaders, requestMethod, responseLBS) | ||
import qualified Network.Wai.Middleware.Throttle as Throttle | ||
|
||
import Cardano.Wallet.API.V1.Headers (applicationJson) | ||
import qualified Cardano.Wallet.API.V1.Types as V1 | ||
|
||
import Pos.Launcher.Configuration (ThrottleSettings (..)) | ||
|
||
|
||
-- | "Attaches" the middlewares to this 'Application'. | ||
withMiddlewares :: [Middleware] -> Application -> Application | ||
withMiddlewares = flip $ foldr ($) | ||
|
||
-- | Only apply a @Middleware@ to request with bodies (we don't consider | ||
-- "DELETE" as one of them). | ||
ifRequestWithBody :: Middleware -> Middleware | ||
ifRequestWithBody = | ||
ifRequest ((`List.elem` [methodPost, methodPut, methodPatch]) . requestMethod) | ||
|
||
-- | A @Middleware@ to throttle requests. | ||
throttleMiddleware :: Maybe ThrottleSettings -> Middleware | ||
throttleMiddleware Nothing app = app | ||
throttleMiddleware (Just ts) app = \req respond -> do | ||
throttler <- Throttle.initThrottler | ||
Throttle.throttle throttleSettings throttler app req respond | ||
where | ||
throttleSettings = Throttle.defaultThrottleSettings | ||
{ Throttle.onThrottled = \microsTilRetry -> | ||
let | ||
err = V1.RequestThrottled microsTilRetry | ||
in | ||
responseLBS (V1.toHttpErrorStatus err) [applicationJson] (encode err) | ||
, Throttle.throttleRate = fromIntegral $ tsRate ts | ||
, Throttle.throttlePeriod = fromIntegral $ tsPeriod ts | ||
, Throttle.throttleBurst = fromIntegral $ tsBurst ts | ||
} | ||
|
||
-- | A @Middleware@ to default a specific Header when not provided | ||
withDefaultHeader :: Header -> Middleware | ||
withDefaultHeader header = ifRequestWithBody $ \app req send -> | ||
let | ||
headers = | ||
requestHeaders req | ||
|
||
req' = | ||
if any (on (==) fst header) headers then | ||
req | ||
else | ||
req { requestHeaders = header : headers } | ||
in | ||
app req' send |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This simple change already makes the parser accept both
application/json
andapplication/json;charset=utf-8
.Actually,
contentType
is defined using the head of what's returned bycontentTypes
, which in the case ofJSON
isapplication/json;charset=utf-8
!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KtorZ Interesting, good catch! I am wondering if this change, alone, suffice to make the content type lenient or if we do need the extra middleware machinery at all? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This alone covers the requirements 1, 2 and 4 from CO-369
The middleware machinery covers the requirement 3.