#!/bin/usr/env python # -*- coding: utf-8 -*- import os, base64, re import xml.dom.minidom, urllib import sql, adminsite, config from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler from SimpleHTTPServer import SimpleHTTPRequestHandler os.chdir("site") db = sql.create_fake_data() class MyRequestHandler(BaseHTTPRequestHandler): visiteur = 0 def test_password(self): if self.headers.get('Authorization') != "Basic "+base64.b64encode(config.login+":"+config.passwd): self.send_response(401) self.send_header("WWW-Authenticate: Basic realm", '"apache cfg"') self.end_headers() self.wfile.write("""

401 Unauthorized

""") return False return True def get_file(self): if os.access("."+self.path, os.F_OK) == False: self.send_response(404) self.send_header("Content-Type", "text/html") self.end_headers() if os.path.isfile("404.html"): f = open("404.html",'rb') t = f.read() f.close() self.wfile.write(t) else: self.wfile.write("""

404 Not found

""") return if os.path.isfile("."+self.path) == False: self.send_response(401) self.send_header("Content-Type", "text/html") self.end_headers() self.wfile.write("""

401 Unauthorized

""") return #Etag st = os.stat("."+self.path) etag = str(st[9])[5:]+"-"+str(st[8])[5:] #print str(self.headers.get("If-None-Match")) + " : "+etag if self.headers.get("If-None-Match") == etag: self.send_response(304) self.send_header("Etag", etag) self.end_headers() return #Send file self.send_response(200) self.send_header("Etag", etag) #print re.match("^.*(\.[a-zA-Z]{2,4})$", self.path).group(1) ext = re.match("^.*(\.[a-zA-Z]{2,4})$", self.path).group(1) ct = "text/plain; charset=utf8"; if ext == '.jpg': ct = "image/jpeg" if ext == '.png': ct = "image/png" if ext == '.gif': ct = "image/gif" if ext == '.html': ct = "text/html" if ext == '.js': ct = "text/javascript" if ext == '.css': ct = "text/css" if ext == '.xul': ct = "application/vnd.mozilla.xul+xml" if ext == '.xbl': ct = "text/xml" if ext == '.xml': ct = "text/xml" self.send_header("Content-Type", ct) self.end_headers() fd = open("."+self.path,'rb') t = fd.read() fd.close() self.wfile.write(t) return def dyn_a(self): self.send_response(200) self.send_header("Content-Type", "text/html") self.end_headers() MyRequestHandler.visiteur += 1 self.wfile.write("""

Hello world !

Vous avez demander la page %s. Votre IP est %s. Vous etes le visiteur %d : %s""" % (self.path, self.client_address, MyRequestHandler.visiteur, self.headers)) return; def cut_arg(self, args): r = [] for t in args.split("&"): r.append( list( urllib.unquote(t).split("=",1) ) ) return dict(r) def data(self): args = self.cut_arg(self.path[6:]) if 'a' in args: if (args['a'] == "adddomain") and ('iduser' in args) and ('domain' in args): return self.adddomain(int(args['iduser']),args['domain']); if (args['a'] == "getdomains") and ('iduser' in args) : return self.getdomains(int(args['iduser'])) if (args['a'] == "deldomain") and ('iddomain' in args) : return self.deldomain(int(args['iddomain'])) if (args['a'] == "addsite") and ('iddomain' in args) and ('site' in args) : return self.addsite(int(args['iddomain']), args['site'] ) if (args['a'] == "getsite") and ('idsite' in args) : return self.getsite(int(args['idsite'])) if (args['a'] == "updsite") and ('idsite' in args) and ('json' in args): return self.updsite(int(args['idsite']), args['json']) if (args['a'] == "delsite") and ('idsite' in args) : return self.delsite(int(args['idsite'])) if (args['a'] == "getvhost") and ('iddomain' in args) : return self.getvhost(int(args['iddomain'])) print args['a']; self.send_response(404) self.send_header("Content-Type", "text/html") self.end_headers() if os.path.isfile("404.html"): open("404.html",'rb') t = f.read() f.close() self.wfile.write(t) else: self.wfile.write("""

404 Not found

""") return def adddomain(self, id_user, domain): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() adminsite.adminsite(db).add_domain(id_user, domain) self.wfile.write( 'Ok' ) return def getdomains(self, id_user): self.send_response(200) self.send_header("Content-Type", "text/xml") self.end_headers() ad = adminsite.adminsite(db); self.wfile.write( ad.get_rdf_sites_of_user_id(id_user) ) return def deldomain(self, id_domain): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() adminsite.adminsite(db).del_domain(id_domain) self.wfile.write( 'Ok' ) return def addsite(self,id_domain,site): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() ad = adminsite.adminsite(db) ad.add_site_by_id_domain(id_domain,site) self.wfile.write("Ok") return def getsite(self,id_site): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() ad = adminsite.adminsite(db); self.wfile.write( ad.get_json_of_site_by_id(id_site) ) return def updsite(self,id_site, json): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() ad = adminsite.adminsite(db) ad.upd_site_by_json(id_site, json) self.wfile.write("Ok") return; def delsite(self, id_site): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() ad = adminsite.adminsite(db) ad.del_site_by_id(id_site) self.wfile.write("Ok") return; def getvhost(self, id_domain): self.send_response(200) self.send_header("Content-Type", "text/plain") self.end_headers() ad = adminsite.adminsite(db) self.wfile.write(ad.get_vhost(id_domain)) return; def do_GET(self): if self.test_password() == False: return if self.path[0:6] == "/data?": return self.data() self.get_file() return #httpd = HTTPServer(('',8080), SimpleHTTPRequestHandler) print "Ready" httpd = HTTPServer(('',8050), MyRequestHandler) httpd.serve_forever()