Jaguar-dart / jaguar

Jaguar, a server framework built for speed, simplicity and extensible. ORM, Session, Authentication & Authorization, OAuth
http://jaguar-dart.github.io
463 stars 34 forks source link

Should we give Response a default utf-8 charset? #119

Closed lvscar closed 6 years ago

lvscar commented 6 years ago

In the Responseobject initializing process, If the response value contain some non-latin characters, at the same time the user didn't set the optional charset parameter to 'utf-8'. The Jaguar server will just output an "empty" HTTP response ( content-length: 0 , empty HTTP body) without any error message displayed in terminal .

I found there have some related discussion and some information about standards. According to these linked standard webpages, we could set the default charset to utf-8 at least for JSON response safely.

lvscar commented 6 years ago

I have submitted a PR to reveal the “silenced failure”.

tejainece commented 6 years ago

Will fix this.

tejainece commented 6 years ago

Which method do you use to write JSON response?

My plan is to set the default charset to utf-8 only when the response is going to be JSON.

  1. Mux HTTP methods: getJson, posJson, etc.
  2. Controller HTTP methods: GetJson, PostJson, etc
  3. Response.json()
tejainece commented 6 years ago

It looks like you use Response.json, the other two methods already use utf-8 by default.

tejainece commented 6 years ago

Try Jaguar 2.1.26, it uses utf-8 by default for Response.json.

Would this be enough?

lvscar commented 6 years ago

I prefer directly construct JSON and Response object because heavily depend on toEncodable in my case.

Beside JSON response, any XHR request/response should be encoded in utf-8. ref. And because utf-8 is backwards compatibility with ASCII 0, I hope Jaguar could use the utf-8 as default charset for all kinds of response.

tejainece commented 6 years ago

Try Jaguar 2.1.26, it uses utf-8 by default for Response.

tejainece commented 6 years ago

Let me know if it has fixed the problem.

lvscar commented 6 years ago

After upgrade to Jaguar 2.1.27. I confirm the issue is resolved!

tejainece commented 6 years ago

@lvscar Here is a video about various methods for JSON serialization using Jaguar: https://www.youtube.com/watch?v=zW_PTzmw2N0&t=12s

There is also jaguar_serializer package that generates serializersfor models: https://github.com/Jaguar-dart/jaguar_serializer/wiki

lvscar commented 6 years ago

New tricks learned! "ctx.bodyAsJson(convert: Func)" & "Map toJSON" both very handy in common api getaway programming. Love Jaguar more~

lvscar commented 6 years ago

After upgrade to Jaguar 2.2.10 , I found the default utf-8 charset for Response & Response.json was missing.

lvscar commented 6 years ago

PR #123 submitted