home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / url-canonicalizer.js < prev    next >
Text File  |  2006-08-07  |  6KB  |  147 lines

  1. function PROT_URLCanonicalizer() {
  2. throw new Error("No need to instantiate a canonicalizer at this point.");
  3. }
  4. PROT_URLCanonicalizer.debugZone = "urlcanonicalizer";
  5. PROT_URLCanonicalizer.hexChars_ = "0123456789ABCDEF";
  6. PROT_URLCanonicalizer.hexPairToInt_ = function(hh) {
  7. return Number("0x" + hh);
  8. }
  9. PROT_URLCanonicalizer.toHex_ = function(val) {
  10. var retVal = PROT_URLCanonicalizer.hexChars_.charAt((val >> 4) & 15) +
  11. PROT_URLCanonicalizer.hexChars_.charAt(val & 15);
  12. return retVal;
  13. }
  14. PROT_URLCanonicalizer.canonicalizeURL_ = function(url) {
  15. var arrayOfASCIIVals = PROT_URLCanonicalizer.fullyDecodeURL_(url);
  16. return PROT_URLCanonicalizer.specialEncodeURL_(arrayOfASCIIVals);
  17. }
  18. PROT_URLCanonicalizer.fullyDecodeURL_ = function(url) {
  19. var nextIteration = url.split("");
  20. var output = [];
  21. while (nextIteration.length) {
  22. var decodedAPercent = false;
  23. var thisIteration = nextIteration;
  24. var nextIteration = [];
  25. var i = 0;
  26. while (i < thisIteration.length) {
  27. var c = thisIteration[i];
  28. if (c == "%" && i + 2 < thisIteration.length) {
  29. var asciiVal =
  30. PROT_URLCanonicalizer.hexPairToInt_(thisIteration[i + 1] +
  31. thisIteration[i + 2]);
  32. if (!isNaN(asciiVal)) {
  33. i += 2;                   // Valid HH sequence; consume it
  34. if (asciiVal == 0)        // We special case nulls
  35. asciiVal = 1;
  36. c = String.fromCharCode(asciiVal);
  37. if (c == "%")
  38. decodedAPercent = true;
  39. }
  40. }
  41. if (decodedAPercent)
  42. nextIteration[nextIteration.length] = c;
  43. else
  44. output[output.length] = c.charCodeAt(0);
  45. ++i;
  46. }
  47. }
  48. return output;
  49. }
  50. PROT_URLCanonicalizer.fullyDecodeURLAsString_ = function(url) {
  51. var arrayOfASCIIVals = PROT_URLCanonicalizer.fullyDecodeURL_(url);
  52. var s = "";
  53. for (var i = 0; i < arrayOfASCIIVals.length; i++)
  54. s += String.fromCharCode(arrayOfASCIIVals[i]);
  55. return s;
  56. }
  57. PROT_URLCanonicalizer.specialEncodeURL_ = function(arrayOfASCIIValues) {
  58. var output = [];
  59. for (var i = 0; i < arrayOfASCIIValues.length; i++) {
  60. var n = arrayOfASCIIValues[i];
  61. if (n <= 32 || n == 37 || n >= 127)
  62. output.push("%" + ((!n) ? "01" : PROT_URLCanonicalizer.toHex_(n)));
  63. else
  64. output.push(String.fromCharCode(n));
  65. }
  66. return output.join("");
  67. }
  68. function TEST_PROT_URLCanonicalizer() {
  69. if (G_GDEBUG) {
  70. var z = "urlcanonicalizer UNITTEST";
  71. G_debugService.enableZone(z);
  72. G_Debug(z, "Starting");
  73. var hexify = PROT_URLCanonicalizer.toHex_;
  74. var shouldHaveLeadingZeros = hexify(0) + hexify(1);
  75. G_Assert(z, shouldHaveLeadingZeros == "0001",
  76. "Need to append leading zeros to hex rep value <= 15 !")
  77. var dec = PROT_URLCanonicalizer.fullyDecodeURLAsString_;
  78. G_Assert(z, dec("") == "", "decoding empty string");
  79. var allCharsEncoded = "";
  80. var allCharsEncodedLowercase = "";
  81. var allCharsAsString = "";
  82. allCharsEncoded += "%01";
  83. allCharsEncodedLowercase += "%01";
  84. allCharsAsString += String.fromCharCode(1);
  85. for (var i = 1; i < 256; i++) {
  86. allCharsEncoded += "%" + PROT_URLCanonicalizer.toHex_(i);
  87. allCharsEncodedLowercase += "%" +
  88. PROT_URLCanonicalizer.toHex_(i).toLowerCase();
  89. allCharsAsString += String.fromCharCode(i);
  90. }
  91. G_Assert(z, dec(allCharsEncoded) == allCharsAsString, "decoding escaped");
  92. G_Assert(z, dec(allCharsEncodedLowercase) == allCharsAsString,
  93. "decoding lowercase");
  94. G_Assert(z, dec("%") == "%", "1 percent");
  95. G_Assert(z, dec("%xx") == "%xx", "1 percent, two non-hex");
  96. G_Assert(z, dec("%%") == "%%", "2 percent");
  97. G_Assert(z, dec("%%%") == "%%%", "3 percent");
  98. G_Assert(z, dec("%%%%") == "%%%%", "4 percent");
  99. G_Assert(z, dec("%1") == "%1", "1 percent, one nonhex");
  100. G_Assert(z, dec("%1z") == "%1z", "1 percent, two nonhex");
  101. G_Assert(z, dec("a%1z") == "a%1z", "nonhex, 1 percent, two nonhex");
  102. G_Assert(z, dec("abc%d%e%fg%hij%klmno%") == "abc%d%e%fg%hij%klmno%",
  103. "lots of percents, no hex");
  104. G_Assert(z, dec("%25") == "%", "single-encoded %");
  105. G_Assert(z, dec("%25%32%35") == "%", "double-encoded %");
  106. G_Assert(z, dec("asdf%25%32%35asd") == "asdf%asd", "double-encoded % 2");
  107. G_Assert(z, dec("%%%25%32%35asd%%") == "%%%asd%%", "double-encoded % 3");
  108. G_Assert(z, dec("%25%32%35%25%32%35%25%32%35") == "%%%",
  109. "sequenctial double-encoded %");
  110. G_Assert(z, dec("%2525252525252525") == "%", "many-encoded %");
  111. G_Assert(z, dec("%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A22%252833%252944_55%252B") == "~a!b@c#d$e%f^00&11*22(33)44_55+",
  112. "4x-encoded string");
  113. var enc = PROT_URLCanonicalizer.specialEncodeURL_;
  114. G_Assert(z, enc([]) == "", "encoding empty array");
  115. var no = [];
  116. var noAsString = "";
  117. for (var i = 33; i < 127; i++)
  118. if (i != 37) {                      // skip %
  119. no.push(i);
  120. noAsString += String.fromCharCode(i);
  121. }
  122. G_Assert(z, enc(no) == noAsString, "chars to not encode");
  123. var yes = [];
  124. var yesAsString = "";
  125. var yesExpectedString = "";
  126. yes.push(0);
  127. yesAsString += String.fromCharCode(1);
  128. yesExpectedString += "%01";
  129. for (var i = 1; i < 256; i++)
  130. if (i < 33 || i == 37 || i > 126) {
  131. yes.push(i);
  132. yesAsString += String.fromCharCode(i);
  133. var hex = i.toString(16).toUpperCase();
  134. yesExpectedString += "%" + ((i < 16) ? "0" : "") + hex;
  135. }
  136. G_Assert(z, enc(yes) == yesExpectedString, "chars to encode");
  137. var c = PROT_URLCanonicalizer.canonicalizeURL_;
  138. G_Assert(z, c("http://www.google.com") == "http://www.google.com",
  139. "http://www.google.com");
  140. G_Assert(z, c("http://%31%36%38%2e%31%38%38%2e%39%39%2e%32%36/%2E%73%65%63%75%72%65/%77%77%77%2E%65%62%61%79%2E%63%6F%6D/") == "http://168.188.99.26/.secure/www.ebay.com/",
  141. "fully encoded ebay");
  142. G_Assert(z, c("http://195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/") == "http://195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/",
  143. "long url with spaces that stays same");
  144. G_Debug(z, "PASSED");
  145. }
  146. }
  147.