SF.net SVN: fclient: [86] trunk/sandbox/fcp/fcp2_0_uri.py
Status: Pre-Alpha
Brought to you by:
jurner
From: <ju...@us...> - 2008-01-30 13:19:44
|
Revision: 86 http://fclient.svn.sourceforge.net/fclient/?rev=86&view=rev Author: jurner Date: 2008-01-30 05:19:47 -0800 (Wed, 30 Jan 2008) Log Message: ----------- check keys to more depth Modified Paths: -------------- trunk/sandbox/fcp/fcp2_0_uri.py Modified: trunk/sandbox/fcp/fcp2_0_uri.py =================================================================== --- trunk/sandbox/fcp/fcp2_0_uri.py 2008-01-29 11:28:06 UTC (rev 85) +++ trunk/sandbox/fcp/fcp2_0_uri.py 2008-01-30 13:19:47 UTC (rev 86) @@ -1,82 +1,93 @@ -"""Freennet Client Protocol uri""" +"""Freennet Client Protocol uri and related methods""" +import base64 import re +import urlparse +#************************************************************************************** +# freenet base64 for keys +#************************************************************************************** +def base64UrlsaveDecode(string): + """Decodes a base64 urlsave encoded string as encoded by freenet + @param string: string to decode + @return: decoded string + + @raise TypeError: if the string can not be decoded + @note: this function handles non-standard encoding as used by freenet (see: freenet/support/base64.java) + """ + # freenet uses - for + and ~ for / + altchars = '-~' + + # padding may be ommitted or not + padding = 4 - len(string) % 4 + if padding: + string += '=' * padding + return base64.b64decode(string, altchars) + +#**************************************************************************************** +# freenet keys +# +# KeyType@32 bytes hash, 32 bytes encryption key, 5 bytes extra +# +# all byte components are base64 encoded. Freenet uses base64 without padding +# and uses the following altchars for urlsave encode: - for + and ~ for / +# see: freenet/support/base64.java +# +# so a key as the user gets it to see is: +# KeyType@43 bytes, 43 bytes, 7 bytes ..of [A-Za-z0-9\-~] +# +#*************************************************************************************** +KeyPat = re.compile( +r''' +^(CHK | SSK | SVK | USK) @ +( + [a-z0-9\-~]{43}, + [a-z0-9\-~]{43}, + [a-z0-9\-~]{7} +) +''', re.I | re.X) #TODO: ignorecase? + + +def keyType(uri): + """Returns the ky type of a freenet key or None if the type could not be determined""" + if uri.startswith('KSK@'): + return 'KSK' + result = KeyPat.match(uri) + if result is None: + return None + return result.group(1) + #********************************************************************* # #********************************************************************* - +def stripUri(uri): + """Strips scheme and location parts from an uri""" + result = urlparse.urlsplit(uri)[2] + result = result.lstrip('/') + return result + + class Uri(object): - """Wrapper class for freenet uris""" + KeyTypeSSK = 'SSK' + KeyTypeKSK = 'KSK' + KeyTypeCHK = 'CHK' + KeyTypeUSK = 'USK' + KeyTypeSVK = 'SVK' + KeyTypeInvalid = '' + KeysTypesAll = (KeyTypeSSK, KeyTypeKSK, KeyTypeCHK, KeyTypeUSK, KeyTypeSVK) - KeySSK = 'SSK@' - KeyKSK = 'KSK@' - KeyCHK = 'CHK@' - KeyUSK = 'USK@' - KeySVK = 'SVK@' - KeyUnknown = '' - KeysAll = (KeySSK, KeyKSK, KeyCHK, KeyUSK, KeySVK) - - ReUriPattern = re.compile('(%s.*?)(?= |\Z)' % '.*?|'.join(KeysAll), re.I) - ReKeyPattern = re.compile('(%s)' % '|'.join(KeysAll), re.I) - def __init__(self, uri): - """ - @param uri: uri to wrap - @param cvar ReUriPattern: pattern matching a freenet uri - @param cvar ReKeyPattern: pattern matching the key type of a freenet uri + self.uri = stripUri(uri) - @note: any dfecorations prefixing the freenet part of the uri uri are stripped if possible + result = keyType(self.uri) + self.keyType = self.KeyTypeInvalid if result is None else result + + def __nonzero__(self): + return self.keyType != self.KeyTypeInvalid - >>> uri = FcpUri('freenet:SSK@foo/bar') - >>> str(uri) - 'SSK@foo/bar' - >>> uri.keyType() == FcpUri.KeySSK - True - >>> uri.split() - ('SSK@foo', 'bar') - >>> uri.fileName() - 'bar' - >>> uri = FcpUri('http://SSK@foo/bar') - >>> str(uri) - 'SSK@foo/bar' - - # uris not containing freenet keys are left unchanged - >>> uri = FcpUri('http://foo/bar') - >>> str(uri) - 'http://foo/bar' - >>> uri.keyType() == FcpUri.KeyUnknown - True - >>> uri.split() - ('http://foo/bar', '') - >>> uri.fileName() - 'http://foo/bar' - - """ - self.uri = uri - - result = self.ReUriPattern.search(uri) - if result is not None: - self.uri = result.group(0) - - def __str__(self): - return str(self.uri) - - def __unicode__(self): - return unicode(self.uri) - - def keyType(self): - """Retuns the key type of the uri - @return: one of the Key* consts - """ - result = self.ReKeyPattern.search(self.uri) - if result is not None: - return result.group(0).upper() - return self.KeyUnknown - def split(self): """Splits the uri @return: tuple(freenet-key, file-name) @@ -86,6 +97,7 @@ return head, tail return self.uri, '' + def fileName(self): """Returns the filename part of the uri @return: str @@ -94,4 +106,6 @@ if tail: return tail return self.uri - + + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |