Skip to content

Commit

Permalink
Support more charsets using iconv-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidka committed Aug 4, 2019
1 parent db3736e commit 59cd930
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
1 change: 1 addition & 0 deletions build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const externals = [
"child_process",
"dgram",
"url",
"iconv-lite",
"koa",
"koa-router",
"koa-compress",
Expand Down
44 changes: 39 additions & 5 deletions lib/cwmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { IncomingMessage, ServerResponse } from "http";
import { Readable } from "stream";
import { promisify } from "util";
import { TLSSocket } from "tls";
import { decode, encodingExists } from "iconv-lite";
import { dependencies as DEPENDENCIES } from "../package.json";
import { parseXmlDeclaration } from "./xml-parser";
import * as debug from "./debug";
Expand Down Expand Up @@ -1106,6 +1107,15 @@ export function listener(
});
}

function decodeString(buffer: Buffer, charset: string): string {
try {
return buffer.toString(charset);
} catch (err) {
if (encodingExists(charset)) return decode(buffer, charset);
}
return null;
}

async function listenerAsync(
httpRequest: IncomingMessage,
httpResponse: ServerResponse
Expand Down Expand Up @@ -1233,10 +1243,36 @@ async function listenerAsync(
if (!charset) {
const parse = parseXmlDeclaration(body);
const e = parse ? parse.find(s => s.localName === "encoding") : null;
charset = e ? e.value : "utf8";
charset = e ? e.value.toLowerCase() : "utf8";
}

const bodyStr = body.toString(charset);
const bodyStr = decodeString(body, charset);

if (bodyStr == null) {
const msg = `Unknown encoding '${charset}'`;
logger.accessError({
message: "XML parse error",
parseError: msg,
sessionContext: sessionContext || {
httpRequest: httpRequest,
httpResponse: httpResponse
}
});
httpResponse.setHeader("Content-Length", Buffer.byteLength(msg));
httpResponse.writeHead(400, { Connection: "close" });
if (sessionContext) {
if (sessionContext.debug)
debug.outgoingHttpResponse(httpResponse, sessionContext.deviceId, msg);
} else {
const cacheSnapshot = await localCache.getCurrentSnapshot();
const d = !!localCache.getConfig(cacheSnapshot, "cwmp.debug", {
remoteAddress: httpResponse.connection.remoteAddress
});
if (d) debug.outgoingHttpResponse(httpResponse, null, msg);
}
httpResponse.end(msg);
return;
}

async function parsedRpc(_sessionContext, rpc, parseWarnings): Promise<void> {
for (const w of parseWarnings) {
Expand Down Expand Up @@ -1283,9 +1319,7 @@ async function listenerAsync(
const d = !!localCache.getConfig(cacheSnapshot, "cwmp.debug", {
remoteAddress: httpResponse.connection.remoteAddress
});
if (d) {
debug.outgoingHttpResponse(httpResponse, null, error.message);
}
if (d) debug.outgoingHttpResponse(httpResponse, null, error.message);
}
httpResponse.end(error.message);
return;
Expand Down
36 changes: 33 additions & 3 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"tools": "tools"
},
"dependencies": {
"iconv-lite": "^0.5.0",
"jsonwebtoken": "^8.5.1",
"koa": "^2.7.0",
"koa-bodyparser": "^4.2.1",
Expand Down

0 comments on commit 59cd930

Please sign in to comment.