[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/http/ Conform to RFC2616 for PUT requests by returning a Location-header.

Wolfgang Schnerring wosc at wosc.de
Thu Sep 28 04:25:54 EDT 2006


Log message for revision 70412:
  Conform to RFC2616 for PUT requests by returning a Location-header.
  
  This is a merge from branches/ctheune-issue-125, rev. 70404-70411.
  

Changed:
  A   Zope3/trunk/src/zope/app/http/ftests/
  U   Zope3/trunk/src/zope/app/http/put.py
  U   Zope3/trunk/src/zope/app/http/tests/test_put.py

-=-
Copied: Zope3/trunk/src/zope/app/http/ftests (from rev 70411, Zope3/branches/ctheune-issue-125/src/zope/app/http/ftests)

Modified: Zope3/trunk/src/zope/app/http/put.py
===================================================================
--- Zope3/trunk/src/zope/app/http/put.py	2006-09-28 08:16:37 UTC (rev 70411)
+++ Zope3/trunk/src/zope/app/http/put.py	2006-09-28 08:25:52 UTC (rev 70412)
@@ -20,12 +20,17 @@
 from zope.lifecycleevent import ObjectCreatedEvent
 from zope.interface import implements
 from zope.filerepresentation.interfaces import IWriteFile
-from zope.filerepresentation.interfaces import IWriteDirectory, IFileFactory
+from zope.filerepresentation.interfaces import \
+    IWriteDirectory, IReadDirectory, IFileFactory
 
 from zope.app.http.interfaces import INullResource
 
+from zope.app import zapi
+
+
 class NullResource(object):
     """Object representing objects to be created by a `PUT`.
+
     """
 
     implements(INullResource)
@@ -39,6 +44,7 @@
     """Put handler for null resources (new file-like things)
 
     This view creates new objects in containers.
+
     """
 
     def __init__(self, context, request):
@@ -60,7 +66,9 @@
             ext = "."
 
         # Get a "directory" surrogate for the container
-        dir = IWriteDirectory(container, None)
+        # XXX Argh. Why don't we have a unioned Interface for that?!?
+        dir_write = IWriteDirectory(container)
+        dir_read = IReadDirectory(container)
 
         # Now try to get a custom factory for he container
         factory = queryAdapter(container, IFileFactory, ext)
@@ -75,13 +83,20 @@
         newfile = factory(name, request.getHeader('content-type', ''), data)
         notify(ObjectCreatedEvent(newfile))
 
-        dir[name] = newfile
+        dir_write[name] = newfile
+        # Ickyness with non-predictable support for containment: 
+        #   make sure we get a containment proxy
+        newfile = dir_read[name]
 
         request.response.setStatus(201)
+        request.response.setHeader(
+            'Location', zapi.absoluteURL(newfile, request))
         return ''
 
+
 class FilePUT(object):
     """Put handler for existing file-like things
+
     """
 
     def __init__(self, context, request):
@@ -95,11 +110,9 @@
         file = self.context
         adapter = IWriteFile(file)
 
-        # TODO: Need to add support for large files
-        data = body.read()
+        chunk = body.read(2**6)
+        while chunk:
+            adapter.write(chunk)
+            chunk = body.read(2**6)
 
-        adapter.write(data)
-
         return ''
-
-

Modified: Zope3/trunk/src/zope/app/http/tests/test_put.py
===================================================================
--- Zope3/trunk/src/zope/app/http/tests/test_put.py	2006-09-28 08:16:37 UTC (rev 70411)
+++ Zope3/trunk/src/zope/app/http/tests/test_put.py	2006-09-28 08:25:52 UTC (rev 70412)
@@ -21,10 +21,12 @@
 from zope.interface import implements
 from zope.publisher.browser import TestRequest
 from zope.filerepresentation.interfaces import IWriteFile
-from zope.filerepresentation.interfaces import IWriteDirectory, IFileFactory
+from zope.filerepresentation.interfaces import IWriteDirectory, IReadDirectory, IFileFactory
 
 import zope.app.http.put
 from zope.app.testing.placelesssetup import PlacelessSetup
+from zope.app.component.testing import PlacefulSetup, Place
+from zope.location.interfaces import ILocation
 
 class File(object):
 
@@ -38,21 +40,30 @@
     def write(self, data):
         self.data = data
 
-class Container(object):
+class Container(Place):
 
-    implements(IWriteDirectory, IFileFactory)
+    implements(IWriteDirectory, IReadDirectory, IFileFactory, ILocation)
 
+    __name__ = None
+    __parent__ = None
+
     def __setitem__(self, name, object):
+        object.__name__ = name
+        object.__parent__ = self
         setattr(self, name, object)
 
+    def __getitem__(self, name):
+        return getattr(self, name)
+
     def __call__(self, name, content_type, data):
         return File(name, content_type, data)
 
 
-class TestNullPUT(PlacelessSetup, TestCase):
+class TestNullPUT(PlacefulSetup, TestCase):
 
     def test(self):
-        container = Container()
+        container = Container("put")
+        self.rootFolder["put"] = container
         content = "some content\n for testing"
         request = TestRequest(StringIO(content),
                               {'CONTENT_TYPE': 'test/foo',
@@ -71,13 +82,16 @@
 
         # Check HTTP Response
         self.assertEqual(request.response.getStatus(), 201)
+        self.assertEqual(request.response.getHeader("Location"),
+                         "http://127.0.0.1/put/spam")
 
     def test_bad_content_header(self):
         ## The previous behavour of the PUT method was to fail if the request
         ## object had a key beginning with 'HTTP_CONTENT_' with a status of 501.
         ## This was breaking the new Twisted server, so I am now allowing this
         ## this type of request to be valid.
-        container = Container()
+        container = Container("/put")
+        self.rootFolder["put"] = container
         content = "some content\n for testing"
         request = TestRequest(StringIO(content),
                               {'CONTENT_TYPE': 'test/foo',



More information about the Zope3-Checkins mailing list