from allmydata.util.assertutil import _assert
from allmydata import codec, hashtree
from allmydata.Crypto.Cipher import AES
-from allmydata.uri import unpack_uri
+from allmydata.uri import unpack_uri, unpack_extension
from allmydata.interfaces import IDownloadTarget, IDownloader
from allmydata.encode import NotEnoughPeersError
# "vb is %s but should be a ValidatedBucket" % (vb,)
def _unpack_uri_extension_data(self, data):
- d = {}
- while data:
- colon = data.index(":")
- key = data[:colon]
- data = data[colon+1:]
-
- colon = data.index(":")
- number = data[:colon]
- length = int(number)
- data = data[colon+1:]
-
- value = data[:length]
- assert data[length] == ","
- data = data[length+1:]
-
- d[key] = value
-
- # convert certain things to numbers
- for intkey in ("size", "segment_size", "num_segments",
- "needed_shares", "total_shares"):
- if intkey in d:
- d[intkey] = int(d[intkey])
- return d
+ return unpack_extension(data)
def _obtain_uri_extension(self, ignored):
# all shareholders are supposed to have a copy of uri_extension, and
# -*- test-case-name: allmydata.test.test_encode -*-
-import re
from zope.interface import implements
from twisted.internet import defer
from twisted.python import log
+from allmydata import uri
from allmydata.hashtree import HashTree
from allmydata.Crypto.Cipher import AES
from allmydata.util import mathutil, hashutil
def send_uri_extension_to_all_shareholders(self):
log.msg("%s: sending uri_extension" % self)
- pieces = []
- for k in sorted(self.uri_extension_data.keys()):
- value = self.uri_extension_data[k]
- if isinstance(value, (int, long)):
- value = "%d" % value
- assert isinstance(value, str), k
- assert re.match(r'^[a-zA-Z_\-]+$', k)
- pieces.append(k + ":" + hashutil.netstring(value))
- uri_extension = "".join(pieces)
+ uri_extension = uri.pack_extension(self.uri_extension_data)
self.uri_extension_hash = hashutil.uri_extension_hash(uri_extension)
dl = []
for shareid in self.landlords.keys():
-from allmydata.util import idlib
+import re
+from allmydata.util import idlib, hashutil
# the URI shall be an ascii representation of the file. It shall contain
# enough information to retrieve and validate the contents. It shall be
return d
+def pack_extension(data):
+ pieces = []
+ for k in sorted(data.keys()):
+ value = data[k]
+ if isinstance(value, (int, long)):
+ value = "%d" % value
+ assert isinstance(value, str), k
+ assert re.match(r'^[a-zA-Z_\-]+$', k)
+ pieces.append(k + ":" + hashutil.netstring(value))
+ uri_extension = "".join(pieces)
+ return uri_extension
+
+def unpack_extension(data):
+ d = {}
+ while data:
+ colon = data.index(":")
+ key = data[:colon]
+ data = data[colon+1:]
+
+ colon = data.index(":")
+ number = data[:colon]
+ length = int(number)
+ data = data[colon+1:]
+
+ value = data[:length]
+ assert data[length] == ","
+ data = data[length+1:]
+
+ d[key] = value
+
+ # convert certain things to numbers
+ for intkey in ("size", "segment_size", "num_segments",
+ "needed_shares", "total_shares"):
+ if intkey in d:
+ d[intkey] = int(d[intkey])
+ return d
+