Skip to content

Commit ad6b4c4

Browse files
author
mfiess
authored
Reverting CORS (pxscene#605)
1 parent b9d5a27 commit ad6b4c4

File tree

13 files changed

+272
-400
lines changed

13 files changed

+272
-400
lines changed

examples/pxScene2d/src/pxArchive.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pxArchive::~pxArchive()
1313
gUIThreadQueue.removeAllTasksForObject(this);
1414
}
1515

16-
rtError pxArchive::initFromUrl(const rtString& url, const rtString& origin)
16+
rtError pxArchive::initFromUrl(const rtString& url)
1717
{
1818
mReady = new rtPromise;
1919
mLoadStatus = new rtMapObject;
@@ -30,12 +30,6 @@ rtError pxArchive::initFromUrl(const rtString& url, const rtString& origin)
3030
mLoadStatus.set("sourceType", "http");
3131
mLoadStatus.set("statusCode", -1);
3232
mDownloadRequest = new rtFileDownloadRequest(url, this);
33-
if (!origin.isEmpty())
34-
{
35-
rtString headerOrigin("Origin:");
36-
headerOrigin.append(origin.cString());
37-
mDownloadRequest->additionalHttpHeaders().push_back(headerOrigin);
38-
}
3933
mDownloadRequest->setCallbackFunction(pxArchive::onDownloadComplete);
4034
rtFileDownloader::instance()->addToDownloadQueue(mDownloadRequest);
4135
}

examples/pxScene2d/src/pxArchive.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class pxArchive: public rtObject
4545
pxArchive();
4646
virtual ~pxArchive();
4747

48-
rtError initFromUrl(const rtString& url, const rtString& origin = rtString());
48+
rtError initFromUrl(const rtString& url);
4949
rtError ready(rtObjectRef& r) const;
5050

5151
rtError loadStatus(rtObjectRef& v) const;

examples/pxScene2d/src/pxScene2d.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "rtString.h"
2929
#include "rtNode.h"
3030
#include "rtPathUtils.h"
31-
#include "rtUrlUtils.h"
3231

3332
#include "pxCore.h"
3433
#include "pxOffscreen.h"
@@ -1539,7 +1538,7 @@ rtDefineObject(pxRoot,pxObject);
15391538
int gTag = 0;
15401539

