forked from njoyce/AMFBench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
250 lines (167 loc) · 5.96 KB
/
server.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
Provides a simple AMF remoting gateway that will strip the remoting wrapper and
dump the raw payload. Use in conjunction with C{amfbench/flex/main.swf}
"""
import os.path
import sys
import logging
from optparse import OptionParser
import struct
import mimetypes
import amfbench
class BaseMiddleware(object):
"""
@ivar app: The WSGI application that this middleware component wraps.
"""
def __init__(self, app):
self.app = app
class CrossdomainMiddleware(BaseMiddleware):
"""
Responds to '/crossdomain.xml' requests
"""
xml = ('<?xml version="1.0"?><cross-domain-policy>'
'<allow-access-from domain="*" to-ports="*" secure="false"/>'
'</cross-domain-policy>')
def __call__(self, environ, start_response):
if environ['PATH_INFO'] == '/crossdomain.xml':
start_response('200 OK', [
('Content-Type', 'application/xml'),
('Content-Length', str(len(self.xml)))
])
return [self.xml]
return self.app(environ, start_response)
class ServeStatic(BaseMiddleware):
"""
Serves static files.
"""
def __init__(self, app, dir_name, url_root):
BaseMiddleware.__init__(self, app)
self.dir_name = os.path.abspath(dir_name)
self.url_root = url_root
mimetypes.init()
def __call__(self, environ, start_response):
if not environ['PATH_INFO'].startswith(self.url_root):
return self.app(environ, start_response)
file_name = environ['PATH_INFO'][len(self.url_root):]
full_path = os.path.abspath(os.path.join(self.dir_name, file_name))
if not os.path.isfile(full_path):
return self.app(environ, start_response)
f = open(full_path, 'rb')
ext = os.path.splitext(full_path)[1]
bytes = f.read()
start_response('200 OK', [
('Content-Type', mimetypes.types_map[ext]),
('Content-Length', str(len(bytes)))
])
return [bytes]
class DecodingGeneratorGateway(BaseMiddleware):
"""
Accepts Flash Remoting requests and dumps it to a named file, returning a
dummy response.
"""
url = '/gw'
@staticmethod
def strip_envelope(bytes):
amf_version = struct.unpack('!H', bytes[:2])[0]
assert bytes[2:6] == '\x00\x00\x00\x01'
l = struct.unpack('!H', bytes[6:8])[0]
service_method = bytes[8:l+8]
bytes = bytes[l+8:]
l = struct.unpack('!H', bytes[0:2])[0]
uid = bytes[2:l+2]
bytes = bytes[l+2:]
if amf_version == 0:
boundary = '\n\x00\x00\x00\x01'
elif amf_version == 3:
boundary = '\x00\x00\x00\x01\x11'
else:
raise RuntimeError('Unknown AMF type')
bytes = bytes[bytes.find(boundary) + len(boundary):]
builder_name, size = service_method.split('-')
return builder_name, size, amf_version, uid, bytes
@staticmethod
def generate_response(uid):
# return an empty success response
ret = '\x00\x00\x00\x00\x00\x01'
s = '%s/onResult' % (uid,)
ret += struct.pack('!H', len(s))
ret += s
ret += '\x00\x04null\x00\x00\x00\x00\x05'
return ret
def __call__(self, environ, start_response):
if environ['PATH_INFO'] != self.url:
return self.app(environ, start_response)
if environ.get('REQUEST_METHOD', 'GET') == 'GET':
start_response('400 Bad Request', [])
return ['This gateway only accepts POST requests']
bytes = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH']))
builder_name, size, amf_version, uid, bytes = self.strip_envelope(bytes)
fn = amfbench.get_blob_filename(
builder_name, int(size), amf_version)
dn = os.path.dirname(fn)
try:
os.makedirs(dn)
except OSError:
pass
f = open(fn, 'wb')
f.write(bytes)
f.flush()
f.close()
del f
ret = self.generate_response(uid)
# flash doesn't respond to 204 :-/
start_response('200 OK', [
('Content-Length', str(len(ret))),
('Content-Type', 'application/x-amf')
])
return [ret]
def build_argparse():
parser = OptionParser()
parser.add_option("--iface", default='127.0.0.1', dest='iface',
help='The network interface to bind to. Supply 0.0.0.0 for all')
parser.add_option("--port", default=8080, type='int', dest='port',
help='The port to bind to.')
return parser
class Redirector(BaseMiddleware):
"""
Redirects from one url to another
"""
def __init__(self, app, url_from, url_to):
BaseMiddleware.__init__(self, app)
self.url_from = url_from
self.url_to = url_to
def __call__(self, environ, start_response):
if environ.get('PATH_INFO', None) != self.url_from:
return self.app(environ, start_response)
start_response('302 Moved Temporarily', [
('Location', self.url_to)
])
return []
def parse_options():
parser = build_argparse()
return parser.parse_args()
def get_app(options):
def four_oh_four(environ, start_response):
start_response('404 Not Found', [])
return ['<html><body><h1>404 Not Found</h1></body></html>']
app = four_oh_four
app = Redirector(app, '/', '/flex/DecodingGenerator.swf')
app = CrossdomainMiddleware(app)
app = DecodingGeneratorGateway(app)
app = ServeStatic(app, 'flex', '/flex/')
return app
def run_server(options):
from wsgiref import simple_server
httpd = simple_server.WSGIServer(
(options.iface, options.port),
simple_server.WSGIRequestHandler,
)
httpd.set_app(get_app(options))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
import server
options, args = parse_options()
server.run_server(options)