SF.net SVN: fclient:[694] trunk/fclient/src/fclient/lib/fcp2
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-07-20 11:47:13
|
Revision: 694
http://fclient.svn.sourceforge.net/fclient/?rev=694&view=rev
Author: jUrner
Date: 2008-07-20 11:46:26 +0000 (Sun, 20 Jul 2008)
Log Message:
-----------
permanent redirect are now handled automatically (if desired)
(should work in theory)
Modified Paths:
--------------
trunk/fclient/src/fclient/lib/fcp2/client.py
trunk/fclient/src/fclient/lib/fcp2/consts.py
trunk/fclient/src/fclient/lib/fcp2/test/test_client.py
Modified: trunk/fclient/src/fclient/lib/fcp2/client.py
===================================================================
--- trunk/fclient/src/fclient/lib/fcp2/client.py 2008-07-20 10:50:47 UTC (rev 693)
+++ trunk/fclient/src/fclient/lib/fcp2/client.py 2008-07-20 11:46:26 UTC (rev 694)
@@ -358,6 +358,7 @@
initTime=None,
persistentUserData='',
handleFilenameCollision=False,
+ handlePermanentRedirect=False,
):
"""Registers a request
@param msg: message to register
@@ -368,7 +369,8 @@
@param initTime: (int) init time of the request or None to set it to now
@param persistentUserData: (str) anyuser defined persistent data
@param userData: (any) any user defined non persistent data
-
+ @param handlePermanentRedirect: (bool) if True, permanent redirect are handled automatically. for example USK@.../-1
+ will look for the current edition. if handlePermanentRedirect is True, the request is automatically resend to retrieve the current edition
@return: (str) identifer of therequest
@note: the identifier returned is unique to the client but may not be unique to the node
"""
@@ -382,6 +384,7 @@
msg['InitTime'] = time.time() if initTime is None else initTime
if requestType & consts.ConstRequestType.MaskGet:
msg['HandleFilenameCollision'] = handleFilenameCollision
+ msg['HandlePermanentRedirect'] = handlePermanentRedirect
msg['UserData'] = userData
msg['PersistentUserData'] = persistentUserData
msg['ClientToken'] = ''
@@ -855,16 +858,27 @@
self.events.RequestRemoved(initialRequest)
return True
+ if code == consts.ConstFetchError.PermanentRedirect:
+ if initialRequest['HandlePermanentRedirect']:
+ initialRequest['PermanentRedirectHandled'] = True
+ initialRequest['Modified'] = {consts.ConstRequestModified.PermanentRedirect: initialRequest['URI']}
+ initialRequest['URI'] = msg['RedirectURI']
+ initialRequest.updatePersistentParams()
+ self.events.RequestModified(initialRequest)
+ self.sendMessage(initialRequest)
+ return True
+
# check if it is one of our requests for key information
if code == consts.ConstFetchError.TooBig and initialRequest['RequestType'] & consts.ConstRequestType.GetKeyInfo:
initialRequest['MetadataContentType'] = msg.get('ExpectedMetadata.ContentType', '')
initialRequest['DataLength'] = msg.get('ExpectedDataLength', -1)
initialRequest['RequestStatus'] |= consts.ConstRequestStatus.Success
self._finalizeRequest(msg, initialRequest, self.events.RequestCompleted)
- else:
- initialRequest['ErrorMessage'] = msg
- initialRequest['RequestStatus'] |= consts.ConstRequestStatus.Error
- self._finalizeRequest(msg, initialRequest, self.events.RequestFailed)
+ return True
+
+ initialRequest['ErrorMessage'] = msg
+ initialRequest['RequestStatus'] |= consts.ConstRequestStatus.Error
+ self._finalizeRequest(msg, initialRequest, self.events.RequestFailed)
return True
@@ -1261,6 +1275,7 @@
userData,
persistentUserData,
handleFilenameCollision=False,
+ handlePermanentRedirect=False,
**messageParams
):
"""Requests a key from the node
@@ -1270,6 +1285,8 @@
@param persistentUserData: any string to associate to the request as persistent data or None
@param handleFilenameCollision: (bool) if True, the file is automatically renamed if "filename" collides
with a file object already present on disk
+ @param handlePermanentRedirect: (bool) if True, permanent redirect are handled automatically. for example USK@.../-1
+ will look for the current edition. if handlePermanentRedirect is True, the request is automatically resend to retrieve the current edition
@param messageParams: keyword arguments to pass along with the ClientGet message (uppercase first letter!!).
If the value of a keyword is None, it is ignored.
@@ -1284,6 +1301,7 @@
msg,
requestType,
handleFilenameCollision=handleFilenameCollision,
+ handlePermanentRedirect=handlePermanentRedirect,
persistentUserData=persistentUserData,
userData=userData,
)
@@ -1305,6 +1323,7 @@
userData=None,
persistentUserData='',
+ handlePermanentRedirect=False,
):
"""Requests datae from the node
@@ -1320,6 +1339,8 @@
@param priorityClass: (L{consts.ConstPriority}) priority of the request
@param userData: any non persistent data to associate to the request
@param persistentUserData: any string to associate to the request as persistent data
+ @param handlePermanentRedirect: (bool) if True, permanent redirect are handled automatically. for example USK@.../-1
+ will look for the current edition. if handlePermanentRedirect is True, the request is automatically resend to retrieve the current edition
@return: (str) request identifier
@@ -1338,6 +1359,7 @@
consts.ConstRequestType.GetData,
userData,
persistentUserData,
+ handlePermanentRedirect=handlePermanentRedirect,
# Fcp params
AllowedMimeTypes = allowedMimeTypes,
@@ -1371,6 +1393,7 @@
userData=None,
persistentUserData='',
handleFilenameCollision=False,
+ handlePermanentRedirect=False,
):
"""Requests a file from the node
@@ -1385,11 +1408,14 @@
@param maxSize: (int) maximum size of the file in bytes or None to set no limited
@param persistence: (L{consts.ConstPersistence}) persistence of the request
@param priorityClass: (L{consts.ConstPriority}) priority of the request
+ @param userData: any non persistent data to associate to the request
+ @param persistentUserData: any string to associate to the request as persistent data
@param handleFilenameCollision: (bool) if True, the file is automatically renamed if "filename" collides
with a file object already present on disk
- @param userData: any non persistent data to associate to the request
- @param persistentUserData: any string to associate to the request as persistent data
+ @param handlePermanentRedirect: (bool) if True, permanent redirect are handled automatically. for example USK@.../-1
+ will look for the current edition. if handlePermanentRedirect is True, the request is automatically resend to retrieve the current edition
+
@return: (str) request identifier
@event: (L{events.Events.RequestCompleted}) triggered when the request is complete
@@ -1407,6 +1433,7 @@
userData,
persistentUserData,
handleFilenameCollision=handleFilenameCollision,
+ handlePermanentRedirect=handlePermanentRedirect,
# Fcp params
AllowedMimeTypes = allowedMimeTypes,
@@ -1436,6 +1463,7 @@
userData=None,
persistentUserData='',
+ handlePermanentRedirect=False,
):
"""Requests info about a key
@@ -1448,6 +1476,8 @@
@param priorityClass: (L{consts.ConstPriority}) priority of the request
@param userData: any non persistent data to associate to the request
@param persistentUserData: any string to associate to the request as persistent data
+ @param handlePermanentRedirect: (bool) if True, permanent redirect are handled automatically. for example USK@.../-1
+ will look for the current edition. if handlePermanentRedirect is True, the request is automatically resend to retrieve the current edition
@return: (str) request identifier
@@ -1466,6 +1496,7 @@
consts.ConstRequestType.GetKeyInfo,
userData,
persistentUserData,
+ handlePermanentRedirect=handlePermanentRedirect,
# Fcp params
Global=False,
Modified: trunk/fclient/src/fclient/lib/fcp2/consts.py
===================================================================
--- trunk/fclient/src/fclient/lib/fcp2/consts.py 2008-07-20 10:50:47 UTC (rev 693)
+++ trunk/fclient/src/fclient/lib/fcp2/consts.py 2008-07-20 11:46:26 UTC (rev 694)
@@ -418,15 +418,17 @@
@cvar Identifier: the identifier has been moodified
@cvar PersistentUserData: persistent user data has been modified
@cvar PriorityClass: the priority class has been modified
+ @cvar PermanentRedirect: url of a request has been modified because of a redirect
@note: the FcModified member of the params dict of a request may contain
one or more of the bitflags
"""
Null = 0x0
- Filename = 0x8
+ Filename = 0x2
Identifier = 0x4
- PersistentUserData = 0x1
- PriorityClass = 0x2
+ PersistentUserData = 0x8
+ PriorityClass = 0x10
+ PermanentRedirect = 0x20
class ConstRequestStatus(_BaseBitFlags):
"""Request status flags
Modified: trunk/fclient/src/fclient/lib/fcp2/test/test_client.py
===================================================================
--- trunk/fclient/src/fclient/lib/fcp2/test/test_client.py 2008-07-20 10:50:47 UTC (rev 693)
+++ trunk/fclient/src/fclient/lib/fcp2/test/test_client.py 2008-07-20 11:46:26 UTC (rev 694)
@@ -2607,7 +2607,88 @@
self.assertHasNextMessage(None)
self.failIf(requestsAll)
+#***********************************************************************************
+#
+#***********************************************************************************
+class TestHandlePermanentRedirect(BaseTestClient):
+
+ def test_doHandleRedirect(self):
+ self.connectClient()
+
+# request a file
+ myKey = fcp2.Key('USK@blah,blah,blah/foo/-1')
+ myIdentifier = self.client.getData(
+ myKey,
+ handlePermanentRedirect=True,
+ )
+ myRequest = self.client.getRequest(myIdentifier)
+ requestsAll = self.client.getRequests()
+ # client schould send a ClientGet
+ self.assertHasNextMessage(fcp2.MsgClientGet)
+
+ # respond with a Getfailed
+ self.sendResponseMessage(
+ 'GetFailed',
+ Identifier=myIdentifier,
+ Code=27, # PermanentRedirect
+ RedirectURI=fcp2.Key('USK@blah,blah,blah/foo/99'),
+ )
+
+ # check if RequestModified event has been triggered
+ msg = self.assertHasNextEvent(
+ self.client.events.RequestModified,
+ fcp2.MsgClientGet,
+ )
+
+ modified = msg['Modified'][fcp2.ConstRequestModified.PermanentRedirect]
+ self.assertEqual(modified, myKey)
+
+ # check flags
+ self.failUnless(myRequest['HandlePermanentRedirect'])
+ self.failUnless(myRequest['PermanentRedirectHandled'])
+
+ # client should resend request
+ self.assertHasNextMessage(fcp2.MsgClientGet)
+ self.assertHasNextEvent(None)
+ self.assertHasNextMessage(None)
+
+
+ def test_doNotHandleRedirect(self):
+ self.connectClient()
+
+# request a file
+ myKey = fcp2.Key('USK@blah,blah,blah/foo/-1')
+ myIdentifier = self.client.getData(
+ myKey,
+ handlePermanentRedirect=False,
+ )
+ myRequest = self.client.getRequest(myIdentifier)
+ requestsAll = self.client.getRequests()
+
+ # client schould send a ClientGet
+ self.assertHasNextMessage(fcp2.MsgClientGet)
+
+ # respond with a Getfailed
+ self.sendResponseMessage(
+ 'GetFailed',
+ Identifier=myIdentifier,
+ Code=27, # PermanentRedirect
+ RedirectURI=fcp2.Key('USK@blah,blah,blah/foo/99'),
+ )
+
+ # check if RequestModified event has been triggered
+ msg = self.assertHasNextEvent(
+ self.client.events.RequestFailed,
+ fcp2.MsgClientGet,
+ )
+
+ # check flags
+ self.failIf(myRequest['HandlePermanentRedirect'])
+ self.failIf(myRequest['PermanentRedirectHandled'])
+ self.assertHasNextEvent(None)
+ self.assertHasNextMessage(None)
+
#***********************************************************************************
#
#***********************************************************************************
@@ -2996,6 +3077,7 @@
Test_DDA,
TestCollisions,
+ TestHandlePermanentRedirect,
Test_getPluginInfo,
Test_sendPluginMessage,
Test_generateKeypair,
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|