From aeab9780d8e841be1683493097bee1eaaf262933 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 15 Apr 2015 19:49:40 -0600 Subject: [PATCH 01/11] Fixes initial-configs that were not restored when opening a project containing IOU VMs. --- gns3server/handlers/api/iou_handler.py | 4 ---- gns3server/modules/iou/iou_vm.py | 12 ++++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/gns3server/handlers/api/iou_handler.py b/gns3server/handlers/api/iou_handler.py index 98bd4cf7..acf3b5b1 100644 --- a/gns3server/handlers/api/iou_handler.py +++ b/gns3server/handlers/api/iou_handler.py @@ -59,8 +59,6 @@ class IOUHandler: for name, value in request.json.items(): if hasattr(vm, name) and getattr(vm, name) != value: setattr(vm, name, value) - if "initial_config_content" in request.json: - vm.initial_config = request.json.get("initial_config_content") response.set_status(201) response.json(vm) @@ -108,8 +106,6 @@ class IOUHandler: for name, value in request.json.items(): if hasattr(vm, name) and getattr(vm, name) != value: setattr(vm, name, value) - if "initial_config_content" in request.json: - vm.initial_config = request.json.get("initial_config_content") response.json(vm) @classmethod diff --git a/gns3server/modules/iou/iou_vm.py b/gns3server/modules/iou/iou_vm.py index 305250e0..5edf33a9 100644 --- a/gns3server/modules/iou/iou_vm.py +++ b/gns3server/modules/iou/iou_vm.py @@ -317,9 +317,9 @@ class IOUVM(BaseVM): """ if self.initial_config_file: - content = self.initial_config + content = self.initial_config_content content = content.replace(self._name, new_name) - self.initial_config = content + self.initial_config_content = content super(IOUVM, IOUVM).name.__set__(self, new_name) @@ -939,7 +939,7 @@ class IOUVM(BaseVM): log.warn("could not determine if layer 1 keepalive messages are supported by {}: {}".format(os.path.basename(self._path), e)) @property - def initial_config(self): + def initial_config_content(self): """ Returns the content of the current initial-config file. """ @@ -952,10 +952,10 @@ class IOUVM(BaseVM): with open(config_file) as f: return f.read() except OSError as e: - raise IOUError("Can't read configuration file '{}'".format(config_file)) + raise IOUError("Can't read configuration file '{}': {}".format(config_file, e)) - @initial_config.setter - def initial_config(self, initial_config): + @initial_config_content.setter + def initial_config_content(self, initial_config): """ Update the initial config From 4136c29b0ff875ae53ea03188d8136b63e4a6d36 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 15 Apr 2015 19:50:40 -0600 Subject: [PATCH 02/11] Config paths are not used when updating Dynamips or IOU VM settings. --- gns3server/schemas/dynamips_vm.py | 10 ---------- gns3server/schemas/iou.py | 4 ---- 2 files changed, 14 deletions(-) diff --git a/gns3server/schemas/dynamips_vm.py b/gns3server/schemas/dynamips_vm.py index 934646c8..8888f6a6 100644 --- a/gns3server/schemas/dynamips_vm.py +++ b/gns3server/schemas/dynamips_vm.py @@ -284,20 +284,10 @@ VM_UPDATE_SCHEMA = { "type": "string", "minLength": 1, }, - "startup_config": { - "description": "path to the IOS startup configuration file", - "type": "string", - "minLength": 1, - }, "startup_config_content": { "description": "Content of IOS startup configuration file", "type": "string", }, - "private_config": { - "description": "path to the IOS private configuration file", - "type": "string", - "minLength": 1, - }, "private_config_content": { "description": "Content of IOS private configuration file", "type": "string", diff --git a/gns3server/schemas/iou.py b/gns3server/schemas/iou.py index 743dfcfc..011607ba 100644 --- a/gns3server/schemas/iou.py +++ b/gns3server/schemas/iou.py @@ -127,10 +127,6 @@ IOU_UPDATE_SCHEMA = { "description": "Always up ethernet interface", "type": ["boolean", "null"] }, - "initial_config": { - "description": "Path to the initial configuration of IOU", - "type": ["string", "null"] - }, "initial_config_content": { "description": "Initial configuration of IOU", "type": ["string", "null"] From b5e01f756005475e3cc3fa05ec3153cd4e429ab2 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 16 Apr 2015 09:51:02 +0200 Subject: [PATCH 03/11] Fix IOU licence check flag --- gns3server/modules/iou/iou_vm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gns3server/modules/iou/iou_vm.py b/gns3server/modules/iou/iou_vm.py index 305250e0..d479c295 100644 --- a/gns3server/modules/iou/iou_vm.py +++ b/gns3server/modules/iou/iou_vm.py @@ -372,8 +372,8 @@ class IOUVM(BaseVM): Checks for a valid IOU key in the iourc file (paranoid mode). """ - license_check = self._manager.config.get_section_config("IOU").getboolean("license_check", False) - if license_check: + license_check = self._manager.config.get_section_config("IOU").getboolean("license_check", True) + if license_check is False: return config = configparser.ConfigParser() From 343e007809a1f1915407fb1d4135122b94027bde Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 16 Apr 2015 20:17:06 -0600 Subject: [PATCH 04/11] Ignore the "OSError: [WinError 0] The operation completed successfully" exception in Windows. --- gns3server/server.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gns3server/server.py b/gns3server/server.py index dca8ff29..0dd5d641 100644 --- a/gns3server/server.py +++ b/gns3server/server.py @@ -225,6 +225,11 @@ class Server: try: self._loop.run_forever() + except OSError as e: + # This is to ignore OSError: [WinError 0] The operation completed successfully + # exception on Windows. + if not sys.platform.startswith("win") and not e.winerror == 0: + raise except TypeError as e: # This is to ignore an asyncio.windows_events exception # on Windows when the process gets the SIGBREAK signal From 3926390d307a67689936d7f1104c9cd03446e820 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Sat, 18 Apr 2015 09:22:37 +0200 Subject: [PATCH 05/11] Fix tests broken by a previous commit --- gns3server/handlers/api/iou_handler.py | 2 +- tests/modules/iou/test_iou_vm.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gns3server/handlers/api/iou_handler.py b/gns3server/handlers/api/iou_handler.py index acf3b5b1..3314e580 100644 --- a/gns3server/handlers/api/iou_handler.py +++ b/gns3server/handlers/api/iou_handler.py @@ -305,4 +305,4 @@ class IOUHandler: vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) response.set_status(200) - response.json({"content": vm.initial_config}) + response.json({"content": vm.initial_config_content}) diff --git a/tests/modules/iou/test_iou_vm.py b/tests/modules/iou/test_iou_vm.py index 731f6974..c66cfb99 100644 --- a/tests/modules/iou/test_iou_vm.py +++ b/tests/modules/iou/test_iou_vm.py @@ -81,11 +81,11 @@ def test_vm(project, manager): assert vm.id == "00010203-0405-0607-0809-0a0b0c0d0e0f" -def test_vm_initial_config(project, manager): +def test_vm_initial_config_content(project, manager): vm = IOUVM("test", "00010203-0405-0607-0808-0a0b0c0d0e0f", project, manager) - vm.initial_config = "hostname %h" + vm.initial_config_content = "hostname %h" assert vm.name == "test" - assert vm.initial_config == "hostname test" + assert vm.initial_config_content == "hostname test" assert vm.id == "00010203-0405-0607-0808-0a0b0c0d0e0f" @@ -287,10 +287,10 @@ def test_update_initial_config_empty(vm): assert f.read() == content -def test_update_initial_config_h(vm): +def test_update_initial_config_content_hostname(vm): content = "hostname %h\n" vm.name = "pc1" - vm.initial_config = content + vm.initial_config_content = content with open(vm.initial_config_file) as f: assert f.read() == "hostname pc1\n" From 531e95463cea6a37310a7512a311c91eb973b5dc Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 20 Apr 2015 10:12:17 +0200 Subject: [PATCH 06/11] Add qemu-kvm to the list of binary --- gns3server/modules/qemu/__init__.py | 2 +- tests/modules/qemu/test_qemu_manager.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gns3server/modules/qemu/__init__.py b/gns3server/modules/qemu/__init__.py index e5bf698a..510f6aab 100644 --- a/gns3server/modules/qemu/__init__.py +++ b/gns3server/modules/qemu/__init__.py @@ -67,7 +67,7 @@ class Qemu(BaseManager): for path in paths: try: for f in os.listdir(path): - if (f.startswith("qemu-system") or f == "qemu" or f == "qemu.exe") and \ + if (f.startswith("qemu-system") or f.startswith("qemu-kvm") or f == "qemu" or f == "qemu.exe") and \ os.access(os.path.join(path, f), os.X_OK) and \ os.path.isfile(os.path.join(path, f)): qemu_path = os.path.join(path, f) diff --git a/tests/modules/qemu/test_qemu_manager.py b/tests/modules/qemu/test_qemu_manager.py index ee732d11..fcdfe477 100644 --- a/tests/modules/qemu/test_qemu_manager.py +++ b/tests/modules/qemu/test_qemu_manager.py @@ -32,7 +32,7 @@ def test_get_qemu_version(loop): def test_binary_list(loop): - files_to_create = ["qemu-system-x86", "qemu-system-x42", "hello"] + files_to_create = ["qemu-system-x86", "qemu-system-x42", "qemu-kvm", "hello"] for file_to_create in files_to_create: path = os.path.join(os.environ["PATH"], file_to_create) @@ -44,6 +44,7 @@ def test_binary_list(loop): qemus = loop.run_until_complete(asyncio.async(Qemu.binary_list())) assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x86"), "version": "2.2.0"} in qemus + assert {"path": os.path.join(os.environ["PATH"], "qemu-kvm"), "version": "2.2.0"} in qemus assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x42"), "version": "2.2.0"} in qemus assert {"path": os.path.join(os.environ["PATH"], "hello"), "version": "2.2.0"} not in qemus From 8b879c061443a69217fe341fd1a916c084ea63d8 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 20 Apr 2015 16:57:03 +0200 Subject: [PATCH 07/11] Support wireshark remote capture --- gns3server/handlers/__init__.py | 1 + gns3server/handlers/api/file_handler.py | 54 +++++++++++++++++++++ gns3server/schemas/file.py | 32 +++++++++++++ tests/handlers/api/base.py | 4 +- tests/handlers/api/test_file.py | 62 +++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 gns3server/handlers/api/file_handler.py create mode 100644 gns3server/schemas/file.py create mode 100644 tests/handlers/api/test_file.py diff --git a/gns3server/handlers/__init__.py b/gns3server/handlers/__init__.py index 307eece3..5db20453 100644 --- a/gns3server/handlers/__init__.py +++ b/gns3server/handlers/__init__.py @@ -27,6 +27,7 @@ from gns3server.handlers.api.virtualbox_handler import VirtualBoxHandler from gns3server.handlers.api.vpcs_handler import VPCSHandler from gns3server.handlers.api.config_handler import ConfigHandler from gns3server.handlers.api.server_handler import ServerHandler +from gns3server.handlers.api.file_handler import FileHandler from gns3server.handlers.upload_handler import UploadHandler from gns3server.handlers.index_handler import IndexHandler diff --git a/gns3server/handlers/api/file_handler.py b/gns3server/handlers/api/file_handler.py new file mode 100644 index 00000000..1dc2e31b --- /dev/null +++ b/gns3server/handlers/api/file_handler.py @@ -0,0 +1,54 @@ +# +# Copyright (C) 2015 GNS3 Technologies Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import asyncio + +from ...web.route import Route +from ...schemas.file import FILE_STREAM_SCHEMA + + +class FileHandler: + + @classmethod + @Route.get( + r"/files/stream", + description="Stream a file from the server", + status_codes={ + 200: "File retrieved", + 404: "File doesn't exist" + }, + input=FILE_STREAM_SCHEMA + ) + def read(request, response): + try: + with open(request.json.get("location"), "rb") as f: + loop = asyncio.get_event_loop() + response.content_type = "application/octet-stream" + response.set_status(200) + response.enable_chunked_encoding() + # Very important: do not send a content lenght otherwise QT close the connection but curl can consume the Feed + response.content_length = None + + response.start(request) + + while True: + data = yield from loop.run_in_executor(None, f.read, 16) + if len(data) == 0: + yield from asyncio.sleep(0.1) + else: + response.write(data) + except FileNotFoundError: + response.set_status(404) diff --git a/gns3server/schemas/file.py b/gns3server/schemas/file.py new file mode 100644 index 00000000..38ce7a10 --- /dev/null +++ b/gns3server/schemas/file.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2015 GNS3 Technologies Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +FILE_STREAM_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Request validation retrieval of a file stream", + "type": "object", + "properties": { + "location": { + "description": "File path", + "type": ["string"], + "minLength": 1 + } + }, + "additionalProperties": False, + "required": ["location"] +} diff --git a/tests/handlers/api/base.py b/tests/handlers/api/base.py index 6a4fd02e..4b677d17 100644 --- a/tests/handlers/api/base.py +++ b/tests/handlers/api/base.py @@ -44,7 +44,7 @@ class Query: def delete(self, path, **kwargs): return self._fetch("DELETE", path, **kwargs) - def _get_url(self, path, version): + def get_url(self, path, version): if version is None: return "http://{}:{}{}".format(self._host, self._port, path) return "http://{}:{}/v{}{}".format(self._host, self._port, version, path) @@ -62,7 +62,7 @@ class Query: @asyncio.coroutine def go(future): - response = yield from aiohttp.request(method, self._get_url(path, api_version), data=body) + response = yield from aiohttp.request(method, self.get_url(path, api_version), data=body) future.set_result(response) future = asyncio.Future() asyncio.async(go(future)) diff --git a/tests/handlers/api/test_file.py b/tests/handlers/api/test_file.py new file mode 100644 index 00000000..0b110b2e --- /dev/null +++ b/tests/handlers/api/test_file.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2015 GNS3 Technologies Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +This test suite check /files endpoint +""" + +import json +import asyncio +import aiohttp + +from gns3server.version import __version__ + + +def test_stream(server, tmpdir, loop): + with open(str(tmpdir / "test"), 'w+') as f: + f.write("hello") + + def go(future): + query = json.dumps({"location": str(tmpdir / "test")}) + headers = {'content-type': 'application/json'} + response = yield from aiohttp.request("GET", server.get_url("/files/stream", 1), data=query, headers=headers) + response.body = yield from response.content.read(5) + with open(str(tmpdir / "test"), 'a') as f: + f.write("world") + response.body += yield from response.content.read(5) + response.close() + future.set_result(response) + + future = asyncio.Future() + asyncio.async(go(future)) + response = loop.run_until_complete(future) + assert response.status == 200 + assert response.body == b'helloworld' + + +def test_stream_file_not_found(server, tmpdir, loop): + def go(future): + query = json.dumps({"location": str(tmpdir / "test")}) + headers = {'content-type': 'application/json'} + response = yield from aiohttp.request("GET", server.get_url("/files/stream", 1), data=query, headers=headers) + response.close() + future.set_result(response) + + future = asyncio.Future() + asyncio.async(go(future)) + response = loop.run_until_complete(future) + assert response.status == 404 From bf618d321c5570974641ab50866dea86c6b4d10a Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 22 Apr 2015 10:39:43 +0200 Subject: [PATCH 08/11] Max 100 thread executor --- gns3server/main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gns3server/main.py b/gns3server/main.py index ac6c6b19..2ced1294 100644 --- a/gns3server/main.py +++ b/gns3server/main.py @@ -21,6 +21,8 @@ import datetime import sys import locale import argparse +import asyncio +import concurrent from gns3server.server import Server from gns3server.web.logger import init_logger @@ -173,6 +175,9 @@ def main(): Project.clean_project_directory() + executor = concurrent.futures.ThreadPoolExecutor(max_workers=100) # We allow 100 parallel executors + loop = asyncio.get_event_loop().set_default_executor(executor) + CrashReport.instance() host = server_config["host"] port = int(server_config["port"]) From fc289fd868dfe7a48cf79936c88675d3e046eccb Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 22 Apr 2015 17:13:39 +0200 Subject: [PATCH 09/11] Basic upstart script --- README.rst | 26 ++++++++++++++++++++++++++ init/gns3.conf.upstart | 19 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 init/gns3.conf.upstart diff --git a/README.rst b/README.rst index a0761c36..091b5fc9 100644 --- a/README.rst +++ b/README.rst @@ -26,6 +26,12 @@ unstable ******** *Never* use this branch for production. Major new features pull requests goes here. +Linux +----- + +GNS3 is perhaps packaged for your distribution: +* Gentoo: https://packages.gentoo.org/package/net-misc/gns3-server + Linux (Debian based) -------------------- @@ -62,6 +68,26 @@ To run tests use: py.test -v + +Run as daemon +*************** + +You will found init sample script for various systems +inside the init directory. + +upstart +~~~~~~~ + +For ubuntu < 15.04 + +You need to copy init/gns3.conf.upstart to /etc/init/gns3.conf + +.. code:: bash + + sudo chown root /etc/init/gns3.conf + sudo service gns3 start + + Windows ------- diff --git a/init/gns3.conf.upstart b/init/gns3.conf.upstart new file mode 100644 index 00000000..6ddccb8a --- /dev/null +++ b/init/gns3.conf.upstart @@ -0,0 +1,19 @@ +description "GNS3 server" +author "GNS3 Team" + +start on filesystem or runlevel [2345] +stop on shutdown + +script + echo $$ > /var/run/gns3.pid + exec start-stop-daemon --start -c gns3 --exec /usr/local/bin/gns3server +end script + +pre-start script + echo "[`date`] GNS3 Starting" >> /var/log/gns3.log +end script + +pre-stop script + rm /var/run/gns3.pid + echo "[`date`] GNS3 Stopping" >> /var/log/gns3.log +end script From 9c549b175fd3bd2a90184e7532e61e2df26ddf11 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 22 Apr 2015 17:28:58 +0200 Subject: [PATCH 10/11] Add --log options for daemon support --- gns3server/main.py | 4 +++- gns3server/web/logger.py | 7 +++++-- init/gns3.conf.upstart | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/gns3server/main.py b/gns3server/main.py index ac6c6b19..ee32440f 100644 --- a/gns3server/main.py +++ b/gns3server/main.py @@ -92,6 +92,7 @@ def parse_arguments(argv, config): "quiet": config.getboolean("quiet", False), "debug": config.getboolean("debug", False), "live": config.getboolean("live", False), + "logfile": config.getboolean("logfile", ""), } parser = argparse.ArgumentParser(description="GNS3 server version {}".format(__version__)) @@ -109,6 +110,7 @@ def parse_arguments(argv, config): parser.add_argument("-d", "--debug", action="store_true", help="show debug logs") parser.add_argument("--live", action="store_true", help="enable code live reload") parser.add_argument("--shell", action="store_true", help="start a shell inside the server (debugging purpose only you need to install ptpython before)") + parser.add_argument("--log", help="send output to logfile instead of console") return parser.parse_args(argv) @@ -141,7 +143,7 @@ def main(): if args.debug: level = logging.DEBUG - user_log = init_logger(level, quiet=args.quiet) + user_log = init_logger(level, logfile=args.log, quiet=args.quiet) user_log.info("GNS3 server version {}".format(__version__)) current_year = datetime.date.today().year user_log.info("Copyright (c) 2007-{} GNS3 Technologies Inc.".format(current_year)) diff --git a/gns3server/web/logger.py b/gns3server/web/logger.py index 6c9c1d2b..90865f25 100644 --- a/gns3server/web/logger.py +++ b/gns3server/web/logger.py @@ -78,8 +78,11 @@ class ColouredStreamHandler(logging.StreamHandler): self.handleError(record) -def init_logger(level, quiet=False): - if sys.platform.startswith("win"): +def init_logger(level, logfile=None, quiet=False): + if logfile and len(logfile) > 0: + stream_handler = logging.FileHandler(logfile) + stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{") + elif sys.platform.startswith("win"): stream_handler = logging.StreamHandler(sys.stdout) stream_handler.formatter = ColouredFormatter("{asctime} {levelname} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{") else: diff --git a/init/gns3.conf.upstart b/init/gns3.conf.upstart index 6ddccb8a..aadf4c9b 100644 --- a/init/gns3.conf.upstart +++ b/init/gns3.conf.upstart @@ -6,7 +6,7 @@ stop on shutdown script echo $$ > /var/run/gns3.pid - exec start-stop-daemon --start -c gns3 --exec /usr/local/bin/gns3server + exec start-stop-daemon --start -c gns3 --exec /usr/local/bin/gns3server --log /var/log/gns3.log end script pre-start script From 83f2509cfe778b69a1aed2475d58047f81317e17 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 22 Apr 2015 10:47:49 +0200 Subject: [PATCH 11/11] Cleanup exceptions --- gns3server/handlers/api/file_handler.py | 12 +++++++++--- gns3server/main.py | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/gns3server/handlers/api/file_handler.py b/gns3server/handlers/api/file_handler.py index 1dc2e31b..e705ffc2 100644 --- a/gns3server/handlers/api/file_handler.py +++ b/gns3server/handlers/api/file_handler.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import asyncio +import aiohttp from ...web.route import Route from ...schemas.file import FILE_STREAM_SCHEMA @@ -28,17 +29,19 @@ class FileHandler: description="Stream a file from the server", status_codes={ 200: "File retrieved", - 404: "File doesn't exist" + 404: "File doesn't exist", + 409: "Can't access to file" }, input=FILE_STREAM_SCHEMA ) def read(request, response): + response.enable_chunked_encoding() + try: with open(request.json.get("location"), "rb") as f: loop = asyncio.get_event_loop() response.content_type = "application/octet-stream" response.set_status(200) - response.enable_chunked_encoding() # Very important: do not send a content lenght otherwise QT close the connection but curl can consume the Feed response.content_length = None @@ -51,4 +54,7 @@ class FileHandler: else: response.write(data) except FileNotFoundError: - response.set_status(404) + raise aiohttp.web.HTTPNotFound() + except OSError as e: + raise aiohttp.web.HTTPConflict(text=str(e)) + diff --git a/gns3server/main.py b/gns3server/main.py index 2ced1294..7404365c 100644 --- a/gns3server/main.py +++ b/gns3server/main.py @@ -175,7 +175,7 @@ def main(): Project.clean_project_directory() - executor = concurrent.futures.ThreadPoolExecutor(max_workers=100) # We allow 100 parallel executors + executor = concurrent.futures.ThreadPoolExecutor(max_workers=100) # We allow 100 parallel executors loop = asyncio.get_event_loop().set_default_executor(executor) CrashReport.instance()