Mercurial > web-gnusto
changeset 78:de12cd84cf0a
Added a cgi-bin file that downloads zcode files from the IF archive.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 21 May 2008 10:45:54 -0700 |
parents | 19c686bef710 |
children | 9d735fe565d0 |
files | .hgignore cgi-bin/xhr_proxy.py server.py |
diffstat | 3 files changed, 49 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Wed May 21 09:41:05 2008 -0700 +++ b/.hgignore Wed May 21 10:45:54 2008 -0700 @@ -2,3 +2,4 @@ .sconsign.dblite *.orig *.min.* +if-archive
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cgi-bin/xhr_proxy.py Wed May 21 10:45:54 2008 -0700 @@ -0,0 +1,44 @@ +#! /usr/bin/env python + +import re +import os +import cgi +import cgitb +import urllib2 +import distutils.dir_util + +cgitb.enable() + +form = cgi.FieldStorage() +if form.has_key("file"): + path = form["file"].value +else: + path = "" + +ROOT_DIR = os.path.abspath("if-archive") +ZCODE_REGEXP = r".*\.z[1-8]$" + +localpath = os.path.normpath(os.path.join(ROOT_DIR, path)) + +result = None + +if not re.match(ZCODE_REGEXP, path): + result = "ERROR: File does not appear to be a zcode file." +if not localpath.startswith(ROOT_DIR): + result = "ERROR: Security violation: can't retrieve file below root dir." +elif os.path.exists(localpath): + result = "SUCCESS: Path exists." +else: + try: + fileobj = urllib2.urlopen("http://www.ifarchive.org/if-archive/%s" % path) + except urllib2.HTTPError, e: + result = "ERROR: HTTP Response %s" % e + if not result: + contents = fileobj.read() + distutils.dir_util.mkpath(os.path.dirname(localpath)) + open(localpath, "wb").write(contents) + result = "SUCCESS: File retrieved." + +print "Content-Type: text/plain" +print +print result
--- a/server.py Wed May 21 09:41:05 2008 -0700 +++ b/server.py Wed May 21 10:45:54 2008 -0700 @@ -1,11 +1,11 @@ -import SimpleHTTPServer -import SocketServer +import CGIHTTPServer +import BaseHTTPServer PORT = 8000 -Handler = SimpleHTTPServer.SimpleHTTPRequestHandler +Handler = CGIHTTPServer.CGIHTTPRequestHandler -httpd = SocketServer.TCPServer(("", PORT), Handler) +httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler) print "Serving files at http://localhost:%d" % PORT