Windows workarounds
[zeroinstall/solver.git] / tests / server.py
blobbce769eb722d2e2f151303e59579c6655dc4d8a2
1 #!/usr/bin/env python
2 import os, sys, urlparse
3 import BaseHTTPServer
4 import traceback
6 next_step = None
8 class Give404:
9 def __init__(self, path):
10 self.path = path
12 def __str__(self):
13 return self.path
15 def __repr__(self):
16 return "404 on " + self.path
18 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
19 def do_GET(self):
20 parsed = urlparse.urlparse(self.path)
22 leaf = os.path.basename(parsed.path)
24 acceptable = dict([(str(x), x) for x in next_step])
26 resp = acceptable.get(parsed.path, None) or \
27 acceptable.get(leaf, None) or \
28 acceptable.get('*', None)
30 if not resp:
31 self.send_error(404, "Expected %s; got %s" % (next_step, parsed.path))
32 elif parsed.path.startswith('/key-info/'):
33 self.send_response(200)
34 self.end_headers()
35 self.wfile.write('<key-lookup/>')
36 self.wfile.close()
37 elif os.path.exists(leaf) and not isinstance(resp, Give404):
38 self.send_response(200)
39 self.end_headers()
40 self.wfile.write(file(leaf).read())
41 self.wfile.close()
42 else:
43 self.send_error(404, "Missing: %s" % leaf)
45 def handle_requests(*script):
46 server_address = ('localhost', 8000)
47 httpd = BaseHTTPServer.HTTPServer(server_address, MyHandler)
48 child = os.fork()
49 if child:
50 return child
51 # We are the child
52 try:
53 sys.stderr = sys.stdout
54 #sys.stdout = sys.stderr
55 print "Waiting for request"
56 global next_step
57 for next_step in script:
58 if type(next_step) != tuple: next_step = (next_step,)
59 for x in next_step:
60 httpd.handle_request()
61 print "Done"
62 os._exit(0)
63 except:
64 traceback.print_exc()
65 os._exit(1)