/*
	This file is part of solidity.
	solidity 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.
	solidity 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 solidity.  If not, see .
*/
// SPDX-License-Identifier: GPL-3.0
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#if defined(_WIN32)
#include 
#include 
#endif
using namespace std;
using namespace solidity::lsp;
// {{{ Transport
optional Transport::receive()
{
	auto const headers = parseHeaders();
	if (!headers)
	{
		error({}, ErrorCode::ParseError, "Could not parse RPC headers.");
		return nullopt;
	}
	if (!headers->count("content-length"))
	{
		error({}, ErrorCode::ParseError, "No content-length header found.");
		return nullopt;
	}
	string const data = readBytes(stoul(headers->at("content-length")));
	Json::Value jsonMessage;
	string jsonParsingErrors;
	solidity::util::jsonParseStrict(data, jsonMessage, &jsonParsingErrors);
	if (!jsonParsingErrors.empty() || !jsonMessage || !jsonMessage.isObject())
	{
		error({}, ErrorCode::ParseError, "Could not parse RPC JSON payload. " + jsonParsingErrors);
		return nullopt;
	}
	return {move(jsonMessage)};
}
void Transport::trace(std::string _message, Json::Value _extra)
{
	if (m_logTrace != TraceValue::Off)
	{
		Json::Value params;
		if (_extra.isObject())
			params = move(_extra);
		params["message"] = move(_message);
		notify("$/logTrace", move(params));
	}
}
optional