Skip to content

Commit f302bfc

Browse files
author
epriestley
committed
Break Aphlict's flash policy server into a separate class
Summary: Ref T4324. One of the server we start just sends pre-canned XML responses. Separate it out of the main file and hand it all the objects it interacts with in structured, reasonable ways. Test Plan: Hit "Send Test Notification", saw notification, saw flash policy info in the log. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T4324 Differential Revision: https://secure.phabricator.com/D8257
1 parent 28fe44d commit f302bfc

File tree

2 files changed

+74
-23
lines changed

2 files changed

+74
-23
lines changed

support/aphlict/server/aphlict_server.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
var JX = require('./lib/javelin').JX;
1010

11+
JX.require('lib/AphlictFlashPolicyServer', __dirname);
1112
JX.require('lib/AphlictListenerList', __dirname);
1213
JX.require('lib/AphlictLog', __dirname);
1314

@@ -64,33 +65,15 @@ var http = require('http');
6465
var url = require('url');
6566
var querystring = require('querystring');
6667

67-
6868
process.on('uncaughtException', function (err) {
69-
log("\n<<< UNCAUGHT EXCEPTION! >>>\n\n" + err);
69+
debug.log("\n<<< UNCAUGHT EXCEPTION! >>>\n\n" + err);
7070
process.exit(1);
7171
});
7272

73-
function getFlashPolicy() {
74-
return [
75-
'<?xml version="1.0"?>',
76-
'<!DOCTYPE cross-domain-policy SYSTEM ' +
77-
'"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">',
78-
'<cross-domain-policy>',
79-
'<allow-access-from domain="*" to-ports="'+config.port+'"/>',
80-
'</cross-domain-policy>'
81-
].join('\n');
82-
}
83-
84-
net.createServer(function(socket) {
85-
socket.write(getFlashPolicy() + '\0');
86-
socket.end();
87-
88-
debug.log('[' + socket.remoteAddress + '] Sent Flash Policy');
89-
90-
socket.on('error', function (e) {
91-
debug.log('Error in policy server: ' + e);
92-
});
93-
}).listen(843);
73+
var flash_server = new JX.AphlictFlashPolicyServer()
74+
.setDebugLog(debug)
75+
.setAccessPort(config.port)
76+
.start();
9477

9578

9679
var send_server = net.createServer(function(socket) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var JX = require('javelin').JX;
2+
3+
var net = require('net');
4+
5+
/**
6+
* Server which handles cross-domain policy requests for Flash.
7+
*
8+
* var server = new AphlictFlashPolicyServer()
9+
* .setAccessPort(9999)
10+
* .start();
11+
*/
12+
JX.install('AphlictFlashPolicyServer', {
13+
14+
members: {
15+
_server: null,
16+
_port: 843,
17+
_accessPort: null,
18+
_debug: null,
19+
20+
setDebugLog : function(log) {
21+
this._debug = log;
22+
return this;
23+
},
24+
25+
setAccessPort : function(port) {
26+
this._accessPort = port;
27+
return this;
28+
},
29+
30+
start: function() {
31+
this._server = net.createServer(JX.bind(this, this._didConnect));
32+
this._server.listen(this._port);
33+
return this;
34+
},
35+
36+
_didConnect: function(socket) {
37+
this._log('<FlashPolicy> Policy Request From %s', socket.remoteAddress);
38+
39+
socket.on('error', JX.bind(this, this._didSocketError, socket));
40+
41+
socket.write(this._getFlashPolicyResponse());
42+
socket.end();
43+
},
44+
45+
_didSocketError: function(socket, error) {
46+
this._log('<FlashPolicy> Socket Error: %s', error);
47+
},
48+
49+
_log: function(pattern) {
50+
this._debug && this._debug.log.apply(this._debug, arguments);
51+
},
52+
53+
_getFlashPolicyResponse: function() {
54+
var policy = [
55+
'<?xml version="1.0"?>',
56+
'<!DOCTYPE cross-domain-policy SYSTEM ' +
57+
'"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">',
58+
'<cross-domain-policy>',
59+
'<allow-access-from domain="*" to-ports="' + this._accessPort + '"/>',
60+
'</cross-domain-policy>'
61+
];
62+
63+
return policy.join("\n") + "\0";
64+
}
65+
66+
}
67+
68+
});

0 commit comments

Comments
 (0)