Commit 4f7235bd authored by Pavel Spirek's avatar Pavel Spirek

NACM now has same REST api root as data

parent 1aba2a2c
Pipeline #72 skipped
......@@ -10,7 +10,6 @@ CONFIG_HTTP = {
"DOC_ROOT": "doc-root",
"DOC_DEFAULT_NAME": "index.html",
"API_ROOT": "/restconf",
"NACM_API_ROOT": "/restconf_nacm",
"SERVER_NAME": "hyper-h2",
"PORT": 8443,
......@@ -30,13 +29,11 @@ CONFIG = {
}
NACM_ADMINS = CONFIG["NACM"]["ALLOWED_USERS"]
NACM_API_ROOT_data = os.path.join(CONFIG_HTTP["NACM_API_ROOT"], "data")
API_ROOT_data = os.path.join(CONFIG_HTTP["API_ROOT"], "data")
def load_config(filename: str):
global NACM_ADMINS
global NACM_API_ROOT_data
global API_ROOT_data
try:
......@@ -53,7 +50,6 @@ def load_config(filename: str):
# Shortcuts
NACM_ADMINS = CONFIG["NACM"]["ALLOWED_USERS"]
NACM_API_ROOT_data = os.path.join(CONFIG_HTTP["NACM_API_ROOT"], "data")
API_ROOT_data = os.path.join(CONFIG_HTTP["API_ROOT"], "data")
......
......@@ -5,7 +5,6 @@ HTTP_SERVER:
DOC_ROOT: "jetconf/doc-root"
DOC_DEFAULT_NAME: "index.html"
API_ROOT: "/restconf"
NACM_API_ROOT: "/restconf_nacm"
SERVER_NAME: "hyper-h2"
SERVER_SSL_CERT: "jetconf/server.crt"
......
......@@ -28,6 +28,10 @@ class DataHelpers:
n = new_node
return n
@staticmethod
def path_first_ns(api_pth: str) -> str:
return api_pth[1:].split("/", maxsplit=1)[0].split(":", maxsplit=1)[0]
class DateTimeHelpers:
@staticmethod
......
This diff is collapsed.
......@@ -8,7 +8,7 @@ from h2.connection import H2Connection
from h2.events import DataReceived, RequestReceived, RemoteSettingsChanged
import jetconf.http_handlers as handlers
from .config import CONFIG_HTTP, NACM_API_ROOT_data, API_ROOT_data, load_config, print_config
from .config import CONFIG_HTTP, API_ROOT_data, load_config, print_config
from .nacm import NacmConfig
from .data import JsonDatastore
......@@ -49,6 +49,19 @@ class H2Protocol(asyncio.Protocol):
self.transport.write(self.conn.data_to_send())
self.client_cert = self.transport.get_extra_info('peercert')
def send_empty(self, stream_id: int, status_code: str, status_msg: str, status_in_body: bool = True):
response = status_code + " " + status_msg + "\n" if status_in_body else ""
response_bytes = response.encode()
response_headers = (
(":status", status_code),
("content-type", "text/plain"),
("content-length", len(response_bytes)),
("server", CONFIG_HTTP["SERVER_NAME"]),
)
self.conn.send_headers(stream_id, response_headers)
self.conn.send_data(stream_id, response_bytes, end_stream=True)
def data_received(self, data: bytes):
events = self.conn.receive_data(data)
self.transport.write(self.conn.data_to_send())
......@@ -83,7 +96,9 @@ class H2Protocol(asyncio.Protocol):
h = h2_handlers.get_handler(headers[":method"], url_path)
if h:
h(self, headers, data, stream_id)
h(self, stream_id, headers, data)
else:
self.send_empty(stream_id, "400", "Bad Request")
def handle_get_delete(self, headers: OrderedDict, stream_id: int):
# Handle GET, DELETE
......@@ -92,7 +107,9 @@ class H2Protocol(asyncio.Protocol):
h = h2_handlers.get_handler(headers[":method"], url_path)
if h:
h(self, headers, stream_id)
h(self, stream_id, headers)
else:
self.send_empty(stream_id, "400", "Bad Request")
def run():
......@@ -119,10 +136,6 @@ def run():
api_post = handlers.create_post_api(ex_datastore)
api_put = handlers.create_put_api(ex_datastore)
api_delete = handlers.create_api_delete(ex_datastore)
nacm_api_get = handlers.create_get_nacm_api(ex_datastore)
nacm_api_post = handlers.create_post_nacm_api(ex_datastore)
nacm_api_put = handlers.create_put_nacm_api(ex_datastore)
nacm_api_delete = handlers.create_nacm_api_delete(ex_datastore)
h2_handlers = HandlerList()
h2_handlers.register_handler(lambda m, p: (m == "GET") and (p == CONFIG_HTTP["API_ROOT"]), api_get_root)
......@@ -131,13 +144,8 @@ def run():
h2_handlers.register_handler(lambda m, p: (m == "PUT") and (p.startswith(API_ROOT_data)), api_put)
h2_handlers.register_handler(lambda m, p: (m == "DELETE") and (p.startswith(API_ROOT_data)), api_delete)
h2_handlers.register_handler(lambda m, p: (m == "GET") and (p == CONFIG_HTTP["NACM_API_ROOT"]), api_get_root)
h2_handlers.register_handler(lambda m, p: (m == "GET") and (p.startswith(NACM_API_ROOT_data)), nacm_api_get)
h2_handlers.register_handler(lambda m, p: (m == "POST") and (p.startswith(NACM_API_ROOT_data)), nacm_api_post)
h2_handlers.register_handler(lambda m, p: (m == "PUT") and (p.startswith(NACM_API_ROOT_data)), nacm_api_put)
h2_handlers.register_handler(lambda m, p: (m == "DELETE") and (p.startswith(NACM_API_ROOT_data)), nacm_api_delete)
h2_handlers.register_handler(lambda m, p: m == "GET", handlers.get_file)
h2_handlers.register_default_handler(handlers.unknown_req_handler)
# HTTP server init
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment