[tests] put scgi application into separate file

personal/stbuehler/wip
Stefan Bühler 9 years ago
parent 1c22f3aa2b
commit 921982f0fc
  1. 65
      tests/run-scgi-envcheck.py
  2. 67
      tests/t-scgi.py

@ -0,0 +1,65 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import traceback
servsocket = socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM)
def parsereq(data):
a = data.split(':', 1)
if len(a) != 2: return False
hlen, rem = a
hlen = int(hlen)
if hlen < 16: raise Exception("invalid request")
if len(rem) < hlen + 1: return False
if rem[hlen] != ',' or rem[hlen-1] != '\0': raise Exception("invalid request")
header = rem[:hlen-1]
body = rem[hlen+1:]
header = header.split('\0')
if len(header) < 4: raise Exception("invalid request: not enough header entries")
if header[0] != "CONTENT_LENGTH": raise Exception("invalid request: missing CONTENT_LENGTH")
clen = int(header[1])
if len(body) < clen: return False
env = dict()
while len(header) > 0:
if len(header) == 1: raise Exception("invalid request: missing value for key")
key, value = header[0:2]
header = header[2:]
if '' == key: raise Exception("invalid request: empty key")
if env.has_key(key): raise Exception("invalid request: duplicate key")
env[key] = value
if not env.has_key('SCGI') or env['SCGI'] != '1':
raise Exception("invalid request: missing/broken SCGI=1 header")
return {'env': env, 'body': body}
try:
while 1:
conn, addr = servsocket.accept()
result_status = 200
result = ''
try:
print 'Accepted connection'
data = ''
header = False
while not header:
newdata = conn.recv(1024)
if len(newdata) == 0: raise Exception("invalid request: unexpected EOF")
data += newdata
header = parsereq(data)
envvar = header['env']['QUERY_STRING']
result = header['env'][envvar]
except KeyboardInterrupt:
raise
except Exception as e:
print traceback.format_exc()
result_status = 500
result = str(e)
try:
conn.sendall("Status: " + str(result_status) + "\r\nContent-Type: text/plain\r\n\r\n")
conn.sendall(result)
conn.close()
except:
print traceback.format_exc()
except KeyboardInterrupt:
pass

@ -7,69 +7,6 @@ import socket
import os
import base
SCGI_ENVCHECK="""#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import traceback
servsocket = socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM)
def parsereq(data):
a = data.split(':', 1)
if len(a) != 2: return False
hlen, rem = a
hlen = int(hlen)
if hlen < 16: raise Exception("invalid request")
if len(rem) < hlen + 1: return False
if rem[hlen] != ',' or rem[hlen-1] != '\\0': raise Exception("invalid request")
header = rem[:hlen-1]
body = rem[hlen+1:]
header = header.split('\\0')
if len(header) < 4: raise Exception("invalid request: not enough header entries")
if header[0] != "CONTENT_LENGTH": raise Exception("invalid request: missing CONTENT_LENGTH")
clen = int(header[1])
if len(body) < clen: return False
env = dict()
while len(header) > 0:
if len(header) == 1: raise Exception("invalid request: missing value for key")
key, value = header[0:2]
header = header[2:]
if '' == key: raise Exception("invalid request: empty key")
if env.has_key(key): raise Exception("invalid request: duplicate key")
env[key] = value
if not env.has_key('SCGI') or env['SCGI'] != '1':
raise Exception("invalid request: missing/broken SCGI=1 header")
return {'env': env, 'body': body}
while 1:
conn, addr = servsocket.accept()
result_status = 200
result = ''
try:
print 'Connected from', addr
data = ''
header = False
while not header:
newdata = conn.recv(1024)
if len(newdata) == 0: raise Exception("invalid request: unexpected EOF")
data += newdata
header = parsereq(data)
envvar = header['env']['QUERY_STRING']
result = header['env'][envvar]
except Exception as e:
print traceback.format_exc()
result_status = 500
result = str(e)
try:
conn.sendall("Status: " + str(result_status) + "\\r\\nContent-Type: text/plain\\r\\n\\r\\n")
conn.sendall(result)
conn.close()
except:
print traceback.format_exc()
"""
class SCGI(Service):
name = "scgi"
binary = [ None ]
@ -77,14 +14,13 @@ class SCGI(Service):
def __init__(self):
super(SCGI, self).__init__()
self.sockfile = os.path.join(base.Env.dir, "tmp", "sockets", self.name + ".sock")
self.binary = [ os.path.join(base.Env.sourcedir, "run-scgi-envcheck.py") ]
def Prepare(self):
sockdir = self.tests.PrepareDir(os.path.join("tmp", "sockets"))
program = self.tests.PrepareFile('scgi-envcheck.py', SCGI_ENVCHECK, mode = 0755)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(os.path.relpath(self.sockfile))
sock.listen(8)
self.binary = [ program ]
self.fork(*self.binary, inp = sock)
def Cleanup(self):
@ -94,7 +30,6 @@ class SCGI(Service):
except BaseException, e:
print >>sys.stderr, "Couldn't delete socket '%s': %s" % (self.sockfile, e)
self.tests.CleanupDir(os.path.join("tmp", "sockets"))
self.tests.CleanupFile('scgi-envcheck.py')
class TestPathInfo1(CurlRequest):

Loading…
Cancel
Save