refactor URI_extension handlers out of encode/download and into uri.py

This commit is contained in:
Brian Warner 2007-06-11 18:25:18 -07:00
parent 48a8c32ed7
commit 382888899b
3 changed files with 43 additions and 35 deletions

View File

@ -9,7 +9,7 @@ from allmydata.util import idlib, mathutil, hashutil
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
@ -342,29 +342,7 @@ class FileDownloader:
# "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

View File

@ -1,9 +1,9 @@
# -*- 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
@ -433,15 +433,7 @@ class Encoder(object):
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():

View File

@ -1,5 +1,6 @@
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
@ -41,3 +42,40 @@ def unpack_uri(uri):
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