diff --git a/src/engine_uring.cpp b/src/engine_uring.cpp index e9420a1..a3c1fa4 100644 --- a/src/engine_uring.cpp +++ b/src/engine_uring.cpp @@ -736,7 +736,7 @@ void automata_t::parse_and_raise_request() noexcept { auto parsed_request = std::get(parsed_request_or_error); scratch.is_http = request.size() != parsed_request.body.size(); - scratch.dynamic_packet = parsed_request.body; + scratch.dynamic_packet = {parsed_request.body.data(), parsed_request.json_length}; if (scratch.dynamic_packet.size() > ram_page_size_k) { sjd::parser parser; if (parser.allocate(scratch.dynamic_packet.size(), scratch.dynamic_packet.size() / 2) != sj::SUCCESS) diff --git a/src/helpers/parse.hpp b/src/helpers/parse.hpp index 3990a43..3c58443 100644 --- a/src/helpers/parse.hpp +++ b/src/helpers/parse.hpp @@ -67,6 +67,7 @@ inline std::variant find_callback(named_callb if (!doc.is_object()) return default_error_t{-32600, "The JSON sent is not a valid request object."}; + // We don't support JSON-RPC before version 2.0. sj::simdjson_result version = doc["jsonrpc"]; if (!version.is_string() || version.get_string().value_unsafe() != "2.0") @@ -78,10 +79,12 @@ inline std::variant find_callback(named_callb bool id_invalid = (id.is_double() && !id.is_int64() && !id.is_uint64()) || id.is_object() || id.is_array(); if (id_invalid) return default_error_t{-32600, "The request must have integer or string id."}; + sj::simdjson_result method = doc["method"]; bool method_invalid = !method.is_string(); if (method_invalid) return default_error_t{-32600, "The method must be a string."}; + sj::simdjson_result params = doc["params"]; bool params_present_and_invalid = !params.is_array() && !params.is_object() && params.error() == sj::SUCCESS; if (params_present_and_invalid) @@ -117,6 +120,7 @@ struct parsed_request_t { std::string_view content_type{}; std::string_view content_length{}; std::string_view body{}; + int json_length; }; /** @@ -166,8 +170,11 @@ inline std::variant split_body_headers(std::s if (pos == std::string_view::npos) return default_error_t{-32700, "Invalid JSON was received by the server."}; req.body = body.substr(pos + 4); - } else + auto res = std::from_chars(req.content_length.begin(), req.content_length.end(), req.json_length); + } else { + req.json_length = body.size(); req.body = body; + } return req; }