haskell / hackage-server

Hackage-Server: A Haskell Package Repository
http://hackage.haskell.org
Other
416 stars 198 forks source link

Wrong content type when "Accept: application/json;charset=utf-8" is specified #839

Open abonie opened 5 years ago

abonie commented 5 years ago

Compare responses to

$ curl -I -H "Accept: application/json" http://hackage.haskell.org/users/

and

$ curl -I -H "Accept: application/json;charset=utf-8" http://hackage.haskell.org/users/

First one has Content-Type: application/json; charset=utf-8 as expected, but the second one has Content-Type: text/html; charset=utf-8 (and the response body would also be html in that case).

hvr commented 5 years ago

While I can confirm this is a legit bug, it's been a long-time pet peeve of mine that application/json; charset=utf-8 is not a valid MIME type for the JSON protocol (see also https://stackoverflow.com/questions/4990095/json-specification-and-usage-of-bom-charset-encoding)

abonie commented 5 years ago

That is a fair point, but seems like this unfortunate application/json; charset=utf-8 is popular enough to justify supporting it anyway. For example I noticed this bug while trying to write a simple client using servant-client, and turns out that this is the Accept header it creates by default if you want JSON...

gbaz commented 5 years ago

Indeed we're hilariously inconsistent on this. e.g. we actually generate that content type!

https://github.com/haskell/hackage-server/blob/c812ec1f6dd8fb4d997354c862f92bf2f050c75e/Distribution/Server/Framework/ResponseContentTypes.hs#L167

@abonie if you want to patch it, I think this is all that needs to be toyed with (?): https://github.com/haskell/hackage-server/blob/b4638cd2879dc4300c696a0c96b5e9425a9f66bc/Distribution/Server/Framework/Resource.hs#L535

hvr commented 4 years ago

For example I noticed this bug while trying to write a simple client using servant-client, and turns out that this is the Accept header it creates by default if you want JSON...

Fyi, I don't recall why servan-client would intentionally send such an out-of-spec charset parameter; and in fact I'd rather want to have hackage-server reply with a 406 response to weed out broken clients than to silently fail; but here's what I do to deal with this non-compliant charset parameter being sent by servant-client in other code where I've been using servant-client to speak to Hackage's API

https://github.com/haskell-CI/hackage-matrix-builder/blob/master/src-lib/HackageApi.hs