+ Base 64 function. Fixed error message
authorPhilippe Le Hegaret <plh@w3.org>
Sun, 28 Aug 2011 09:55:52 -0400
changeset 122 b29d8749df21
parent 121 1f8c4d9b2f51
child 123 1e0a2eef52b9
+ Base 64 function. Fixed error message
src/main/resources/scripts/Base64.js
src/main/resources/scripts/Utils.js
src/main/resources/scripts/tracker.js
src/main/resources/scripts/tracker_test.js
src/main/resources/templates/geolocation.ssp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/resources/scripts/Base64.js	Sun Aug 28 09:55:52 2011 -0400
@@ -0,0 +1,63 @@
+var B64 = {
+  _internal : {
+  	  END_OF_INPUT : -1,
+	  base64Chars : new Array(
+    'A','B','C','D','E','F','G','H',
+    'I','J','K','L','M','N','O','P',
+    'Q','R','S','T','U','V','W','X',
+    'Y','Z','a','b','c','d','e','f',
+    'g','h','i','j','k','l','m','n',
+    'o','p','q','r','s','t','u','v',
+    'w','x','y','z','0','1','2','3',
+    '4','5','6','7','8','9','+','/'
+     ),
+
+     base64Str : null,
+     base64Count : 0,
+	 setBase64Str : function (str) {
+        this.base64Str = str;
+        this.base64Count = 0;
+     },
+     readBase64 : function () {    
+       if (!this.base64Str) return this.END_OF_INPUT;
+       if (this.base64Count >= this.base64Str.length) return this.END_OF_INPUT;
+         var c = this.base64Str.charCodeAt(this.base64Count) & 0xff;
+         this.base64Count++;
+         return c;
+     }
+  },
+  encode : function (str) {
+    this._internal.setBase64Str(str);
+    var result = '';
+    var inBuffer = new Array(3);
+    var lineCount = 0;
+    var done = false;
+    while (!done && (inBuffer[0] = this._internal.readBase64()) != this._internal.END_OF_INPUT){
+        inBuffer[1] = this._internal.readBase64();
+        inBuffer[2] = this._internal.readBase64();
+        result += (this._internal.base64Chars[ inBuffer[0] >> 2 ]);
+        if (inBuffer[1] != this._internal.END_OF_INPUT){
+            result += (this._internal.base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
+            if (inBuffer[2] != this._internal.END_OF_INPUT){
+                result += (this._internal.base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
+                result += (this._internal.base64Chars [inBuffer[2] & 0x3F]);
+            } else {
+                result += (this._internal.base64Chars [((inBuffer[1] << 2) & 0x3c)]);
+                result += ('=');
+                done = true;
+            }
+        } else {
+            result += (this._internal.base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
+            result += ('=');
+            result += ('=');
+            done = true;
+        }
+        lineCount += 4;
+        if (lineCount >= 76){
+            result += ('\n');
+            lineCount = 0;
+        }
+    }
+    return result;
+   }
+}
--- a/src/main/resources/scripts/Utils.js	Fri Aug 26 16:01:59 2011 -0400
+++ b/src/main/resources/scripts/Utils.js	Sun Aug 28 09:55:52 2011 -0400
@@ -163,7 +163,7 @@
     	if (!!resource.userId && !!resource.passwd) {
     		// we do basic auth
     		xhr.setRequestHeader("Authorization",
-    							Base64.encode('Basic ' + resource.userId + ':' + resource.passwd));
+    							B64.encode('Basic ' + resource.userId + ':' + resource.passwd));
     	}
     	xhr.send(resource.data);
     } catch (e) {
--- a/src/main/resources/scripts/tracker.js	Fri Aug 26 16:01:59 2011 -0400
+++ b/src/main/resources/scripts/tracker.js	Sun Aug 28 09:55:52 2011 -0400
@@ -249,7 +249,7 @@
     	}
   	}
   	Util.postAsJSON(user.getPostRunURI(),
-  	                { userId : user.userId, passwd : user.passwd, data: app.currentActivity },
+  	                { userId : user.userId, passwd : user.passwd, data : app.currentActivity },
   	                success, error);
   } else {
   	Util.log("[ERROR] Your workout doesn't contain enough events");
--- a/src/main/resources/scripts/tracker_test.js	Fri Aug 26 16:01:59 2011 -0400
+++ b/src/main/resources/scripts/tracker_test.js	Sun Aug 28 09:55:52 2011 -0400
@@ -15,14 +15,14 @@
   	}
   }
   var error = function(obj) {
-    if (typeof(obj) === "XMLHttpRequest") {
+    if (String(obj) === "[object XMLHttpRequest]") {
     	Util.log("[ERROR] XHR " + obj.status + " "  + obj.statusText);
     } else {
   		Util.log("XHR error " + obj);
   	}
-  	if (!Util.store.set("track_gps_app", app.currentActivity)) {
+  	if (!Util.store.set("track_gps_app", a)) {
     	Util.log("[ERROR] Can't store the workout");
     }
   }
-  Util.postAsJSON(user.getPostRunURI(), a, success, error);
+  Util.postAsJSON(user.getPostRunURI(), { userId : user.userId, passwd : user.passwd, data : a }, success, error);
 }
--- a/src/main/resources/templates/geolocation.ssp	Fri Aug 26 16:01:59 2011 -0400
+++ b/src/main/resources/templates/geolocation.ssp	Sun Aug 28 09:55:52 2011 -0400
@@ -9,6 +9,8 @@
  <title>HTML5 Track</title>
  <script src='/scripts/json2.js'>
  </script>
+ <script src='/scripts/Base64.js'>
+ </script>
  <script src="/scripts/et-min.js"></script>
  <script src='/scripts/Utils.js'>
  </script>