annotate MoinMoin/server/server_standalone.py @ 3107:c6e39279f83b

refactor logging usage
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 23 Feb 2008 22:59:30 +0100
parents 80e1a910a2f1
children a48929a5036c
rev   line source
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
2 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
3 MoinMoin - Stand-alone HTTP Server
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
4
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
5 This is a simple, fast and very easy to install server. Its
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
6 recommended for personal wikis or public wikis with little load.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
7
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
8 It is not well tested in public wikis with heavy load. In these case
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
9 you might want to use twisted, fast cgi or mod python, or if you
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
10 can't use those, cgi.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
11
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
12 Minimal usage:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
13
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
14 from MoinMoin.server.server_standalone import StandaloneConfig, run
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
15
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
16 class Config(StandaloneConfig):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
17 docs = '/usr/share/moin/wiki/htdocs'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
18 user = 'www-data'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
19 group = 'www-data'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
20
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
21 run(Config)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
22
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
23 See more options in StandaloneConfig class.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
24
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
25 For security, the server will not run as root. If you try to run it
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
26 as root, it will run as the user and group in the config. If you run
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
27 it as a normal user, it will run with your regular user and group.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
28
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
29 Significant contributions to this module by R. Church <rc@ghostbitch.org>
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
30
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
31 @copyright: 2001-2004 MoinMoin:JuergenHermann,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
32 2005 MoinMoin:AlexanderSchremmer,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
33 2005 MoinMoin:NirSoffer
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
34 @license: GNU GPL, see COPYING for details.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
35 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
36
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3105
diff changeset
37 import os, sys, time, socket, errno, shutil
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
38 import BaseHTTPServer, SimpleHTTPServer, SocketServer
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
39
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3105
diff changeset
40 from MoinMoin.server import getLogger
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3105
diff changeset
41 logging = getLogger(__name__)
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3105
diff changeset
42
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3105
diff changeset
43 # use this to temporarily and selectively enable debug logging for this module
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3105
diff changeset
44 #logging.setLevel(logging.DEBUG)
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3105
diff changeset
45
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
46 from MoinMoin import version, wikiutil
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
47 from MoinMoin.server import Config, switchUID
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
48 from MoinMoin.request import request_standalone
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
49 from MoinMoin.util import timefuncs
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
50
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
51 # Server globals
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
52 httpd = None
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
53 config = None
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
54
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
55
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
56 class SimpleServer(BaseHTTPServer.HTTPServer):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
57 """ Simplest server, serving one request after another
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
58
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
59 This server is good for personal wiki, or when lowest memory
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
60 footprint is needed.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
61 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
62 use_threads = False
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
63
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
64 def __init__(self, config):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
65 self.htdocs = config.docs
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
66 self.request_queue_size = config.requestQueueSize
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
67 self._abort = 0
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
68 address = (config.interface, config.port)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
69 BaseHTTPServer.HTTPServer.__init__(self, address, MoinRequestHandler)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
70
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
71 def server_activate(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
72 BaseHTTPServer.HTTPServer.server_activate(self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
73 logging.info("Serving on %s:%d" % self.server_address)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
74
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
75 def serve_forever(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
76 """Handle one request at a time until we die """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
77 while not self._abort:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
78 self.handle_request()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
79
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
80 def die(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
81 """Abort this server instance's serving loop """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
82 # Close hotshot profiler
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
83 if config.hotshotProfile:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
84 config.hotshotProfile.close()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
85
2420
d268d5467cfe standalone server: fix AttributeError when not using cProfileProfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2419
diff changeset
86 if config.cProfileProfile and config.cProfile:
2419
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
87 config.cProfile.dump_stats(config.cProfileProfile)
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
88
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
89 # Set abort flag, then make request to wake the server
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
90 self._abort = 1
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
91 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
92 import httplib
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
93 addr = self.server_address
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
94 if not addr[0]:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
95 addr = ("localhost", addr[1])
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
96 req = httplib.HTTP('%s:%d' % addr)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
97 req.connect()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
98 req.putrequest('DIE', '/')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
99 req.endheaders()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
100 del req
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
101 except socket.error, err:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
102 # Ignore certain errors
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
103 if err.args[0] not in [errno.EADDRNOTAVAIL, ]:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
104 raise
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
105
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
106
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
107 class ThreadingServer(SimpleServer):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
108 """ Serve each request in a new thread
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
109
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
110 This server is used since release 1.3 and seems to work nice with
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
111 little load.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
112
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
113 From release 1.3.5 there is a thread limit, that should help to
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
114 limit the load on the server.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
115 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
116 use_threads = True
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
117
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
118 def __init__(self, config):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
119 self.thread_limit = config.threadLimit
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
120 from threading import Condition
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
121 self.lock = Condition()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
122 SimpleServer.__init__(self, config)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
123
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
124 def process_request(self, request, client_address):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
125 """ Start a new thread to process the request
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
126
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
127 If the thread limit has been reached, wait on the lock. The
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
128 next thread will notify when finished.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
129 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
130 from threading import Thread, activeCount
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
131 self.lock.acquire()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
132 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
133 if activeCount() > self.thread_limit:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
134 self.lock.wait()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
135 if self._abort:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
136 return
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
137 t = Thread(target=self.process_request_thread,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
138 args=(request, client_address))
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
139 t.start()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
140 finally:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
141 self.lock.release()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
142
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
143 def process_request_thread(self, request, client_address):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
144 """ Called for each request on a new thread
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
145
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
146 Notify the main thread on the end of each request.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
147 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
148 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
149 self.finish_request(request, client_address)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
150 except:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
151 self.handle_error(request, client_address)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
152 self.close_request(request)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
153 self.lock.acquire()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
154 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
155 # Main thread might be waiting
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
156 self.lock.notify()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
157 finally:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
158 self.lock.release()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
159
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
160
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
161 class ThreadPoolServer(SimpleServer):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
162 """ Threading server using a pool of threads
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
163
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
164 This is a new experimental server, using a pool of threads instead
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
165 of creating new thread for each request. This is similar to Apache
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
166 worker mpm, with a simpler constant thread pool.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
167
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
168 This server is 5 times faster than ThreadingServer for static
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
169 files, and about the same for wiki pages.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
170
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
171 TODO: sometimes the server won't exit on Conrol-C, and continue to
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
172 run with few threads (you can kill it with kill -9). Same problem
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
173 exist with the twisted server. When the problem is finally solved,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
174 remove the commented debug prints.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
175 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
176 use_threads = True
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
177
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
178 def __init__(self, config):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
179 self.queue = []
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
180 # The size of the queue need more testing
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
181 self.queueSize = config.threadLimit * 2
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
182 self.poolSize = config.threadLimit
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
183 from threading import Condition
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
184 self.lock = Condition()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
185 SimpleServer.__init__(self, config)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
186
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
187 def serve_forever(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
188 """ Create a thread pool then invoke base class method """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
189 from threading import Thread
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
190 for dummy in range(self.poolSize):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
191 t = Thread(target=self.serve_forever_thread)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
192 t.start()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
193 SimpleServer.serve_forever(self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
194
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
195 def process_request(self, request, client_address):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
196 """ Called for each request
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
197
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
198 Insert the request into the queue. If the queue is full, wait
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
199 until one of the request threads pop a request. During the wait,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
200 new connections might be dropped.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
201 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
202 self.lock.acquire()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
203 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
204 if len(self.queue) >= self.queueSize:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
205 self.lock.wait()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
206 if self._abort:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
207 return
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
208 self.queue.insert(0, (request, client_address))
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
209 self.lock.notify()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
210 finally:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
211 self.lock.release()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
212
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
213 def serve_forever_thread(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
214 """ The main loop of request threads
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
215
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
216 Pop a request from the queue and process it.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
217 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
218 while not self._abort:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
219 request, client_address = self.pop_request()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
220 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
221 self.finish_request(request, client_address)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
222 except:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
223 self.handle_error(request, client_address)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
224 self.close_request(request)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
225 # sys.stderr.write('thread exiting...\n')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
226
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
227 def pop_request(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
228 """ Pop a request from the queue
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
229
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
230 If the queue is empty, wait for notification. If the queue was
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
231 full, notify the main thread which may be waiting.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
232 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
233 self.lock.acquire()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
234 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
235 while not self._abort:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
236 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
237 item = self.queue.pop()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
238 if len(self.queue) == self.queueSize - 1:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
239 # Queue was full - main thread might be waiting
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
240 self.lock.notify()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
241 return item
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
242 except IndexError:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
243 self.lock.wait()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
244 finally:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
245 self.lock.release()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
246 # sys.stderr.write('thread exiting...\n')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
247 sys.exit()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
248
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
249 def die(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
250 """ Wake all threads then invoke base class die
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
251
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
252 Threads should exist when _abort is True.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
253 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
254 self._abort = True
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
255 self.wake_all_threads()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
256 time.sleep(0.1)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
257 SimpleServer.die(self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
258
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
259 def wake_all_threads(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
260 self.lock.acquire()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
261 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
262 # sys.stderr.write('waking up all threads...\n')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
263 self.lock.notifyAll()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
264 finally:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
265 self.lock.release()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
266
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
267
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
268 class ForkingServer(SocketServer.ForkingMixIn, SimpleServer):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
269 """ Serve each request in a new process
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
270
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
271 This is new untested server, first tests show rather pathetic cgi
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
272 like performance. No data is cached between requests.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
273
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
274 The mixin has its own process limit.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
275 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
276 max_children = 10
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
277
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
278
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
279 class MoinRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
280
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
281 bufferSize = 8 * 1024 # used to serve static files
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
282 staticExpire = 365 * 24 * 3600 # 1 year expiry for static files
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
283
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
284 def __init__(self, request, client_address, server):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
285 self.server_version = "MoinMoin %s %s %s" % (version.release,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
286 version.revision,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
287 server.__class__.__name__)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
288 self.expires = 0
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
289 SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, request,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
290 client_address, server)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
291
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
292 def log_message(self, format, *args):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
293 logging.info("%s %s" % (self.address_string(), format % args))
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
294
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
295 # -------------------------------------------------------------------
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
296 # do_METHOD dispatchers - called for each request
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
297
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
298 def do_DIE(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
299 if self.server._abort:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
300 self.log_error("Shutting down")
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
301
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
302 def do_ALL(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
303 """ Handle requests (request type GET/HEAD/POST is in self.command)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
304
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
305 Separate between wiki pages and css and image url by similar
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
306 system as cgi and twisted, the url_prefix_static prefix.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
307 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
308 prefix = config.url_prefix_static
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
309 if self.path.startswith(prefix + '/'):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
310 self.path = self.path[len(prefix):]
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
311 self.serve_static_file()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
312 elif self.path in ['/favicon.ico', '/robots.txt']:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
313 self.serve_static_file()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
314 else:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
315 self.serve_moin()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
316
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
317 do_POST = do_ALL
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
318 do_GET = do_ALL
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
319 do_HEAD = do_ALL
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
320
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
321 # -------------------------------------------------------------------
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
322 # Serve methods
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
323
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
324 def serve_static_file(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
325 """ Serve files from the htdocs directory """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
326 self.expires = self.staticExpire
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
327 path = self.path.split("?", 1)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
328 if len(path) > 1:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
329 self.path = path[0] # XXX ?params
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
330
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
331 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
332 fn = getattr(SimpleHTTPServer.SimpleHTTPRequestHandler, 'do_' + self.command)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
333 fn(self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
334 except socket.error, err:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
335 # Ignore certain errors
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
336 if err.args[0] not in [errno.EPIPE, errno.ECONNABORTED]:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
337 raise
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
339 def serve_moin(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
340 """ Serve a request using moin """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
341 # don't make an Expires header for wiki pages
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
342 self.expires = 0
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
343
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
344 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
345 req = request_standalone.Request(self, properties=config.properties)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
346 req.run()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
347 except socket.error, err:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
348 # Ignore certain errors
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
349 if err.args[0] not in [errno.EPIPE, errno.ECONNABORTED]:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
350 raise
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
351
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
352 def translate_path(self, uri):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
353 """ Translate a /-separated PATH to the local filename syntax.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
354
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
355 Components that mean special things to the local file system
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
356 (e.g. drive or directory names) are ignored.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
357 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
358 path = wikiutil.url_unquote(uri, want_unicode=False)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
359 path = path.replace('\\', '/')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
360 words = path.split('/')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
361 words = filter(None, words)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
362
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
363 path = self.server.htdocs
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
364 bad_uri = 0
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
365 for word in words:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
366 drive, word = os.path.splitdrive(word)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
367 if drive:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
368 bad_uri = 1
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
369 head, word = os.path.split(word)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
370 if word in (os.curdir, os.pardir):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
371 bad_uri = 1
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
372 continue
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
373 path = os.path.join(path, word)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
374
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
375 if bad_uri:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
376 self.log_error("Detected bad request URI '%s', translated to '%s'"
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
377 % (uri, path, ))
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
378 return path
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
379
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
380 def end_headers(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
381 """overload the default end_headers, inserting expires header"""
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
382 if self.expires:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
383 now = time.time()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
384 expires = now + self.expires
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
385 self.send_header('Expires', timefuncs.formathttpdate(expires))
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
386 SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
387
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
388 def copyfile(self, source, outputfile):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
389 """Copy all data between two file objects.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
390
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
391 Modify the base class method to change the buffer size. Test
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
392 shows that for the typical static files we serve, 8K buffer is
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
393 faster than the default 16K buffer.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
394 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
395 shutil.copyfileobj(source, outputfile, length=self.bufferSize)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
396
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
397 def address_string(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
398 """We don't want to do reverse DNS lookups, just return IP address."""
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
399 return self.client_address[0]
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
400
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
401
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
402 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
403 from tlslite.api import TLSSocketServerMixIn, X509, X509CertChain, SessionCache, parsePEMKey
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
404 from tlslite.TLSConnection import TLSConnection
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
405 except ImportError:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
406 pass
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
407 else:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
408 class SecureRequestRedirect(BaseHTTPServer.BaseHTTPRequestHandler):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
409 def handle(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
410 self.close_connection = 1
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
411 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
412 self.raw_requestline = self.rfile.readline()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
413 except socket.error:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
414 return
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
415 if self.parse_request():
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
416 host = self.headers.get('Host', socket.gethostname())
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
417 path = self.path
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
418 else:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
419 host = '%s:%s' % (socket.gethostname(),
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
420 self.request.getsockname()[1])
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
421 path = '/'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
422
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
423 self.requestline = 'ERROR: Redirecting to https://%s%s' % (host, path)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
424 self.request_version = 'HTTP/1.1'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
425 self.command = 'GET'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
426 self.send_response(301, 'Document Moved')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
427 self.send_header('Date', self.date_time_string())
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
428 self.send_header('Location', 'https://%s%s' % (host, path))
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
429 self.send_header('Connection', 'close')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
430 self.send_header('Content-Length', '0')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
431 self.wfile.write('\r\n')
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
432
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
433 class SecureThreadPoolServer(TLSSocketServerMixIn, ThreadPoolServer):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
434 def __init__(self, config):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
435 ThreadPoolServer.__init__(self, config)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
436
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
437 cert = open(config.ssl_certificate).read()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
438 x509 = X509()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
439 x509.parse(cert)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
440 self.certChain = X509CertChain([x509])
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
441
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
442 priv = open(config.ssl_privkey).read()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
443 self.privateKey = parsePEMKey(priv, private=True)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
444
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
445 self.sessionCache = SessionCache()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
446
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
447 def finish_request(self, sock, client_address):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
448 # Peek into the packet, if it starts with GET or POS(T) then
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
449 # redirect, otherwise let TLSLite handle the connection.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
450 peek = sock.recv(3, socket.MSG_PEEK).lower()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
451 if peek == 'get' or peek == 'pos':
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
452 SecureRequestRedirect(sock, client_address, self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
453 return
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
454 tls_connection = TLSConnection(sock)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
455 if self.handshake(tls_connection):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
456 self.RequestHandlerClass(tls_connection, client_address, self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
457 else:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
458 # This will probably fail because the TLSConnection has
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
459 # already written SSL stuff to the socket. But not sure what
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
460 # else we should do.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
461 SecureRequestRedirect(sock, client_address, self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
462
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
463 def handshake(self, tls_connection):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
464 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
465 tls_connection.handshakeServer(certChain=self.certChain,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
466 privateKey=self.privateKey,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
467 sessionCache=self.sessionCache)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
468 tls_connection.ignoreAbruptClose = True
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
469 return True
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
470 except:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
471 return False
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
472
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
473
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
474 def memoryProfileDecorator(func, profile):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
475 """ Return a profiled function """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
476 def profiledFunction(*args, **kw):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
477 profile.addRequest()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
478 return func(*args, **kw)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
479 return profiledFunction
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
480
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
481
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
482 def hotshotProfileDecorator(func, profile):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
483 """ Return a profiled function """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
484 profile.moin_requests_done = 0
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
485 def profiledFunction(*args, **kw):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
486 profile.moin_requests_done += 1
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
487 if profile.moin_requests_done == 1:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
488 # Don't profile first request, its not interesting
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
489 return func(*args, **kw)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
490 return profile.runcall(func, *args, **kw)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
491
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
492 return profiledFunction
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
493
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
494
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
495 def quit(signo, stackframe):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
496 """ Signal handler for aborting signals """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
497 global httpd, config
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
498 logging.info("Thanks for using MoinMoin!")
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
499
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
500 fname = config.pycallgraph_output
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
501 if fname:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
502 import pycallgraph
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
503 if fname.endswith('.png'):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
504 pycallgraph.make_dot_graph(fname)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
505 elif fname.endswith('.dot'):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
506 pycallgraph.save_dot(fname)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
507
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
508 if httpd:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
509 httpd.die()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
510
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
511
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
512 def registerSignalHandlers(func):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
513 """ Register signal handlers on platforms that support it """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
514 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
515 import signal
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
516 signal.signal(signal.SIGABRT, func)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
517 signal.signal(signal.SIGINT, func)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
518 signal.signal(signal.SIGTERM, func)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
519 except ImportError:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
520 pass
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
521
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
522
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
523 def makeServer(config):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
524 """ Create a new server, based on the the platform capabilities
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
525
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
526 Try to create the server class specified in the config. If threads
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
527 are not available, fallback to ForkingServer. If fork is not
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
528 available, fallback to a SimpleServer.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
529 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
530 serverClass = globals()[config.serverClass]
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
531 if serverClass.use_threads:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
532 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
533 import threading
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
534 except ImportError:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
535 serverClass = ForkingServer
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
536 if serverClass is ForkingServer and not hasattr(os, "fork"):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
537 serverClass = SimpleServer
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
538 if serverClass.__name__ != config.serverClass:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
539 logging.error('%s is not available on this platform, falling back '
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
540 'to %s\n' % (config.serverClass, serverClass.__name__))
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
541
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
542 from MoinMoin import config as _config
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
543 _config.use_threads = serverClass.use_threads
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
544 return serverClass(config)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
545
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
546 # ------------------------------------------------------------------------
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
547 # Public interface
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
548
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
549 class StandaloneConfig(Config):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
550 """ Standalone server default config """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
551 name = 'moin'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
552 properties = {}
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
553 docs = '/usr/share/moin/htdocs'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
554 user = 'www-data'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
555 group = 'www-data'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
556 port = 8000
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
557 interface = 'localhost'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
558
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
559 # Advanced options
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
560 serverClass = 'ThreadPoolServer'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
561 threadLimit = 10
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
562 # The size of the listen backlog. Twisted uses a default of 50.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
563 # Tests on Mac OS X show many failed request with backlog of 5 or 10.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
564 requestQueueSize = 50
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
565
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
566 # Development options
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
567 memoryProfile = None
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
568 hotshotProfile = None
2420
d268d5467cfe standalone server: fix AttributeError when not using cProfileProfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2419
diff changeset
569 cProfile = None # internal use only
d268d5467cfe standalone server: fix AttributeError when not using cProfileProfile
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2419
diff changeset
570 cProfileProfile = None
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
571 pycallgraph_output = None
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
572
2419
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
573 def cProfileDecorator(func, profile):
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
574 """ Return a profiled function """
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
575 profile.moin_requests_done = 0
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
576 def profiledFunction(*args, **kw):
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
577 profile.moin_requests_done += 1
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
578 if profile.moin_requests_done == 1:
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
579 # Don't profile first request, it's not interesting
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
580 return func(*args, **kw)
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
581 return profile.runcall(func, *args, **kw)
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
582
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
583 return profiledFunction
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
584
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
585 def run(configClass):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
586 """ Create and run a moin server
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
587
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
588 See StandaloneConfig for available options
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
589
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
590 @param configClass: config class
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
591 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
592 # Run only once!
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
593 global httpd, config
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
594 if httpd is not None:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
595 raise RuntimeError("You can run only one server per process!")
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
596
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
597 config = configClass()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
598
2419
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
599 if config.hotshotProfile and config.cProfileProfile:
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
600 raise RuntimeError("You cannot run two profilers simultaneously.")
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
601
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
602 # Install hotshot profiled serve_moin method. To compare with other
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
603 # servers, we profile the part that create and run the request.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
604 if config.hotshotProfile:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
605 import hotshot
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
606 config.hotshotProfile = hotshot.Profile(config.hotshotProfile)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
607 MoinRequestHandler.serve_moin = hotshotProfileDecorator(
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
608 MoinRequestHandler.serve_moin, config.hotshotProfile)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
609
2419
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
610 if config.cProfileProfile:
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
611 import cProfile
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
612 # Create a new cProfile.Profile object using config.cProfileProfile
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
613 # as the path for the output file.
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
614 config.cProfile = cProfile.Profile()
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
615 MoinRequestHandler.serve_moin = cProfileDecorator(
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
616 MoinRequestHandler.serve_moin, config.cProfile)
123974b5c8b5 added cProfile support to standalone, thanks to rayvd
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2338
diff changeset
617
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
618 # Install a memory profiled serve_moin method
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
619 if config.memoryProfile:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
620 config.memoryProfile.sample()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
621 MoinRequestHandler.serve_moin = memoryProfileDecorator(
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
622 MoinRequestHandler.serve_moin, config.memoryProfile)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
623
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
624 # initialize pycallgraph, if wanted
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
625 if config.pycallgraph_output:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
626 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
627 import pycallgraph
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
628 pycallgraph.settings['include_stdlib'] = False
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
629 pcg_filter = pycallgraph.GlobbingFilter(exclude=['pycallgraph.*',
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
630 'unknown.*',
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
631 ],
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
632 max_depth=9999)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
633 pycallgraph.start_trace(reset=True, filter_func=pcg_filter)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
634 except ImportError:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
635 config.pycallgraph_output = None
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
636
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
637
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
638 registerSignalHandlers(quit)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
639 httpd = makeServer(config)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
640
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
641 # Run as a safe user (posix only)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
642 if os.name == 'posix' and os.getuid() == 0:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
643 switchUID(config.uid, config.gid)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
644
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
645 httpd.serve_forever()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
646