-
Notifications
You must be signed in to change notification settings - Fork 6
/
rich_web.py
100 lines (83 loc) · 3.12 KB
/
rich_web.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# imports for tornado
import tornado
from tornado import web, httpserver, ioloop
# imports for logging
import traceback
import os
from os import path
# imports for rich
import richlibrary
Metadata = {
"Name" : "Rich Header",
"Version" : "1.0",
"Description" : "./README.md",
"Copyright" : "Copyright 2016 Holmes Group LLC",
"License" : "./LICENSE"
}
def RichHeaderRun(objpath):
parser = richlibrary.RichLibrary(objpath)
return parser.parse()
class Service(tornado.web.RequestHandler):
def get(self):
try:
filename = self.get_argument("obj", strip=False)
fullPath = os.path.join('/tmp/', filename)
data = RichHeaderRun(fullPath)
self.write(data)
except tornado.web.MissingArgumentError:
raise tornado.web.HTTPError(400)
except richlibrary.FileSizeError:
self.write({'error': richlibrary.err2str(-2)})
except richlibrary.MZSignatureError:
self.write({'error': richlibrary.err2str(-3)})
except richlibrary.MZPointerError:
self.write({'error': richlibrary.err2str(-4)})
except richlibrary.PESignatureError:
self.write({'error': richlibrary.err2str(-5)})
except richlibrary.RichSignatureError:
self.write({'error': richlibrary.err2str(-6)})
except richlibrary.DanSSignatureError:
self.write({'error': richlibrary.err2str(-7)})
except richlibrary.HeaderPaddingError:
self.write({'error': richlibrary.err2str(-8)})
except richlibrary.RichLengthError:
self.write({'error': richlibrary.err2str(-9)})
except Exception as e:
self.write({"error": traceback.format_exc(e)})
class Info(tornado.web.RequestHandler):
# Emits a string which describes the purpose of the analytics
def get(self):
info = """
<p>{name:s} - {version:s}</p>
<hr>
<p>{description:s}</p>
""".format(
name = str(Metadata["Name"]).replace("\n", "<br>"),
version = str(Metadata["Version"]).replace("\n", "<br>"),
description = str(Metadata["Description"]).replace("\n", "<br>"),
license = str(Metadata["License"]).replace("\n", "<br>")
)
self.write(info)
class Application(tornado.web.Application):
def __init__(self):
for key in ["Description", "License"]:
fpath = Metadata[key]
if os.path.isfile(fpath):
with open(fpath) as file:
Metadata[key] = file.read()
handlers = [
(r'/', Info),
(r'/analyze/', Service),
]
settings = dict(
template_path=path.join(path.dirname(__file__), 'templates'),
static_path=path.join(path.dirname(__file__), 'static'),
)
tornado.web.Application.__init__(self, handlers, **settings)
self.engine = None
def main():
server = tornado.httpserver.HTTPServer(Application())
server.listen(8080)
tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
main()