-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·84 lines (65 loc) · 2.11 KB
/
client.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
#!/usr/bin/env python
"""
Client driver for PIPE.
"""
from __future__ import print_function
import os
import sys
import urllib2
import urllib
import cookielib
import urlparse
class RedirectBlocker(urllib2.HTTPErrorProcessor):
"""
This subclass disables automatic redirect handling in the client.
"""
def __init__(self):
pass
def http_response(self, request, response):
return response
https_response = http_response
# http://stackoverflow.com/a/11744894/1028526
def main():
"""
Main parser for a PIPE shell script.
"""
# Set up background.
prefixes = (os.environ.get('HTTPPATH') or "").split(";")
commands = (url.strip() for url in sys.argv[1].split("|"))
previous = None
if "" not in prefixes:
prefixes.append("")
# Prepare a redirect-less URL opener.
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(RedirectBlocker,
urllib2.HTTPCookieProcessor(cookiejar))
for command in commands:
for prefix in prefixes:
# Build a plausible URL with this prefix.
url = prefix + "/" + command
if not url.startswith('http'):
continue
# Fetch each code, as appropriate.
result = opener.open(url, urllib.urlencode({'source': previous}))
# Handle resulting data.
if result.code == 303:
previous = urlparse.urljoin(url, result.info()['Location'])
elif result.code in (200, 201):
previous = url
elif result.code == 404:
continue
else:
print("URL: {!r}".format(url))
print("HTTP Error: {!r}".format(result.code))
print("")
print(result.read())
sys.exit(1)
break
else:
print("Unknown command: {!r}".format(command))
sys.exit(2)
# Retrieve the final result from the last item in the pipeline.
result = urllib.urlopen(previous)
print(result.read())
if __name__ == "__main__":
main()