~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/request/request_cgi.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin - CGI Request Implementation for std. CGI web servers
 
4
    like Apache or IIS or others.
 
5
 
 
6
    @copyright: 2001-2003 by Juergen Hermann <jh@web.de>,
 
7
                2003-2006 MoinMoin:ThomasWaldmann
 
8
    @license: GNU GPL, see COPYING for details.
 
9
"""
 
10
import sys, os, cgi
 
11
 
 
12
from MoinMoin import log
 
13
logging = log.getLogger(__name__)
 
14
 
 
15
from MoinMoin.request import RequestBase, RemoteClosedConnection
 
16
 
 
17
class Request(RequestBase):
 
18
    """ specialized on CGI requests """
 
19
 
 
20
    def __init__(self, properties={}):
 
21
        try:
 
22
            # force input/output to binary
 
23
            if sys.platform == "win32":
 
24
                import msvcrt
 
25
                msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
 
26
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
 
27
 
 
28
            self._setup_vars_from_std_env(os.environ)
 
29
            RequestBase.__init__(self, properties)
 
30
 
 
31
        except Exception, err:
 
32
            self.fail(err)
 
33
 
 
34
    def _setup_args_from_cgi_form(self):
 
35
        """ Override to create cgi form """
 
36
        form = cgi.FieldStorage(keep_blank_values=1)
 
37
        return RequestBase._setup_args_from_cgi_form(self, form)
 
38
 
 
39
    def read(self, n):
 
40
        """ Read from input stream. """
 
41
        if n is None:
 
42
            logging.warning("calling request.read(None) might block")
 
43
            return sys.stdin.read()
 
44
        else:
 
45
            return sys.stdin.read(n)
 
46
 
 
47
    def write(self, *data):
 
48
        """ Write to output stream. """
 
49
        data = self.encode(data)
 
50
        try:
 
51
            sys.stdout.write(data)
 
52
        except Exception:
 
53
            raise RemoteClosedConnection()
 
54
 
 
55
    def flush(self):
 
56
        sys.stdout.flush()
 
57
 
 
58
    def finish(self):
 
59
        RequestBase.finish(self)
 
60
        # flush the output, ignore errors caused by the user closing the socket
 
61
        try:
 
62
            sys.stdout.flush()
 
63
        except IOError, ex:
 
64
            import errno
 
65
            if ex.errno != errno.EPIPE:
 
66
                raise
 
67
 
 
68
    def _emit_http_headers(self, headers):
 
69
        """ private method to send out preprocessed list of HTTP headers """
 
70
        for header in headers:
 
71
            self.write("%s\r\n" % header)
 
72
        self.write("\r\n")
 
73