HTTPServlet.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebKit » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebKit » HTTPServlet.py
from Common import *
from Servlet import Servlet


class HTTPServlet(Servlet):
  """A HTTP servlet.

  HTTPServlet implements the respond() method to invoke methods such as
  respondToGet() and respondToPut() depending on the type of HTTP request.
  Example HTTP request methods are GET, POST, HEAD, etc.
  Subclasses implement HTTP method FOO in the Python method respondToFoo.
  Unsupported methods return a "501 Not Implemented" status.

  Note that HTTPServlet inherits awake() and respond() methods from
  Servlet and that subclasses may make use of these.

  See also: Servlet

  FUTURE
    * Document methods (take hints from Java HTTPServlet documentation)

  """


  ## Init ##

  def __init__(self):
    Servlet.__init__(self)
    self._methodForRequestType = {}  # a cache; see respond()


  ## Transactions ##

  def respond(self, trans):
    """Respond to a request.

    Invokes the appropriate respondToSomething() method depending on the
    type of request (e.g., GET, POST, PUT, ...).

    """
    request = trans.request()
    httpMethodName = request.method()
    # For GET and HEAD, handle the HTTP If-Modified-Since header:
    # if the object's last modified time is the same
    # as the IMS header, we're done.
    if httpMethodName in ('GET', 'HEAD'):
      lm = self.lastModified(trans)
      if lm:
        lm = time.strftime('%a, %d %b %Y %H:%M:%S GMT',
          time.gmtime(lm))
        trans.response().setHeader('Last-Modified', lm)
        ifModifiedSince = request.environ().get(
          'HTTP_IF_MODIFIED_SINCE', None)
        if not ifModifiedSince:
          ifModifiedSince = request.environ().get(
            'If-Modified-Since', None)
        if ifModifiedSince and ifModifiedSince.split(';')[0] == lm:
          trans.response().setStatus(304, 'Not Modified')
          # print "304", request.serverSidePath()
          return
    method = self._methodForRequestType.get(httpMethodName, None)
    if not method:
      methName = 'respondTo' + httpMethodName.capitalize()
      method = getattr(self, methName, self.notImplemented)
      self._methodForRequestType[httpMethodName] = method
    method(trans)

  def notImplemented(self, trans):
    trans.response().setStatus(501, 'Not Implemented')

  def lastModified(self, trans):
    """Get time of last modification.

    Return this object's Last-Modified time (as a float),
    or None (meaning don't know or not applicable).

    """
    return None

  def respondToHead(self, trans):
    """Respond to a HEAD request.

    A correct but inefficient implementation.

    """
    res = trans.response()
    w = res.write
    res.write = lambda *args: None
    self.respondToGet(trans)
    res.write = w
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.