15411540
pxScene2d::pxScene2d(bool top, pxScriptView* scriptView)
1542-
: start(0), sigma_draw(0), sigma_update(0), end2(0), frameCount(0), mWidth(0), mHeight(0), mStopPropagation(false), mContainer(NULL), mShowDirtyRectangle(false),
1541+
: start(0), sigma_draw(0), sigma_update(0), end2(0), frameCount(0), mWidth(0), mHeight(0), mStopPropagation(false), mContainer(NULL), mShowDirtyRectangle(false),
15431542
mInnerpxObjects(), mDirty(true), mTestView(NULL), mDisposed(false)
15441543
{
15451544
mRoot = new pxRoot(this);
@@ -1549,11 +1548,6 @@ pxScene2d::pxScene2d(bool top, pxScriptView* scriptView)
15491548
mScriptView = scriptView;
15501549
mTag = gTag++;
15511550

1552-
if (scriptView != NULL)
1553-
{
1554-
mOrigin = rtUrlGetOrigin(scriptView->getUrl().cString());
1555-
}
1556-
15571551
// make sure that initial onFocus is sent
15581552
rtObjectRef e = new rtMapObject;
15591553
mRoot->setFocusInternal(true);

examples/pxScene2d/src/pxScene2d.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ class pxScene2d: public rtObject, public pxIView
12971297
rtMethodNoArgAndNoReturn("dispose",dispose);
12981298

12991299
pxScene2d(bool top = true, pxScriptView* scriptView = NULL);
1300-
virtual ~pxScene2d()
1300+
virtual ~pxScene2d()
13011301
{
13021302
rtLogDebug("***** deleting pxScene2d\n");
13031303
if (mTestView != NULL)
@@ -1456,7 +1456,7 @@ class pxScene2d: public rtObject, public pxIView
14561456
{
14571457
rtError e = RT_FAIL;
14581458
rtRef<pxArchive> a = new pxArchive;
1459-
if (a->initFromUrl(url, mOrigin) == RT_OK)
1459+
if (a->initFromUrl(url) == RT_OK)
14601460
{
14611461
archive = a;
14621462
e = RT_OK;
@@ -1519,7 +1519,6 @@ class pxScene2d: public rtObject, public pxIView
15191519
#endif
15201520
bool mPointerHidden;
15211521
std::vector<rtObjectRef> mInnerpxObjects;
1522-
rtString mOrigin;
15231522
public:
15241523
void hidePointer( bool hide )
15251524
{

examples/pxScene2d/src/rcvrcore/AppSceneContext.js

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,86 @@ var ClearInterval = clearInterval;
2121

2222
var http_wrap = require('rcvrcore/http_wrap');
2323
var https_wrap = require('rcvrcore/https_wrap');
24-
var AccessControl = require('rcvrcore/utils/AccessControl');
24+
25+
// function to check whether the page being loaded is from local machine or remote machine
26+
function isLocalApp(loadurl)
27+
{
28+
if ((loadurl.length > 4) && (loadurl.substring(0, 4) === "http"))
29+
{
30+
if ((loadurl.length >= 16) && ((loadurl.substring(0, 16) === "http://localhost") || (loadurl.substring(0, 16) === "http://127.0.0.1")))
31+
{
32+
return true;
33+
}
34+
else if ((loadurl.length >= 17) && ((loadurl.substring(0, 17) === "https://localhost") || (loadurl.substring(0, 17) === "https://127.0.0.1")))
35+
{
36+
return true;
37+
}
38+
return false;
39+
}
40+
41+
else if ((loadurl.length >= 9) && (loadurl.substring(0, 9) === "localhost"))
42+
{
43+
return true;
44+
}
45+
else if ((loadurl.length >= 17) && (loadurl.substring(0, 17) === "127.0.0.1"))
46+
{
47+
return true;
48+
}
49+
//check for a filename as url
50+
else if ((loadurl.length > 0) && (((loadurl.charCodeAt(0) >= 65) && (loadurl.charCodeAt(0) <= 90)) || ((loadurl.charCodeAt(0) >= 97) && (loadurl.charCodeAt(0) <= 122))))
51+
{
52+
return true;
53+
}
54+
return false;
55+
}
56+
57+
// function to check whether the page being loaded is from local machine or remote machine for IPV6 machines
58+
function isLocalIPV6App(loadurl)
59+
{
60+
if ((loadurl.length > 4) && (loadurl.substring(0, 4) === "http"))
61+
{
62+
if ((loadurl.length >= 12) && (loadurl.substring(0, 12) === "http://[::1]"))
63+
{
64+
return true;
65+
}
66+
else if ((loadurl.length >= 24) && (loadurl.substring(0, 24) === "http://[0:0:0:0:0:0:0:1]"))
67+
{
68+
return true;
69+
}
70+
else if ((loadurl.length >= 13) && (loadurl.substring(0, 13) === "https://[::1]"))
71+
{
72+
return true;
73+
}
74+
else if ((loadurl.length >= 25) && (loadurl.substring(0, 25) === "https://[0:0:0:0:0:0:0:1]"))
75+
{
76+
return true;
77+
}
78+
return false;
79+
}
80+
81+
else if ((loadurl.length >= 5) && (loadurl.substring(0, 5) === "[::1]"))
82+
{
83+
return true;
84+
}
85+
else if ((loadurl.length >= 17) && (loadurl.substring(0, 17) === "[0:0:0:0:0:0:0:1]"))
86+
{
87+
return true;
88+
}
89+
else if ((loadurl.length >= 4) && (loadurl.substring(0, 4) === "::1"))
90+
{
91+
return true;
92+
}
93+
else if ((loadurl.length >= 16) && (loadurl.substring(0, 16) === "0:0:0:0:0:0:0:1"))
94+
{
95+
return true;
96+
}
97+
//check for a filename as url
98+
else if ((loadurl.length > 0) && (((loadurl.charCodeAt(0) >= 65) && (loadurl.charCodeAt(0) <= 90)) || ((loadurl.charCodeAt(0) >= 97) && (loadurl.charCodeAt(0) <= 122))))
99+
{
100+
return true;
101+
}
102+
return false;
103+
}
25104

26105
function AppSceneContext(params) { // container, innerscene, packageUrl) {
27106
// this.container = params.sceneContainer;
@@ -42,7 +121,6 @@ function AppSceneContext(params) { // container, innerscene, packageUrl) {
42121
this.queryParams = {};
43122
this.packageUrl = params.packageUrl;
44123
}
45-
this.accessControl = new AccessControl(this.packageUrl);
46124
this.defaultBaseUri = "";
47125
this.basePackageUri = "";
48126
this.sandbox = {};
@@ -482,7 +560,16 @@ AppSceneContext.prototype.include = function(filePath, currentXModule) {
482560
onImportComplete([modData, origFilePath]);
483561
return;
484562
} else if( filePath === 'http' || filePath === 'https' ) {
485-
modData = filePath === 'http' ? new http_wrap(_this.accessControl) : new https_wrap(_this.accessControl);
563+
if (filePath === 'http')
564+
{
565+
modData = new http_wrap();
566+
}
567+
else
568+
{
569+
modData = new https_wrap();
570+
}
571+
var localapp = (isLocalApp(_this.packageUrl) || isLocalIPV6App(_this.packageUrl));
572+
modData.setLocalApp(localapp);
486573
onImportComplete([modData, origFilePath]);
487574
return;
488575
} else if( filePath.substring(0, 9) === "px:scene.") {

examples/pxScene2d/src/rcvrcore/http_wrap.js

Lines changed: 103 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,122 @@
22

33
var http = require('http');
44

5-
function HttpWrap(accessControl) {
6-
// do not expose accessControl through 'this.accessControl'
7-
var _accessControl = accessControl;
5+
function isLocalAccess(reqOptions)
6+
{
7+
if (((reqOptions.hostname) && ((reqOptions.hostname === "localhost") || (reqOptions.hostname === "127.0.0.1"))) || ((reqOptions.host) && ((reqOptions.host === "localhost") || (reqOptions.host === "127.0.0.1"))))
8+
{
9+
return true;
10+
}
11+
else if (((reqOptions.hostname) && ((reqOptions.hostname === "[::1]") || (reqOptions.hostname === "[0:0:0:0:0:0:0:1]"))) || ((reqOptions.host) && ((reqOptions.host === "[::1]") || (reqOptions.host === "[0:0:0:0:0:0:0:1]"))))
12+
{
13+
return true;
14+
}
15+
else if (((reqOptions.hostname) && ((reqOptions.hostname === "::1") || (reqOptions.hostname === "0:0:0:0:0:0:0:1"))) || ((reqOptions.host) && ((reqOptions.host === "::1") || (reqOptions.host === "0:0:0:0:0:0:0:1"))))
16+
{
17+
return true;
18+
}
19+
return false;
20+
}
21+
22+
function HttpWrap()
23+
{
24+
this.localApp = false;
25+
}
26+
27+
HttpWrap.prototype.IncomingMessage = http.IncomingMessage;
28+
HttpWrap.prototype.METHODS = http.METHODS;
29+
HttpWrap.prototype.OutgoingMessage = http.OutgoingMessage;
830

9-
HttpWrap.prototype.IncomingMessage = http.IncomingMessage;
10-
HttpWrap.prototype.METHODS = http.METHODS;
11-
HttpWrap.prototype.OutgoingMessage = http.OutgoingMessage;
12-
HttpWrap.prototype.globalAgent = http.globalAgent;
31+
HttpWrap.prototype.setLocalApp = function(isLocalApp) {
32+
this.localApp = isLocalApp;
33+
};
1334

14-
// Server functionality needs to be disabled.
15-
//HttpWrap.prototype.ServerResponse = http.ServerResponse;
16-
//HttpWrap.prototype.STATUS_CODES = http.STATUS_CODES;
17-
//HttpWrap.prototype.Server = http.Server;
18-
//HttpWrap.prototype.createServer = http.createServer;
35+
HttpWrap.prototype.getLocalApp = function() {
36+
return this.localApp;
37+
};
1938

20-
HttpWrap.prototype.request = function (options, cb) {
21-
if (_accessControl) {
22-
if (_accessControl.isLocalAccessFromRemote(options)) {
23-
console.log("localhost urls cannot be accessed by remote applications");
24-
return;
25-
}
26-
options = _accessControl.wrapHttpRequestOptions(options);
27-
cb = _accessControl.wrapHttpResponseCallback(cb);
39+
HttpWrap.prototype.request = function(options, cb) {
40+
if (true == isLocalAccess(options))
41+
{
42+
if (false == this.localApp)
43+
{
44+
console.log("localhost urls cannot be accessed by remote applications");
45+
return;
2846
}
29-
return http.request(options, cb);
30-
};
47+
}
48+
return http.request(options, cb);
49+
};
3150

32-
// http.request == new http.ClientRequest
33-
HttpWrap.prototype.ClientRequest = function (options, cb) {
34-
if (_accessControl) {
35-
if (_accessControl.isLocalAccessFromRemote(options)) {
36-
console.log("localhost urls cannot be accessed by remote applications");
37-
return;
38-
}
39-
options = _accessControl.wrapHttpRequestOptions(options);
40-
cb = _accessControl.wrapHttpResponseCallback(cb);
51+
HttpWrap.prototype.ClientRequest = function(options, cb) {
52+
if (true == isLocalAccess(options))
53+
{
54+
if (false == this.localApp)
55+
{
56+
console.log("localhost urls cannot be accessed by remote applications");
57+
return;
4158
}
42-
return http.ClientRequest(options, cb);
43-
};
59+
}
60+
return http.ClientRequest(options, cb);
61+
};
4462

45-
// http.get == http.request (+end)
46-
HttpWrap.prototype.get = function (options, cb) {
47-
if (_accessControl) {
48-
if (_accessControl.isLocalAccessFromRemote(options)) {
49-
console.log("localhost urls cannot be accessed by remote applications");
50-
return;
51-
}
52-
options = _accessControl.wrapHttpRequestOptions(options);
53-
cb = _accessControl.wrapHttpResponseCallback(cb);
63+
HttpWrap.prototype.get = function(options, cb) {
64+
if (true == isLocalAccess(options))
65+
{
66+
if (false == this.localApp)
67+
{
68+
console.log("localhost urls cannot be accessed by remote applications");
69+
return;
5470
}
55-
return http.get(options, cb);
56-
};
71+
}
72+
return http.get(options, cb);
73+
};
5774

58-
HttpWrap.prototype.Agent = function (options) {
59-
if (_accessControl) {
60-
if (_accessControl.isLocalAccessFromRemote(options)) {
61-
console.log("localhost urls cannot be accessed by remote applications");
62-
return;
63-
}
75+
HttpWrap.prototype.Agent = function(options) {
76+
if (true == isLocalAccess(options))
77+
{
78+
if (false == this.localApp)
79+
{
80+
console.log("localhost urls cannot be accessed by remote applications");
81+
return;
6482
}
65-
return http.Agent(options);
66-
};
83+
}
84+
return http.Agent(options);
85+
};
6786

68-
// TODO CORS?
69-
// deprecated
70-
HttpWrap.prototype.Client = function (port, host) {
71-
if (_accessControl) {
72-
if (_accessControl.isLocalAccessFromRemote(host)) {
73-
console.log("localhost urls cannot be accessed by remote applications");
74-
return;
75-
}
87+
HttpWrap.prototype.globalAgent = function(options) {
88+
if (true == isLocalAccess(options))
89+
{
90+
if (false == this.localApp)
91+
{
92+
console.log("localhost urls cannot be accessed by remote applications");
93+
return;
7694
}
77-
return http.Client(port, host);
78-
};
95+
}
96+
return http.globalAgent(options);
97+
};
7998

80-
// TODO CORS?
81-
// deprecated
82-
HttpWrap.prototype.createClient = function (port, host) {
83-
if (_accessControl) {
84-
if (_accessControl.isLocalAccessFromRemote(host)) {
85-
console.log("localhost urls cannot be accessed by remote applications");
86-
return;
87-
}
99+
HttpWrap.prototype.Client = function(port, host) {
100+
if ((host === "localhost") || (host === "127.0.0.1"))
101+
{
102+
if (false == this.localApp)
103+
{
104+
console.log("localhost urls cannot be accessed by remote applications");
105+
return;
88106
}
89-
return http.createClient(port, host);
90-
};
91-
}
107+
}
108+
return http.Client(port,host);
109+
};
110+
111+
HttpWrap.prototype.createClient = function(port, host) {
112+
if ((host === "localhost") || (host === "127.0.0.1"))
113+
{
114+
if (false == this.localApp)
115+
{
116+
console.log("localhost urls cannot be accessed by remote applications");
117+
return;
118+
}
119+
}
120+
return http.createClient(port,host);
121+
};
92122

93123
module.exports = HttpWrap;

0 commit comments

Comments
 (0)