diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index d2b03da0..5029232f 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -19,7 +19,6 @@ class ReceiveModeWeb: self.web = web self.can_upload = True - self.upload_count = 0 self.uploads_in_progress = [] self.define_routes() @@ -52,7 +51,7 @@ class ReceiveModeWeb: # Tell the GUI the receive mode directory for this file self.web.add_request(self.web.REQUEST_UPLOAD_SET_DIR, request.path, { - 'id': request.upload_id, + 'id': request.history_id, 'filename': basename, 'dir': request.receive_mode_dir }) @@ -272,10 +271,9 @@ class ReceiveModeRequest(Request): # Prevent new uploads if we've said so (timer expired) if self.web.receive_mode.can_upload: - # Create an upload_id, attach it to the request - self.upload_id = self.web.receive_mode.upload_count - - self.web.receive_mode.upload_count += 1 + # Create an history_id, attach it to the request + self.history_id = self.web.receive_mode.cur_history_id + self.web.receive_mode.cur_history_id += 1 # Figure out the content length try: @@ -302,10 +300,10 @@ class ReceiveModeRequest(Request): if not self.told_gui_about_request: # Tell the GUI about the request self.web.add_request(self.web.REQUEST_STARTED, self.path, { - 'id': self.upload_id, + 'id': self.history_id, 'content_length': self.content_length }) - self.web.receive_mode.uploads_in_progress.append(self.upload_id) + self.web.receive_mode.uploads_in_progress.append(self.history_id) self.told_gui_about_request = True @@ -337,19 +335,19 @@ class ReceiveModeRequest(Request): try: if self.told_gui_about_request: - upload_id = self.upload_id + history_id = self.history_id if not self.web.stop_q.empty() or not self.progress[self.filename]['complete']: # Inform the GUI that the upload has canceled self.web.add_request(self.web.REQUEST_UPLOAD_CANCELED, self.path, { - 'id': upload_id + 'id': history_id }) else: # Inform the GUI that the upload has finished self.web.add_request(self.web.REQUEST_UPLOAD_FINISHED, self.path, { - 'id': upload_id + 'id': history_id }) - self.web.receive_mode.uploads_in_progress.remove(upload_id) + self.web.receive_mode.uploads_in_progress.remove(history_id) except AttributeError: pass @@ -375,7 +373,7 @@ class ReceiveModeRequest(Request): # Update the GUI on the upload progress if self.told_gui_about_request: self.web.add_request(self.web.REQUEST_PROGRESS, self.path, { - 'id': self.upload_id, + 'id': self.history_id, 'progress': self.progress }) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index eb6525d1..3a01cb8f 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -44,8 +44,7 @@ class SendBaseModeWeb: self.files = {} # Dictionary mapping file paths to filenames on disk self.root_files = {} # This is only the root files and dirs, as opposed to all of them self.cleanup_filenames = [] - self.visit_count = 0 - self.download_count = 0 + self.cur_history_id = 0 self.file_info = {'files': [], 'dirs': []} self.gzip_individual_files = {} self.init() @@ -80,12 +79,12 @@ class SendBaseModeWeb: def directory_listing(self, filenames, path='', filesystem_path=None): # Tell the GUI about the directory listing - download_id = self.download_count - self.download_count += 1 + history_id = self.cur_history_id + self.cur_history_id += 1 self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, '/{}'.format(path), { - 'id': download_id, + 'id': history_id, 'method': request.method, - 'directory_listing': True + 'status_code': 200 }) # If filesystem_path is None, this is the root directory listing @@ -144,10 +143,10 @@ class SendBaseModeWeb: path = request.path # Tell GUI the individual file started - download_id = self.download_count - self.download_count += 1 + history_id = self.cur_history_id + self.cur_history_id += 1 self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, path, { - 'id': download_id, + 'id': history_id, 'filesize': filesize, 'method': request.method }) @@ -178,7 +177,7 @@ class SendBaseModeWeb: sys.stdout.flush() self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_PROGRESS, path, { - 'id': download_id, + 'id': history_id, 'bytes': downloaded_bytes }) done = False @@ -188,7 +187,7 @@ class SendBaseModeWeb: # Tell the GUI the individual file was canceled self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_CANCELED, path, { - 'id': download_id + 'id': history_id }) fp.close() diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 60620e2a..c9d9b229 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -60,10 +60,6 @@ class ShareModeWeb(SendBaseModeWeb): static_url_path=self.web.static_url_path)) return self.web.add_security_headers(r) - # Each download has a unique id - download_id = self.download_count - self.download_count += 1 - # Prepare some variables to use inside generate() function below # which is outside of the request context shutdown_func = request.environ.get('werkzeug.server.shutdown') @@ -81,8 +77,10 @@ class ShareModeWeb(SendBaseModeWeb): self.filesize = self.download_filesize # Tell GUI the download started + history_id = self.cur_history_id + self.cur_history_id += 1 self.web.add_request(self.web.REQUEST_STARTED, path, { - 'id': download_id, + 'id': history_id, 'use_gzip': use_gzip }) @@ -102,7 +100,7 @@ class ShareModeWeb(SendBaseModeWeb): # The user has canceled the download, so stop serving the file if not self.web.stop_q.empty(): self.web.add_request(self.web.REQUEST_CANCELED, path, { - 'id': download_id + 'id': history_id }) break @@ -124,7 +122,7 @@ class ShareModeWeb(SendBaseModeWeb): sys.stdout.flush() self.web.add_request(self.web.REQUEST_PROGRESS, path, { - 'id': download_id, + 'id': history_id, 'bytes': downloaded_bytes }) self.web.done = False @@ -135,7 +133,7 @@ class ShareModeWeb(SendBaseModeWeb): # tell the GUI the download has canceled self.web.add_request(self.web.REQUEST_CANCELED, path, { - 'id': download_id + 'id': history_id }) fp.close() diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 5a96b324..c4d5385f 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -63,6 +63,9 @@ class Web: self.auth = HTTPBasicAuth() self.auth.error_handler(self.error401) + # This tracks the history id + self.cur_history_id = 0 + # Verbose mode? if self.common.verbose: self.verbose_mode() @@ -193,20 +196,52 @@ class Web: self.force_shutdown() print("Someone has made too many wrong attempts to guess your password, so OnionShare has stopped the server. Start sharing again and send the recipient a new address to share.") + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 401 + }) + r = make_response(render_template('401.html', static_url_path=self.static_url_path), 401) return self.add_security_headers(r) def error403(self): + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 403 + }) + self.add_request(Web.REQUEST_OTHER, request.path) r = make_response(render_template('403.html', static_url_path=self.static_url_path), 403) return self.add_security_headers(r) def error404(self): + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 404 + }) + self.add_request(Web.REQUEST_OTHER, request.path) r = make_response(render_template('404.html', static_url_path=self.static_url_path), 404) return self.add_security_headers(r) def error405(self): + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 405 + }) + r = make_response(render_template('405.html', static_url_path=self.static_url_path), 405) return self.add_security_headers(r) diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 28f2607d..55e5c1d4 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -28,17 +28,6 @@ class WebsiteModeWeb(SendBaseModeWeb): """ Render the onionshare website. """ - - # Each download has a unique id - visit_id = self.visit_count - self.visit_count += 1 - - # Tell GUI the page has been visited - self.web.add_request(self.web.REQUEST_STARTED, path, { - 'id': visit_id, - 'action': 'visit' - }) - return self.render_logic(path) def directory_listing_template(self, path, files, dirs): diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index ce783d46..cd8fe529 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -394,9 +394,9 @@ class IndividualFileHistoryItem(HistoryItem): self.progress_bar.hide() return - # Is this a directory listing? - if self.directory_listing: - self.status_code_label.setText("200") + # Is a status code already sent? + if 'status_code' in data: + self.status_code_label.setText("{}".format(data['status_code'])) self.status = HistoryItem.STATUS_FINISHED self.progress_bar.hide() return @@ -415,6 +415,7 @@ class IndividualFileHistoryItem(HistoryItem): self.progress_bar.setValue(downloaded_bytes) if downloaded_bytes == self.progress_bar.total_bytes: + self.status_code_label.setText("200") self.progress_bar.hide() self.status = HistoryItem.STATUS_FINISHED