b@2264: #!/usr/bin/python b@2264: b@2264: # Detect the Python version to switch code between 2.x and 3.x b@2264: # http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime b@2264: import sys b@2264: b@2264: import inspect b@2264: import os b@2264: import pickle b@2264: import datetime nicholas@2430: import operator nicholas@2430: import xml.etree.ElementTree as ET nicholas@2431: import copy nicholas@2510: import string nicholas@2510: import random b@2264: b@2264: if sys.version_info[0] == 2: b@2264: # Version 2.x b@2264: import BaseHTTPServer b@2264: import urllib2 b@2264: import urlparse b@2264: elif sys.version_info[0] == 3: b@2264: # Version 3.x b@2264: from http.server import BaseHTTPRequestHandler, HTTPServer b@2264: import urllib as urllib2 b@2264: b@2264: # Go to right folder. b@2264: scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory b@2264: os.chdir(scriptdir) # does this work? b@2264: b@2264: PSEUDO_PATH = '../tests/' b@2264: pseudo_files = [] nicholas@2450: pseudo_index = 0 nicholas@2430: for filename in os.listdir(PSEUDO_PATH): b@2264: print(filename) b@2264: if filename.endswith('.xml'): b@2264: pseudo_files.append(filename) b@2264: b@2264: curSaveIndex = 0; b@2264: curFileName = 'test-0.xml' nicholas@2430: while(os.path.isfile('../saves/'+curFileName)): b@2264: curSaveIndex += 1; b@2264: curFileName = 'test-'+str(curSaveIndex)+'.xml' b@2264: b@2264: if len(pseudo_files) > 0: b@2264: pseudo_index = curSaveIndex % len(pseudo_files) b@2264: else: b@2264: pseudo_index = 0 b@2264: b@2264: print('URL: http://localhost:8000/index.html') b@2264: b@2264: def send404(s): b@2264: s.send_response(404) b@2264: s.send_header("Content-type", "text/html") b@2264: s.end_headers() b@2264: b@2264: def processFile(s): b@2264: if sys.version_info[0] == 2: b@2264: s.path = s.path.rsplit('?') b@2264: s.path = s.path[0] b@2264: s.path = s.path[1:len(s.path)] b@2264: st = s.path.rsplit(',') b@2264: lenSt = len(st) b@2264: fmt = st[lenSt-1].rsplit('.') b@2264: fpath = "../"+urllib2.unquote(s.path) n@2432: size = os.path.getsize(fpath) b@2264: fileDump = open(fpath) b@2264: s.send_response(200) b@2264: b@2264: if (fmt[1] == 'html'): b@2264: s.send_header("Content-type", 'text/html') b@2264: elif (fmt[1] == 'css'): b@2264: s.send_header("Content-type", 'text/css') b@2264: elif (fmt[1] == 'js'): b@2264: s.send_header("Content-type", 'application/javascript') b@2264: else: b@2264: s.send_header("Content-type", 'application/octet-stream') nicholas@2542: fileRead = fileDump.read() nicholas@2542: s.send_header("Content-Length", len(fileRead)) b@2264: s.end_headers() nicholas@2542: s.wfile.write(fileRead) b@2264: fileDump.close() b@2264: elif sys.version_info[0] == 3: b@2264: s.path = s.path.rsplit('?') b@2264: s.path = s.path[0] b@2264: s.path = s.path[1:len(s.path)] b@2264: st = s.path.rsplit(',') b@2264: lenSt = len(st) b@2264: fmt = st[lenSt-1].rsplit('.') b@2264: fpath = "../"+urllib2.parse.unquote(s.path) b@2264: s.send_response(200) b@2264: if (fmt[1] == 'html'): b@2264: s.send_header("Content-type", 'text/html') b@2264: fileDump = open(fpath, encoding='utf-8') b@2264: fileBytes = bytes(fileDump.read(), "utf-8") b@2264: fileDump.close() b@2264: elif (fmt[1] == 'css'): b@2264: s.send_header("Content-type", 'text/css') b@2264: fileDump = open(fpath, encoding='utf-8') b@2264: fileBytes = bytes(fileDump.read(), "utf-8") b@2264: fileDump.close() b@2264: elif (fmt[1] == 'js'): b@2264: s.send_header("Content-type", 'application/javascript') b@2264: fileDump = open(fpath, encoding='utf-8') b@2264: fileBytes = bytes(fileDump.read(), "utf-8") b@2264: fileDump.close() b@2264: else: b@2264: s.send_header("Content-type", 'application/octet-stream') b@2264: fileDump = open(fpath, 'rb') b@2264: fileBytes = fileDump.read() b@2264: fileDump.close() b@2264: s.send_header("Content-Length", len(fileBytes)) b@2264: s.end_headers() b@2264: s.wfile.write(fileBytes) nicholas@2510: nicholas@2510: def requestKey(s): nicholas@2510: reply = "" nicholas@2510: key = '' nicholas@2510: while key == '': nicholas@2510: tempKey = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32)); nicholas@2510: if (os.path.isfile("saves/save-"+tempKey+".xml") == False): nicholas@2510: key = tempKey nicholas@2510: s.send_response(200) nicholas@2510: s.send_header("Content-type", "application/xml"); nicholas@2510: s.end_headers() nicholas@2510: reply = "OK"+key+"" nicholas@2510: if sys.version_info[0] == 2: nicholas@2510: s.wfile.write(reply) nicholas@2510: elif sys.version_info[0] == 3: nicholas@2510: s.wfile.write(bytes(reply, "utf-8")) nicholas@2510: file = open("../saves/save-"+key+".xml",'w') nicholas@2510: file.write("") nicholas@2510: file.close() nicholas@2510: b@2264: b@2264: def saveFile(self): b@2264: global curFileName b@2264: global curSaveIndex b@2264: options = self.path.rsplit('?') b@2264: options = options[1].rsplit('=') b@2264: key = options[1] b@2264: varLen = int(self.headers['Content-Length']) b@2264: postVars = self.rfile.read(varLen) b@2264: print("Saving file key "+key) nicholas@2376: file = open('../saves/save-'+key+'.xml','wb') b@2264: file.write(postVars) b@2264: file.close() b@2264: try: b@2264: wbytes = os.path.getsize('../saves/save-'+key+'.xml') b@2264: except OSError: b@2264: self.send_response(200) b@2264: self.send_header("Content-type", "text/xml") b@2264: self.end_headers() b@2264: self.wfile.write('Could not open file') b@2264: self.send_response(200) b@2264: self.send_header("Content-type", "text/xml") b@2264: self.end_headers() nicholas@2376: reply = 'OK"saves/'+curFileName+'"' nicholas@2382: if sys.version_info[0] == 2: nicholas@2382: self.wfile.write(reply) nicholas@2382: elif sys.version_info[0] == 3: nicholas@2382: self.wfile.write(bytes(reply, "utf-8")) b@2264: curSaveIndex += 1 b@2264: curFileName = 'test-'+str(curSaveIndex)+'.xml' b@2264: nicholas@2430: def poolXML(s): nicholas@2430: pool = ET.parse('../tests/pool.xml') nicholas@2430: root = pool.getroot() nicholas@2430: setupNode = root.find("setup"); nicholas@2430: poolSize = setupNode.get("poolSize",0); nicholas@2430: if (poolSize == 0): nicholas@2430: s.path = s.path.split("/php",1)[0]+"/tests/pool/xml" nicholas@2430: processFile(s) nicholas@2430: return nicholas@2431: poolSize = int(poolSize) nicholas@2430: # Set up the store will all the test page key nodes nicholas@2430: pages = {}; nicholas@2430: for page in root.iter("page"): nicholas@2430: id = page.get("id") nicholas@2430: pages[id] = 0 nicholas@2430: # Read the saves and determine the completed pages nicholas@2430: for filename in os.listdir("../saves/"): nicholas@2430: if filename.endswith(".xml"): nicholas@2430: save = ET.parse("../saves/"+filename) nicholas@2430: save_root = save.getroot(); nicholas@2431: if (save_root.find("waet").get("url") == "http://localhost:8000/php/pool.php"): nicholas@2431: for page in save_root.findall("./page"): nicholas@2431: id = page.get("ref") nicholas@2430: pages[id] = pages[id] + 1 nicholas@2430: nicholas@2430: # Sort the dictionary nicholas@2431: rot_pages = {} nicholas@2431: for key, value in pages.items(): nicholas@2431: if (value in rot_pages): nicholas@2431: rot_pages[value].append(key) nicholas@2431: else: nicholas@2431: rot_pages[value] = [key] nicholas@2431: nicholas@2431: Keys = list(rot_pages) nicholas@2431: print ("Current pool state:") nicholas@2431: print (rot_pages) nicholas@2431: nicholas@2431: return_node = ET.fromstring(''); nicholas@2431: return_node.append(copy.deepcopy(root.find("setup"))) nicholas@2431: page_elements = root.findall("page") nicholas@2430: nicholas@2431: # Now append the pages nicholas@2431: i = 0 nicholas@2431: while(len(return_node.findall("page")) < poolSize): nicholas@2431: if (i > 0): nicholas@2431: for page in return_node.iter("page"): nicholas@2431: page.set("alwaysInclude","true") nicholas@2431: for id in rot_pages[Keys[i]]: nicholas@2431: return_node.append(copy.deepcopy(root.find('./page[@id="'+id+'"]'))) nicholas@2431: i=i+1 nicholas@2431: s.send_response(200) nicholas@2431: s.send_header("Content-type", "text/xml") nicholas@2431: s.end_headers() nicholas@2431: s.wfile.write(ET.tostring(return_node)) nicholas@2431: b@2264: def http_do_HEAD(s): b@2264: s.send_response(200) b@2264: s.send_header("Content-type", "text/html") b@2264: s.end_headers() b@2264: b@2264: def http_do_GET(request): nicholas@2450: global pseudo_index b@2264: if(request.client_address[0] == "127.0.0.1"): b@2264: if (request.path == "/favicon.ico"): b@2264: send404(request) nicholas@2510: elif (request.path.split('?',1)[0] == "/php/requestKey.php"): nicholas@2510: requestKey(request); nicholas@2430: elif (request.path.split('?',1)[0] == "/php/pool.php"): nicholas@2430: poolXML(request); b@2264: else: b@2264: request.path = request.path.split('?',1)[0] b@2264: if (request.path == '/'): b@2264: request.path = '/index.html' b@2264: elif (request.path == '/pseudo.xml'): nicholas@2450: request.path = PSEUDO_PATH + pseudo_files[pseudo_index] b@2264: print(request.path) b@2264: pseudo_index += 1 b@2264: pseudo_index %= len(pseudo_files) b@2264: processFile(request) b@2264: else: b@2264: send404(request) b@2264: b@2264: def http_do_POST(request): b@2264: if(request.client_address[0] == "127.0.0.1"): b@2264: if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/php/save.php"): b@2264: saveFile(request) b@2264: else: b@2264: send404(request) b@2264: b@2264: if sys.version_info[0] == 2: b@2264: class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): b@2264: def do_HEAD(s): b@2264: http_do_HEAD(s) b@2264: def do_GET(request): b@2264: http_do_GET(request) b@2264: def do_POST(request): b@2264: http_do_POST(request) b@2264: def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler): b@2264: server_address = ('', 8000) b@2264: httpd = server_class(server_address, handler_class) b@2264: httpd.serve_forever() b@2264: run() b@2264: elif sys.version_info[0] == 3: b@2264: class MyHandler(BaseHTTPRequestHandler): b@2264: def do_HEAD(s): b@2264: send404(s) b@2264: def do_GET(request): b@2264: http_do_GET(request) b@2264: def do_POST(request): b@2264: http_do_POST(request) b@2264: def run(server_class=HTTPServer,handler_class=MyHandler): b@2264: server_address = ('', 8000) b@2264: httpd = server_class(server_address, handler_class) b@2264: httpd.serve_forever() b@2264: run()