Skip to content
This repository was archived by the owner on Aug 9, 2018. It is now read-only.

Commit 2dd2ed8

Browse files
committed
Merge pull request #10 from kshay/master
Add support for $http.jsonp() requests.
2 parents cab6acb + bbc2f18 commit 2dd2ed8

File tree

1 file changed

+103
-103
lines changed

1 file changed

+103
-103
lines changed

lib/ngoverrides.js

Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -338,128 +338,128 @@ function registerModule(context) {
338338
reqMethod, reqUrl, reqData, callback, headers,
339339
timeout, withCredentials, responseType
340340
) {
341-
startRequest();
341+
var isJSONP = false;
342342
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 {
345373
setTimeout(
346374
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);
348378
endRequest();
349379
},
350380
1
351381
);
352382
return;
353383
}
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
369396
}
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;
375402
}
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-
}
389403

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);
415421
}
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;
426429
}
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*$/, '');
441436
}
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();
451442
}
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;
456452
}
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();
460457
}
461-
req.end();
458+
);
459+
if (reqData) {
460+
req.write(reqData);
462461
}
462+
req.end();
463463
};
464464

465465
// Extra interface to allow our server code to detect when

0 commit comments

Comments
 (0)