-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathreversehttp.py
189 lines (166 loc) · 6.72 KB
/
reversehttp.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import SocketServer
import StringIO
import BaseHTTPServer
import time
import httplib
from urllib import urlencode
from urlparse import urlsplit
def fetch(url, params = None, contentType = "application/x-www-form-urlencoded"):
pieces = urlsplit(url)
conn = httplib.HTTPConnection(pieces[1])
if params:
conn.request("POST", pieces[2],
body = params,
headers = {"Content-type": contentType})
else:
conn.request("GET", pieces[2])
return conn.getresponse()
class ReverseHttpServer(SocketServer.BaseServer):
def __init__(self, label, server_address, RequestHandlerClass):
SocketServer.BaseServer.__init__(self, server_address, RequestHandlerClass)
self.label = label
self.nextReq = None
self.location = None
self.failureDelay = 2
self.token = "-"
self.leaseSeconds = 30
self.reportPollExceptions = False
self.locationChangeCallback = None
def handle_request(self):
"""This code was originally written for python 2.5. In python
2.6, BaseServer has been refactored to support timeout,
implemented using select, for socket servers. Since here we're
abusing the structure of BaseServer to get requests via HTTP,
we don't have a file handle we can give to select---or at
least, we don't have one *readily available*. Consequently, we
override handle_request here to return to the python 2.5
behaviour. While this makes the server work again,
unfortunately the new timeout and shutdown behaviours
available in 2.6 may not work well with this class."""
if hasattr(self, '_handle_request_noblock'):
## We're in python 2.6.
## Don't worry, in python 2.6, _handle_request_noblock
## with the definition of get_request below *will* block!
return self._handle_request_noblock()
else:
## We're in some other python so rely on base behaviour.
return SocketServer.BaseServer.handle_request(self)
def serve_forever(self):
"""See the comment for handle_request. We override here to
return to the python 2.5 behaviour, so that we can shoehorn
our weird request-fetching mechanism into the standard httpd
classes."""
if hasattr(self, '_handle_request_noblock'):
## We're in python 2.6.
## Copy the code from 2.5 verbatim. Ick.
while 1:
self.handle_request()
else:
## We're in some other python so rely on base behaviour.
return SocketServer.BaseServer.serve_forever(self)
def get_request(self):
while 1:
try:
declareMode = (self.nextReq == None)
if declareMode:
resp = fetch(self.server_address,
urlencode({"name": self.label, "token": self.token}))
else:
resp = fetch(self.nextReq)
if resp.status >= 200 and resp.status < 300:
self.failureDelay = 2
if declareMode:
linkHeaders = parseLinkHeaders(resp)
self.nextReq = linkHeaders["first"]
locationText = linkHeaders["related"]
if locationText:
self.location = locationText
self.on_location_changed()
continue
else:
clientAddr = resp.getheader("Requesting-Client").split(":")
thisReq = self.nextReq
self.nextReq = parseLinkHeaders(resp)["next"]
return (ReverseHttpRequest(thisReq, self.server_address, resp.read()),
clientAddr)
except:
if self.reportPollExceptions:
self.report_poll_exception()
time.sleep(self.failureDelay)
if self.failureDelay < 30:
self.failureDelay = self.failureDelay * 2
def handle_error(self, request, client_address):
if not request.closed:
try:
request.write("HTTP/1.0 500 Internal Server Error\r\n\r\n")
request.close()
except:
pass
def report_poll_exception(self):
import traceback
traceback.print_exc()
def on_location_changed(self):
if self.locationChangeCallback:
self.locationChangeCallback(self)
def parseLinkHeaders(resp):
result = {}
for linkHeader in resp.getheader("Link").split(", "):
for piece in linkHeader.split(";"):
piece = piece.strip()
if piece[0] == '<':
url = piece[1:-1]
elif piece[:5].lower() == 'rel="':
rel = piece[5:-1]
if url and rel:
result[rel] = url
return result
class ForkingReverseHttpServer(SocketServer.ForkingMixIn, ReverseHttpServer): pass
class ThreadingReverseHttpServer(SocketServer.ThreadingMixIn, ReverseHttpServer): pass
class ReverseHttpRequest:
def __init__(self, replyUrl, server_address, body):
self.replyUrl = replyUrl
self.server_address = server_address
self.body = body
self.responseBuffer = StringIO.StringIO()
self.closed = False
def makefile(self, mode, bufsize):
if mode[0] == 'r':
return StringIO.StringIO(self.body)
elif mode[0] == 'w':
return self
def write(self, x):
return self.responseBuffer.write(x)
def flush(self):
pass
def close(self):
self.responseBuffer.flush()
respbody = self.responseBuffer.getvalue()
fetch(self.replyUrl, respbody, "message/http")
self.closed = True
def test():
import sys
if len(sys.argv) > 1:
label = sys.argv[1]
else:
label = 'python'
if len(sys.argv) > 2:
s = sys.argv[2]
else:
s = 'http://localhost:8000/reversehttp'
class TestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
counter = 0
def do_POST(self):
self.do_GET()
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write("This is response #" + str(TestHandler.counter) + "\r\n")
TestHandler.counter = TestHandler.counter + 1
def updateLocation(httpd):
print 'Serving HTTP on '+httpd.location+' ...'
httpd = ReverseHttpServer(label, s, TestHandler)
httpd.locationChangeCallback = updateLocation
httpd.serve_forever()
if __name__ == '__main__':
test()