Commit a77c6550 authored by Xiaowu Zhang's avatar Xiaowu Zhang

add url check

parent f1cdd9f5
......@@ -23,7 +23,7 @@
<script src="./playlist.js" type="text/javascript"></script>
<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..."/>
......
/*global window, rJS, RSVP, console, jQuery, $, JSON, Handlebars,
loopEventListener, RegExp, alert, promiseEventListener */
/*jslint maxlen:180, nomen: true */
/*jslint maxlen:180, nomen: true, regexp: true */
(function (window, rJS, $, Handlebars, loopEventListener) {
......@@ -10,13 +10,21 @@
.getElementById('network').innerHTML,
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) {
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 false;
}
return true;
return re.test(ip);
}
function check(value) {
......@@ -27,11 +35,17 @@
portEnd,
ipValue = value;
g.__element.getElementsByTagName('ul')[0].innerHTML = " ";
if (ipValue.indexOf("/", ipValue.length - 1) === -1) {
info.innerHTML = " not end with /";
http = ipValue.indexOf("http");
if (ipValue.indexOf("www.") !== -1) {
if (http === -1) {
info.innerHTML = "please start with http:// or https://";
return;
}
if (!checkUrl(ipValue)) {
info.innerHTML = "url invalide";
return;
}
http = ipValue.indexOf("http://");
} else {
ipValue = ipValue.substring(ipValue.indexOf("//") + 2);
port = ipValue.indexOf(":");
portEnd = ipValue.indexOf(":/");
......@@ -39,18 +53,23 @@
ipValue = ipValue.substring(0, port);
}
if (http === -1) {
info.innerHTML = " please start ip with http://";
info.innerHTML = "please start ip with http:// or https://";
return;
}
if (checkIp(ipValue) === false) {
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 (checkIp(ipValue) === false) {
info.innerHTML =
"invalide ip: ip should like xxx.xxx.xxx.xxx(xxx is between 0 ~ 255)";
if (!endWith(value, "/")) {
info.innerHTML = "not end with /";
return;
}
}
return new RSVP.Queue()
.push(function () {
return g.plSave({"ip": value});
......
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