@@ -538,6 +538,7 @@ def view(result_path, port, verbose):
538538 import tempfile
539539 import threading
540540 import http .server
541+ import contextlib
541542
542543 from qiime2 .sdk import Result
543544
@@ -559,40 +560,40 @@ def view(result_path, port, verbose):
559560 class Handler (http .server .SimpleHTTPRequestHandler ):
560561 def do_GET (self ):
561562 # Redirect the output from these requests to devnull if not verbose
562- if not verbose :
563- sys .stderr = open (os .devnull , 'w' )
564-
565- # Determine if this is a request for the file we are supposed to be
566- # viewing
567- if self .path == result_path :
568- if not os .path .exists (self .path ):
569- self .send_error (404 )
563+ with contextlib .redirect_stderr (
564+ sys .stderr if verbose else open (os .devnull , 'w' )):
565+ # Determine if this is a request for the file we are supposed
566+ # to be viewing
567+ if self .path == result_path :
568+ if not os .path .exists (self .path ):
569+ self .send_error (404 )
570+ else :
571+ self .send_response (200 )
572+ with open (self .path , 'rb' ) as file :
573+ self .wfile .write (file .read ())
574+ # Determine if this is a request for a file within the
575+ # visualization
576+ elif self .path .startswith (f'/_/{ session } /{ result .uuid } /' ):
577+ file_path = self .path .split (str (result .uuid ))[1 ]
578+ file_path = extracted_path + file_path
579+ file_path = os .path .abspath (file_path )
580+
581+ if not os .path .exists (file_path ) or \
582+ not file_path .startswith (extracted_path ):
583+ self .send_error (404 )
584+ else :
585+ self .send_response (200 )
586+ self .send_header ('Access-Control-Allow-Origin' , '*' )
587+ self .end_headers ()
588+
589+ with open (file_path , 'rb' ) as file :
590+ self .wfile .write (file .read ())
591+ # Otherwise default to super class. This will respond
592+ # appropriately to requests for assets that are part of the
593+ # vendored view app and will reject any requests for files
594+ # outside the served directory
570595 else :
571- self .send_response (200 )
572- with open (self .path , 'rb' ) as file :
573- self .wfile .write (file .read ())
574- # Determine if this is a request for a file within the
575- # visualization
576- elif self .path .startswith (f'/_/{ session } /{ result .uuid } /' ):
577- file_path = self .path .split (str (result .uuid ))[1 ]
578- file_path = extracted_path + file_path
579- file_path = os .path .abspath (file_path )
580-
581- if not os .path .exists (file_path ) or \
582- not file_path .startswith (extracted_path ):
583- self .send_error (404 )
584- else :
585- self .send_response (200 )
586- self .send_header ('Access-Control-Allow-Origin' , '*' )
587- self .end_headers ()
588-
589- with open (file_path , 'rb' ) as file :
590- self .wfile .write (file .read ())
591- # Otherwise default to super class. This will respond appropriately
592- # to requests for assets that are part of the vendored view app and
593- # will reject any requests for files outside the served directory
594- else :
595- super ().do_GET ()
596+ super ().do_GET ()
596597
597598 VENDOR_PATH = 'q2cli/assets/view/'
598599
0 commit comments