0

I have web application written in Yii2. Inside beforeAction() i want to set my custom message to user with explanation of problem:

Yii::$app->response->setStatusCode(403, 'Brak dostępu. Skontaktuj się z administratorem.');

Is it possible to use uft-8 "reason text" in HTTP/1.1 response header?

BPS
  • 1,133
  • 1
  • 17
  • 38
  • 1
    I don’t see what a translation should achieve in that place to begin with. If you want to show a translated error message to the user _in_ the page, that would be one thing - but HTTP headers should rather not need “translating” in the first place IMHO. – 04FS Feb 25 '19 at 14:08
  • You can also include an HTML body. Under [most conditions](https://blogs.msdn.microsoft.com/ieinternals/2010/08/18/friendly-http-error-pages/), a web browser would display it. – Tom Blodget Feb 25 '19 at 18:09

1 Answers1

2

RFC 7230 does not give any instructions on how to parse the "reason text" element of the status line, so no encoding is implied or specified.

The ABNF grammar indicates that your string would be valid if presented in any "ASCII-compatible" encoding, such as UTF-8:

reason-phrase  = *( HTAB / SP / VCHAR / obs-text )

VCHAR means "visible ASCII character", and obs-text is any byte in the range 0x80 to 0xFF.

However, there is no definition of how those bytes should be interpreted. Instead, the specification recommends that clients do not parse it at all:

The reason-phrase element exists for the sole purpose of providing a textual description associated with the numeric status code, mostly out of deference to earlier Internet application protocols that were more frequently used with interactive text clients. A client SHOULD ignore the reason-phrase content.

So go ahead and put whatever text you like there if you're using it for low-level debugging or with a custom client. But don't be surprised if standard clients either ignore it or misinterpret it as a different character encoding.

Community
  • 1
  • 1
IMSoP
  • 89,526
  • 13
  • 117
  • 169