From d81b45da6e90aa18984d02cabfc0ea702a9f85c4 Mon Sep 17 00:00:00 2001
From: Mehdi Abaakouk <sileht@sileht.net>
Date: Fri, 27 Jan 2012 11:32:50 +0100
Subject: [PATCH] Add error message for empty argument

---
 lg.py                 |  7 ++++++-
 static/style.css      |  5 +++++
 templates/layout.html | 11 ++++++++++-
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/lg.py b/lg.py
index 71f322c..66a4998 100755
--- a/lg.py
+++ b/lg.py
@@ -10,7 +10,7 @@ from urllib import quote, unquote
 from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve
 
 from bird import BirdSocketSingleton
-from flask import Flask, render_template, jsonify, redirect, session, request
+from flask import Flask, render_template, jsonify, redirect, session, request, abort
 
 app = Flask(__name__)
 app.config.from_pyfile('lg.cfg')
@@ -92,6 +92,7 @@ def error_page(text):
 
 @app.route("/whois/<query>")
 def whois(query):
+	if not query.strip(): abort(404)
 	try:
 		asnum = int(query)
 		query = "as%d"%asnum
@@ -134,6 +135,8 @@ def summary(hosts, proto="ipv4"):
 @app.route("/detail/<hosts>/<proto>")
 def detail(hosts, proto):
 	name = request.args.get('q', '')
+	if not name.strip(): abort(404)
+
 	set_session("detail", hosts, proto, name)
 	command = "show protocols all %s" % name
 
@@ -151,6 +154,7 @@ def detail(hosts, proto):
 @app.route("/traceroute/<hosts>/<proto>")
 def traceroute(hosts, proto):
 	q = request.args.get('q', '')
+	if not q.strip(): abort(404)
 	set_session("traceroute", hosts, proto, q)
 
 	infos = {}
@@ -181,6 +185,7 @@ def show_route_for_detail(hosts, proto):
 
 def show_route(req_type, hosts, proto):
 	expression = unquote(request.args.get('q', ''))
+	if not expression.strip(): abort(404)
 	set_session(req_type, hosts, proto, expression)
 
 	all = (req_type.endswith("detail") and " all" or "" )
diff --git a/static/style.css b/static/style.css
index 22bae5a..2ef0139 100644
--- a/static/style.css
+++ b/static/style.css
@@ -105,6 +105,11 @@ input#ipopup{
 	width: auto;
 }
 
+.error{
+	font-weight:normal;
+	color:red;
+}
+
 /* POPUP */
 .jqifade,
 .popupfade{
diff --git a/templates/layout.html b/templates/layout.html
index 8dff43e..913cd16 100644
--- a/templates/layout.html
+++ b/templates/layout.html
@@ -15,7 +15,7 @@ $( function() {
 		});
 
 		previous_req_type = "{{session.req_type}}".replace("_detail","")
-		function update_view(nopopup){
+		function update_view(nopopup, msg){
 			next_req_type = $("#req_type").val().replace("_detail","")
 			if (previous_req_type != next_req_type) {
 				$("#request_args").val("")
@@ -40,6 +40,11 @@ $( function() {
 						'<input type="text" id="ipopup" name="ipopup" value="' + $("#request_args").val() + '">') +
 						'<br />History:' + 
 						'<ul id="popup_menu">' + menu_html + '</ul>'
+
+					if ( msg != undefined && msg != "" )
+						txt += '<br /><span class="error">Error: ' + msg + '</span>'
+
+
 					$.prompt(txt, 
 						{ 
 						prefix: 'popup',
@@ -104,6 +109,10 @@ $( function() {
 					document.location = "/summary/" + $("#hosts").val() + "/" + $("#proto").val();
 					break;
 				default:
+					if (!$.trim($("#request_args").val())) {
+						update_view(false, "Missing arguments")
+						return
+					}
 					document.location = "/" + $("#req_type").val() + "/" + $("#hosts").val() + "/" + $("#proto").val() + "?q=" + $("#request_args").val() ;
 					break;
 			}