Skip to content

Commit

Permalink
Header fix for http2 - httpClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
TekMonksGitHub committed Nov 29, 2023
1 parent 57454f7 commit 4e5af6c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions backend/server/lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ function _doCall(reqStr, options, secure, sslObj) {
secure ? https : http; // use the right connection factory based on http2, http1/ssl or http1/http
let resp, ignoreEvents = false, resPiped, _skipProtocolErrors = false;
if (sslObj & typeof sslObj == "object") try { await _addSecureOptions(options, sslObj) } catch (err) { reject(err); return; };
const sendError = (error) => { reject(error); ignoreEvents = true; };
const sendError = (error) => {
reject(error); ignoreEvents = true;
};
options.headers = _squishHeaders(options.headers); // squish the headers - needed specially for HTTP/2 but good anyways

if (secure && (!sslObj?._org_monkshu_httpclient_forceHTTP1)) { // for http2 case
Expand Down Expand Up @@ -190,13 +192,14 @@ function _doCall(reqStr, options, secure, sslObj) {
sendError(`Content type doesn't match acceptable content. Requested ${options.headers.accept} != ${headers["content-type"]}.`);
return;
}
const statusOK = Math.trunc(headers.status / 200) == 1 && headers.status % 200 < 100;
let status; try{status = parseInt(headers[http2.constants.HTTP2_HEADER_STATUS]||headers.status)} catch(err) {status=500};
const statusOK = (Math.trunc(status / 200) == 1) && (status % 200 < 100);
if (!statusOK) sendError(`Bad status, ${headers.status}.`); else _skipProtocolErrors = true;

const encoding = utils.getObjectKeyValueCaseInsensitive(headers, "content-encoding") || "identity";
if (encoding.toLowerCase() == "gzip") { resPiped = zlib.createGunzip(); req.pipe(resPiped); } else resPiped = req;
resPiped.on("data", chunk => {
if (!ignoreEvents) resp = resp ? Buffer.concat([resp, chunk]) : chunk;
if (!ignoreEvents) resp = resp ? Buffer.concat([resp, chunk]) : chunk; else return;
if (resp.length == utils.getObjectKeyValueCaseInsensitive(headers, "content-length")) _processEnd();
});
resPiped.on("error", error => sendError(error));
Expand Down
2 changes: 1 addition & 1 deletion build_number
Original file line number Diff line number Diff line change
@@ -1 +1 @@
765
766

0 comments on commit 4e5af6c

Please sign in to comment.