Skip to content

Commit

Permalink
Fix the full request followed by partial
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Reed committed May 21, 2024
1 parent 02548db commit de7ef58
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/engine_uring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ void automata_t::parse_and_raise_request() noexcept {

auto parsed_request = std::get<parsed_request_t>(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)
Expand Down
9 changes: 8 additions & 1 deletion src/helpers/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ inline std::variant<named_callback_t, default_error_t> 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<sjd::element> version = doc["jsonrpc"];
if (!version.is_string() || version.get_string().value_unsafe() != "2.0")
Expand All @@ -78,10 +79,12 @@ inline std::variant<named_callback_t, default_error_t> 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<sjd::element> 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<sjd::element> params = doc["params"];
bool params_present_and_invalid = !params.is_array() && !params.is_object() && params.error() == sj::SUCCESS;
if (params_present_and_invalid)
Expand Down Expand Up @@ -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;
};

/**
Expand Down Expand Up @@ -166,8 +170,11 @@ inline std::variant<parsed_request_t, default_error_t> 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;
}
Expand Down

0 comments on commit de7ef58

Please sign in to comment.