In NdnProtocolInfo.jsm, added splitUri, and use in ndnProtocolService.js and ndnToolbar.js.
diff --git a/js/ndnProtocol/components/ndnProtocolService.js b/js/ndnProtocol/components/ndnProtocolService.js
index ca8cc1b..69e0675 100644
--- a/js/ndnProtocol/components/ndnProtocolService.js
+++ b/js/ndnProtocol/components/ndnProtocolService.js
@@ -30,12 +30,9 @@
newURI: function(aSpec, aOriginCharset, aBaseURI)
{
// We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
- var spec = aSpec.trim();
- var preSearch = spec.split('?', 1)[0];
- var searchAndHash = spec.substr(preSearch.length).trim();
-
+ var uriParts = splitUri(aSpec);
var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
- uri.spec = preSearch.trim() + searchAndHash;
+ uri.spec = uriParts.protocol + uriParts.name + uriParts.search + uriParts.hash;
return uri;
},
@@ -44,21 +41,12 @@
var thisNdnProtocol = this;
try {
- // Decode manually since nsIURI doesn't have selectors for hash, etc.
- var spec = aURI.spec.trim();
- var preHash = spec.split('#', 1)[0];
- var hash = spec.substr(preHash.length).trim();
- var preSearch = preHash.split('?', 1)[0];
- var search = preHash.substr(preSearch.length).trim();
- // Set nameString to the preSearch without the protocol.
- var nameString = preSearch.trim();
- if (nameString.indexOf(':') >= 0)
- nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
+ var uriParts = splitUri(aURI.spec);
var template = new Interest(new Name([]));
// Use the same default as NDN.expressInterest.
template.interestLifetime = 4000; // milliseconds
- var searchWithoutNdn = extractNdnSearch(search, template);
+ var searchWithoutNdn = extractNdnSearch(uriParts.search, template);
var segmentTemplate = new Interest(new Name([]));
// Only use the interest selectors which make sense for fetching further segments.
@@ -67,14 +55,14 @@
segmentTemplate.interestLifetime = template.interestLifetime;
var requestContent = function(contentListener) {
- var name = new Name(nameString);
+ var name = new Name(uriParts.name);
// TODO: Strip off an ending implicit digest before checking the last component?
var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
// Use the same NDN object each time.
thisNdnProtocol.ndn.expressInterest(name,
new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
- aURI.originCharset, searchWithoutNdn + hash, segmentTemplate),
+ aURI.originCharset, searchWithoutNdn + uriParts.hash, segmentTemplate),
template);
};
diff --git a/js/ndnProtocol/content/ndnToolbar.js b/js/ndnProtocol/content/ndnToolbar.js
index 20518a3..129913a 100644
--- a/js/ndnProtocol/content/ndnToolbar.js
+++ b/js/ndnProtocol/content/ndnToolbar.js
@@ -8,17 +8,8 @@
}
// Parse the same as in ndnProtocolService newChannel.
- var spec = window._content.document.location.href.trim();
- var preHash = spec.split('#', 1)[0];
- var hash = spec.substr(preHash.length).trim();
- var preSearch = preHash.split('?', 1)[0];
- var search = preHash.substr(preSearch.length).trim();
- // Set nameString to the preSearch without the protocol.
- var nameString = preSearch.trim();
- if (nameString.indexOf(':') >= 0)
- nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
-
- var name = new Name(nameString);
+ var uriParts = splitUri(window._content.document.location.href);
+ var name = new Name(uriParts.name);
var indexOfVersion = getIndexOfVersion(name);
if (indexOfVersion < 0) {
alert("The ndn address does not have a version");
@@ -26,9 +17,10 @@
}
var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
- var searchWithChildSelector = (search == "" ? "?" : search + "&") + "ndn.ChildSelector=1";
+ var searchWithChildSelector =
+ (uriParts.search == "" ? "?" : uriParts.search + "&") + "ndn.ChildSelector=1";
- var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + hash;
+ var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash;
window._content.document.location = uri;
}
diff --git a/js/ndnProtocol/modules/NdnProtocolInfo.jsm b/js/ndnProtocol/modules/NdnProtocolInfo.jsm
index a2fb10d..bf8bf55 100644
--- a/js/ndnProtocol/modules/NdnProtocolInfo.jsm
+++ b/js/ndnProtocol/modules/NdnProtocolInfo.jsm
@@ -3,7 +3,7 @@
* See COPYING for copyright and distribution information.
*/
-var EXPORTED_SYMBOLS = ["addNdnHubChangedListener", "setConnectedNdnHub"];
+var EXPORTED_SYMBOLS = ["addNdnHubChangedListener", "setConnectedNdnHub", "splitUri"];
const Cc = Components.classes;
const Ci = Components.interfaces;
@@ -51,4 +51,32 @@
// Ignore error from the listener.
}
}
+}
+
+/*
+ * Split the URI spec and return an object with protocol (including ':'), name,
+ * search (including '?') and hash value (including '#').
+ * All result strings are trimmed. This does not unescape the name.
+ * The name may include a host and port.
+ */
+function splitUri(spec) {
+ spec = spec.trim();
+ var result = {};
+ var preHash = spec.split('#', 1)[0];
+ result.hash = spec.substr(preHash.length).trim();
+ var preSearch = preHash.split('?', 1)[0];
+ result.search = preHash.substr(preSearch.length).trim();
+
+ preSearch = preSearch.trim();
+ var colonIndex = preSearch.indexOf(':');
+ if (colonIndex >= 0) {
+ result.protocol = preSearch.substr(0, colonIndex + 1).trim();
+ result.name = preSearch.substr(colonIndex + 1).trim();
+ }
+ else {
+ result.protocol = "";
+ result.name = preSearch;
+ }
+
+ return result;
}
\ No newline at end of file