HttpChannelHandler.js
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Generated by CoffeeScript 1.8.0
var Channel, HttpChannelHandler, channelManager, handleCreate, handleError, handleGet, handleOptions, handlePost, setCORSHeaders, setCacheHeaders, utils, _;
_ = require('underscore');
utils = require('./utils');
Channel = require('./Channel');
channelManager = require('./channelManager');
module.exports = utils.registerClass(HttpChannelHandler = (function() {
function HttpChannelHandler(pathPrefix) {
this.pathPrefix = pathPrefix;
if (this.pathPrefix === '/ws/client') {
this.isClient = true;
} else if (this.pathPrefix === '/ws/target') {
this.isClient = false;
} else {
utils.pitch("invalid pathPrefix: " + this.pathPrefix);
}
this.isTarget = !this.isClient;
}
HttpChannelHandler.prototype.handle = function(request, response, uri) {
var channelName, parts;
setCORSHeaders(request, response);
setCacheHeaders(request, response);
if (uri[0] !== '/') {
return handleError(request, response, 404);
}
if (uri === '/') {
if (request.method === 'OPTIONS') {
return handleOptions(request, response);
}
if (request.method === 'POST') {
return handleCreate(this.pathPrefix, this.isClient, request, response);
}
return handleError(request, response, 405);
}
parts = uri.split('/');
if (parts.length > 2) {
return handleError(request, response, 404);
}
channelName = parts[1];
if (request.method === 'OPTIONS') {
return handleOptions(request, response);
}
if (request.method === 'GET') {
return handleGet(request, response, channelName);
}
if (request.method === 'POST') {
return handlePost(request, response, channelName);
}
return handleError(request, response, 405);
};
return HttpChannelHandler;
})());
handleCreate = function(pathPrefix, isClient, request, response) {
var channel, id, remoteAddress, _ref, _ref1;
id = (_ref = request.body) != null ? _ref.id : void 0;
remoteAddress = ((_ref1 = request.connection) != null ? _ref1.remoteAddress : void 0) || "";
channel = new Channel(pathPrefix, id, remoteAddress, isClient);
response.contentType('application/json');
return response.send(JSON.stringify({
channel: channel.name,
id: channel.id
}));
};
handleGet = function(request, response, channelName) {
var channel, remoteAddress, _ref;
remoteAddress = ((_ref = request.connection) != null ? _ref.remoteAddress : void 0) || "";
channel = channelManager.getChannel(channelName, remoteAddress);
if (!channel) {
return handleError(request, response, 404);
}
return channel.getMessages((function(_this) {
return function(messages) {
if (channel.isClosed) {
return handleError(request, response, 404);
}
if (!messages) {
return handleError(request, response, 404);
}
response.contentType('application/json');
return response.send(JSON.stringify(messages));
};
})(this));
};
handlePost = function(request, response, channelName) {
var channel, remoteAddress, _ref;
remoteAddress = ((_ref = request.connection) != null ? _ref.remoteAddress : void 0) || "";
channel = channelManager.getChannel(channelName, remoteAddress);
if (!channel) {
return handleError(request, response, 404);
}
channel.handleMessages(request.body);
return response.send('');
};
handleOptions = function(request, response) {
return response.send('');
};
handleError = function(request, response, status) {
return response.send(status);
};
setCORSHeaders = function(request, response) {
var origin;
origin = request.header('Origin');
if (!origin) {
return;
}
response.header('Access-Control-Allow-Origin', origin);
response.header('Access-Control-Max-Age', '600');
return response.header('Access-Control-Allow-Methods', 'GET, POST');
};
setCacheHeaders = function(request, response) {
response.header('Pragma', 'no-cache');
response.header('Expires', '0');
response.header('Cache-Control', 'no-cache');
return response.header('Cache-Control', 'no-store');
};