heroiclabs / nakama-cpp

Generic C/C++ client for Nakama server.
https://heroiclabs.com/docs/cpp-client-guide
Apache License 2.0
69 stars 25 forks source link

Malformed `Content-Type` header breaks with Nakama 2.7.x + #21

Closed leoncarl closed 4 years ago

leoncarl commented 4 years ago

I'm experiencing an issue with the nakama-cpp lib talking to nakama in a docker container. It is sending a Content-Type: text/plain; charset=utf-8, application/json header (which is not valid AFAIK). I get back an golang mime type error.

I suspect this has always been an issue but only errors with 2.7 / golang 1.3.x (have not confirmed this). One annoyance is that the error never hit the nakama logs so for a while I was suspecting docker networking barfing on it before forwarding to the container. It was only after I realized that gRPC code 3 was being returned that I knew it was reaching Nakama but not error logging.

With nakama 2.5.1 container (does not error)

$ curl -X POST "http://127.0.0.1:7350/v2/account/authenticate/custom?create=true&username=" -H "Content-Type: text/plain; charset=utf-8, application/json" -H "Authorization: Basic <redacted>" -H "Accept: application/json" -d "{\"id\":\"<redacted>\"}"; echo
{"created":true,"token":"<redacted>"}

With nakama 2.7.0 container (does error)

$ curl -X POST "http://127.0.0.1:7350/v2/account/authenticate/custom?create=true&username=" -H "Content-Type: text/plain; charset=utf-8, application/json" -H "Authorization: Basic <redacted>" -H "Accept: application/json" -d "{\"id\":\"<redacted>\"}"; echo
{"error":"mime: invalid media parameter","code":3,"message":"mime: invalid media parameter"}

I suspect the issue is with cpprestsdk you use not being very smart/careful about which headers are actually allowed to have multiple of (as a comma separated list of values: https://github.com/heroiclabs/nakama-cpp/blob/master/third_party/cpprestsdk/Release/include/cpprest/http_headers.h#L172-L191 ) Since request.set_body() is called with no second content-type property https://github.com/heroiclabs/nakama-cpp/blob/v2.1.0/src/NHttpClientCppRest.cpp#L89-L95 the default of text/plain; charset=utf-8 is being used: https://github.com/heroiclabs/nakama-cpp/blob/master/third_party/cpprestsdk/Release/include/cpprest/http_msg.h#L677-L704 However, you are then adding more headers, including a Content-Type: application/json one https://github.com/heroiclabs/nakama-cpp/blob/v2.1.0/src/NHttpClientCppRest.cpp#L94 that was set earlier: https://github.com/heroiclabs/nakama-cpp/blob/v2.1.0/src/RestClient.cpp#L159

Dimon4eg commented 4 years ago

@leoncarl Which nakama-cpp version do you use? This issue has been patched in 2.2.0 version and later properly fixed in master branch and not released yet. So, please try 2.2.0 version.

Proper fix is here https://github.com/heroiclabs/nakama-cpp/commit/8fcaa597b037032f3cfc4b75e82881ecbb612ad9

leoncarl commented 4 years ago

Yeah, we are on 2.1.0 currently. For now we've just patched 2.1.0. We will likely update to the next version > 2.2.0 when it is released. Cheers.