4344fccfaa7b014f4662f07e6d5ba214c61ab2dd
[nivanova/samba-autobuild/.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 from __future__ import print_function
23
24 def render_placeholder(environ, start_response):
25     """Send the user a simple placeholder about missing SWAT."""
26     status = '200 OK'
27     response_headers = [('Content-type', 'text/html')]
28     start_response(status, response_headers)
29
30     yield "<!doctype html>\n"
31     yield "<html>\n"
32     yield "  <title>The Samba web service</title>\n"
33     yield "</html>\n"
34
35     yield "<body>\n"
36     yield "<p>Welcome to this Samba web server.</p>\n"
37     yield "<p>This page is a simple placeholder. You probably want to install "
38     yield "SWAT. More information can be found "
39     yield "<a href='http://wiki.samba.org/index.php/SWAT2'>on the wiki</a>.</p>"
40     yield "</p>\n"
41     yield "</body>\n"
42     yield "</html>\n"
43
44
45 def __call__(environ, start_response):
46     """Handle a HTTP request."""
47     from wsgiref.util import application_uri, shift_path_info
48     from urlparse import urljoin
49
50     try:
51         import swat
52     except ImportError as e:
53         print("NO SWAT: %r" % e)
54         have_swat = False
55     else:
56         have_swat = True
57
58     orig_path = environ['PATH_INFO']
59     name = shift_path_info(environ)
60
61     if name == "":
62         if have_swat:
63             start_response('301 Redirect',
64                            [('Location', urljoin(application_uri(environ), 'swat')), ])
65             return []
66         else:
67             return render_placeholder(environ, start_response)
68     elif have_swat and name == "swat":
69         return swat.__call__(environ, start_response)
70     else:
71         status = '404 Not found'
72         response_headers = [('Content-type', 'text/html')]
73         start_response(status, response_headers)
74         return ["The path %s (%s) was not found" % (orig_path, name)]
75
76
77 if __name__ == '__main__':
78     from wsgiref import simple_server
79     httpd = simple_server.make_server('localhost', 8090, __call__)
80     print("Serving HTTP on port 8090...")
81     httpd.serve_forever()