3 from __future__
import print_function
8 if sys
.version_info
[0] > 2:
9 from urllib
import parse
as urlparse
# Python 3
10 from http
import server
14 import BaseHTTPServer
as server
15 import cPickle
as pickle
20 def __init__(self
, path
):
27 return "404 on " + self
.path
29 class MyHandler(server
.BaseHTTPRequestHandler
):
31 parsed
= urlparse
.urlparse(self
.path
)
34 if parsed
.path
.startswith('/redirect/'):
35 self
.send_response(302)
36 self
.wfile
.write(('Location: /' + parsed
.path
[1:].split('/', 1)[1]).encode('utf-8'))
39 leaf
= os
.path
.basename(parsed
.path
)
41 acceptable
= dict([(str(x
), x
) for x
in next_step
])
43 resp
= acceptable
.get(parsed
.path
, None) or \
44 acceptable
.get(leaf
, None) or \
45 acceptable
.get('*', None)
47 # (don't use a symlink as they don't work on Windows)
48 if leaf
== 'latest.xml':
50 elif parsed
.path
== '/0mirror/feeds/http/example.com:8000/Hello.xml/impl/sha1=3ce644dc725f1d21cfcf02562c76f375944b266a':
51 leaf
= 'HelloWorld.tgz'
54 self
.send_error(404, "Expected %s; got %s" % (next_step
, parsed
.path
))
55 elif parsed
.path
.startswith('/key-info/'):
56 self
.send_response(200)
58 self
.wfile
.write(b
'<key-lookup><item vote="good">Approved for testing</item></key-lookup>')
60 elif os
.path
.exists(leaf
) and not isinstance(resp
, Give404
):
61 self
.send_response(200)
63 with
open(leaf
, 'rb') as stream
:
64 self
.wfile
.write(stream
.read())
67 self
.send_error(404, "Missing: %s" % leaf
)
69 def handle_requests(*script
):
70 from subprocess
import Popen
, PIPE
72 # Pass the script on the command line as a pickle.
74 [sys
.executable
, __file__
, repr(pickle
.dumps(script
)) ],
75 stdout
=PIPE
, universal_newlines
=True)
77 # Make sure the server is actually running before we try to
79 l
= child
.stdout
.readline()
80 assert l
== 'Waiting for request\n', l
84 # Grab the script that was passed on the command line from the parent
85 script
= pickle
.loads(eval(sys
.argv
[1]))
86 server_address
= ('localhost', 8000)
87 httpd
= server
.HTTPServer(server_address
, MyHandler
)
89 sys
.stderr
= sys
.stdout
90 #sys.stdout = sys.stderr
91 print("Waiting for request")
92 sys
.stdout
.flush() # Make sure the "Waiting..." message is seen by the parent
94 for next_step
in script
:
95 if type(next_step
) != tuple: next_step
= (next_step
,)
97 httpd
.handle_request()
101 traceback
.print_exc()
104 if __name__
== '__main__':
105 # This is the child process. We have to import ourself and
106 # run the main routine there, or the pickled Give404 instances
107 # passed from the parent won't be recognized as having the
108 # same class (server.Give404).