b@2264
|
1 #!/usr/bin/python
|
b@2264
|
2
|
b@2264
|
3 # Detect the Python version to switch code between 2.x and 3.x
|
b@2264
|
4 # http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime
|
b@2264
|
5 import sys
|
b@2264
|
6
|
b@2264
|
7 import inspect
|
b@2264
|
8 import os
|
b@2264
|
9 import pickle
|
b@2264
|
10 import datetime
|
nicholas@2430
|
11 import operator
|
nicholas@2430
|
12 import xml.etree.ElementTree as ET
|
nicholas@2431
|
13 import copy
|
nicholas@2510
|
14 import string
|
nicholas@2510
|
15 import random
|
nicholas@3028
|
16 import errno
|
b@2264
|
17
|
b@2264
|
18 if sys.version_info[0] == 2:
|
b@2264
|
19 # Version 2.x
|
b@2264
|
20 import BaseHTTPServer
|
b@2264
|
21 import urllib2
|
b@2264
|
22 import urlparse
|
b@2264
|
23 elif sys.version_info[0] == 3:
|
b@2264
|
24 # Version 3.x
|
b@2264
|
25 from http.server import BaseHTTPRequestHandler, HTTPServer
|
b@2264
|
26 import urllib as urllib2
|
b@2264
|
27
|
b@2264
|
28 # Go to right folder.
|
b@2264
|
29 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
|
b@2264
|
30 os.chdir(scriptdir) # does this work?
|
b@2264
|
31
|
nicholas@3028
|
32 try:
|
nicholas@3028
|
33 os.makedirs("../saves")
|
nicholas@3028
|
34 except OSError as e:
|
nicholas@3028
|
35 if e.errno != errno.EEXIST:
|
nicholas@3028
|
36 raise
|
nicholas@3028
|
37
|
b@2264
|
38 PSEUDO_PATH = '../tests/'
|
b@2264
|
39 pseudo_files = []
|
nicholas@2450
|
40 pseudo_index = 0
|
nicholas@2430
|
41 for filename in os.listdir(PSEUDO_PATH):
|
b@2264
|
42 print(filename)
|
b@2264
|
43 if filename.endswith('.xml'):
|
b@2264
|
44 pseudo_files.append(filename)
|
b@2264
|
45
|
b@2264
|
46 curSaveIndex = 0;
|
b@2264
|
47 curFileName = 'test-0.xml'
|
nicholas@2430
|
48 while(os.path.isfile('../saves/'+curFileName)):
|
b@2264
|
49 curSaveIndex += 1;
|
b@2264
|
50 curFileName = 'test-'+str(curSaveIndex)+'.xml'
|
b@2264
|
51
|
b@2264
|
52 if len(pseudo_files) > 0:
|
b@2264
|
53 pseudo_index = curSaveIndex % len(pseudo_files)
|
b@2264
|
54 else:
|
b@2264
|
55 pseudo_index = 0
|
b@2264
|
56
|
b@2264
|
57 print('URL: http://localhost:8000/index.html')
|
b@2264
|
58
|
b@2264
|
59 def send404(s):
|
b@2264
|
60 s.send_response(404)
|
b@2264
|
61 s.send_header("Content-type", "text/html")
|
b@2264
|
62 s.end_headers()
|
b@2264
|
63
|
b@2264
|
64 def processFile(s):
|
b@2264
|
65 if sys.version_info[0] == 2:
|
b@2264
|
66 s.path = s.path.rsplit('?')
|
b@2264
|
67 s.path = s.path[0]
|
b@2264
|
68 s.path = s.path[1:len(s.path)]
|
b@2264
|
69 st = s.path.rsplit(',')
|
b@2264
|
70 lenSt = len(st)
|
b@2264
|
71 fmt = st[lenSt-1].rsplit('.')
|
nicholas@2981
|
72 fmt = fmt[len(fmt)-1]
|
b@2264
|
73 fpath = "../"+urllib2.unquote(s.path)
|
n@2432
|
74 size = os.path.getsize(fpath)
|
nicholas@2981
|
75 fileDump = open(fpath, mode='rb')
|
b@2264
|
76 s.send_response(200)
|
b@2264
|
77
|
nicholas@2981
|
78 if (fmt == 'html'):
|
b@2264
|
79 s.send_header("Content-type", 'text/html')
|
nicholas@2981
|
80 elif (fmt == 'css'):
|
b@2264
|
81 s.send_header("Content-type", 'text/css')
|
nicholas@2981
|
82 elif (fmt == 'js'):
|
b@2264
|
83 s.send_header("Content-type", 'application/javascript')
|
b@2264
|
84 else:
|
b@2264
|
85 s.send_header("Content-type", 'application/octet-stream')
|
nicholas@3139
|
86 s.send_header("Cache-Control", "no-cache")
|
nicholas@2542
|
87 fileRead = fileDump.read()
|
nicholas@2542
|
88 s.send_header("Content-Length", len(fileRead))
|
b@2264
|
89 s.end_headers()
|
nicholas@2542
|
90 s.wfile.write(fileRead)
|
b@2264
|
91 fileDump.close()
|
b@2264
|
92 elif sys.version_info[0] == 3:
|
b@2264
|
93 s.path = s.path.rsplit('?')
|
b@2264
|
94 s.path = s.path[0]
|
b@2264
|
95 s.path = s.path[1:len(s.path)]
|
b@2264
|
96 st = s.path.rsplit(',')
|
b@2264
|
97 lenSt = len(st)
|
b@2264
|
98 fmt = st[lenSt-1].rsplit('.')
|
b@2264
|
99 fpath = "../"+urllib2.parse.unquote(s.path)
|
b@2264
|
100 s.send_response(200)
|
b@2264
|
101 if (fmt[1] == 'html'):
|
b@2264
|
102 s.send_header("Content-type", 'text/html')
|
b@2264
|
103 fileDump = open(fpath, encoding='utf-8')
|
b@2264
|
104 fileBytes = bytes(fileDump.read(), "utf-8")
|
b@2264
|
105 fileDump.close()
|
b@2264
|
106 elif (fmt[1] == 'css'):
|
b@2264
|
107 s.send_header("Content-type", 'text/css')
|
b@2264
|
108 fileDump = open(fpath, encoding='utf-8')
|
b@2264
|
109 fileBytes = bytes(fileDump.read(), "utf-8")
|
b@2264
|
110 fileDump.close()
|
b@2264
|
111 elif (fmt[1] == 'js'):
|
b@2264
|
112 s.send_header("Content-type", 'application/javascript')
|
b@2264
|
113 fileDump = open(fpath, encoding='utf-8')
|
b@2264
|
114 fileBytes = bytes(fileDump.read(), "utf-8")
|
b@2264
|
115 fileDump.close()
|
b@2264
|
116 else:
|
b@2264
|
117 s.send_header("Content-type", 'application/octet-stream')
|
b@2264
|
118 fileDump = open(fpath, 'rb')
|
b@2264
|
119 fileBytes = fileDump.read()
|
b@2264
|
120 fileDump.close()
|
nicholas@3139
|
121 s.send_header("Cache-Control", "no-cache")
|
b@2264
|
122 s.send_header("Content-Length", len(fileBytes))
|
b@2264
|
123 s.end_headers()
|
b@2264
|
124 s.wfile.write(fileBytes)
|
nicholas@2510
|
125
|
nicholas@2510
|
126 def requestKey(s):
|
nicholas@2510
|
127 reply = ""
|
nicholas@2510
|
128 key = ''
|
nicholas@2510
|
129 while key == '':
|
nicholas@2510
|
130 tempKey = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32));
|
nicholas@2510
|
131 if (os.path.isfile("saves/save-"+tempKey+".xml") == False):
|
nicholas@2510
|
132 key = tempKey
|
nicholas@2940
|
133 options = s.path.rsplit('?')
|
nicholas@2940
|
134 options = options[1].rsplit('&')
|
nicholas@2940
|
135 for option in options:
|
nicholas@2940
|
136 optionPair = option.rsplit('=')
|
nicholas@2940
|
137 if optionPair[0] == "saveFilenamePrefix":
|
nicholas@2940
|
138 prefix = optionPair[1]
|
nicholas@2940
|
139 if prefix == None:
|
nicholas@2940
|
140 prefix = "save"
|
nicholas@2510
|
141 s.send_response(200)
|
nicholas@3139
|
142 s.send_header("Content-Type", "application/xml")
|
nicholas@3139
|
143 s.send_header("Cache-Control", "no-cache")
|
nicholas@2510
|
144 s.end_headers()
|
nicholas@2510
|
145 reply = "<response><state>OK</state><key>"+key+"</key></response>"
|
nicholas@2510
|
146 if sys.version_info[0] == 2:
|
nicholas@2510
|
147 s.wfile.write(reply)
|
nicholas@2510
|
148 elif sys.version_info[0] == 3:
|
nicholas@2510
|
149 s.wfile.write(bytes(reply, "utf-8"))
|
nicholas@2940
|
150 file = open("../saves/"+prefix+"-"+key+".xml",'w')
|
nicholas@2932
|
151 file.write("<waetresult key=\""+key+"\"/>")
|
nicholas@2510
|
152 file.close()
|
nicholas@2510
|
153
|
b@2264
|
154
|
b@2264
|
155 def saveFile(self):
|
b@2264
|
156 global curFileName
|
b@2264
|
157 global curSaveIndex
|
b@2264
|
158 options = self.path.rsplit('?')
|
nicholas@2722
|
159 options = options[1].rsplit('&')
|
n@2997
|
160 update = False
|
nicholas@2722
|
161 for option in options:
|
nicholas@2722
|
162 optionPair = option.rsplit('=')
|
nicholas@2722
|
163 if optionPair[0] == "key":
|
nicholas@2722
|
164 key = optionPair[1]
|
nicholas@2722
|
165 elif optionPair[0] == "saveFilenamePrefix":
|
nicholas@2722
|
166 prefix = optionPair[1]
|
n@2997
|
167 elif optionPair[0] == "state":
|
n@2997
|
168 update = optionPair[1] == "update"
|
nicholas@2722
|
169 if key == None:
|
nicholas@2722
|
170 self.send_response(404)
|
nicholas@2722
|
171 return
|
nicholas@2722
|
172 if prefix == None:
|
nicholas@2722
|
173 prefix = "save"
|
b@2264
|
174 varLen = int(self.headers['Content-Length'])
|
b@2264
|
175 postVars = self.rfile.read(varLen)
|
b@2264
|
176 print("Saving file key "+key)
|
nicholas@2722
|
177 filename = prefix+'-'+key+'.xml'
|
n@2997
|
178 if update:
|
n@2997
|
179 filename = "update-"+filename
|
nicholas@2722
|
180 file = open('../saves/'+filename,'wb')
|
b@2264
|
181 file.write(postVars)
|
b@2264
|
182 file.close()
|
b@2264
|
183 try:
|
nicholas@2722
|
184 wbytes = os.path.getsize('../saves/'+filename)
|
b@2264
|
185 except OSError:
|
b@2264
|
186 self.send_response(200)
|
b@2264
|
187 self.send_header("Content-type", "text/xml")
|
b@2264
|
188 self.end_headers()
|
b@2264
|
189 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
|
b@2264
|
190 self.send_response(200)
|
b@2264
|
191 self.send_header("Content-type", "text/xml")
|
b@2264
|
192 self.end_headers()
|
nicholas@2722
|
193 reply = '<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+filename+'"</file></response>'
|
nicholas@2382
|
194 if sys.version_info[0] == 2:
|
nicholas@2382
|
195 self.wfile.write(reply)
|
nicholas@2382
|
196 elif sys.version_info[0] == 3:
|
nicholas@2382
|
197 self.wfile.write(bytes(reply, "utf-8"))
|
b@2264
|
198 curSaveIndex += 1
|
b@2264
|
199 curFileName = 'test-'+str(curSaveIndex)+'.xml'
|
n@2997
|
200 if update == False:
|
nicholas@3066
|
201 if(os.path.isfile("../saves/update-"+filename)):
|
nicholas@3066
|
202 os.remove("../saves/update-"+filename)
|
nicholas@2934
|
203
|
nicholas@2934
|
204 def testSave(self):
|
nicholas@2934
|
205 self.send_response(200)
|
nicholas@2934
|
206 self.send_header("Content-type", "text/xml")
|
nicholas@2934
|
207 self.end_headers()
|
nicholas@2934
|
208 filename = "../saves/test-save.xml"
|
nicholas@2934
|
209 file = open(filename,'wb')
|
nicholas@2934
|
210 if sys.version_info[0] == 2:
|
nicholas@2934
|
211 file.write("<xml></xml>")
|
nicholas@2934
|
212 elif sys.version_info[0] == 3:
|
nicholas@2934
|
213 file.write(bytes("<xml></xml>", "utf-8"))
|
nicholas@2934
|
214 file.close()
|
nicholas@2934
|
215 message = ""
|
nicholas@2934
|
216 try:
|
nicholas@2934
|
217 wbytes = os.path.getsize(filename)
|
nicholas@2934
|
218 except OSError:
|
nicholas@2934
|
219 message = '<response state="error"><message>Could not open file</message></response>';
|
nicholas@2934
|
220 if sys.version_info[0] == 2:
|
nicholas@2934
|
221 self.wfile.write(message)
|
nicholas@2934
|
222 elif sys.version_info[0] == 3:
|
nicholas@2934
|
223 self.wfile.write(bytes(message, "utf-8"))
|
nicholas@2934
|
224 return
|
nicholas@2934
|
225 os.remove(filename)
|
nicholas@2934
|
226 message = '<response state="OK"><message>OK</message></response>';
|
nicholas@2934
|
227 if sys.version_info[0] == 2:
|
nicholas@2934
|
228 self.wfile.write(message)
|
nicholas@2934
|
229 elif sys.version_info[0] == 3:
|
nicholas@2934
|
230 self.wfile.write(bytes(message, "utf-8"))
|
b@2264
|
231
|
nicholas@2430
|
232 def poolXML(s):
|
nicholas@2430
|
233 pool = ET.parse('../tests/pool.xml')
|
nicholas@2430
|
234 root = pool.getroot()
|
nicholas@2430
|
235 setupNode = root.find("setup");
|
nicholas@2430
|
236 poolSize = setupNode.get("poolSize",0);
|
nicholas@2430
|
237 if (poolSize == 0):
|
nicholas@2430
|
238 s.path = s.path.split("/php",1)[0]+"/tests/pool/xml"
|
nicholas@2430
|
239 processFile(s)
|
nicholas@2430
|
240 return
|
nicholas@2431
|
241 poolSize = int(poolSize)
|
nicholas@2430
|
242 # Set up the store will all the test page key nodes
|
nicholas@2430
|
243 pages = {};
|
nicholas@2430
|
244 for page in root.iter("page"):
|
nicholas@2430
|
245 id = page.get("id")
|
nicholas@2430
|
246 pages[id] = 0
|
nicholas@2430
|
247 # Read the saves and determine the completed pages
|
nicholas@2430
|
248 for filename in os.listdir("../saves/"):
|
nicholas@2430
|
249 if filename.endswith(".xml"):
|
nicholas@2430
|
250 save = ET.parse("../saves/"+filename)
|
nicholas@2430
|
251 save_root = save.getroot();
|
nicholas@2431
|
252 if (save_root.find("waet").get("url") == "http://localhost:8000/php/pool.php"):
|
nicholas@2431
|
253 for page in save_root.findall("./page"):
|
nicholas@2431
|
254 id = page.get("ref")
|
nicholas@2430
|
255 pages[id] = pages[id] + 1
|
nicholas@2430
|
256
|
nicholas@2430
|
257 # Sort the dictionary
|
nicholas@2431
|
258 rot_pages = {}
|
nicholas@2431
|
259 for key, value in pages.items():
|
nicholas@2431
|
260 if (value in rot_pages):
|
nicholas@2431
|
261 rot_pages[value].append(key)
|
nicholas@2431
|
262 else:
|
nicholas@2431
|
263 rot_pages[value] = [key]
|
nicholas@2431
|
264
|
nicholas@2431
|
265 Keys = list(rot_pages)
|
nicholas@2431
|
266 print ("Current pool state:")
|
nicholas@2431
|
267 print (rot_pages)
|
nicholas@2431
|
268
|
nicholas@2431
|
269 return_node = ET.fromstring('<waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd"/>');
|
nicholas@2431
|
270 return_node.append(copy.deepcopy(root.find("setup")))
|
nicholas@2431
|
271 page_elements = root.findall("page")
|
nicholas@2430
|
272
|
nicholas@2431
|
273 # Now append the pages
|
nicholas@2431
|
274 i = 0
|
nicholas@2431
|
275 while(len(return_node.findall("page")) < poolSize):
|
nicholas@2431
|
276 if (i > 0):
|
nicholas@2431
|
277 for page in return_node.iter("page"):
|
nicholas@2431
|
278 page.set("alwaysInclude","true")
|
nicholas@2431
|
279 for id in rot_pages[Keys[i]]:
|
nicholas@2431
|
280 return_node.append(copy.deepcopy(root.find('./page[@id="'+id+'"]')))
|
nicholas@2431
|
281 i=i+1
|
nicholas@2431
|
282 s.send_response(200)
|
nicholas@2431
|
283 s.send_header("Content-type", "text/xml")
|
nicholas@2431
|
284 s.end_headers()
|
nicholas@2431
|
285 s.wfile.write(ET.tostring(return_node))
|
nicholas@2431
|
286
|
b@2264
|
287 def http_do_HEAD(s):
|
b@2264
|
288 s.send_response(200)
|
b@2264
|
289 s.send_header("Content-type", "text/html")
|
b@2264
|
290 s.end_headers()
|
b@2264
|
291
|
b@2264
|
292 def http_do_GET(request):
|
nicholas@2450
|
293 global pseudo_index
|
nicholas@3139
|
294 print(request.path)
|
b@2264
|
295 if(request.client_address[0] == "127.0.0.1"):
|
b@2264
|
296 if (request.path == "/favicon.ico"):
|
b@2264
|
297 send404(request)
|
nicholas@2510
|
298 elif (request.path.split('?',1)[0] == "/php/requestKey.php"):
|
nicholas@3139
|
299 requestKey(request)
|
nicholas@2430
|
300 elif (request.path.split('?',1)[0] == "/php/pool.php"):
|
nicholas@3139
|
301 poolXML(request)
|
nicholas@2934
|
302 elif (request.path.split('?',1)[0] == "/php/test_write.php"):
|
nicholas@3139
|
303 testSave(request)
|
b@2264
|
304 else:
|
b@2264
|
305 request.path = request.path.split('?',1)[0]
|
b@2264
|
306 if (request.path == '/'):
|
b@2264
|
307 request.path = '/index.html'
|
b@2264
|
308 elif (request.path == '/pseudo.xml'):
|
nicholas@2450
|
309 request.path = PSEUDO_PATH + pseudo_files[pseudo_index]
|
b@2264
|
310 print(request.path)
|
b@2264
|
311 pseudo_index += 1
|
b@2264
|
312 pseudo_index %= len(pseudo_files)
|
b@2264
|
313 processFile(request)
|
b@2264
|
314 else:
|
b@2264
|
315 send404(request)
|
b@2264
|
316
|
b@2264
|
317 def http_do_POST(request):
|
b@2264
|
318 if(request.client_address[0] == "127.0.0.1"):
|
b@2264
|
319 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/php/save.php"):
|
b@2264
|
320 saveFile(request)
|
nicholas@3139
|
321 elif (request.path.split('?',1)[0] == "/php/requestKey.php"):
|
nicholas@3139
|
322 requestKey(request)
|
b@2264
|
323 else:
|
b@2264
|
324 send404(request)
|
b@2264
|
325
|
b@2264
|
326 if sys.version_info[0] == 2:
|
b@2264
|
327 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
b@2264
|
328 def do_HEAD(s):
|
b@2264
|
329 http_do_HEAD(s)
|
b@2264
|
330 def do_GET(request):
|
b@2264
|
331 http_do_GET(request)
|
b@2264
|
332 def do_POST(request):
|
b@2264
|
333 http_do_POST(request)
|
b@2264
|
334 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler):
|
b@2264
|
335 server_address = ('', 8000)
|
b@2264
|
336 httpd = server_class(server_address, handler_class)
|
b@2264
|
337 httpd.serve_forever()
|
b@2264
|
338 run()
|
b@2264
|
339 elif sys.version_info[0] == 3:
|
b@2264
|
340 class MyHandler(BaseHTTPRequestHandler):
|
b@2264
|
341 def do_HEAD(s):
|
b@2264
|
342 send404(s)
|
b@2264
|
343 def do_GET(request):
|
b@2264
|
344 http_do_GET(request)
|
b@2264
|
345 def do_POST(request):
|
b@2264
|
346 http_do_POST(request)
|
b@2264
|
347 def run(server_class=HTTPServer,handler_class=MyHandler):
|
b@2264
|
348 server_address = ('', 8000)
|
b@2264
|
349 httpd = server_class(server_address, handler_class)
|
b@2264
|
350 httpd.serve_forever()
|
b@2264
|
351 run()
|