2016-03-05 07:26:47 +08:00
var SteamCommunity = require ( '../index.js' ) ;
2016-03-05 08:35:04 +08:00
SteamCommunity . prototype . httpRequest = function ( uri , options , callback , source ) {
2016-03-05 07:26:47 +08:00
if ( typeof uri === 'object' ) {
2016-03-05 08:35:04 +08:00
source = callback ;
2016-03-05 07:26:47 +08:00
callback = options ;
options = uri ;
uri = options . url || options . uri ;
2016-03-05 12:50:49 +08:00
} else if ( typeof options === 'function' ) {
source = callback ;
callback = options ;
options = { } ;
2016-03-05 07:26:47 +08:00
}
options . url = options . uri = uri ;
if ( this . _httpRequestConvenienceMethod ) {
options . method = this . _httpRequestConvenienceMethod ;
delete this . _httpRequestConvenienceMethod ;
}
2016-03-05 08:35:04 +08:00
var requestID = ++ this . _httpRequestID ;
source = source || "" ;
2016-03-05 13:14:36 +08:00
var self = this ;
var continued = false ;
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 07:26:47 +08:00
}
2016-03-05 13:14:36 +08:00
function continueRequest ( err ) {
if ( continued ) {
return ;
}
2016-03-05 07:26:47 +08:00
2016-03-05 13:14:36 +08:00
continued = true ;
if ( err ) {
2016-03-08 14:37:20 +08:00
if ( callback ) {
callback ( err ) ;
}
2016-03-05 13:14:36 +08:00
return ;
2016-03-05 07:26:47 +08:00
}
2016-03-05 13:14:36 +08:00
self . request ( options , function ( err , response , body ) {
var hasCallback = ! ! callback ;
var httpError = options . checkHttpError !== false && self . _checkHttpError ( err , response , callback ) ;
2016-03-08 14:37:20 +08:00
var communityError = ! options . json && options . checkCommunityError !== false && self . _checkCommunityError ( body , httpError ? function ( ) { } : callback ) ; // don't fire the callback if hasHttpError did it already
var tradeError = ! options . json && options . checkTradeError !== false && self . _checkTradeError ( body , httpError || communityError ? function ( ) { } : callback ) ; // don't fire the callback if either of the previous already did
2016-08-10 03:45:21 +08:00
var jsonError = options . json && ! body ? new Error ( "Malformed JSON response" ) : null ;
2016-03-05 13:14:36 +08:00
2016-08-10 03:45:21 +08:00
self . emit ( 'postHttpRequest' , requestID , source , options , httpError || communityError || tradeError || jsonError || null , response , body , {
2016-03-05 13:14:36 +08:00
"hasCallback" : hasCallback ,
"httpError" : httpError ,
"communityError" : communityError ,
2016-08-10 03:45:21 +08:00
"tradeError" : tradeError ,
"jsonError" : jsonError
2016-03-05 13:14:36 +08:00
} ) ;
2016-08-10 03:45:21 +08:00
if ( hasCallback ) {
if ( jsonError ) {
callback . call ( self , jsonError , response ) ;
} else if ( ! ( httpError || communityError || tradeError ) ) {
callback . apply ( self , arguments ) ;
}
2016-03-05 13:14:36 +08:00
}
} ) ;
}
2016-03-05 07:26:47 +08:00
} ;
SteamCommunity . prototype . httpRequestGet = function ( ) {
this . _httpRequestConvenienceMethod = "GET" ;
return this . httpRequest . apply ( this , arguments ) ;
} ;
SteamCommunity . prototype . httpRequestPost = function ( ) {
this . _httpRequestConvenienceMethod = "POST" ;
return this . httpRequest . apply ( this , arguments ) ;
} ;
2016-03-07 14:32:02 +08:00
SteamCommunity . prototype . _notifySessionExpired = function ( err ) {
this . emit ( 'sessionExpired' , err ) ;
} ;
2016-03-05 07:26:47 +08:00
SteamCommunity . prototype . _checkHttpError = function ( err , response , callback ) {
2016-06-30 11:08:40 +08:00
if ( err ) {
2016-03-05 07:26:47 +08:00
callback ( err ) ;
return err ;
}
2016-06-30 11:08:40 +08:00
if ( response . statusCode >= 300 && response . statusCode <= 399 && response . headers . location . indexOf ( '/login' ) != - 1 ) {
2016-03-05 07:26:47 +08:00
err = new Error ( "Not Logged In" ) ;
callback ( err ) ;
2016-03-07 14:32:02 +08:00
this . _notifySessionExpired ( err ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
2016-06-30 11:08:40 +08:00
if ( response . statusCode == 403 && response . body && response . body . match ( /<div id="parental_notice_instructions">Enter your PIN below to exit Family View.<\/div>/ ) ) {
err = new Error ( "Family View Restricted" ) ;
callback ( err ) ;
return err ;
}
if ( response . statusCode >= 400 ) {
2016-03-05 07:26:47 +08:00
err = new Error ( "HTTP error " + response . statusCode ) ;
err . code = response . statusCode ;
callback ( err ) ;
return err ;
}
return false ;
} ;
SteamCommunity . prototype . _checkCommunityError = function ( html , callback ) {
2016-03-06 07:58:39 +08:00
var err ;
2016-03-15 09:39:45 +08:00
if ( typeof html === 'string' && html . match ( /<h1>Sorry!<\/h1>/ ) ) {
2016-03-05 07:26:47 +08:00
var match = html . match ( /<h3>(.+)<\/h3>/ ) ;
2016-03-06 07:58:39 +08:00
err = new Error ( match ? match [ 1 ] : "Unknown error occurred" ) ;
callback ( err ) ;
return err ;
}
2016-03-15 09:39:45 +08:00
if ( typeof html === 'string' && html . match ( /g_steamID = false;/ ) && html . match ( /<h1>Sign In<\/h1>/ ) ) {
2016-03-06 07:58:39 +08:00
err = new Error ( "Not Logged In" ) ;
2016-03-05 07:26:47 +08:00
callback ( err ) ;
2016-03-07 14:32:02 +08:00
this . _notifySessionExpired ( err ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
return false ;
} ;
SteamCommunity . prototype . _checkTradeError = function ( html , callback ) {
2016-03-15 09:39:45 +08:00
if ( typeof html !== 'string' ) {
2016-03-05 13:40:45 +08:00
return false ;
}
2016-03-05 07:26:47 +08:00
var match = html . match ( /<div id="error_msg">\s*([^<]+)\s*<\/div>/ ) ;
if ( match ) {
var err = new Error ( match [ 1 ] . trim ( ) ) ;
2016-06-04 08:10:33 +08:00
callback ( err ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
return false ;
} ;