X Tutup
The Wayback Machine - https://web.archive.org/web/20210116143902/https://github.com/restify/node-restify/issues/1852
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restify can't sendRaw none `utf8` data - it always encoded to utf-8 #1852

Open
denhub opened this issue Jul 24, 2020 · 0 comments
Open

Restify can't sendRaw none `utf8` data - it always encoded to utf-8 #1852

denhub opened this issue Jul 24, 2020 · 0 comments

Comments

@denhub
Copy link

@denhub denhub commented Jul 24, 2020

Hello!

Thanks for Restify - it's absolutely cool!

I found a bug in the function sendRaw - when you try to send none utf8 data in response - you can't do it. Data always will be encoded to utf-8.

I use charSet - to set latin1 encoding for sending ansi mp3-file.

res.charSet('latin1').sendRaw(200, mp3fileData,{
	'Content-Type': 'audio/mpeg',
});

But it's take affect only for Content-Type field.

Cause

In response.js does not send second param encoding to http write method. Http Docs.

res.write(res._data);

Are you willing and able to fix this?

Yes I can.

    // Send body if it was provided
    if (res._data) {
        // https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback
        // Default: 'utf8'
        let encoding = 'utf8';

        // Check :: is encoding valid?
        // in my point of view - we must validate encoding - in charSet() method
        // https://nodejs.org/docs/latest-v12.x/api/buffer.html#buffer_buffers_and_character_encodings
        let characterEncodingsSupportedByNodeJs = [
            'utf8',
            'utf16le',
            'latin1',
            'base64',
            'hex',
            'ascii',
            'binary',
            'ucs2',
        ];
        let characterEncodingsSupportedByNodeJs_id = characterEncodingsSupportedByNodeJs.indexOf(res._charSet);
        if ( characterEncodingsSupportedByNodeJs_id > -1 ) encoding = characterEncodingsSupportedByNodeJs[characterEncodingsSupportedByNodeJs_id];

        res.write(res._data, encoding);
    }

``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.
X Tutup