[Zope3-checkins] CVS: Zope3/src/zope/app/http - __init__.py:1.1 configure.zcml:1.1 put.py:1.1 traversal.py:1.1

Jim Fulton jim@zope.com
Fri, 7 Feb 2003 10:59:38 -0500


Update of /cvs-repository/Zope3/src/zope/app/http
In directory cvs.zope.org:/tmp/cvs-serv24670/src/zope/app/http

Added Files:
	__init__.py configure.zcml put.py traversal.py 
Log Message:
Implemented HTTP PUT. Do do this, I had to:

- Implement working HTTP publication, request, response

- Change the server setup so that rather than having a Browser
  server and an XML-RPC server, there is an HTTP server that 
  uses:

  o Browser request, response, and publication for browser (GET, POST, 
    and HEAD) requests,

  o XMLRPC request, response, and publication for xml-rpc (POST 
    w content-type=='text/xml') requests,

  o HTTP request, response, and publication for all other HTTP requests.

  XML-RPC now runs on the same port, 8080, as browser requests.

- Implemented HEAD.

- Implemented some simple PUT views that use the
  file-system-reprentation adapter framework. (This is the replacement
  for VFS that is also used by FTP and may be used as part of
  file-system synchronization.) 
  



=== Added File Zope3/src/zope/app/http/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/http/configure.zcml ===
<zopeConfigure xmlns='http://namespaces.zope.org/zope'>

<content class=".put.NullResource">
  <allow interface="zope.app.interfaces.http.INullResource" />
</content>

<view 
  for="zope.app.interfaces.container.ISimpleReadContainer"
  name="_traverse"
  type="zope.publisher.interfaces.http.IHTTPPresentation"
  factory=".traversal.ContainerTraverser"
  permission="zope.Public"
  allowed_interface="zope.publisher.interfaces.IPublishTraverse"
  />

<view 
  for="zope.app.interfaces.container.IItemContainer"
  name="_traverse"
  type="zope.publisher.interfaces.http.IHTTPPresentation"
  factory=".traversal.ItemTraverser"
  permission="zope.Public"
  allowed_interface="zope.publisher.interfaces.IPublishTraverse"
  />

<view 
  for="zope.app.interfaces.http.INullResource"
  name="PUT"
  type="zope.publisher.interfaces.http.IHTTPPresentation"
  factory=".put.NullPUT"
  permission="zope.Public"
  allowed_attributes="PUT"
  />

<view 
  for="*"
  name="PUT"
  type="zope.publisher.interfaces.http.IHTTPPresentation"
  factory=".put.FilePUT"
  permission="zope.Public"
  allowed_attributes="PUT"
  />

</zopeConfigure>


=== Added File Zope3/src/zope/app/http/put.py ===
##############################################################################
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: put.py,v 1.1 2003/02/07 15:59:37 jim Exp $
"""
__metaclass__ = type

from zope.component import getAdapter, queryAdapter
from zope.app.interfaces.http import INullResource
from zope.app.interfaces.file import IWriteFile, IWriteDirectory, IFileFactory
from zope.app.interfaces.container import IZopeWriteContainer
from zope.app.event import publish
from zope.app.event.objectevent import ObjectCreatedEvent
        
class NullResource:
    """Object representing objects to be created by a PUT.
    """

    __implements__ = INullResource

    def __init__(self, container, name):
        self.container = container
        self.name = name


class NullPUT:
    """Put handler for null resources (new file-like things)

    This view creates new objects in containers.
    """

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def PUT(self):
        body = self.request.bodyFile
        name = self.context.name
        container = self.context.container

        # Find the extension
        ext_start = name.rfind('.')
        if ext_start > 0:
            ext = name[ext_start:]
        else:
            ext = "."

        # Get a "directory" surrogate for the container
        dir = queryAdapter(container, IWriteDirectory)

        # Get the zope adapter for that
        dir = getAdapter(dir, IZopeWriteContainer)

        # Now try to get a custom factory for he container
        factory = queryAdapter(container, IFileFactory, name=ext)

        # Fall back to a non-custom one
        if factory is None:
            factory = getAdapter(container, IFileFactory)

        # XXX Need to add support for large files
        data = body.read()

        newfile = factory(name,
                          self.request.getHeader('content-type', ''),
                          data)
        publish(self.context, ObjectCreatedEvent(newfile))
        dir.setObject(name, newfile)
        
        return ''

class FilePUT:
    """Put handler for existing file-like things
    """

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def PUT(self):
        body = self.request.bodyFile
        file = self.context
        adapter = getAdapter(file, IWriteFile)

        # XXX Need to add support for large files
        data = body.read()

        adapter.write(data)

        return ''

        


=== Added File Zope3/src/zope/app/http/traversal.py ===
##############################################################################
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
##############################################################################
"""HTTPP-specific traversers

For straight HTTP, we need to be able to create null resources.
We also never traverse to views.

$Id: traversal.py,v 1.1 2003/02/07 15:59:37 jim Exp $
"""
__metaclass__ = type

from zope.publisher.interfaces import IPublishTraverse
from zope.app.interfaces.container import ISimpleReadContainer, IItemContainer
from zope.app.http.put import NullResource
from zope.exceptions import NotFoundError

class ContainerTraverser:
    
    __implements__ = IPublishTraverse
    __used_for__ = ISimpleReadContainer

    def __init__(self, container, request):
        self.context = container
        self.request = request

    def publishTraverse(self, request, name):
        subob = self.context.get(name, None)
        if subob is None:
            subob = self.nullResource(request, name)

        return subob

    def nullResource(self, request, name):
        # we traversed to something that doesn't exist.

        # The name must be the last name in the path, so the traversal
        # name stack better be empty:
        if request.getTraversalStack():
            raise NotFoundError(self.context, name, request)

        # This should only happen for a PUT:
        if request.method != 'PUT':
            raise NotFoundError(self.context, name, request)

        return NullResource(self.context, name)

class ItemTraverser(ContainerTraverser):

    __used_for__ = IItemContainer

    def publishTraverse(self, request, name):
        context = self.context

        try:
            return context[name]
        except KeyError:
            return self.nullResource(request, name)