Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Better query parsing and error handling.

  • Loading branch information...
commit 156141845042d0ac6d231823c311b2a1c7ffa86d 1 parent e48ac04
@scottgonzalez authored
Showing with 19 additions and 14 deletions.
  1. +19 −14 lib/server.js
View
33 lib/server.js
@@ -78,36 +78,41 @@ var handlers = [
} },
{ path: "/recv", handler: function(channel, request, response) {
var query = qs.parse(url.parse(request.url).query),
- since = query.since,
- id = query.id,
- session;
+ since = parseInt(query.since, 10),
+ session = channel.sessions[query.id];
- if (isNaN(parseInt(since))) {
+ if (!session) {
+ response.simpleJSON(400, { error: "No such session id." });
+ return;
+ }
+
+ if (isNaN(since)) {
response.simpleJSON(400, { error: "Must supply since parameter." });
return;
}
-
- since = parseInt(since, 10);
- session = channel.sessions[id];
- if (session) session.poke();
+
+ session.poke();
channel.query(since, function(messages) {
- if (session) session.poke();
+ session.poke();
response.simpleJSON(200, { messages: messages });
});
} },
{ path: "/send", handler: function(channel, request, response) {
var query = qs.parse(url.parse(request.url).query),
- id = query.id,
- text = query.text;
+ text = query.text,
+ session = channel.sessions[query.id];
- var session = channel.sessions[id];
- if (!session || !text) {
+ if (!session) {
response.simpleJSON(400, { error: "No such session id." });
return;
}
- session.poke();
+ if (!text || !text.length) {
+ response.simpleJSON(400, { error: "Must supply text parameter." });
+ return;
+ }
+ session.poke();
channel.appendMessage(session.nick, "msg", text);
response.simpleJSON(200, {});
} }
Please sign in to comment.
Something went wrong with that request. Please try again.