-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipServer.py
72 lines (68 loc) · 2.72 KB
/
ZipServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
try:
from SimpleHTTPServer import SimpleHTTPRequestHandler as Handler
except ImportError:
from http.server import SimpleHTTPRequestHandler as Handler
import cgi
import logging
import urllib
import os
#This is a simple server that is used to download the zip files for the samples in ibmstreams github samples repository
#Samples are mirrored here in static/samples
#valid requests are of the form get=Category/Sample e.g get=ODM/GPXToTuple
#where Category is a folder within the samples directory
class DownloadZipHandler(Handler):
def redirect(self):
#redirect this request to the main samples page.
print "Ignoring request %s" % self.path
self.send_response(301)
self.send_header("Location", "https://ibmstreams.github.io/samples")
self.end_headers()
#return false if this request is invalid (no path, missing param, or path is invalid)
#return the requested path otherwise.
def is_valid_path(self):
valid = False
arg_index = self.path.find('?')
if arg_index >= 0:
args = cgi.parse_qs(self.path[arg_index+1:])
if ("get" in args):
path = args["get"][0]
print path
path=urllib.unquote(path)
print path
if os.path.exists("samples/" + path):
if "." in path or ".." in path or "~" in path or "*" in path:
print "invalid characters in path"
return False
else:
return path
else:
print path + "is not in samples dir. Current dir: " + os.getcwd()
return valid
def do_GET(self):
'''
Handle a GET request.
'''
# Parse out the arguments.
# The arguments follow a '?' in the URL. Here is an example:
# http://example.com?arg1=val1
print "got request " + self.path
return_value = self.is_valid_path()
if return_value is False:
self.redirect()
else:
path = return_value
path = path.strip('/')
folders = path.split("/")
zipName = "samples/" + path + "/" + folders[-1] +".zip"
baseName=folders[-1]+".zip"
if os.path.exists(zipName):
f = open(zipName,"rb")
self.send_response(200)
self.send_header('Content-type', 'application/zip')
self.send_header('Content-Disposition','attachment; filename="'+baseName+'"')
self.end_headers()
self.wfile.write(f.read())
f.close()
else:
print ("Ignoring request for " + path)
self.redirect()