1717from ..session import register_session_implement , CoroutineBasedSession , ThreadBasedSession , Session
1818from ..utils import get_free_port , STATIC_PATH , parse_file_size
1919
20+ LOCAL_STATIC_URL = '/_pywebio_static'
2021
2122def filename_ok (f ):
2223 return not f .startswith (('.' , '_' ))
@@ -183,7 +184,7 @@ def get_app_from_path(request_path, base, index, reload=False):
183184 return 'error' , 404
184185
185186
186- def _path_deploy (base , port = 0 , host = '' , static_dir = None , cdn = True , max_payload_size = 2 ** 20 * 200 ,
187+ def _path_deploy (base , port = 0 , host = '' , static_dir = None , max_payload_size = 2 ** 20 * 200 ,
187188 ** tornado_app_settings ):
188189 if not host :
189190 host = '0.0.0.0'
@@ -195,19 +196,15 @@ def _path_deploy(base, port=0, host='', static_dir=None, cdn=True, max_payload_s
195196
196197 abs_base = os .path .normpath (os .path .abspath (base ))
197198
198- cdn = cdn_validation (cdn , 'warn' , stacklevel = 4 ) # if CDN is not available, warn user and disable CDN
199- cdn_url = '/_pywebio_static/' if not cdn else cdn
200-
201199 register_session_implement (CoroutineBasedSession )
202200 register_session_implement (ThreadBasedSession )
203201
204- RequestHandler = yield cdn_url , abs_base
202+ RequestHandler = yield abs_base
205203
206204 handlers = []
207205 if static_dir is not None :
208206 handlers .append ((r"/static/(.*)" , StaticFileHandler , {"path" : static_dir }))
209- if not cdn :
210- handlers .append ((r"/_pywebio_static/(.*)" , StaticFileHandler , {"path" : STATIC_PATH }))
207+ handlers .append ((LOCAL_STATIC_URL + r"/(.*)" , StaticFileHandler , {"path" : STATIC_PATH }))
211208 handlers .append ((r"/.*" , RequestHandler ))
212209
213210 print_listen_address (host , port )
@@ -254,20 +251,27 @@ def path_deploy(base, port=0, host='',
254251 tornado_app_settings .setdefault ('websocket_max_message_size' , max_payload_size ) # Backward compatible
255252 tornado_app_settings ['websocket_max_message_size' ] = parse_file_size (tornado_app_settings ['websocket_max_message_size' ])
256253 gen = _path_deploy (base , port = port , host = host ,
257- static_dir = static_dir ,
258- cdn = cdn , debug = debug ,
254+ static_dir = static_dir , debug = debug ,
259255 max_payload_size = max_payload_size ,
260256 ** tornado_app_settings )
261257
262- cdn_url , abs_base = next (gen )
258+ cdn = cdn_validation (cdn , 'warn' , stacklevel = 3 ) # if CDN is not available, warn user and disable CDN
259+
260+ abs_base = next (gen )
263261
264262 index_func = {True : partial (default_index_page , base = abs_base ), False : lambda p : '403 Forbidden' }.get (index , index )
265263
266- Handler = webio_handler (lambda : None , cdn_url , allowed_origins = allowed_origins ,
264+ Handler = webio_handler (lambda : None , cdn = cdn , allowed_origins = allowed_origins ,
267265 check_origin = check_origin , reconnect_timeout = reconnect_timeout )
268266
269267 class WSHandler (Handler ):
270268
269+ def get_cdn (self ):
270+ _cdn = super ().get_cdn ()
271+ if not _cdn :
272+ return LOCAL_STATIC_URL
273+ return _cdn
274+
271275 def get_app (self ):
272276 reload = self .get_query_argument ('reload' , None ) is not None
273277 type , res = get_app_from_path (self .request .path , abs_base , index = index_func , reload = reload )
@@ -307,12 +311,13 @@ def path_deploy_http(base, port=0, host='',
307311 page .MAX_PAYLOAD_SIZE = max_payload_size = parse_file_size (max_payload_size )
308312
309313 gen = _path_deploy (base , port = port , host = host ,
310- static_dir = static_dir ,
311- cdn = cdn , debug = debug ,
314+ static_dir = static_dir , debug = debug ,
312315 max_payload_size = max_payload_size ,
313316 ** tornado_app_settings )
314317
315- cdn_url , abs_base = next (gen )
318+ cdn = cdn_validation (cdn , 'warn' , stacklevel = 3 ) # if CDN is not available, warn user and disable CDN
319+
320+ abs_base = next (gen )
316321 index_func = {True : partial (default_index_page , base = abs_base ), False : lambda p : '403 Forbidden' }.get (index , index )
317322
318323 def get_app (context : TornadoHttpContext ):
@@ -327,11 +332,18 @@ def get_app(context: TornadoHttpContext):
327332 app_name = context .request_url_parameter ('app' , 'index' )
328333 return res .get (app_name ) or res ['index' ]
329334
330- handler = HttpHandler (app_loader = get_app , cdn = cdn_url ,
331- session_expire_seconds = session_expire_seconds ,
332- session_cleanup_interval = session_cleanup_interval ,
333- allowed_origins = allowed_origins ,
334- check_origin = check_origin )
335+ class HttpPathDeployHandler (HttpHandler ):
336+ def get_cdn (self , context ):
337+ _cdn = super ().get_cdn (context )
338+ if not _cdn :
339+ return LOCAL_STATIC_URL
340+ return _cdn
341+
342+ handler = HttpPathDeployHandler (app_loader = get_app , cdn = cdn ,
343+ session_expire_seconds = session_expire_seconds ,
344+ session_cleanup_interval = session_cleanup_interval ,
345+ allowed_origins = allowed_origins ,
346+ check_origin = check_origin )
335347
336348 class ReqHandler (tornado .web .RequestHandler ):
337349 def options (self ):
0 commit comments