Commit a77c6550 authored by Xiaowu Zhang's avatar Xiaowu Zhang

add url check

parent f1cdd9f5
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
<script src="./playlist.js" type="text/javascript"></script> <script src="./playlist.js" type="text/javascript"></script>
<body> <body>
<input type="text" class="inputIp" placeholder="input ip: http://xxx.xxx.xxx.xxx:port/(xxx is between 0 ~ 255)"/> <input type="text" class="inputIp" placeholder="input ip: http://xxx.xxx.xxx.xxx:port/(xxx is between 0 ~ 255) or url"/>
<input type="search" class="research" placeholder="research..."/> <input type="search" class="research" placeholder="research..."/>
......
/*global window, rJS, RSVP, console, jQuery, $, JSON, Handlebars, /*global window, rJS, RSVP, console, jQuery, $, JSON, Handlebars,
loopEventListener, RegExp, alert, promiseEventListener */ loopEventListener, RegExp, alert, promiseEventListener */
/*jslint maxlen:180, nomen: true */ /*jslint maxlen:180, nomen: true, regexp: true */
(function (window, rJS, $, Handlebars, loopEventListener) { (function (window, rJS, $, Handlebars, loopEventListener) {
...@@ -10,13 +10,21 @@ ...@@ -10,13 +10,21 @@
.getElementById('network').innerHTML, .getElementById('network').innerHTML,
network = Handlebars.compile(network_source); network = Handlebars.compile(network_source);
function endWith(str, end) {
return (str.indexOf(end, str.length - end.length) !== -1);
}
function contain(str, s) {
return (str.indexOf(s) !== -1);
}
function checkUrl(url) {
return (contain(url, ".com") || contain(url, ".net") || contain(url, ".fr"));
}
function checkIp(ip) { function checkIp(ip) {
var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/; var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
if (!re.test(ip)) { return re.test(ip);
return false;
}
return true;
} }
function check(value) { function check(value) {
...@@ -27,29 +35,40 @@ ...@@ -27,29 +35,40 @@
portEnd, portEnd,
ipValue = value; ipValue = value;
g.__element.getElementsByTagName('ul')[0].innerHTML = " "; g.__element.getElementsByTagName('ul')[0].innerHTML = " ";
if (ipValue.indexOf("/", ipValue.length - 1) === -1) { http = ipValue.indexOf("http");
info.innerHTML = " not end with /"; if (ipValue.indexOf("www.") !== -1) {
return; if (http === -1) {
} info.innerHTML = "please start with http:// or https://";
http = ipValue.indexOf("http://"); return;
ipValue = ipValue.substring(ipValue.indexOf("//") + 2); }
port = ipValue.indexOf(":"); if (!checkUrl(ipValue)) {
portEnd = ipValue.indexOf(":/"); info.innerHTML = "url invalide";
if (port !== -1) { return;
ipValue = ipValue.substring(0, port); }
} } else {
if (http === -1) { ipValue = ipValue.substring(ipValue.indexOf("//") + 2);
info.innerHTML = " please start ip with http://"; port = ipValue.indexOf(":");
return; portEnd = ipValue.indexOf(":/");
} if (port !== -1) {
if (port === -1 || portEnd !== -1) { ipValue = ipValue.substring(0, port);
info.innerHTML = "input port number"; }
return; if (http === -1) {
} info.innerHTML = "please start ip with http:// or https://";
if (checkIp(ipValue) === false) { return;
info.innerHTML = }
"invalide ip: ip should like xxx.xxx.xxx.xxx(xxx is between 0 ~ 255)"; if (checkIp(ipValue) === false) {
return; info.innerHTML =
"invalide ip: ip should like xxx.xxx.xxx.xxx(xxx is between 0 ~ 255)";
return;
}
if (port === -1 || portEnd !== -1) {
info.innerHTML = "input port number";
return;
}
if (!endWith(value, "/")) {
info.innerHTML = "not end with /";
return;
}
} }
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment