review packaging - isolate wsgi in a subdir
[plcapi.git] / apache / ModPythonJson.py
diff --git a/apache/ModPythonJson.py b/apache/ModPythonJson.py
new file mode 100644 (file)
index 0000000..22dbdc7
--- /dev/null
@@ -0,0 +1,61 @@
+#
+# Apache mod_python interface for JSON requests
+#
+# Aaron Klingaman <alk@absarokasoft.com>
+# Mark Huang <mlhuang@cs.princeton.edu>
+#
+# Copyright (C) 2004-2006 The Trustees of Princeton University
+#
+
+import sys
+import traceback
+import xmlrpclib
+from mod_python import apache
+
+from PLC.Debug import log
+
+from PLC.API import PLCAPI
+api = PLCAPI()
+
+def handler(req):
+    try:
+        if req.method != "POST":
+            req.content_type = "text/html"
+            req.send_http_header()
+            req.write("""
+<html><head>
+<title>PLCAPI JSON Interface</title>
+</head><body>
+<h1>PLCAPI JSON Interface</h1>
+<p>Please POST JSON to access the PLCAPI.</p>
+</body></html>
+""")
+            return apache.OK
+
+        # Read request
+        request = req.read(int(req.headers_in['content-length']))
+
+        # mod_python < 3.2: The IP address portion of remote_addr is
+        # incorrect (always 0.0.0.0) when IPv6 is enabled.
+        # http://issues.apache.org/jira/browse/MODPYTHON-64?page=all
+        (remote_ip, remote_port) = req.connection.remote_addr
+        remote_addr = (req.connection.remote_ip, remote_port)
+
+        # Handle request
+        response = api.handle_json(remote_addr, request)
+
+        # Shut down database connection, otherwise up to MaxClients DB
+        # connections will remain open.
+        api.db.close()
+
+        # Write response
+        req.content_type = "text/json; charset=" + api.encoding
+        req.send_http_header()
+        req.write(response)
+
+        return apache.OK
+
+    except Exception, err:
+        # Log error in /var/log/httpd/(ssl_)?error_log
+        print >> log, err, traceback.format_exc()
+        return apache.HTTP_INTERNAL_SERVER_ERROR