31d0812de5ec094390b9257783ec881483c59309
[sfa.git] / sfa / server / modpython / SfaRegistryModPython.py
1 #
2 # Apache mod_python interface
3 #
4 # Aaron Klingaman <alk@absarokasoft.com>
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 #
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 #
9
10 import sys
11 import traceback
12 from mod_python import apache
13
14 from sfa.util.sfalogging import logger
15 from sfa.planetlab.server import SfaApi
16
17 api = SfaApi(interface='registry')
18
19 def handler(req):
20     try:
21         if req.method != "POST":
22             req.content_type = "text/html"
23             req.send_http_header()
24             req.write("""
25 <html><head>
26 <title>SFA Registry API XML-RPC/SOAP Interface</title>
27 </head><body>
28 <h1>SFA Registry API XML-RPC/SOAP Interface</h1>
29 <p>Please use XML-RPC or SOAP to access the SFA API.</p>
30 </body></html>
31 """)
32             return apache.OK
33
34         # Read request
35         request = req.read(int(req.headers_in['content-length']))
36
37         # mod_python < 3.2: The IP address portion of remote_addr is
38         # incorrect (always 0.0.0.0) when IPv6 is enabled.
39         # http://issues.apache.org/jira/browse/MODPYTHON-64?page=all
40         (remote_ip, remote_port) = req.connection.remote_addr
41         remote_addr = (req.connection.remote_ip, remote_port)
42
43         # Handle request
44         response = api.handle(remote_addr, request)
45
46         # Write response
47         req.content_type = "text/xml; charset=" + api.encoding
48         req.send_http_header()
49         req.write(response)
50
51         return apache.OK
52
53     except Exception, err:
54         # Log error in /var/log/httpd/(ssl_)?error_log
55         logger.log_exc('%r'%err)
56         return apache.HTTP_INTERNAL_SERVER_ERROR