Skip to content

Commit

Permalink
fix(#2367): handle response body decode (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
lohxt1 committed May 30, 2024
1 parent 470d162 commit 3ded960
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/bruno-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"graphql": "^16.6.0",
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.2",
"iconv-lite": "^0.6.3",
"is-valid-path": "^0.1.1",
"js-yaml": "^4.1.0",
"json-bigint": "^1.0.0",
Expand Down
10 changes: 8 additions & 2 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
transformPasswordCredentialsRequest
} = require('./oauth2-helper');
const Oauth2Store = require('../../store/oauth2');
const iconv = require('iconv-lite');

// override the default escape function to prevent escaping
Mustache.escape = function (value) {
Expand Down Expand Up @@ -258,13 +259,18 @@ const configureRequest = async (
};

const parseDataFromResponse = (response) => {
const dataBuffer = Buffer.from(response.data);
// Parse the charset from content type: https://stackoverflow.com/a/33192813
const charsetMatch = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['content-type'] || '');
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#using_exec_with_regexp_literals
const charsetValue = charsetMatch?.[1];
const dataBuffer = Buffer.from(response.data);
// Overwrite the original data for backwards compatibility
let data = dataBuffer.toString(charsetValue || 'utf-8');
let data;
if (iconv.encodingExists(charsetValue)) {
data = iconv.decode(dataBuffer, charsetValue);
} else {
data = iconv.decode(dataBuffer, 'utf-8');
}
// Try to parse response to JSON, this can quietly fail
try {
// Filter out ZWNBSP character
Expand Down
6 changes: 6 additions & 0 deletions packages/bruno-tests/src/echo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ router.get('/bom-json-test', (req, res) => {
return res.send(jsonWithBom);
});

router.get('/iso-enc', (req, res) => {
res.set('Content-Type', 'text/plain; charset=ISO-8859-1');
const responseText = 'éçà';
return res.send(Buffer.from(responseText, 'latin1'));
});

module.exports = router;

0 comments on commit 3ded960

Please sign in to comment.