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

corrupted multibyte characters in node #38

Closed
vkurchatkin opened this issue Jan 27, 2015 · 5 comments
Closed

corrupted multibyte characters in node #38

vkurchatkin opened this issue Jan 27, 2015 · 5 comments

Comments

@vkurchatkin
Copy link

Multibyte characters on chunk boundaries get corrupted.

@mzabriskie
Copy link
Member

@vkurchatkin Would you mind providing example code so that I know how to reproduce this?

@vkurchatkin
Copy link
Author

Here it is

var axios = require('axios');
var http = require('http');
var assert = require('assert');

var str = Array(100000).join('ж');

http.createServer(function(req, res) {
  res.setHeader('Content-Type', 'text/html; charset=UTF-8');
  res.end(str);
}).listen(4444, function() {
  axios.get('http://localhost:4444/').then(function(response) {
    process.nextTick(function() {
      assert(response.data === str);
    });
  });
});

I'm not sure that this test is 100% reliable (or that a 100% reliable test of this exists at all)

@mzabriskie
Copy link
Member

Thanks. I'll give this a look.

@vkurchatkin
Copy link
Author

Here is a quick fix:

diff --git a/lib/adapters/http.js b/lib/adapters/http.js
index 8a6fbf6..81da7ac 100644
--- a/lib/adapters/http.js
+++ b/lib/adapters/http.js
@@ -52,15 +52,15 @@ module.exports = function httpAdapter(resolve, reject, config) {
   // Create the request
   var transport = parsed.protocol === 'https:' ? https : http;
   var req = transport.request(options, function (res) {
-    var responseText = '';
+    var responseBuffer = [];
     res.on('data', function (chunk) {
-      responseText += chunk;
+      responseBuffer.push(chunk);
     });

     res.on('end', function () {
       var response = {
         data: transformData(
-          responseText,
+          Buffer.concat(responseBuffer).toString('utf8'),
           res.headers,
           config.transformResponse
         ),
@@ -83,4 +83,4 @@ module.exports = function httpAdapter(resolve, reject, config) {

   // Send the request
   req.end(data);
-};
\ No newline at end of file
+};

@mzabriskie
Copy link
Member

@vkurchatkin thank you for your diff. I will apply this and write some tests around it.

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants