node-steamcommunity/components/http.js

152 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-02-14 13:29:21 +08:00
const SteamCommunity = require('../index.js');
2016-03-05 08:35:04 +08:00
SteamCommunity.prototype.httpRequest = function(uri, options, callback, source) {
2019-09-24 16:43:51 +08:00
if (typeof uri == 'object') {
2016-03-05 08:35:04 +08:00
source = callback;
callback = options;
options = uri;
uri = options.url || options.uri;
2019-09-24 16:43:51 +08:00
} else if (typeof options == 'function') {
source = callback;
callback = options;
options = {};
}
options.url = options.uri = uri;
if (this._httpRequestConvenienceMethod) {
options.method = this._httpRequestConvenienceMethod;
delete this._httpRequestConvenienceMethod;
}
2019-09-24 16:43:51 +08:00
let requestID = ++this._httpRequestID;
source = source || '';
2016-03-05 08:35:04 +08:00
2019-09-24 16:43:51 +08:00
let continued = false;
2016-03-05 13:14:36 +08:00
2019-09-24 16:43:51 +08:00
let continueRequest = (err) => {
2016-03-05 13:14:36 +08:00
if (continued) {
return;
}
2016-03-05 13:14:36 +08:00
continued = true;
if (err) {
if (callback) {
callback(err);
}
2016-03-05 13:14:36 +08:00
return;
}
2019-09-24 16:43:51 +08:00
this.request(options, (err, response, body) => {
let hasCallback = !!callback;
let httpError = options.checkHttpError !== false && this._checkHttpError(err, response, callback, body);
let communityError = !options.json && options.checkCommunityError !== false && this._checkCommunityError(body, httpError ? noop : callback); // don't fire the callback if hasHttpError did it already
let tradeError = !options.json && options.checkTradeError !== false && this._checkTradeError(body, httpError || communityError ? noop : callback); // don't fire the callback if either of the previous already did
2021-07-22 16:22:29 +08:00
let jsonError = options.json && options.checkJsonError !== false && !body ? new Error('Malformed JSON response') : null;
2016-03-05 13:14:36 +08:00
2019-09-24 16:43:51 +08:00
this.emit('postHttpRequest', requestID, source, options, httpError || communityError || tradeError || jsonError || null, response, body, {
2021-07-22 16:22:29 +08:00
hasCallback,
httpError,
communityError,
tradeError,
jsonError
2016-03-05 13:14:36 +08:00
});
if (hasCallback && !(httpError || communityError || tradeError)) {
if (jsonError) {
2019-09-24 16:43:51 +08:00
callback.call(this, jsonError, response);
} else {
2019-09-24 16:43:51 +08:00
callback.apply(this, arguments);
}
2016-03-05 13:14:36 +08:00
}
});
2019-09-24 16:43:51 +08:00
};
if (!this.onPreHttpRequest || !this.onPreHttpRequest(requestID, source, options, continueRequest)) {
// No pre-hook, or the pre-hook doesn't want to delay the request.
continueRequest(null);
2016-03-05 13:14:36 +08:00
}
};
SteamCommunity.prototype.httpRequestGet = function() {
2021-07-22 16:22:29 +08:00
this._httpRequestConvenienceMethod = 'GET';
return this.httpRequest.apply(this, arguments);
};
SteamCommunity.prototype.httpRequestPost = function() {
2021-07-22 16:22:29 +08:00
this._httpRequestConvenienceMethod = 'POST';
return this.httpRequest.apply(this, arguments);
};
SteamCommunity.prototype._notifySessionExpired = function(err) {
this.emit('sessionExpired', err);
};
SteamCommunity.prototype._checkHttpError = function(err, response, callback, body) {
if (err) {
callback(err, response, body);
return err;
}
if (response.statusCode >= 300 && response.statusCode <= 399 && response.headers.location.indexOf('/login') != -1) {
2021-07-22 16:22:29 +08:00
err = new Error('Not Logged In');
callback(err, response, body);
this._notifySessionExpired(err);
return err;
}
2019-09-24 16:43:51 +08:00
if (response.statusCode == 403 && typeof response.body == 'string' && response.body.match(/<div id="parental_notice_instructions">Enter your PIN below to exit Family View.<\/div>/)) {
2021-07-22 16:22:29 +08:00
err = new Error('Family View Restricted');
callback(err, response, body);
return err;
}
if (response.statusCode >= 400) {
2021-07-22 16:22:29 +08:00
err = new Error(`HTTP error ${response.statusCode}`);
err.code = response.statusCode;
callback(err, response, body);
return err;
}
return false;
};
SteamCommunity.prototype._checkCommunityError = function(html, callback) {
2019-09-24 16:43:51 +08:00
let err;
2019-09-24 16:43:51 +08:00
if (typeof html == 'string' && html.match(/<h1>Sorry!<\/h1>/)) {
let match = html.match(/<h3>(.+)<\/h3>/);
2021-07-22 16:22:29 +08:00
err = new Error(match ? match[1] : 'Unknown error occurred');
callback(err);
return err;
}
2019-09-24 16:43:51 +08:00
if (typeof html == 'string' && html.match(/g_steamID = false;/) && html.match(/<h1>Sign In<\/h1>/)) {
2021-07-22 16:22:29 +08:00
err = new Error('Not Logged In');
callback(err);
this._notifySessionExpired(err);
return err;
}
return false;
};
SteamCommunity.prototype._checkTradeError = function(html, callback) {
if (typeof html !== 'string') {
2016-03-05 13:40:45 +08:00
return false;
}
2019-09-24 16:43:51 +08:00
let match = html.match(/<div id="error_msg">\s*([^<]+)\s*<\/div>/);
if (match) {
2019-09-24 16:43:51 +08:00
let err = new Error(match[1].trim());
2016-06-04 08:10:33 +08:00
callback(err);
return err;
}
return false;
};
2019-09-24 16:43:51 +08:00
function noop() {}