@@ -18,7 +18,7 @@ var Error = require('./Error');
1818
1919var hasOwn = { } . hasOwnProperty ;
2020
21- var RESPONSE_CONTENT_TYPE = [ 'text/xml' , 'text/xml;charset=utf-8' , 'application/xml' ] ;
21+ var RESPONSE_CONTENT_TYPE_XML = [ 'text/xml' , 'text/xml;charset=utf-8' , 'application/xml' ] ;
2222// Provide extension mechanism for AmazonMws Resource Sub-Classes
2323AmazonMwsResource . extend = utils . protoExtend ;
2424
@@ -168,20 +168,31 @@ AmazonMwsResource.prototype = {
168168
169169 function processResponseType ( res , responseString , callback ) {
170170 //debug('res %o ', res);
171- //debug('res.headers %o ', res.headers);
172- if ( RESPONSE_CONTENT_TYPE . indexOf ( res . headers [ 'content-type' ] . toLowerCase ( ) ) > - 1 ) {
171+ var xmlParser = new xml2js . Parser ( {
172+ mergeAttrs : true ,
173+ explicitArray : false ,
174+ emptyTag : { }
175+ } ) ;
176+ if ( ! res . headers [ 'content-type' ] ) {
177+ debug ( 'Content type has not set, so considered it as XML response' ) ;
178+ xmlParser . parseString ( responseString , function ( err , response ) {
179+ //debug('response after parsing JSON %o ', response);
180+ return callback ( null , response ) ;
181+ } ) ;
182+ } else if ( RESPONSE_CONTENT_TYPE_XML . indexOf ( res . headers [ 'content-type' ] . toLowerCase ( ) ) > - 1 ) {
173183 debug ( 'It is XML Response' ) ;
174- var parser = new xml2js . Parser ( {
175- explicitArray : false ,
176- ignoreAttrs : true
184+ xmlParser . parseString ( responseString , function ( err , response ) {
185+ // debug('response after parsing JSON %o ', response);
186+ return callback ( null , response ) ;
177187 } ) ;
178-
179- parser . parseString ( responseString , function ( err , response ) {
180- //debug('response after parsing JSON %o ', response);
188+ } else if ( _ . includes ( responseString , '?xml' ) ) {
189+ debug ( 'It is XML Response be find out from responseString' ) ;
190+ xmlParser . parseString ( responseString , function ( err , response ) {
191+ // debug('response after parsing JSON %o ', response);
181192 return callback ( null , response ) ;
182193 } ) ;
183194 } else {
184- debug ( 'It is NON-XML Response' ) ;
195+ debug ( 'It is NON-XML Response, so considered it as CSV file ' ) ;
185196 var TAB_DELIMITER = '\t' ;
186197 var COMMA_DELIMITER = ',' ;
187198 parseCSVFile ( res , responseString , TAB_DELIMITER , function ( error , response ) {
@@ -200,29 +211,34 @@ AmazonMwsResource.prototype = {
200211 var dbgResponseBuffer = [ ] ;
201212 var headers = res . headers ;
202213 var statusCode = res . statusCode ;
214+ var contentType = '' ;
215+ if ( headers [ 'content-type' ] ) {
216+ contentType = headers [ 'content-type' ] . toLowerCase ( ) ;
217+ }
203218 try {
204219 statusCode = parseInt ( statusCode , 10 ) ;
205220 } catch ( Exception ) {
206221 debug ( 'Failed to parse statusCode as statusCode not provided in the response. ' , statusCode ) ;
207222 }
208223 var charset = '' ;
209- var content_type = '' ;
210224 var responseString = '' ;
211- if ( headers [ 'content-type' ] . indexOf ( 'charset' ) > - 1 && headers [ 'content-type' ] . split ( ';' ) [ 0 ] && headers [ 'content-type' ] . split ( ';' ) [ 1 ] ) {
212- content_type = headers [ 'content-type' ] . split ( ';' ) [ 0 ] . toLowerCase ( ) ;
213- if ( headers [ 'content-type' ] . split ( ';' ) [ 1 ] . match ( / ^ ( ( \b [ ^ \s = ] + ) = ( ( [ ^ = ] | \\ = ) + ) ) * $ / ) [ 3 ] ) {
214- charset = headers [ 'content-type' ] . split ( ';' ) [ 1 ] . match ( / ^ ( ( \b [ ^ \s = ] + ) = ( ( [ ^ = ] | \\ = ) + ) ) * $ / ) [ 3 ] ;
225+
226+ /**
227+ * Separate the charset & content type
228+ */
229+ if ( contentType . indexOf ( 'charset' ) > - 1 && contentType . split ( ';' ) [ 0 ] && contentType . split ( ';' ) [ 1 ] ) {
230+ if ( contentType . split ( ';' ) [ 1 ] && contentType . split ( ';' ) [ 1 ] . match ( / ^ ( ( \b [ ^ \s = ] + ) = ( ( [ ^ = ] | \\ = ) + ) ) * $ / ) [ 3 ] ) {
231+ charset = contentType . split ( ';' ) [ 1 ] . match ( / ^ ( ( \b [ ^ \s = ] + ) = ( ( [ ^ = ] | \\ = ) + ) ) * $ / ) [ 3 ] ;
215232 }
216- } else {
217- content_type = headers [ 'content-type' ] . toLowerCase ( ) ;
233+ contentType = contentType . split ( ';' ) [ 0 ] . toLowerCase ( ) ;
218234 }
219235
220236 var ResponseHeaders = {
221237 'x-mws-quota-max' : res . headers [ 'x-mws-quota-max' ] || 'unknown' ,
222238 'x-mws-quota-remaining' : res . headers [ 'x-mws-quota-remaining' ] || 'unknown' ,
223239 'x-mws-quota-resetson' : res . headers [ 'x-mws-quota-resetson' ] || 'unknown' ,
224240 'x-mws-timestamp' : res . headers [ 'x-mws-timestamp' ] ,
225- 'content-type' : content_type || 'unknown' ,
241+ 'content-type' : contentType || 'unknown' ,
226242 'content-charset' : charset || 'unknown' ,
227243 'content-length' : res . headers [ 'content-length' ] || 'unknown' ,
228244 'content-md5' : res . headers [ 'content-md5' ] || 'unknown' ,
@@ -269,21 +285,21 @@ AmazonMwsResource.prototype = {
269285 }
270286
271287 debug ( 'responseString ' , responseString ) ;
272- debug ( 'content_type ' , content_type ) ;
288+ debug ( 'contentType ' , contentType ) ;
273289 debug ( 'statusCode ' , statusCode ) ;
274290
275- if ( ! content_type ) {
276- return callback . call ( self , new Error . AmazonMwsAPIError ( {
277- message : 'Content Type is not provided in response received from the AmazonMws API' ,
278- StatusCode : statusCode || 'unknown'
279- } ) , null ) ;
280- }
281-
282291 try {
292+ var errorResponse = { } ;
293+ if ( statusCode > 499 && ! responseString ) {
294+ errorResponse . message = res . statusMessage || 'unknown' ;
295+ errorResponse . Headers = ResponseHeaders ;
296+ errorResponse . StatusCode = statusCode || 'unknown' ;
297+ return callback . call ( self , errorResponse , null ) ;
298+ }
283299 processResponseType ( res , responseString , function ( error , response ) {
284300 if ( response . ErrorResponse ) {
285301 debug ( 'It is ErrorResponse' ) ;
286- var errorResponse = response . ErrorResponse . Error ;
302+ errorResponse = response . ErrorResponse . Error ;
287303 errorResponse . Headers = ResponseHeaders ;
288304 errorResponse . StatusCode = statusCode || 'unknown' ;
289305 errorResponse . RequestId = response . ErrorResponse . RequestID || response . ErrorResponse . RequestId || 'unknown' ;
@@ -301,7 +317,7 @@ AmazonMwsResource.prototype = {
301317 }
302318
303319 var ResponseMetadata = { } ;
304- if ( RESPONSE_CONTENT_TYPE . indexOf ( content_type ) > - 1 ) {
320+ if ( RESPONSE_CONTENT_TYPE_XML . indexOf ( contentType ) > - 1 ) {
305321 /**
306322 * It should execute for only XML response
307323 */
0 commit comments