[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher/HTTP - IHTTPApplicationRequest.py:1.1.2.3 IHTTPApplicationResponse.py:1.1.4.1 IHTTPResponse.py:1.1.4.1 HTTPRequest.py:1.1.2.22 HTTPResponse.py:1.1.2.14 IHTTPRequest.py:1.1.2.3 BrowserPayload.py:NONE IPayload.py:NONE cgi_names.py:NONE
Jim Fulton
jim@zope.com
Tue, 26 Mar 2002 16:26:30 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/Publisher/HTTP
In directory cvs.zope.org:/tmp/cvs-serv7731/Zope/Publisher/HTTP
Modified Files:
Tag: Zope-3x-branch
HTTPRequest.py HTTPResponse.py IHTTPRequest.py
Added Files:
Tag: Zope-3x-branch
IHTTPApplicationRequest.py IHTTPApplicationResponse.py
IHTTPResponse.py
Removed Files:
Tag: Zope-3x-branch
BrowserPayload.py IPayload.py cgi_names.py
Log Message:
Merged the publication refactoring branch into the main branch.
Also renamed:
browser_reaverse -> publishTraverse
browser_default -> browserDefault
=== Zope3/lib/python/Zope/Publisher/HTTP/IHTTPApplicationRequest.py 1.1.2.2 => 1.1.2.3 ===
+#
+# Copyright (c) 2001, 2002 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
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from Zope.Publisher.IApplicationRequest import IApplicationRequest
+from Interface.Attribute import Attribute
+
+class IHTTPApplicationRequest(IApplicationRequest):
+ """HTTP request data.
+
+ This object provides access to request data. This includes, the
+ input headers, server data, and cookies.
+
+ Request objects are created by the object publisher and will be
+ passed to published objects through the argument name, REQUEST.
+
+ The request object is a mapping object that represents a
+ collection of variable to value mappings. In addition, variables
+ are divided into four categories:
+
+ - Environment variables
+
+ These variables include input headers, server data, and other
+ request-related data. The variable names are as <a
+ href="http://hoohoo.ncsa.uiuc.edu/cgi/env.html">specified</a>
+ in the <a
+ href="http://hoohoo.ncsa.uiuc.edu/cgi/interface.html">CGI
+ specification</a>
+
+ - Cookies
+
+ These are the cookie data, if present.
+
+ - Other
+
+ Data that may be set by an application object.
+
+ The request object may be used as a mapping object, in which case
+ values will be looked up in the order: environment variables,
+ other variables, cookies, and special.
+ """
+
+ def __getitem__(key):
+ """Return HTTP request data
+
+ Request data sre retrieved from one of:
+
+ - Environment variables
+
+ These variables include input headers, server data, and other
+ request-related data. The variable names are as <a
+ href="http://hoohoo.ncsa.uiuc.edu/cgi/env.html">specified</a>
+ in the <a
+ href="http://hoohoo.ncsa.uiuc.edu/cgi/interface.html">CGI
+ specification</a>
+
+ - Cookies
+
+ These are the cookie data, if present.
+
+ Cookies are searched before environmental data.
+ """
+
+ def getCookies():
+ """Return the cookie data
+
+ Data are returned as a mapping object, mapping cookie name to value.
+ """
+
+ return IMapping(str, str)
+
+ cookies = Attribute(
+ """Request cookie data
+
+ This is a read-only mapping from variable name to value.
+ """)
+
+ def getHeader(name, default=None):
+ """Get a header value
+
+ Return the named HTTP header, or an optional default
+ argument or None if the header is not found. Note that
+ both original and CGI-ified header names are recognized,
+ e.g. 'Content-Type', 'CONTENT_TYPE' and 'HTTP_CONTENT_TYPE'
+ should all return the Content-Type header, if available.
+ """
+
+ headers = Attribute(
+ """Request header data
+
+ This is a read-only mapping from variable name to value.
+ """)
+
+ URL = Attribute(
+ """Request URL data
+
+ When convered to a string, this gives the effective published URL.
+
+ This is object can also be used as a mapping object. The keys
+ must be integers or strings that can be converted to
+ integers. A non-negative integer returns a URL n steps from
+ the URL of the top-level application objects. A negative
+ integer gives a URL that is -n steps back from the effective
+ URL.
+
+ For example, 'request.URL[-2]' is equivalent to the Zope 2
+ 'request["URL2"]'. The notion is that this would be used in
+ path expressions, like 'request/URL/-2'.
+ """)
+
+
+ def getURL(level=0, path_only=0):
+ """Return the published URL with level names removed from the end.
+
+ If path_only is true, then only a path will be returned.
+ """
+
+ def getApplicationURL(depth=0, path_only=0):
+ """Return the application URL plus depth steps
+
+ If path_only is true, then only a path will be returned.
+ """
+
+
=== Added File Zope3/lib/python/Zope/Publisher/HTTP/IHTTPApplicationResponse.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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
#
##############################################################################
"""
$Id: IHTTPApplicationResponse.py,v 1.1.4.1 2002/03/26 21:25:59 jim Exp $
"""
from Zope.Publisher.IApplicationResponse import IApplicationResponse
from Interface.Attribute import Attribute
class IHTTPApplicationResponse(IApplicationResponse):
"""HTTP Response
"""
def redirect(location, status=302):
"""Causes a redirection without raising an error.
"""
=== Added File Zope3/lib/python/Zope/Publisher/HTTP/IHTTPResponse.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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
#
##############################################################################
"""
$Id: IHTTPResponse.py,v 1.1.4.1 2002/03/26 21:25:59 jim Exp $
"""
from Interface import Interface
class IHTTPResponse(Interface):
"""An object representation of an HTTP response.
The Response type encapsulates all possible responses to HTTP
requests. Responses are normally created by the object publisher.
A published objext can get the respose by calling getResponse on a
request. Normally, published objects use response objects to:
- Provide specific control over output headers,
- Set cookies, or
- Provide stream-oriented output.
If stream oriented output is used, then the response object
passed into the object must be used.
"""
def getStatus():
"""Returns the current HTTP status code as an integer.
"""
def setStatus(status, reason=None):
"""Sets the HTTP status code of the response
The argument may either be an integer or a string from { OK,
Created, Accepted, NoContent, MovedPermanently,
MovedTemporarily, NotModified, BadRequest, Unauthorized,
Forbidden, NotFound, InternalError, NotImplemented,
BadGateway, ServiceUnavailable } that will be converted to the
correct integer value.
"""
def setHeader(name, value, literal=0):
"""Sets an HTTP return header "name" with value "value"
The previous value is cleared. If the literal flag is true,
the case of the header name is preserved, otherwise
word-capitalization will be performed on the header name on
output.
"""
def addHeader(name, value):
"""Add an HTTP Header
Sets a new HTTP return header with the given value, while retaining
any previously set headers with the same name.
"""
def getHeader(name, default=None):
"""Gets a header value
Returns the value associated with a HTTP return header, or
'default' if no such header has been set in the response
yet.
"""
def getHeaders():
"""Returns a mapping of correctly-cased header names to values.
"""
def appendToCookie(name, value):
"""Append text to a cookie value
If a value for the cookie has previously been set, the new
value is appended to the old one separated by a colon.
"""
def expireCookie(name, **kw):
"""Causes an HTTP cookie to be removed from the browser
The response will include an HTTP header that will remove the cookie
corresponding to "name" on the client, if one exists. This is
accomplished by sending a new cookie with an expiration date
that has already passed. Note that some clients require a path
to be specified - this path must exactly match the path given
when creating the cookie. The path can be specified as a keyword
argument.
"""
def setCookie(name, value, **kw):
"""Sets an HTTP cookie on the browser
The response will include an HTTP header that sets a cookie on
cookie-enabled browsers with a key "name" and value
"value". This overwrites any previously set value for the
cookie in the Response object.
"""
def appendToHeader(name, value, delimiter=","):
"""Appends a value to a header
Sets an HTTP return header "name" with value "value",
appending it following a comma if there was a previous value
set for the header.
"""
=== Zope3/lib/python/Zope/Publisher/HTTP/HTTPRequest.py 1.1.2.21 => 1.1.2.22 === (676/776 lines abridged)
#
-# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
-# Version 1.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# 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.
+# FOR A PARTICULAR PURPOSE
+#
##############################################################################
"""
$Id$
"""
-import re, sys, os, time, whrandom, cgi
-from urllib import quote, unquote, splittype, splitport
+import re, time, whrandom
+from urllib import quote, splitport
from types import StringType
from Zope.Publisher.BaseRequest import BaseRequest
from Zope.Publisher.Browser.IBrowserPublisher import IBrowserPublisher
from HTTPResponse import HTTPResponse
-from cgi_names import isCGI_NAME, hide_key
from IHTTPCredentials import IHTTPCredentials
from IHTTPRequest import IHTTPRequest
+from IHTTPApplicationRequest import IHTTPApplicationRequest
-DEFAULT_PORTS = {'http': '80', 'https': '443'}
-STAGGER_RETRIES = 1
+from Zope.Publisher.RequestDataProperty \
+ import RequestDataProperty, RequestDataMapper, RequestDataGetter
+
+class CookieMapper(RequestDataMapper):
+ _mapname = '_cookies'
+
+class HeaderGetter(RequestDataGetter):
+ _gettrname = 'getHeader'
+
+_marker = object()
+
+class URLGetter:
+
[-=- -=- -=- 676 lines omitted -=- -=- -=-]
+ if path_only:
+ return names and ('/' + '/'.join(names)) or '/'
+ else:
+ return (names and ("%s/%s" % (self._app_server, '/'.join(names)))
+ or self._app_server)
+
+ URL = RequestDataProperty(URLGetter)
+
+ ######################################
+ # from: Interface.Common.Mapping.IReadMapping
+
+ def get(self, key, default=None):
+ 'See Interface.Common.Mapping.IReadMapping'
+
+ result = self._cookies.get(key, self)
+ if result is not self: return result
+
+ result = self._environ.get(key, self)
+ if result is not self: return result
+
+ return default
+
+ #
+ ############################################################
+
+ ######################################
+ # from: Interface.Common.Mapping.IEnumerableMapping
+
+ def keys(self):
+ 'See Interface.Common.Mapping.IEnumerableMapping'
+ d = {}
+ d.update(self._environ)
+ d.update(self._cookies)
+ return d.keys()
- # _viewtype is overridden from the BaseRequest
- # to implement IBrowserPublisher
- _viewtype = IBrowserPublisher
base64 = None
@@ -503,8 +483,6 @@
try: del dict['HTTP_CGI_AUTHORIZATION']
except: pass
return dict
-
-
def parse_cookie(
=== Zope3/lib/python/Zope/Publisher/HTTP/HTTPResponse.py 1.1.2.13 => 1.1.2.14 === (498/598 lines abridged)
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
-# Version 1.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# 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.
-
+# FOR A PARTICULAR PURPOSE
+#
+##############################################################################
'''HTTP Response Output formatter
$Id$'''
-__version__='$Revision$'[11:-2]
import sys, re
from types import StringType, ClassType
@@ -18,8 +21,12 @@
from Zope.Publisher.BaseResponse import BaseResponse
from Zope.Publisher.Exceptions import Redirect
+from IHTTPResponse import IHTTPResponse
+from IHTTPApplicationResponse import IHTTPApplicationResponse
+from Zope.Exceptions.ExceptionFormatter import format_exception
-status_reasons={
+# Possible HTTP status responses
+status_reasons = {
100: 'Continue',
101: 'Switching Protocols',
102: 'Processing',
@@ -74,82 +81,70 @@
for key, val in status_reasons.items():
status_codes[val.replace(' ', '').lower()] = key
status_codes[val.lower()] = key
- status_codes[key]=key
- status_codes[str(key)]=key
+ status_codes[key] = key
+ status_codes[str(key)] = key
+
en = [n.lower() for n in dir(__builtins__) if n.endswith('Error')]
+
for name in en:
status_codes[name] = 500
[-=- -=- -=- 498 lines omitted -=- -=- -=-]
self.outputHeaders()
self._wrote_headers = 1
- self.outstream.write(data)
+ self._outstream.write(data)
def outputBody(self):
"""
Outputs the response body.
"""
- self.output(self.body)
+ self.output(self._body)
+
+ def _formatException(etype, value, tb, limit=None):
+ import traceback
+ result=['Traceback (innermost last):']
+ if limit is None:
+ if hasattr(sys, 'tracebacklimit'):
+ limit = sys.tracebacklimit
+ n = 0
+ while tb is not None and (limit is None or n < limit):
+ frame = tb.tb_frame
+ lineno = tb.tb_lineno
+ co = frame.f_code
+ filename = co.co_filename
+ name = co.co_name
+ locals = frame.f_locals
+ globals = frame.f_globals
+ modname = globals.get('__name__', filename)
+ result.append(' Module %s, line %d, in %s'
+ % (modname,lineno,name))
+ try:
+ result.append(' (Object: %s)' %
+ locals[co.co_varnames[0]].__name__)
+ except:
+ pass
+
+ try:
+ result.append(' (Info: %s)' %
+ str(locals['__traceback_info__']))
+ except: pass
+ tb = tb.tb_next
+ n = n+1
+ result.append(' '.join(traceback.format_exception_only(etype, value)))
+ return result
+
+
+ def _createTracebackString(self, t, v, tb):
+ tb = self._formatException(t, v, tb, 200)
+ return '\n'.join(tb)
=== Zope3/lib/python/Zope/Publisher/HTTP/IHTTPRequest.py 1.1.2.2 => 1.1.2.3 ===
"""
-
from Interface import Interface
-class IHTTPRequest(Interface):
-
- def __getitem__(key):
- """Return HTTP request data
-
- Request data are divided into five categories:
-
- - Environment variables
-
- These variables include input headers, server data, and other
- request-related data. The variable names are as <a
- href="http://hoohoo.ncsa.uiuc.edu/cgi/env.html">specified</a>
- in the <a
- href="http://hoohoo.ncsa.uiuc.edu/cgi/interface.html">CGI
- specification</a>
- - Form data
+# XXX Should we extend IRequest?
- These are data extracted from either a URL-encoded query
- string or body, if present.
-
- - Cookies
-
- These are the cookie data, if present.
+class IHTTPRequest(Interface):
- - Lazy Data
+ def setPathSuffix(steps):
+ """Add additional trversal steps to be taken after all other traversal
- These are callables which are deferred until explicitly
- referenced, at which point they are resolved and stored as
- application data.
+ This is used to handle HTTP request methods (except for GET
+ and POST in the case of browser requests) and XML-RPC methods.
+ """
- - Other
+
- Data that may be set by an application object.
- """
=== Removed File Zope3/lib/python/Zope/Publisher/HTTP/BrowserPayload.py ===
=== Removed File Zope3/lib/python/Zope/Publisher/HTTP/IPayload.py ===
=== Removed File Zope3/lib/python/Zope/Publisher/HTTP/cgi_names.py ===