Commits

watermelon  committed b6f7a32

set timeout for ajax calls;
eliminate redundancy of url regular expression;
greatly improve public key validation;

  • Participants
  • Parent commits 79ab512

Comments (0)

Files changed (5)

File src/main/client/comm/Communicator.js

 			url: server_url + login + '/' + file_type,
 			type: "GET",
 			crossDomain: true,
+			timeout: bookmaak.AJAX_TIMEOUT_MS,
 			mimeType: mime_type,
 			complete: function(jqXHR, textStatus) {
 				
 		jQuery.ajax({
 			url: server_url + login + '/' + file.type,
 			type: "PUT",
+			crossDomain: true,
+			timeout: bookmaak.AJAX_TIMEOUT_MS,
 			processData: false,//XXX that does not change anything.... on put we get options request too..
 			data: file.data,
-			crossDomain: true,
 			complete: function(jqXHR, textStatus) {
 				
 				switch(jqXHR.status) {

File src/main/client/constants.js

 //TODO needs documentation
 bookmaak.ERROR_PREFIX = "BOOKMAAK: ";
 
-bookmaak.SYM_KEY_LENGTH = 128;
+bookmaak.SYM_KEY_LENGTH = 128;
+
+bookmaak.AJAX_TIMEOUT_MS = 3000;

File src/main/client/controller/Controller.js

 		aync: false,//we want to probe the server before proceeding TODO maybe we change the wohle process and make the login a real login...
 		type: "HEAD",
 		crossDomain: true,
+		timeout: bookmaak.AJAX_TIMEOUT_MS,
 		complete: function(jqXHR, textStatus) {
 			
 			//TODO for debuging
 		aync: false,//TODO does not work for crossdomain requests... damn
 		type: "HEAD",
 		crossDomain: true,
+		timeout: bookmaak.AJAX_TIMEOUT_MS,
 		complete: function(jqXHR, textStatus) {
 			
 			console.debug("userExists request complete. status: "+jqXHR.status+" textStatus: "+textStatus);

File src/main/client/ui/UiController.js

 	var server_url_is_valid = bookmaak.util.validation.validateServerUrl(_server_url);
 	if(server_url_is_valid) {//fix it if necessary
 		
-		//TODO make a constant and merge with the bookmaak.util.validation.validateServerUrl definition
-		var re_fix_url = /^(?:http:\/\/){0,1}([A-Za-z0-9\.\:\-]+)(?:\/){0,1}$/;
-		
 		//set the server url in Controller.
 		bookmaak.controller.Controller.
-		server_url = _server_url.replace(re_fix_url, "http://$1/");
+		server_url = bookmaak.util.validation.fixServerUrl(_server_url);
 		
 		console.log("server url fixed:"+bookmaak.controller.Controller.server_url);
 	}

File src/main/client/util/validation.js

 	
 	//validate login
 	//begins with a alphachar, followed by at least 3 alphanumeric chars
-	var re_login = /^[A-Za-z]+[A-Za-z0-9]{3,}$/;
+	var re_login = /^\s*[A-Za-z]+[A-Za-z0-9]{3,}\s*$/;
 	
 	console.log(login.match(re_login) || "no match");
 	
 	console.debug("validateRSAPublicKeyPEM("+rsa_public_key_pem+")");
 	
 	//validate public key
-	//TODO make a more sophisticated regexp
-	var re_public_key = /.*/;
-	//TODO here seems to be a bug
+	var re_public_key = new RegExp(
+		"^\s*"
+		+"-{5}(?:BEGIN PUBLIC KEY)-{5}[\n\r]"
+		+"(?:[A-Za-z0-9+\/=]{1,64}[\r\n])+"
+		+"-{5}(?:END PUBLIC KEY)-{5}"
+		+"\s*$");
+	
 	console.log(rsa_public_key_pem.match(re_public_key) || "no match");
 	
 	if(re_public_key.test(rsa_public_key_pem))
 };
 
 
+/**
+ * RE_VALIDATE_URL = RegExp
+ * 
+ * The regualr expression, an url needs to match.<br/>
+ * It's capturing the important url part in between "http://" and the "/"
+ **/
+const RE_VALIDATE_URL = /^\s*(?:http:\/\/){0,1}([A-Za-z0-9\.\:\-]+)(?:\/){0,1}\s*$/;
+/**
+ * REPLACE_FIX_URL = String
+ * 
+ * The replacement string. if used by 
+ * url.replace(re_validat_url, replace_fix_url), an url to be used in ajax calls
+ * is constructed.
+ **/
+const REPLACE_FIX_URL = "http://$1/";
+
 
 /**
  * bookmaak.util.validation.validateServerUrl(String) -> Boolean
 	
 	console.debug("validateServerUrl("+server_url+")");
 	
-	//validate the server url anything, with or without leading http:// and trailing /
-	var re_server_url = /^(?:http:\/\/){0,1}([A-Za-z0-9\.\:\-]+)(?:\/){0,1}$/;
+	//validate the server url
+	console.debug(server_url.match(RE_VALIDATE_URL) || "no match");
 	
-	console.debug(server_url.match(re_server_url) || "no match");
-	
-	if(re_server_url.test(server_url))
+	if(RE_VALIDATE_URL.test(server_url))
 		return true;
 	else
 		return false;
 };
 
+/**
+ * bookmaak.util.validation.fixServerUrl(String) -> String
+ * 
+ * Fixes the server url.<br/>
+ * 		-The leading "http://" is added in case it's missing<br/>
+ * 		-The trailing "/" is added in case it's missing<br/>
+ * 		-Leading and trailing whitespaces are striped
+ * 
+ * **server_url** the server url<br/>
+ * returns true the fixed **server_url**
+ **/
+bookmaak.util.validation.fixServerUrl = function(server_url) {
+	
+	if(!bookmaak.util.isType(server_url, bookmaak.util.TYPE.STRING))
+		return false;
+	
+	console.debug("validateServerUrl("+server_url+")");
+	
+	//validate the server url
+	console.debug(server_url.match(RE_VALIDATE_URL) || "no match");
+	
+	return server_url.replace(RE_VALIDATE_URL, REPLACE_FIX_URL);
+};
 
 
-