@@ -338,128 +338,128 @@ function registerModule(context) {
338
338
reqMethod , reqUrl , reqData , callback , headers ,
339
339
timeout , withCredentials , responseType
340
340
) {
341
- startRequest ( ) ;
341
+ var isJSONP = false ;
342
342
if ( reqMethod . toLowerCase ( ) === 'jsonp' ) {
343
- // jsonp is not supported on the server, so fail quickly
344
- // (but we still have to act asynchronous-like.)
343
+ // We don't want to run an arbitrary callback on the server, so instead
344
+ // we'll just strip off the callback invocation and the caller will get
345
+ // back whatever JSON is inside.
346
+ reqMethod = 'GET' ;
347
+ isJSONP = true ;
348
+ }
349
+ startRequest ( ) ;
350
+ if ( ! serverRequestContext . hasRequest ( ) ) {
351
+ // we can't do HTTP requests yet, because we don't know our own URL.
352
+ console . error ( 'Denied HTTP request' , reqUrl , 'because we have no base URL' ) ;
353
+ callback ( - 1 , undefined , undefined ) ;
354
+ endRequest ( ) ;
355
+ }
356
+ reqUrl = url . resolve ( $location . absUrl ( ) , reqUrl ) ;
357
+ var urlParts = url . parse ( reqUrl ) ;
358
+
359
+ var module ;
360
+ if ( urlParts . protocol === 'http:' ) {
361
+ module = http ;
362
+ if ( ! urlParts . port ) {
363
+ urlParts . port = 80 ;
364
+ }
365
+ }
366
+ else if ( urlParts . protocol === 'https:' ) {
367
+ module = https ;
368
+ if ( ! urlParts . port ) {
369
+ urlParts . port = 443 ;
370
+ }
371
+ }
372
+ else {
345
373
setTimeout (
346
374
function ( ) {
347
- callback ( - 2 , undefined , undefined ) ;
375
+ // FIXME: Figure out what browsers do when an inappropriate
376
+ // protocol is specified and mimic that here.
377
+ callback ( - 1 , undefined , undefined ) ;
348
378
endRequest ( ) ;
349
379
} ,
350
380
1
351
381
) ;
352
382
return ;
353
383
}
354
- else {
355
- if ( ! serverRequestContext . hasRequest ( ) ) {
356
- // we can't do HTTP requests yet, because we don't know our own URL.
357
- console . error ( 'Denied HTTP request' , reqUrl , 'because we have no base URL' ) ;
358
- callback ( - 1 , undefined , undefined ) ;
359
- endRequest ( ) ;
360
- }
361
- reqUrl = url . resolve ( $location . absUrl ( ) , reqUrl ) ;
362
- var urlParts = url . parse ( reqUrl ) ;
363
-
364
- var module ;
365
- if ( urlParts . protocol === 'http:' ) {
366
- module = http ;
367
- if ( ! urlParts . port ) {
368
- urlParts . port = 80 ;
384
+
385
+ var thisRequestId = nextRequestId ;
386
+ nextRequestId ++ ;
387
+ var req = pendingRequests [ thisRequestId ] = module . request (
388
+ {
389
+ hostname : urlParts . hostname ,
390
+ port : urlParts . port ,
391
+ path : urlParts . pathname +
392
+ ( urlParts . search ? urlParts . search : '' ) ,
393
+ method : reqMethod ,
394
+ headers : {
395
+ 'Host' : urlParts . host
369
396
}
370
- }
371
- else if ( urlParts . protocol === 'https:' ) {
372
- module = https ;
373
- if ( ! urlParts . port ) {
374
- urlParts . port = 443 ;
397
+ } ,
398
+ function ( res ) {
399
+ // ignore responses to aborted requests
400
+ if ( ! pendingRequests [ thisRequestId ] ) {
401
+ return ;
375
402
}
376
- }
377
- else {
378
- setTimeout (
379
- function ( ) {
380
- // FIXME: Figure out what browsers do when an inappropriate
381
- // protocol is specified and mimic that here.
382
- callback ( - 1 , undefined , undefined ) ;
383
- endRequest ( ) ;
384
- } ,
385
- 1
386
- ) ;
387
- return ;
388
- }
389
403
390
- var thisRequestId = nextRequestId ;
391
- nextRequestId ++ ;
392
- var req = pendingRequests [ thisRequestId ] = module . request (
393
- {
394
- hostname : urlParts . hostname ,
395
- port : urlParts . port ,
396
- path : urlParts . pathname +
397
- ( urlParts . search ? urlParts . search : '' ) ,
398
- method : reqMethod ,
399
- headers : {
400
- 'Host' : urlParts . host
401
- }
402
- } ,
403
- function ( res ) {
404
- // ignore responses to aborted requests
405
- if ( ! pendingRequests [ thisRequestId ] ) {
406
- return ;
407
- }
408
-
409
- var status = res . statusCode ;
410
- // Angular's interface expects headers as a string,
411
- // so we have to do a bit of an abstraction inversion here.
412
- var headers = '' ;
413
- for ( var k in res . headers ) {
414
- headers += k + ': ' + res . headers [ k ] + '\n' ;
404
+ var status = res . statusCode ;
405
+ // Angular's interface expects headers as a string,
406
+ // so we have to do a bit of an abstraction inversion here.
407
+ var headers = '' ;
408
+ for ( var k in res . headers ) {
409
+ headers += k + ': ' + res . headers [ k ] + '\n' ;
410
+ }
411
+ res . setEncoding ( 'utf8' ) ; // FIXME: what if it's not utf8?
412
+ var resData = [ ] ;
413
+ res . on (
414
+ 'data' ,
415
+ function ( chunk ) {
416
+ // ignore responses to aborted requests
417
+ if ( ! pendingRequests [ thisRequestId ] ) {
418
+ return ;
419
+ }
420
+ resData . push ( chunk ) ;
415
421
}
416
- res . setEncoding ( 'utf8' ) ; // FIXME: what if it's not utf8?
417
- var resData = [ ] ;
418
- res . on (
419
- 'data' ,
420
- function ( chunk ) {
421
- // ignore responses to aborted requests
422
- if ( ! pendingRequests [ thisRequestId ] ) {
423
- return ;
424
- }
425
- resData . push ( chunk ) ;
422
+ ) ;
423
+ res . on (
424
+ 'end' ,
425
+ function ( ) {
426
+ // ignore responses to aborted requests
427
+ if ( ! pendingRequests [ thisRequestId ] ) {
428
+ return ;
426
429
}
427
- ) ;
428
- res . on (
429
- 'end' ,
430
- function ( ) {
431
- // ignore responses to aborted requests
432
- if ( ! pendingRequests [ thisRequestId ] ) {
433
- return ;
434
- }
435
- delete pendingRequests [ thisRequestId ] ;
436
- // Call the callback before endRequest, to give the
437
- // callback a chance to push more requests into the queue
438
- // before we check if we're done.
439
- callback ( status , resData . join ( '' ) , headers ) ;
440
- endRequest ( ) ;
430
+ delete pendingRequests [ thisRequestId ] ;
431
+ var resStr = resData . join ( '' ) ;
432
+ if ( isJSONP ) {
433
+ // Assume everything up to the opening paren is the callback name
434
+ resStr = resStr . replace ( / ^ [ ^ ( ] + \( / , '' )
435
+ . replace ( / \) \s * ; ? \s * $ / , '' ) ;
441
436
}
442
- ) ;
443
- }
444
- ) ;
445
- req . on (
446
- 'error' ,
447
- function ( err ) {
448
- // ignore responses to aborted requests
449
- if ( ! pendingRequests [ thisRequestId ] ) {
450
- return ;
437
+ // Call the callback before endRequest, to give the
438
+ // callback a chance to push more requests into the queue
439
+ // before we check if we're done.
440
+ callback ( status , resStr , headers ) ;
441
+ endRequest ( ) ;
451
442
}
452
- delete pendingRequests [ thisRequestId ] ;
453
- // FIXME: What is a good error response code for this case?
454
- callback ( - 1 , undefined , undefined ) ;
455
- endRequest ( ) ;
443
+ ) ;
444
+ }
445
+ ) ;
446
+ req . on (
447
+ 'error' ,
448
+ function ( err ) {
449
+ // ignore responses to aborted requests
450
+ if ( ! pendingRequests [ thisRequestId ] ) {
451
+ return ;
456
452
}
457
- ) ;
458
- if ( reqData ) {
459
- req . write ( reqData ) ;
453
+ delete pendingRequests [ thisRequestId ] ;
454
+ // FIXME: What is a good error response code for this case?
455
+ callback ( - 1 , undefined , undefined ) ;
456
+ endRequest ( ) ;
460
457
}
461
- req . end ( ) ;
458
+ ) ;
459
+ if ( reqData ) {
460
+ req . write ( reqData ) ;
462
461
}
462
+ req . end ( ) ;
463
463
} ;
464
464
465
465
// Extra interface to allow our server code to detect when
0 commit comments