call*cgi, python: Try to emulate standard CGI behavior closer.
[ashd.git] / python / ashd-wsgi
index 79aad4f..894211d 100755 (executable)
@@ -1,13 +1,14 @@
 #!/usr/bin/python
 
-import sys, os, getopt, threading
+import sys, os, getopt, threading, time
 import ashd.proto, ashd.util
 
 def usage(out):
-    out.write("usage: ashd-wsgi [-hA] [-p MODPATH] HANDLER-MODULE [ARGS...]\n")
+    out.write("usage: ashd-wsgi [-hA] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n")
 
+reqlimit = 0
 modwsgi_compat = False
-opts, args = getopt.getopt(sys.argv[1:], "+hAp:")
+opts, args = getopt.getopt(sys.argv[1:], "+hAp:l:")
 for o, a in opts:
     if o == "-h":
         usage(sys.stdout)
@@ -16,6 +17,8 @@ for o, a in opts:
         sys.path.insert(0, a)
     elif o == "-A":
         modwsgi_compat = True
+    elif o == "-l":
+        reqlimit = int(a)
 if len(args) < 1:
     usage(sys.stderr)
     sys.exit(1)
@@ -89,10 +92,6 @@ def dowsgi(req):
     env["SERVER_PROTOCOL"] = req.ver
     env["REQUEST_METHOD"] = req.method
     env["REQUEST_URI"] = req.url
-    try:
-        env["PATH_INFO"] = unquoteurl(req.rest)
-    except:
-        env["PATH_INFO"] = req.rest
     name = req.url
     p = name.find('?')
     if p >= 0:
@@ -101,8 +100,19 @@ def dowsgi(req):
     else:
         env["QUERY_STRING"] = ""
     if name[-len(req.rest):] == req.rest:
+        # This is the same hack used in call*cgi.
         name = name[:-len(req.rest)]
+    try:
+        pi = unquoteurl(req.rest)
+    except:
+        pi = req.rest
+    if name == '/':
+        # This seems to be normal CGI behavior, but see callcgi.c for
+        # details.
+        pi = "/" + pi
+        name = ""
     env["SCRIPT_NAME"] = name
+    env["PATH_INFO"] = pi
     if "Host" in req: env["SERVER_NAME"] = req["Host"]
     if "X-Ash-Server-Port" in req: env["SERVER_PORT"] = req["X-Ash-Server-Port"]
     if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == "https": env["HTTPS"] = "on"
@@ -170,14 +180,37 @@ def dowsgi(req):
         if hasattr(respiter, "close"):
             respiter.close()
 
+flightlock = threading.Condition()
+inflight = 0
+
 class reqthread(threading.Thread):
     def __init__(self, req):
         super(reqthread, self).__init__(name = "Request handler")
         self.req = req.dup()
     
     def run(self):
+        global inflight
         try:
-            dowsgi(self.req)
+            flightlock.acquire()
+            try:
+                if reqlimit != 0:
+                    start = time.time()
+                    while inflight >= reqlimit:
+                        flightlock.wait(10)
+                        if time.time() - start > 10:
+                            os.abort()
+                inflight += 1
+            finally:
+                flightlock.release()
+            try:
+                dowsgi(self.req)
+            finally:
+                flightlock.acquire()
+                try:
+                    inflight -= 1
+                    flightlock.notify()
+                finally:
+                    flightlock.release()
         finally:
             self.req.close()