Skip to content

Commit 8e4b41c

Browse files
committed
jsonp support for status
1 parent a938c08 commit 8e4b41c

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

app.js

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,36 @@ function getStatus()
5151
return retVal;
5252
}
5353

54+
function parseParams(query)
55+
{
56+
var params = {};
57+
if (query != null && query.length > 0)
58+
{
59+
var pairs = query.split("&");
60+
for (var loop = 0; loop < pairs.length; loop++)
61+
{
62+
var kv = pairs[loop].split('=');
63+
if (kv && kv.length == 2)
64+
{
65+
if (kv[0] in params)
66+
{
67+
params[kv[0]].push(querystring.unescape(kv[1].replace(/\+/g, " ")));
68+
}
69+
else
70+
{
71+
params[kv[0]] = [ querystring.unescape(kv[1].replace(/\+/g, " ")) ];
72+
}
73+
}
74+
else
75+
{
76+
//LATER: do something?
77+
}
78+
}
79+
}
80+
81+
return params;
82+
}
83+
5484
function redirect(response, location)
5585
{
5686
response.writeHead(302, {"Content-Type": "text/plain", "Location" : location});
@@ -78,10 +108,28 @@ function serveFile(response, contentType, fileName)
78108
});
79109
}
80110

81-
function serveStatus(response)
111+
function serveStatus(query, response)
82112
{
83-
response.writeHead(200, {"Content-Type": "text/plain"});
84-
response.write(JSON.stringify(getStatus()));
113+
response.writeHead(200, {
114+
"Content-Type": "text/plain",
115+
'Access-Control-Allow-Origin': '*',
116+
'Access-Control-Allow-Methods': 'POST, GET',
117+
'Access-Control-Max-Age': '604800',
118+
});
119+
120+
var params = parseParams(query);
121+
122+
if ('callback' in params)
123+
{
124+
response.write(params['callback'][0]);
125+
response.write("(");
126+
response.write(JSON.stringify(getStatus()));
127+
response.write(")");
128+
}
129+
else
130+
{
131+
response.write(JSON.stringify(getStatus()));
132+
}
85133
response.end();
86134
}
87135

@@ -124,27 +172,7 @@ function serveTest(query, response)
124172

125173
try
126174
{
127-
var params = {};
128-
var pairs = query.split("&");
129-
for (var loop = 0; loop < pairs.length; loop++)
130-
{
131-
var kv = pairs[loop].split('=');
132-
if (kv && kv.length == 2)
133-
{
134-
if (kv[0] in params)
135-
{
136-
params[kv[0]].push(querystring.unescape(kv[1].replace(/\+/g, " ")));
137-
}
138-
else
139-
{
140-
params[kv[0]] = [ querystring.unescape(kv[1].replace(/\+/g, " ")) ];
141-
}
142-
}
143-
else
144-
{
145-
//LATER: do something?
146-
}
147-
}
175+
var params = parseParams(query);
148176

149177
retVal["params"] = JSON.stringify(params);
150178

@@ -232,8 +260,8 @@ function serveTest(query, response)
232260
html.push("\t\t<tr>\n");
233261
html.push("\t\t\t<th style=\"text-align:center;\">Test</th>\n");
234262
html.push("\t\t\t<th>Input</th>");
235-
html.push("\t\t\t<th>input.replace</th>");
236-
html.push("\t\t\t<th>input.split</th>");
263+
html.push("\t\t\t<th>input.replace()</th>");
264+
html.push("\t\t\t<th>input.split()[]</th>");
237265
html.push("\t\t\t<th>regex.test()</th>");
238266
html.push("\t\t\t<th>regex.exec().index</th>");
239267
html.push("\t\t\t<th>regex.exec()[]</th>");
@@ -381,7 +409,7 @@ http.createServer(function (request, response)
381409
}
382410
else if (parsedUrl.pathname == '/status.json')
383411
{
384-
serveStatus(response);
412+
serveStatus(parsedUrl.query, response);
385413
}
386414
else if (parsedUrl.pathname == '/robots.txt')
387415
{

deploy.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
#
33
# deploy the js backend to CloudFoundry
44
#
5+
# NOTE: you may have to "vmc login" first
56
vmc update

0 commit comments

Comments
 (0)