Skip to content

Commit

Permalink
fix: fix gbk text
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Mar 31, 2019
1 parent 2942e19 commit 93c3559
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/nacos-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.4",
"contributors": "^0.5.1",
"midway-bin": "^0.3.2",
"midway-bin": "1",
"mm": "^2.4.1",
"pedding": "^1.1.0",
"tslint": "^5.11.0",
Expand Down
17 changes: 15 additions & 2 deletions packages/nacos-config/src/http_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { HTTP_CONFLICT, HTTP_NOT_FOUND, HTTP_OK, VERSION } from './const';
import { ClientOptionKeys, IConfiguration, IServerListManager } from './interface';
import * as urllib from 'urllib';
import * as crypto from 'crypto';
import { encodingParams } from './utils';
import { encodingParams, transformGBKToUTF8 } from './utils';

export class HttpAgent {

Expand Down Expand Up @@ -153,7 +153,7 @@ export class HttpAgent {
this.debug('%s %s, got %s, body: %j', method, url, res.status, res.data);
switch (res.status) {
case HTTP_OK:
return res.data;
return this.decodeResData(res, method);
case HTTP_NOT_FOUND:
return null;
case HTTP_CONFLICT:
Expand Down Expand Up @@ -199,4 +199,17 @@ export class HttpAgent {
return `${url}/${this.contextPath}`;
}

decodeResData(res, method = 'GET') {
if (method === 'GET' && /charset=GBK/.test(res.headers[ 'content-type' ]) && this.defaultEncoding === 'utf8') {
try {
return transformGBKToUTF8(res.data);
} catch (err) {
console.error(`transform gbk data to utf8 error, msg=${err.messager}`);
return res.data;
}
} else {
return res.data;
}
}

}
5 changes: 5 additions & 0 deletions packages/nacos-config/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ export function checkParameters(dataIds, group, datumId?) {
assert(exports.isValid(datumId), `[datumId] only allow digital, letter and symbols in [ "_", "-", ".", ":" ], but got ${datumId}`);
}
}


export function transformGBKToUTF8(text) {
return iconv.decode(iconv.encode(text, 'gbk'), 'utf8');
}

0 comments on commit 93c3559

Please sign in to comment.