Break get request handling into parts

This makes it easier to format the requested path before replying to the
requester.
This commit is contained in:
Bryan Schumaker 2010-11-17 19:21:05 -05:00
parent ce68907b9a
commit f01fbecee3
1 changed files with 49 additions and 17 deletions

View File

@ -20,24 +20,58 @@ types = {".html":"text/html", ".js":"text/javascript",
".py":"text/html",
".jpg":"image/jpeg"}
def pyfile(wfile, kw, path, base):
print kw
file = basename(base)
mod = imp.load_source(file, path)
mod.to_html(wfile, kw)
def format_text(text):
text = text.replace("%20", " ")
return text
def format_path(path):
if path == "" or path == "/":
path = html + "/index.html"
return path, None
split = path.split("?", 1)
if len(split) == 1:
return html + path, None
path = split[0]
args = split[1].split("&")
kwargs = {}
for arg in args:
split = arg.split("=", 1)
kwargs[split[0]] = format_text(split[1])
return html + path, kwargs
class HTTPRequest(BaseHTTPRequestHandler):
def do_GET(self):
global html
if self.path == "" or self.path == "/":
self.path = "/index.html"
try:
split = self.path.strip(sep).split(sep)
#print split
if split[0] == "artwork":
split = split[1].split(".")
artist, album = split[0], split[1]
artist = artist.replace("%20", " ")
album = album.replace("%20", " ")
# print "%s, %s" % (artist, album)
fpath = lastfm.get_artwork_tags(artist, album)
else:
fpath = html + self.path
fpath, args = format_path(self.path)
#global html
#if self.path == "" or self.path == "/":
# self.path = "/index.html"
#try:
# split = self.path.split("?", 1)
# self.path = split[0]
# args = ""
# if len(split) > 1:
# args = split[1]
# args = args.split("&")
# split = self.path.strip(sep).split(sep)
# #print split
# if split[0] == "artwork":
# split = split[1].split(".")
# artist, album = split[0], split[1]
# artist = artist.replace("%20", " ")
# album = album.replace("%20", " ")
# # print "%s, %s" % (artist, album)
# fpath = lastfm.get_artwork_tags(artist, album)
# else:
# fpath = html + self.path
#print fpath
if not exists(fpath):
raise IOError
@ -46,9 +80,7 @@ class HTTPRequest(BaseHTTPRequestHandler):
self.send_header('Content-type', '%s' % types[ext])
self.end_headers()
if ext == ".py":
file = basename(base)
mod = imp.load_source(file, fpath)
mod.to_html(self.wfile)
pyfile(self.wfile, args, fpath, base)
else:
f = open(fpath)
self.wfile.write(f.read())