Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/tags/cherrypy-2.1.0-rc1/cherrypy/_cphttpserver.py

Revision 583 (checked in by speno, 3 years ago)

Added cherrypy.request.remotePort; the TCP port from which the client's
connection originates. Added this attribute to the book also.

Line 
1 """
2 Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13     * Neither the name of the CherryPy Team nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 """
28
29 import threading, os, socket
30 import SocketServer, BaseHTTPServer, Queue
31 import cherrypy
32 from cherrypy import _cputil, _cphttptools
33
34 try:
35     import cStringIO as StringIO
36 except ImportError:
37     import StringIO
38
39
40 class CherryHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
41    
42     """CherryPy HTTP request handler with the following commands:
43         
44         o  GET
45         o  HEAD
46         o  POST
47         o  HOTRELOAD
48         
49     """
50    
51     def address_string(self):
52         """ Try to do a reverse DNS based on [server]reverseDNS in the config file """
53         if cherrypy.config.get('server.reverseDNS'):
54             return BaseHTTPServer.BaseHTTPRequestHandler.address_string(self)
55         else:
56             return self.client_address[0]
57    
58     def _headerlist(self):
59         list = []
60         hit = 0
61         for line in self.headers.headers:
62             if line:
63                 if line[0] in ' \t':
64                     # Continuation line. Add to previous entry.
65                     if list:
66                         list[-1][1] += " " + line.lstrip()
67                 else:
68                     # New header. Add a new entry. We don't catch
69                     # ValueError here because we trust rfc822.py.
70                     name, value = line.split(":", 1)
71                     list.append((name.strip(), value.strip()))
72         return list
73    
74     def handle_one_request(self):
75         """Handle a single HTTP request."""
76        
77         self.raw_requestline = self.rfile.readline()
78         if not self.raw_requestline:
79             self.close_connection = 1
80             return
81         if not self.parse_request(): # An error code has been sent, just exit
82             return
83        
84         cherrypy.request.multithread = cherrypy.config.get("server.threadPool") > 1
85         cherrypy.request.multiprocess = False
86         cherrypy.server.request(self.client_address,
87                                 self.address_string(),
88                                 self.raw_requestline,
89                                 self._headerlist(),
90                                 self.rfile, "http")
91         wfile = self.wfile
92         wfile.write("%s %s\r\n" % (self.protocol_version, cherrypy.response.status))
93        
94         has_close_conn = False
95         for name, value in cherrypy.response.headers:
96             wfile.write("%s: %s\r\n" % (name, value))
97             if name.lower == 'connection' and value.lower == 'close':
98                 has_close_conn = True
99         if not has_close_conn:
100             # This server doesn't support persistent connections yet, so we
101             # must add a "Connection: close" header to tell the client that
102             # we will close the connection when we're done sending output.
103             #
104             # From RFC 2616 sec 14.10:
105             # HTTP/1.1 defines the "close" connection option for the sender
106             # to signal that the connection will be closed after completion
107             # of the response. For example,
108             #
109             #    Connection: close
110             #
111             # in either the request or the response header fields indicates
112             # that the connection SHOULD NOT be considered `persistent'
113             # (section 8.1) after the current request/response is complete.
114             #
115             # HTTP/1.1 applications that do not support persistent connections
116             # MUST include the "close" connection option in every message.
117             wfile.write("Connection: close\r\n")
118        
119         wfile.write("\r\n")
120         try:
121             for chunk in cherrypy.response.body:
122                 wfile.write(chunk)
123         except:
124             s, h, b = _cphttptools.bareError()
125             for chunk in b:
126                 wfile.write(chunk)
127        
128         if self.command == "POST":
129             self.connection = self.request
130        
131         # Close the conn, since we do not yet support persistent connections.
132         self.close_connection = 1
133    
134     def log_message(self, format, *args):
135         """ We have to override this to use our own logging mechanism """
136         cherrypy.log(format % args, "HTTP")
137
138
139 class CherryHTTPServer(BaseHTTPServer.HTTPServer):
140    
141     def __init__(self):
142         # Set protocol_version
143         proto = cherrypy.config.get('server.protocolVersion') or "HTTP/1.0"
144         CherryHTTPRequestHandler.protocol_version = proto
145        
146         # Select the appropriate server based on config options
147         sockFile = cherrypy.config.get('server.socketFile')
148         if sockFile:
149             # AF_UNIX socket
150             self.address_family = socket.AF_UNIX
151            
152             # So we can reuse the socket
153             try: os.unlink(sockFile)
154             except: pass
155            
156             # So everyone can access the socket
157             try: os.chmod(sockFile, 0777)
158             except: pass
159            
160             server_address = sockFile
161         else:
162             # AF_INET socket
163             server_address = (cherrypy.config.get('server.socketHost'),
164                               cherrypy.config.get('server.socketPort'))
165        
166         self.request_queue_size = cherrypy.config.get('server.socketQueueSize')
167        
168         BaseHTTPServer.HTTPServer.__init__(self, server_address, CherryHTTPRequestHandler)
169    
170     def server_activate(self):
171         """Override server_activate to set timeout on our listener socket"""
172         self.socket.settimeout(1)
173         BaseHTTPServer.HTTPServer.server_activate(self)
174    
175     def server_bind(self):
176         # Removed getfqdn call because it was timing out on localhost when calling gethostbyaddr
177         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
178         self.socket.bind(self.server_address)
179    
180     def get_request(self):
181         # With Python 2.3 it seems that an accept socket in timeout (nonblocking) mode
182         #  results in request sockets that are also set in nonblocking mode. Since that doesn't play
183         #  well with makefile() (where wfile and rfile are set in SocketServer.py) we explicitly set
184         #  the request socket to blocking
185        
186         request, client_address = self.socket.accept()
187         request.setblocking(1)
188         return request, client_address
189    
190     def handle_request(self):
191         """Override handle_request to trap timeout exception."""
192         try:
193             BaseHTTPServer.HTTPServer.handle_request(self)
194         except socket.timeout:
195             # The only reason for the timeout is so we can notice keyboard
196             # interrupts on Win32, which don't interrupt accept() by default
197             return 1
198         except (KeyboardInterrupt, SystemExit):
199             cherrypy.log("<Ctrl-C> hit: shutting down http server", "HTTP")
200             self.shutdown()
201    
202     def serve_forever(self):
203         """Override serve_forever to handle shutdown."""
204         self.__running = 1
205         while self.__running:
206             self.handle_request()
207     start = serve_forever
208    
209     def shutdown(self):
210         self.__running = 0
211     stop = shutdown
212
213
214 _SHUTDOWNREQUEST = (0,0)
215
216 class ServerThread(threading.Thread):
217    
218     def __init__(self, RequestHandlerClass, requestQueue):
219         threading.Thread.__init__(self)
220         self._RequestHandlerClass = RequestHandlerClass
221         self._requestQueue = requestQueue
222    
223     def run(self):
224         while 1:
225             request, client_address = self._requestQueue.get()
226             if (request, client_address) == _SHUTDOWNREQUEST:
227                 return
228             if self.verify_request(request, client_address):
229                 try:
230                     self.process_request(request, client_address)
231                 except:
232                     self.handle_error(request, client_address)
233                     self.close_request(request)
234             else:
235                 self.close_request(request)
236    
237     def verify_request(self, request, client_address):
238         """ Verify the request.  May be overridden.
239             Return 1 if we should proceed with this request. """
240         return 1
241    
242     def process_request(self, request, client_address):
243         self._RequestHandlerClass(request, client_address, self)
244         self.close_request(request)
245    
246     def close_request(self, request):
247         """ Called to clean up an individual request. """
248         request.close()
249    
250     def handle_error(self, request, client_address):
251         """Handle an error gracefully.  May be overridden."""
252         errorBody = _cputil.formatExc()
253         cherrypy.log(errorBody)
254
255
256 class PooledThreadServer(SocketServer.TCPServer):
257     """A TCP Server using a pool of worker threads. This is superior to the
258        alternatives provided by the Python standard library, which only offer
259        (1) handling a single request at a time, (2) handling each request in
260        a separate thread (via ThreadingMixIn), or (3) handling each request in
261        a separate process (via ForkingMixIn). It's also superior in some ways
262        to the pure async approach used by Twisted because it allows a more
263        straightforward and simple programming model in the face of blocking
264        requests (i.e. you don't have to bother with Deferreds)."""
265    
266     allow_reuse_address = 1
267    
268     def __init__(self):
269         # Set protocol_version
270         proto = cherrypy.config.get('server.protocolVersion') or "HTTP/1.0"
271         CherryHTTPRequestHandler.protocol_version = proto
272        
273         # Select the appropriate server based on config options
274         threadPool = cherrypy.config.get('server.threadPool')
275         server_address = (cherrypy.config.get('server.socketHost'),
276                           cherrypy.config.get('server.socketPort'))
277         self.request_queue_size = cherrypy.config.get('server.socketQueueSize')
278        
279         # I know it says "do not override", but I have to in order to implement SSL support !
280         SocketServer.BaseServer.__init__(self, server_address, CherryHTTPRequestHandler)
281         self.socket = socket.socket(self.address_family, self.socket_type)
282         self.server_bind()
283         self.server_activate()
284        
285         self._numThreads = threadPool
286         self._RequestHandlerClass = CherryHTTPRequestHandler
287         self._ThreadClass = ServerThread
288         self._requestQueue = Queue.Queue()
289         self._workerThreads = []
290    
291     def createThread(self):
292         return self._ThreadClass(self._RequestHandlerClass, self._requestQueue)
293    
294     def server_activate(self):
295         """Override server_activate to set timeout on our listener socket"""
296         self.socket.settimeout(1)
297         SocketServer.TCPServer.server_activate(self)
298    
299     def server_bind(self):
300         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
301         self.socket.bind(self.server_address)
302    
303     def shutdown(self):
304         """Gracefully shutdown a server that is serve_forever()ing."""
305         self.__running = 0
306        
307         # Must shut down threads here so the code that calls
308         # this method can know when all threads are stopped.
309         for worker in self._workerThreads:
310             self._requestQueue.put(_SHUTDOWNREQUEST)
311         current = threading.currentThread()
312         for worker in self._workerThreads:
313             if worker is not current:
314                 worker.join()
315         self._workerThreads = []
316     stop = shutdown
317    
318     def serve_forever(self):
319         """Handle one request at a time until doomsday (or shutdown is called)."""
320         if self._workerThreads == []:
321             for i in xrange(self._numThreads):
322                 self._workerThreads.append(self.createThread())
323             for worker in self._workerThreads:
324                 worker.start()
325         self.__running = 1
326         while self.__running:
327             if not self.handle_request():
328                 break
329         self.server_close()
330     start = serve_forever
331    
332     def handle_request(self):
333         """Override handle_request to enqueue requests rather than handle
334            them synchronously. Return 1 by default, 0 to shutdown the
335            server."""
336         try:
337             request, client_address = self.get_request()
338         except (KeyboardInterrupt, SystemExit):
339             cherrypy.log("<Ctrl-C> hit: shutting down", "HTTP")
340             return 0
341         except socket.error, e:
342             return 1
343         self._requestQueue.put((request, client_address))
344         return 1
345    
346     def get_request(self):
347         # With Python 2.3 it seems that an accept socket in timeout (nonblocking) mode
348         #  results in request sockets that are also set in nonblocking mode. Since that doesn't play
349         #  well with makefile() (where wfile and rfile are set in SocketServer.py) we explicitly set
350         #  the request socket to blocking
351
352         request, client_address = self.socket.accept()
353         if hasattr(request,'setblocking'):
354             request.setblocking(1)
355         return request, client_address
356
357
358 def embedded_server(handler=None):
359     """Selects and instantiates the appropriate server."""
360    
361     # Select the appropriate server based on config options
362     sockFile = cherrypy.config.get('server.socketFile')
363     threadPool = cherrypy.config.get('server.threadPool')
364     if threadPool > 1 and not sockFile:
365         ServerClass = PooledThreadServer
366     else:
367         ServerClass = CherryHTTPServer
368     return ServerClass()
369
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets