From cfdd2b4a2ef903005d525a85af0ae1e1e9b60ff0 Mon Sep 17 00:00:00 2001 From: Johnny Tordgeman Date: Thu, 7 Dec 2023 18:00:52 +0200 Subject: [PATCH 1/3] empty payload handling in pxhttpc --- lib/pxhttpc.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/pxhttpc.js b/lib/pxhttpc.js index 46fff589..7f2d0fd7 100644 --- a/lib/pxhttpc.js +++ b/lib/pxhttpc.js @@ -37,7 +37,12 @@ function callServer(data, headers, uri, callType, config, callback) { if (err.toString().toLowerCase().includes('timeout')) { return callback('timeout'); } else { - return handleError(callback, S2SErrorReason.UNKNOWN_ERROR, `encountered error with risk api call: ${err}`, response); + return handleError( + callback, + S2SErrorReason.UNKNOWN_ERROR, + `encountered error with risk api call: ${err}`, + response, + ); } } @@ -46,7 +51,7 @@ function callServer(data, headers, uri, callType, config, callback) { callback, S2SErrorReason.UNKNOWN_ERROR, `call to perimeterx server returned null or empty response: ${response}`, - response + response, ); } @@ -65,20 +70,35 @@ function callServer(data, headers, uri, callType, config, callback) { const handleOkResponse = (callback, response) => { let data = getDataFromResponse(response); - if (!data) { - return handleError(callback, S2SErrorReason.INVALID_RESPONSE, `unable to get data from response body: ${response.body}`, response); + if (!data && response.statusCode >= 300) { + return handleError( + callback, + S2SErrorReason.INVALID_RESPONSE, + `unable to get data from response body: ${response.body}`, + response, + ); } if (typeof data !== 'object') { try { data = JSON.parse(data); } catch (e) { - return handleError(callback, S2SErrorReason.INVALID_RESPONSE, `error parsing response body - ${data}: ${e}`, response); + return handleError( + callback, + S2SErrorReason.INVALID_RESPONSE, + `error parsing response body - ${data}: ${e}`, + response, + ); } } - if (data.status && data.status !== 0) { - return handleError((err) => callback(err, data), S2SErrorReason.REQUEST_FAILED_ON_SERVER, data.message, response); + if (data && data.status && data.status !== 0) { + return handleError( + (err) => callback(err, data), + S2SErrorReason.REQUEST_FAILED_ON_SERVER, + data.message, + response, + ); } return callback(null, data); From 5a9a58aaee93588a494948c6ec4c213b22839042 Mon Sep 17 00:00:00 2001 From: Johnny Tordgeman Date: Thu, 7 Dec 2023 22:38:11 +0200 Subject: [PATCH 2/3] using failOnEmptyBody --- lib/pxclient.js | 10 +++++++++- lib/pxhttpc.js | 8 ++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/pxclient.js b/lib/pxclient.js index 3b9b616c..dd38216e 100644 --- a/lib/pxclient.js +++ b/lib/pxclient.js @@ -165,7 +165,15 @@ class PxClient { } callServer(data, path, additionalHeaders, config, cb) { - pxHttpc.callServer(data, this.createHeaders(config, additionalHeaders), path, 'activities', config); + pxHttpc.callServer( + data, + this.createHeaders(config, additionalHeaders), + path, + 'activities', + config, + null, + false, + ); if (cb) { cb(); } diff --git a/lib/pxhttpc.js b/lib/pxhttpc.js index 7f2d0fd7..0b4e47f6 100644 --- a/lib/pxhttpc.js +++ b/lib/pxhttpc.js @@ -17,7 +17,7 @@ module.exports = { * @param {string} callType - indication for a query or activities sending * @param {Function} callback - callback function. */ -function callServer(data, headers, uri, callType, config, callback) { +function callServer(data, headers, uri, callType, config, callback, failOnEmptyBody = true) { callback = callback || ((err) => { @@ -56,7 +56,7 @@ function callServer(data, headers, uri, callType, config, callback) { } if (response.statusCode === 200) { - return handleOkResponse(callback, response); + return handleOkResponse(callback, response, failOnEmptyBody); } return handleUnexpectedHttpResponse(callback, response); @@ -67,10 +67,10 @@ function callServer(data, headers, uri, callType, config, callback) { } } -const handleOkResponse = (callback, response) => { +const handleOkResponse = (callback, response, failOnEmptyBody) => { let data = getDataFromResponse(response); - if (!data && response.statusCode >= 300) { + if (!data && failOnEmptyBody) { return handleError( callback, S2SErrorReason.INVALID_RESPONSE, From b3376024ba091c2ff2449d275c14398278706a94 Mon Sep 17 00:00:00 2001 From: Johnny Tordgeman Date: Sun, 10 Dec 2023 11:00:29 +0200 Subject: [PATCH 3/3] Added changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce81471..9c4b6ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [3.11.1] - 2023-12-10 + +### Added + +- `failOnEmptyBody` flag for `callServer` to specify wether or not a request should fail if it has no body. + ## [3.11.0] - 2023-05-16 ### Changed