From e50eae19e618b073f95dcaa10d327f7fa40a2a01 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 25 Jul 2016 18:58:34 +0200 Subject: [PATCH] Support parameters for import --- .../api/controller/project_handler.py | 10 ++++++- gns3server/web/route.py | 27 +++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/gns3server/handlers/api/controller/project_handler.py b/gns3server/handlers/api/controller/project_handler.py index 107519fa..0287e434 100644 --- a/gns3server/handlers/api/controller/project_handler.py +++ b/gns3server/handlers/api/controller/project_handler.py @@ -268,6 +268,14 @@ class ProjectHandler: controller = Controller.instance() + if request.get("path"): + config = Config.instance() + if config.get_section_config("Server").getboolean("local", False) is False: + response.set_status(403) + return + path = request.json.get("path") + name = request.json.get("name") + # We write the content to a temporary location and after we extract it all. # It could be more optimal to stream this but it is not implemented in Python. # Spooled means the file is temporary kept in memory until max_size is reached @@ -278,7 +286,7 @@ class ProjectHandler: if not packet: break temp.write(packet) - project = yield from import_project(controller, request.match_info["project_id"], temp) + project = yield from import_project(controller, request.match_info["project_id"], temp, location=path, name=name) except OSError as e: raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e)) diff --git a/gns3server/web/route.py b/gns3server/web/route.py index 2e30acf2..4f9da445 100644 --- a/gns3server/web/route.py +++ b/gns3server/web/route.py @@ -21,6 +21,7 @@ import jsonschema import asyncio import aiohttp import logging +import urllib import traceback log = logging.getLogger(__name__) @@ -36,8 +37,9 @@ from ..config import Config @asyncio.coroutine def parse_request(request, input_schema): """Parse body of request and raise HTTP errors in case of problems""" + content_length = request.content_length - if content_length is not None and content_length > 0: + if content_length is not None and content_length > 0 and input_schema: body = yield from request.read() try: request.json = json.loads(body.decode('utf-8')) @@ -46,13 +48,21 @@ def parse_request(request, input_schema): raise aiohttp.web.HTTPBadRequest(text="Invalid JSON {}".format(e)) else: request.json = {} - try: - jsonschema.validate(request.json, input_schema) - except jsonschema.ValidationError as e: - log.error("Invalid input query. JSON schema error: {}".format(e.message)) - raise aiohttp.web.HTTPBadRequest(text="Invalid JSON: {} in schema: {}".format( - e.message, - json.dumps(e.schema))) + + # Parse the query string + if len(request.query_string) > 0: + for (k, v) in urllib.parse.parse_qs(request.query_string).items(): + request.json[k] = v[0] + + if input_schema: + try: + jsonschema.validate(request.json, input_schema) + except jsonschema.ValidationError as e: + log.error("Invalid input query. JSON schema error: {}".format(e.message)) + raise aiohttp.web.HTTPBadRequest(text="Invalid JSON: {} in schema: {}".format( + e.message, + json.dumps(e.schema))) + return request @@ -166,6 +176,7 @@ class Route(object): if api_version is None or raw is True: response = Response(request=request, route=route, output_schema=output_schema) + request = yield from parse_request(request, None) yield from func(request, response) return response