eqemu-server/world/web_interface.cpp
Knightly 7ab909ee47 Standardize Licensing
- License was intended to be GPLv3 per earlier commit of GPLv3 LICENSE FILE
- This is confirmed by the inclusion of libraries that are incompatible with GPLv2
- This is also confirmed by KLS and the agreement of KLS's predecessors
- Added GPLv3 license headers to the compilable source files
- Removed Folly licensing in strings.h since the string functions do not match the Folly functions and are standard functions - this must have been left over from previous implementations
- Removed individual contributor license headers since the project has been under the "developer" mantle for many years
- Removed comments on files that were previously automatically generated since they've been manually modified multiple times and there are no automatic scripts referencing them (removed in 2023)
2026-04-01 17:09:57 -07:00

204 lines
4.6 KiB
C++

/* EQEmu: EQEmulator
Copyright (C) 2001-2026 EQEmu Development Team
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "web_interface.h"
#include "common/json/json.h"
#include "world/web_interface_eqw.h"
#include <sstream>
WebInterface::WebInterface(std::shared_ptr<EQ::Net::ServertalkServerConnection> connection)
{
m_connection = connection;
m_connection->OnMessage(ServerOP_WebInterfaceCall, std::bind(&WebInterface::OnCall, this, std::placeholders::_1, std::placeholders::_2));
RegisterEQW(this);
}
WebInterface::~WebInterface()
{
}
void WebInterface::OnCall(uint16 opcode, EQ::Net::Packet &p)
{
Json::Value root;
try {
auto json_str = p.GetCString(0);
std::stringstream ss(json_str);
ss >> root;
}
catch (std::exception &) {
SendError("Could not parse request");
return;
}
std::string method;
Json::Value params;
std::string id;
try {
method = root["method"].asString();
if (method.length() == 0) {
SendError("Invalid request: method not supplied");
return;
}
}
catch (std::exception &) {
SendError("Invalid request: method not supplied");
return;
}
//optional "params" -> Json::Value
try {
params = root["params"];
}
catch (std::exception &) {
params = nullptr;
}
//optional "id" needs to be string
try {
id = root["id"].asString();
}
catch (std::exception &) {
id = "";
}
//check for registered method
auto iter = m_calls.find(method);
if (iter == m_calls.end()) {
//if not exist then error
SendError("Invalid request: method not found", id);
return;
}
iter->second(this, method, id, params);
}
void WebInterface::Send(const Json::Value &value)
{
try {
std::stringstream ss;
ss << value;
EQ::Net::DynamicPacket p;
p.PutString(0, ss.str());
m_connection->Send(ServerOP_WebInterfaceCall, p);
}
catch (std::exception &) {
//Log error
}
}
void WebInterface::SendError(const std::string &message)
{
Json::Value error;
error["error"] = Json::Value();
error["error"]["message"] = message;
Send(error);
}
void WebInterface::SendError(const std::string &message, const std::string &id)
{
Json::Value error;
error["id"] = id;
error["error"] = Json::Value();
error["error"]["message"] = message;
Send(error);
}
void WebInterface::SendEvent(const Json::Value &value)
{
try {
std::stringstream ss;
ss << value;
EQ::Net::DynamicPacket p;
p.PutString(0, ss.str());
m_connection->Send(ServerOP_WebInterfaceEvent, p);
}
catch (std::exception &) {
//Log error
}
}
void WebInterface::AddCall(const std::string &method, WebInterfaceCall call)
{
m_calls.emplace(std::make_pair(method, call));
}
void WebInterface::SendResponse(const std::string &id, const Json::Value &response)
{
Json::Value out;
if(!id.empty())
out["id"] = id;
out["response"] = response;
Send(out);
}
WebInterfaceList::WebInterfaceList()
{
}
WebInterfaceList::~WebInterfaceList()
{
}
void WebInterfaceList::AddConnection(std::shared_ptr<EQ::Net::ServertalkServerConnection> connection)
{
m_interfaces.emplace(std::make_pair(connection->GetUUID(), std::make_unique<WebInterface>(connection)));
}
void WebInterfaceList::RemoveConnection(std::shared_ptr<EQ::Net::ServertalkServerConnection> connection)
{
auto iter = m_interfaces.find(connection->GetUUID());
if (iter != m_interfaces.end()) {
m_interfaces.erase(iter);
return;
}
}
void WebInterfaceList::SendResponse(const std::string &uuid, std::string &id, const Json::Value &response) {
auto iter = m_interfaces.find(uuid);
if (iter != m_interfaces.end()) {
iter->second->SendResponse(id, response);
}
}
void WebInterfaceList::SendEvent(const Json::Value &value) {
for (auto &i : m_interfaces) {
i.second->SendEvent(value);
}
}
void WebInterfaceList::SendError(const std::string &uuid, const std::string &message) {
auto iter = m_interfaces.find(uuid);
if (iter != m_interfaces.end()) {
iter->second->SendError(message);
}
}
void WebInterfaceList::SendError(const std::string &uuid, const std::string &message, const std::string &id) {
auto iter = m_interfaces.find(uuid);
if (iter != m_interfaces.end()) {
iter->second->SendError(message, id);
}
}