Move python modules from source4/scripting/python/ to python/.
[samba.git] / python / samba / web_server / __init__.py
1 # -*- coding: utf-8 -*-
2 #
3 # Unix SMB/CIFS implementation.
4 # Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
5 #
6 # Implementation of SWAT that uses WSGI
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 def render_placeholder(environ, start_response):
23     """Send the user a simple placeholder about missing SWAT."""
24     status = '200 OK'
25     response_headers = [('Content-type', 'text/html')]
26     start_response(status, response_headers)
27
28     yield "<!doctype html>\n"
29     yield "<html>\n"
30     yield "  <title>The Samba web service</title>\n"
31     yield "</html>\n"
32
33     yield "<body>\n"
34     yield "<p>Welcome to this Samba web server.</p>\n"
35     yield "<p>This page is a simple placeholder. You probably want to install "
36     yield "SWAT. More information can be found "
37     yield "<a href='http://wiki.samba.org/index.php/SWAT2'>on the wiki</a>.</p>"
38     yield "</p>\n"
39     yield "</body>\n"
40     yield "</html>\n"
41
42
43 def __call__(environ, start_response):
44     """Handle a HTTP request."""
45     from wsgiref.util import application_uri, shift_path_info
46     from urlparse import urljoin
47
48     try:
49         import swat
50     except ImportError, e:
51         print "NO SWAT: %r" % e
52         have_swat = False
53     else:
54         have_swat = True
55
56     orig_path = environ['PATH_INFO']
57     name = shift_path_info(environ)
58
59     if name == "":
60         if have_swat:
61             start_response('301 Redirect',
62                 [('Location', urljoin(application_uri(environ), 'swat')),])
63             return []
64         else:
65             return render_placeholder(environ, start_response)
66     elif have_swat and name == "swat":
67         return swat.__call__(environ, start_response)
68     else:
69         status = '404 Not found'
70         response_headers = [('Content-type', 'text/html')]
71         start_response(status, response_headers)
72         return ["The path %s (%s) was not found" % (orig_path, name)]
73
74
75 if __name__ == '__main__':
76     from wsgiref import simple_server
77     httpd = simple_server.make_server('localhost', 8090, __call__)
78     print "Serving HTTP on port 8090..."
79     httpd.serve_forever()