28
28
# python_version 2.7 works and can be deployed with Google App Engine Launcher 1.7.6
29
29
30
30
localport = '8080' # normally 8080
31
- website = 'glowscript' # normally glowscript
32
-
33
- weblocs = ["www." + website + ".org" , website + ".org" , "localhost:" + localport ,"127.0.0.1:" + localport ,
34
- "glowscriptdev.spvi.net" ,"uc.r.appspot.com" ,"www.devbasherwo.org" ,"devbasherwo.org" ]
35
-
36
- local_hosts = ['http://localhost' ,'http://127.0.0.1' ]
31
+ weblocs_safe = ["localhost:" + localport , "127.0.0.1:" + localport ,] # only need these for local development
37
32
38
33
import json
39
34
from io import BytesIO
44
39
import flask
45
40
import traceback
46
41
import functools
42
+ import urllib .parse
47
43
48
44
from google .cloud import ndb
49
45
from google .auth .transport import requests
@@ -67,10 +63,12 @@ def chrange(b,e): return set(chr(x) for x in range(ord(b), ord(e)+1))
67
63
68
64
def ndb_wsgi_middleware (wsgi_app ):
69
65
"""
70
- This is helpful for Flask and DNB to play nice together.
66
+ This is helpful for Flask and NDB to play nice together.
71
67
72
68
https://cloud.google.com/appengine/docs/standard/python3/migrating-to-cloud-ndb
73
-
69
+
70
+ We need to be able to access NDB in the application context.
71
+ If we're running a local datastore, make up a dummy project name.
74
72
"""
75
73
76
74
project = emulator and 'glowscript-dev' or None
@@ -93,10 +91,30 @@ def middleware(environ, start_response):
93
91
94
92
return middleware
95
93
94
+ #
95
+ # Now let's deal with the app
96
+ #
97
+
96
98
from . import app , auth
97
99
98
100
app .wsgi_app = ndb_wsgi_middleware (app .wsgi_app ) # Wrap the app in middleware.
99
101
102
+ #
103
+ # set up logging
104
+ #
105
+
106
+ import logging
107
+ import google .cloud .logging # Don't conflict with standard logging
108
+ from google .cloud .logging .handlers import CloudLoggingHandler
109
+
110
+ client = google .cloud .logging .Client ()
111
+ handler = CloudLoggingHandler (client )
112
+ handler .setLevel (logging .INFO )
113
+ app .logger .addHandler (handler )
114
+
115
+ def get_weblocs ():
116
+ return auth .getSetting ('weblocs' , weblocs_safe )
117
+
100
118
#
101
119
# These next few routes are to serve static files in dev mode. GAE will handle these
102
120
# in production
@@ -161,10 +179,9 @@ def idejs_static():
161
179
ide_js = load_idejs ()
162
180
163
181
host_name = get_auth_host_name ()
164
- if host_name .endswith ('uc.r.appspot.com' ): # no sandbox for uc.r.appspot.com test instances
165
- # since we can't authenticate these instances anyway, no sanbox is needed
166
- ide_js = ide_js .replace ('WEBSERVER_NAME_TEMPLATE' ,host_name )
167
- ide_js = ide_js .replace ('SANDBOX_PREFIX_TEMPLATE' ,'https://' )
182
+ if auth .check_auth_host_for_preview (host_name ): # are we running in a preview?
183
+ ide_js = ide_js .replace ('WEBSERVER_NAME_TEMPLATE' ,host_name )
184
+ ide_js = ide_js .replace ('SANDBOX_PREFIX_TEMPLATE' ,'https://' ) # no sandbox
168
185
elif host_name .startswith ('www.' ):
169
186
ide_js = ide_js .replace ('WEBSERVER_NAME_TEMPLATE' ,host_name [4 :])
170
187
ide_js = ide_js .replace ('SANDBOX_PREFIX_TEMPLATE' ,'https://sandbox.' )
@@ -196,14 +213,10 @@ def favicon_static():
196
213
@app .route ('/untrusted/<path:filename>' )
197
214
@no_cache
198
215
def untrusted_static (filename ):
199
- app .logger .info ("serving untrusted" )
200
216
if filename == 'run.js' :
201
217
run_js = module_cache .get ('run.js' )
202
218
if not run_js :
203
- app .logger .info ("not found in cache" )
204
219
run_js = load_runjs ()
205
- else :
206
- app .logger .info ("found in cache" )
207
220
208
221
host_name = get_auth_host_name ()
209
222
if host_name .startswith ('sandbox.' ):
@@ -239,21 +252,22 @@ def get_auth_host_name():
239
252
240
253
def trim_auth_host_name ():
241
254
#
242
- # if the host name has more than four parts, trim off the first part .
243
- # this allows appspot version to be tested that are not in production
244
- # e.g., 20201227t175543-dot-py38- glowscript.uc.r .appspot.com
255
+ # if the host name has more than two parts, return the last two parts only .
256
+ # This allows appspot version to be tested that are not in production
257
+ # e.g., 20201227t175543-dot-glowscript.appspot.com
245
258
#
259
+
246
260
host_header = get_auth_host_name ()
247
261
parts = host_header .split ('.' )
248
- host_header = '.' .join (parts [1 :]) if len (parts )> 4 else host_header
262
+ host_header = '.' .join (parts [- 2 :]) if len (parts )> 2 else host_header
249
263
return host_header
250
264
251
265
def authorize_host ():
252
266
host_header = trim_auth_host_name ()
253
- result = host_header in weblocs
267
+ result = host_header in get_weblocs ()
254
268
255
269
if not result :
256
- print ("Host failed to authorize:" , host_header )
270
+ app . logger . info ("Host failed to authorize:" + host_header + ":" + str ( get_weblocs ()) )
257
271
258
272
return result
259
273
@@ -328,7 +342,13 @@ def parseUrlPath(theRegexp, numGroups):
328
342
for i in range (numGroups ):
329
343
value = m .group (i + 1 )
330
344
if value :
331
- names [i ] = value
345
+ newValue = []
346
+ for char in value :
347
+ if char == '%' or char in unreserved :
348
+ newValue .append (char )
349
+ else :
350
+ newValue .append (urllib .parse .quote (char ))
351
+ names [i ] = '' .join (newValue )
332
352
except :
333
353
raise ParseUrlPathException ('Parsing URL failed' , 400 )
334
354
0 commit comments