superagent icon indicating copy to clipboard operation
superagent copied to clipboard

TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Host"]

Open StanleyWCTan opened this issue 4 years ago • 1 comments

I'm using superagent as a rookie programer, but get stuck on below issue, please shed some light on it, many thanks! Due to some reasons, we still using old version of Election, maybe this is not the root cause.

Electon: v4.x, Node.js: v8.x Superagent: v6.1.0

Site:
https://www.a1distribution.ca/fr Links: ( links is from home page ) http://www.a1imports.ca/fr/Page/Contact http://www.a1imports.ca/fr/Base/ChangeLanguage http://www.a1imports.ca/fr/Catalog?attid=U%E2%80%A0h%C2%A4%24%7D%21c%E2%82%AC%E2%80%A2 http://www.a1imports.ca/fr/Catalog?attid=U%E2%80%A0h%C2%A4%24%7D%40%7D%E2%80%A0o

Code snipet: // ----------------------------------------------- let errorCount = 0; for (let i=0; i<links.length; i++) { let link = links[i]; try { /** * superagent will throw error when request this link( http://www.a1imports.ca/fr/Catalog?attid=U%E2%80%A0h%C2%A4%24%7D%21c%E2%82%AC%E2%80%A2 ) * however, catch block cannot catch the error and cause uncaughtException, the app will crash / let linkRes = await request.get(link).timeout({ response: 30000, deadline: 30000, }).set({"Accept-Encoding" : "gzip,deflate,sdch"}); let txt = linkRes.text; if (txt) { let content = cheerio.load(txt).text(); / * biz code **/ } } catch (err) { if (errorCount == 0) { let code = err.code; if (code != 'ENOTFOUND' && code != 'ERR_TLS_CERT_ALTNAME_INVALID' && code != 'ETIMEDOUT' && code != 'ECONNREFUSED' && code != 'ECONNRESET') logger.warn(err); } errorCount++; if (errorCount >= 3) break; } }

// -----------------------------------------------
process.on('uncaughtException', function(err) { loggerSys.error(err); app.exit(900); });

//--------------------- log ------------------- [2021-09-07 15:50:25.209] [ERROR] sys - TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Host"] at ClientRequest.setHeader (_http_outgoing.js:473:3) at new ClientRequest (_http_client.js:213:12) at Object.request (https.js:280:10) at Request.request (C:\Workspace\Electron\cusdig\node_modules\superagent\lib\node\index.js:795:18) at Request.end (C:\Workspace\Electron\cusdig\node_modules\superagent\lib\node\index.js:951:8) at Request._redirect (C:\Workspace\Electron\cusdig\node_modules\superagent\lib\node\index.js:552:8) at ClientRequest. (C:\Workspace\Electron\cusdig\node_modules\superagent\lib\node\index.js:1020:21) at Object.onceWrapper (events.js:273:13) at ClientRequest.emit (events.js:182:13) at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:555:21)

StanleyWCTan avatar Sep 07 '21 08:09 StanleyWCTan

Came here to report the same. It looks like https://github.com/visionmedia/superagent/blob/master/src/node/index.js#L850 throws an exception when non ascii characters are tried to be used as header values. According to https://nodejs.org/dist/latest-v16.x/docs/api/http.html#requestsetheadername-value it seems such values should be URIEncoded first?

nebulade avatar Oct 07 '22 10:10 nebulade