-
Notifications
You must be signed in to change notification settings - Fork 3
/
dwiki-cache.py
executable file
·91 lines (78 loc) · 2.3 KB
/
dwiki-cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
#
# Force things into the DWiki BFC cache if they aren't already there.
# dwiki-cat.py [-P port] CFG HOSTNAME URL [URL ....]
#
#
import sys
import urlparse
import dwconfig, derrors
import httpcore, htmlrends, context
def start_response(code, headers):
pass
# We don't reuse the environment so we don't bother making a copy before
# we run the app.
def runwsgi(env, app):
app(env, start_response)
def setup_env(url, host, options):
_, _, path, query, _ = urlparse.urlsplit(url)
if options.port == "443":
options.scheme = "https"
if options.scheme == "https" and options.port == "80":
options.port = "443"
env = {
'SERVER_PROTOCOL': 'HTTP/1.0',
'SERVER_NAME': host,
'SERVER_PORT': options.port,
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '9999',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': url,
'SCRIPT_NAME': '',
'PATH_INFO': path,
'QUERY_STRING': query,
'wsgi.version': (1, 0),
'wsgi.url_scheme': options.scheme,
'wsgi.multithread': 0,
'wsgi.multiprocess': 0,
'wsgi.run_once': 1,
'wsgi.input': None,
'wsgi.errors': sys.stderr,
'HTTP_HOST': host,
'HTTP_USER_AGENT': 'DWiki Testbench.py',
}
if options.scheme == "https":
env["HTTPS"] = "on"
return env
def die(msg):
sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
sys.exit(1)
def usage():
sys.stderr.write("usage: %s [options|--help] CONFIG HOSTNAME URL [URL ...]\n" % sys.argv[0])
sys.exit(1)
def setup_options():
parser = dwconfig.setup_options("%prog [--help] [options] CONFIG HOSTNAME URL [URL ...]",
"dwiki-cache 0.1")
parser.add_option('-P', '--port', dest='port', action="store",
type="string",
help="set the server port for the requests, default %default.")
parser.add_option("-S", "--https", dest="scheme", action="store_const",
const="https",
help="pretend the request used HTTPS instead of HTTP.")
parser.set_defaults(port="80", scheme="http")
return parser
def main(args):
parser = setup_options()
(options, args) = parser.parse_args(args)
if len(args) < 3:
usage()
options.extraconfig.append("internal_bfc-force-caching")
try:
app, r = dwconfig.materialize(args[0], options)
except derrors.WikiErr as e:
die("dwiki error: "+str(e))
for url in args[2:]:
env = setup_env(url, args[1], options)
runwsgi(env, app)
if __name__ == "__main__":
main(sys.argv[1:])