-
Notifications
You must be signed in to change notification settings - Fork 629
[CO-370] Make Content-Type parsing more lenient #3596
Conversation
@akegalj This is currently targetting the throttling branch as I added the middleware on top of your refactor. |
@@ -226,7 +226,7 @@ instance FromJSON a => MimeUnrender ValidJSON a where | |||
Right v -> return v | |||
|
|||
instance Accept ValidJSON where | |||
contentType _ = contentType (Proxy @ JSON) | |||
contentTypes _ = contentTypes (Proxy @ JSON) |
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
and application/json;charset=utf-8
.
Actually, contentType
is defined using the head of what's returned by contentTypes
, which in the case of JSON
is application/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.
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.
LGTM
wallet-new/server/Main.hs
Outdated
@@ -109,6 +111,7 @@ actionWithLegacyWallet coreConfig walletConfig txpConfig sscParams nodeParams nt | |||
mconcat [ LegacyPlugins.conversation wArgs | |||
, LegacyPlugins.legacyWalletBackend coreConfig txpConfig wArgs ntpStatus | |||
[ throttleMiddleware (ccThrottle walletConfig) | |||
, withDefaultHeader (Headers.applicationJson) |
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.
you really like these parens :D
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.
Hahaha, yeah, that's called refactoring 😂 And now I don't have hlint to warn me about those since we disabled the extra-parens rule 😶
dcce5c1
to
992ee9c
Compare
430a0ed
to
74cc084
Compare
4de9471
to
bdf68fc
Compare
Add request throttling feature for new wallet. It is possible now to extend list of Middleware's with other middlewares such as gzip Middleware or others defined in wai-extra. Legacy middlewares are also cleaned up and untangled in the same way and it is possible to list all Middlewares top level in a similar way as we list all Plugins and other workers.
We have had a lot of trouble where people will send no content-type or simply 'application/json' and request would fail with 415 Unsupported Media Type. Without knowing where to look, the error looks quite unexpected and could give a lot of headache. This commit does two changes: - It slightly modifies the 'Accept' instance to use 'contentTypes' instead of 'contentType'. The latter is defined from the former by simply taking the head of the list returned by 'contentTypes', which in our case is 'application/json;charset=utf-8' - It provides a default content-type 'application/json' when no content-type is provided
bdf68fc
to
49b06f0
Compare
Description
We have had a lot of trouble where people will send no content-type or
simply 'application/json' and request would fail with 415 Unsupported
Media Type. Without knowing where to look, the error looks quite
unexpected and could give a lot of headache. This commit does two
changes:
It slightly modifies the 'Accept' instance to use 'contentTypes'
instead of 'contentType'. The latter is defined from the former by
simply taking the head of the list returned by 'contentTypes', which in
our case is 'application/json;charset=utf-8'
It provides a default content-type 'application/json' when no
content-type is provided
TODO
develop
Linked issue
[CO-370]
Type of change
Developer checklist
Testing checklist
QA Steps
With cURL:
Content-Type is "application/json;charset=utf-8"
Content-Type is "application/json"
Content-Type is not defined
Content-Type is "application/x-www-form-urlencoded"
Screenshots (if available)