-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwsgi_server.py
303 lines (225 loc) · 10.3 KB
/
wsgi_server.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#pylint: disable-msg=F0401, W0142
from pyramid.config import Configurator
from pyramid.response import Response, FileResponse
from pyramid.exceptions import NotFound
from pyramid.httpexceptions import HTTPFound
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
import pyramid.security as pys
import cherrypy
import apiHandler
import mako.exceptions
from mako.lookup import TemplateLookup
from settings import settings
import os.path
users = {"herp" : "wattttttt"}
def userCheck(userid, dummy_request):
if userid in users:
return True
else:
return False
import logging
import sqlite3
import traceback
class PageResource(object):
log = logging.getLogger("Main.WebSrv")
def __init__(self):
self.base_directory = settings["webCtntPath"]
self.dbPath = settings["dbPath"]
self.lookupEngine = TemplateLookup(directories=[self.base_directory], module_directory='./ctntCache')
self.openDB()
self.apiInterface = apiHandler.ApiInterface(self.conn)
cherrypy.engine.subscribe("exit", self.closeDB)
def openDB(self):
self.log.info("Info Generator Opening DB...")
self.log.info("DB Path = %s", self.dbPath)
self.conn = sqlite3.connect(self.dbPath, check_same_thread=False)
self.log.info("DB opened")
self.log.info("DB opened. Activating 'wal' mode")
rets = self.conn.execute('''PRAGMA journal_mode=wal;''')
# rets = self.conn.execute('''PRAGMA locking_mode=EXCLUSIVE;''')
rets = rets.fetchall()
self.log.info("PRAGMA return value = %s", rets)
def closeDB(self):
self.log.info("Closing DB...",)
try:
self.conn.close()
except:
self.log.error("wat")
self.log.error(traceback.format_exc())
self.log.info("done")
def getResponse(self, reqPath):
with open(reqPath, "rb") as fp:
ret = Response(body=fp.read())
if reqPath.endswith(".js"):
ret.content_type = "text/javascript"
if reqPath.endswith(".css"):
ret.content_type = "text/css"
if reqPath.endswith(".ico"):
ret.content_type = "image/x-icon"
self.log.info("Request for URL %s, inferred MIME type %s", reqPath, ret.content_type)
return ret
def getPage(self, request):
redir = self.checkAuth(request)
if redir:
return redir
else:
return self.getPageNoAuth(request)
def getPageNoAuth(self, request):
self.log.info("Request from %s for path - %s!", request.remote_addr, request.path)
reqPath = request.path.lstrip("/")
if not reqPath:
reqPath = "index.mako"
# Check if there is a mako file at the path, and choose that preferentially over other files.
# Includes adding `.mako` to the path if needed.
makoPath = reqPath + ".mako"
absolute_path = os.path.join(self.base_directory, reqPath)
normalized_path = os.path.normpath(absolute_path)
mako_absolute_path = os.path.join(self.base_directory, makoPath)
mako_normalized_path = os.path.normpath(mako_absolute_path)
if os.path.exists(mako_normalized_path):
normalized_path = mako_normalized_path
reqPath = makoPath
try:
# Block attempts to access directories outside of the content dir
if not normalized_path.startswith(self.base_directory):
raise IOError()
# Conditionally parse and render mako files.
if reqPath.endswith(".mako"):
pgTemplate = self.lookupEngine.get_template(reqPath)
pageContent = pgTemplate.render_unicode(request=request, sqlCon=self.conn, api=self.apiInterface)
return Response(body=pageContent)
else:
absolute_path = os.path.join(self.base_directory, reqPath)
normalized_path = os.path.normpath(absolute_path)
return self.getResponse(normalized_path)
except mako.exceptions.TopLevelLookupException:
self.log.error("404 Request for page at url: %s", reqPath)
pgTemplate = self.lookupEngine.get_template("error.mako")
pageContent = pgTemplate.render_unicode(request=request, sqlCon=self.conn, tracebackStr=traceback.format_exc(), error_str="NO PAGE! 404")
return Response(body=pageContent)
except:
self.log.error("Page rendering error! url: %s", reqPath)
self.log.error(traceback.format_exc())
pgTemplate = self.lookupEngine.get_template("error.mako")
pageContent = pgTemplate.render_unicode(request=request, sqlCon=self.conn, tracebackStr=traceback.format_exc(), error_str="EXCEPTION! WAT?")
return Response(body=pageContent)
def checkAuth(self, request):
userid = pys.authenticated_userid(request)
if userid is None:
self.log.warn("No userID found for request to '%s'. Redirecting to login." % request.path)
return HTTPFound(location=request.route_url('login'))
def getImagePathFromID(self, imageID):
cur = self.conn.cursor()
ret = cur.execute("SELECT (downloadPath) FROM retrieved_pages WHERE id=?;", (imageID, ))
rets = ret.fetchone()
if not rets:
raise ValueError("How did an invalid key get queried for?")
pathS = rets[0]
if not settings["dldCtntPath"] in pathS:
pathS = os.path.join(settings["dldCtntPath"], pathS)
return pathS
def getImagePathOffsetName(self, offset, name):
cur = self.conn.cursor()
ret = cur.execute("SELECT count(downloadPath) FROM retrieved_pages WHERE artistName=?;", (name,))
maxItems = ret.fetchone()[0]
offset = int(offset)
while offset > maxItems: # Clamp to the valid range, modulo so it loops
offset -= maxItems
ret = cur.execute("SELECT (downloadPath) FROM retrieved_pages WHERE artistName=? LIMIT 1 OFFSET ?;", (name, offset))
rets = ret.fetchone()
if not rets:
raise ValueError("How did an invalid key get queried for?")
pathS = rets[0]
if not settings["dldCtntPath"] in pathS:
pathS = os.path.join(settings["dldCtntPath"], pathS)
return pathS
def getImageById(self, request):
redir = self.checkAuth(request)
if redir:
return redir
self.log.info("Request from %s for Image by ID - %s!", request.remote_addr, request.matchdict)
imagePath = self.getImagePathFromID(request.matchdict["imageID"])
return FileResponse(imagePath)
def getImageByArtistOffset(self, request):
redir = self.checkAuth(request)
if redir:
return redir
self.log.info("Request from %s for Image by artist/offset - %s!", request.remote_addr, request.matchdict)
imagePath = self.getImagePathOffsetName(request.matchdict["offset"], request.matchdict["artist"])
return FileResponse(imagePath)
def getSiteSource(self, request):
redir = self.checkAuth(request)
if redir:
return redir
siteName = request.matchdict["siteName"]
self.log.info("Request from %s for Site Source %s!", request.remote_addr, request.matchdict)
pgTemplate = self.lookupEngine.get_template("genSiteArtistList.mako")
pageContent = pgTemplate.render_unicode(request=request, sqlCon=self.conn, siteSource=siteName)
return Response(body=pageContent)
def getArtistSource(self, request):
redir = self.checkAuth(request)
if redir:
return redir
siteName = request.matchdict["siteName"]
sourceName = request.matchdict["sourceName"]
pageNumber = request.matchdict["pageNumber"]
self.log.info("Request from %s Artist page = %s, %s, %s", request.remote_addr, siteName, sourceName, pageNumber)
pgTemplate = self.lookupEngine.get_template("genArtistList.mako")
pageContent = pgTemplate.render_unicode(request=request, sqlCon=self.conn, siteSource=siteName, artist=sourceName, pageNumberStr=pageNumber)
return Response(body=pageContent)
def sign_in_out(self, request):
username = request.POST.get('username')
password = request.POST.get('password')
if username:
self.log.info("Login attempt: u = %s, pass = %s", username, password)
if username in users and users[username] == password:
self.log.info("Successful Login!")
age = 60*60*24*32
headers = pys.remember(request, username, max_age='%d' % age)
reqPath = request.path.lstrip("/")
reqPath = reqPath + ".mako"
pgTemplate = self.lookupEngine.get_template(reqPath)
pageContent = pgTemplate.render_unicode(request=request)
return Response(body=pageContent, headers=headers)
else:
self.log.warn("Invalid user. Deleting cookie.")
headers = pys.forget(request)
else:
self.log.warn("No user specified - Deleting cookie.")
headers = pys.forget(request)
return HTTPFound(location=request.route_url('login'))
def getApi(self, request):
return self.apiInterface.handleApiCall(request)
def buildApp():
resource = PageResource()
authn_policy = AuthTktAuthenticationPolicy('lolwattttt', hashalg='sha256', callback=userCheck)
authz_policy = ACLAuthorizationPolicy()
config = Configurator()
config.set_authentication_policy(authn_policy)
config.set_authorization_policy(authz_policy)
config.add_route(name='login', pattern='/login')
config.add_route(name='do_login', pattern='/login-check')
config.add_route(name='auth', pattern='/login')
config.add_route(name='root', pattern='/')
config.add_route(name='get-image-by-id', pattern='/images/byid/{imageID}')
config.add_route(name='get-image-by-offset', pattern='/images/byoffset/{artist}/{offset}')
config.add_route(name='get-site-source', pattern='/source/bysite/{siteName}')
config.add_route(name='get-artist-source', pattern='/source/byartist/{siteName}/{sourceName}/{pageNumber}')
config.add_route(name='api', pattern='/api')
config.add_route(name='leaf', pattern='/*page')
config.add_view(resource.getPageNoAuth, http_cache=0, route_name='login')
config.add_view(resource.sign_in_out, http_cache=0, route_name='do_login')
config.add_view(resource.getPage, http_cache=0, route_name='root')
config.add_view(resource.getImageById, http_cache=0, route_name='get-image-by-id')
config.add_view(resource.getImageByArtistOffset, http_cache=0, route_name='get-image-by-offset')
config.add_view(resource.getSiteSource, http_cache=0, route_name='get-site-source')
config.add_view(resource.getArtistSource, http_cache=0, route_name='get-artist-source')
config.add_view(resource.getPage, http_cache=0, route_name='leaf')
config.add_view(resource.getApi, http_cache=0, route_name='api')
config.add_view(resource.getPage, http_cache=0, context=NotFound)
config.add_view(route_name='auth', match_param='action=in', renderer='string', request_method='POST')
config.add_view(route_name='auth', match_param='action=out', renderer='string')
app = config.make_wsgi_app()
return app
app = buildApp()