import string,cgi,urlparse,os,urllib,sys,time
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
vncDict = {}
typeAppDict = {'html':'firefox','msword':'oowriter'}
global Server
Server = 0
def html_escape(s, replace=string.replace):
s = replace(s, "&", "&")
s = replace(s, "<", "<")
return replace(s, ">", ">",)
def getMimeType(url):
return urllib.urlopen(url).info().getsubtype()
def getFile(url):
return urllib.urlretrieve(url)[0]
def getPath(type,url):
if type == 'html':
return url
else:
return getFile(url)
def getAppCommand(type):
return typeAppDict.get(type)
class MyHandler(BaseHTTPRequestHandler):
url = None
id = None
def FORK():
return os.fork()
def REAP():
try:
a = -1
while a != 0:
a,b = os.waitpid(0,os.WNOHANG)
except: None
def EXIT():
sys.exit(0)._exit()
def Spawn(command, args):
REAP()
if 0 == FORK():
global Server
Server.socket.close()
self.wfile.close()
self.connection.close()
try:
os.execvp(command, args)
except:
print "exec failed:" + command + " " + str(args)
EXIT()
def startVNC(display):
Spawn( 'vncserver', ['vncserver','-geometry','800x600','-depth','24',':'+display] )
return
def startApp(app,path,display):
Spawn( 'vncserver', ['kstart','--fullscreen',app,'--display=:'+display,path] )
return
def getVNC(self):
dispNum = 0
if vncDict.has_key(self.id):
self.wfile.write( ""+str(vncDict.get(self.id))+"" )
self.wfile.flush()
self.wfile.close()
self.connection.close()
return ""+str(vncDict.get(self.id))+""
else:
displays = vncDict.values()
if displays == []:
dispNum = 1
else:
displays.sort()
dispNum = len(displays)+1
self.wfile.write( ""+str(dispNum)+"" )
self.wfile.flush()
self.wfile.close()
self.connection.close()
vncDict.update({self.id:dispNum})
print "startVNC("+str(dispNum)+")"
startVNC(str(dispNum), self.wfile)
type = getMimeType(self.url)
print "startApp("+getAppCommand(type)+","+getPath(type,self.url)+","+str(dispNum)+")"
startApp(getAppCommand(type),getPath(type,self.url),str(dispNum), self.wfile)
print ""+str(dispNum)+""
return ""+str(dispNum)+""
def handleRequest(self):
parsed = urlparse.urlparse(self.path)
query = parsed[4]
verb = parsed[2]
if verb == "/echo":
parsedQuery = cgi.parse_qs(query)
self.wfile.write( "" + html_escape(parsedQuery['what'][0]) + "\n" )
return
elif verb == "/shutdown":
try:
os.system("killall Xvnc 2>/dev/null ");
except:
None
vncDict = {}
self.wfile.write( "OK\n" )
return
elif verb == "/launch":
parsedQuery = cgi.parse_qs(query)
self.url = ""
self.id = ""
if parsedQuery.has_key('url'):
self.url = parsedQuery['url'][0]
else:
return "No url found"
if parsedQuery.has_key('id'):
self.id = parsedQuery['id'][0]
else:
return "No id found"
return self.getVNC()
return "Request not understood"
def do_GET(self):
try:
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.rfile.close()
answer = self.handleRequest()
print answer
#WFILE
#self.wfile.write(answer)
#self.wfile.close()
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def main():
global Server
Server = HTTPServer(('', 5899), MyHandler)
try:
print 'started httpserver...'
Server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
Server.socket.close()
if __name__ == '__main__':
main()