Removed prompts from background download dialogs
[zeroinstall/zeroinstall-limyreth.git] / tests / server.py
blob681d204103dff7464c38a30b353b4b7fe8bf607a
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 # (don't use a symlink as they don't work on Windows)
31 if leaf == 'latest.xml':
32 leaf = 'Hello.xml'
34 if not resp:
35 self.send_error(404, "Expected %s; got %s" % (next_step, parsed.path))
36 elif parsed.path.startswith('/key-info/'):
37 self.send_response(200)
38 self.end_headers()
39 self.wfile.write('<key-lookup/>')
40 self.wfile.close()
41 elif os.path.exists(leaf) and not isinstance(resp, Give404):
42 self.send_response(200)
43 self.end_headers()
44 self.wfile.write(file(leaf).read())
45 self.wfile.close()
46 else:
47 self.send_error(404, "Missing: %s" % leaf)
49 def handle_requests(*script):
50 server_address = ('localhost', 8000)
51 httpd = BaseHTTPServer.HTTPServer(server_address, MyHandler)
52 child = os.fork()
53 if child:
54 return child
55 # We are the child
56 try:
57 sys.stderr = sys.stdout
58 #sys.stdout = sys.stderr
59 print "Waiting for request"
60 global next_step
61 for next_step in script:
62 if type(next_step) != tuple: next_step = (next_step,)
63 for x in next_step:
64 httpd.handle_request()
65 print "Done"
66 os._exit(0)
67 except:
68 traceback.print_exc()
69 os._exit(1)