我目前正在使用 HttpServer 并遇到以下问题:
我想注册我的控制器,当我收到请求时,我想检查其中一个控制器是否具有请求的 URL 的处理程序,然后调用该处理程序。
到目前为止一切顺利,但我不知道如何用 C++ 解决它,在 PHP 中它会很容易。
在 Easteregg 控制器函数 getControllerCallbacks 中,我想返回可用的回调,但它不会编译,因为我无法将派生类的成员函数指针转换为基类 (Controller) 的成员函数指针 (EastereggController)。我曾考虑将 ControllerCallback 设为“模板类”,但随后我必须知道 ControllerHandler 中的 Controller 类,我不知道,因为我有一个 std::vector
我对 C++ 很陌生,也许我忽略了一些东西。
这是我现在的代码:
控制器.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include "Config.h"
#include "Request.h"
#include "ControllerCallback.h"
namespace HttpServer {
class ControllerCallback;
class Controller {
public:
Controller();
virtual std::vector<ControllerCallback> getControllerCallbacks() = 0;
private:
Config *config;
const Request *request;
};
}
#endif // CONTROLLER_H
EastereggController.h
#ifndef EASTEREGGCONTROLLER_H
#define EASTEREGGCONTROLLER_H
#include "../HttpServer/Controller.h"
namespace Controller {
class EastereggController: public HttpServer::Controller {
public:
EastereggController() {};
std::vector<HttpServer::ControllerCallback> getControllerCallbacks();
HttpServer::Reply easteregg();
};
}
#endif // EASTEREGGCONTROLLER_H
EastereggController.cpp
#include "EastereggController.h"
namespace Controller {
std::vector<HttpServer::ControllerCallback> EastereggController::getControllerCallbacks() {
std::vector<HttpServer::ControllerCallback> controllerCallbacks;
HttpServer::ControllerCallback callback;
callback.pathTemplate = "/easteregg";
callback.handlerFunctionPtr = &EastereggController::easteregg;
return controllerCallbacks;
}
HttpServer::Reply EastereggController::easteregg() {
HttpServer::Reply rep;
rep.content.append("you have found an easteregg\n");
rep.status = HttpServer::Reply::ok;
rep.headers.resize(2);
rep.headers[0].name = "Content-Length";
rep.headers[0].value = std::to_string(rep.content.size());
rep.headers[1].name = "Content-Type";
rep.headers[1].value = "text/plain";
return rep;
}
}
控制器回调.h
#ifndef CONTROLLERCALLBACK_H
#define CONTROLLERCALLBACK_H
#include "Reply.h"
#include <string>
namespace HttpServer {
class Controller;
class ControllerCallback {
public:
ControllerCallback()
: pathTemplate("") {};
//,handlerFunctionPtr(nullptr){}
std::string pathTemplate;
Reply(Controller::*handlerFunctionPtr)();
};
}
#endif
控制器处理程序.h
#ifndef CONTROLLERHANDLER_H
#define CONTROLLERHANDLER_H
#include <vector>
#include <string>
#include "Controller.h"
#include "Config.h"
#include "Request.h"
#include "Reply.h"
namespace HttpServer {
class ControllerHandler {
public:
ControllerHandler(Config &conf);
void registerController(Controller &controller);
bool invokeController(std::string requestPath, Request &req, Reply &rep);
private:
Config &config;
std::vector<Controller *> controllers;
};
}
#endif // CONTROLLERHANDLER_H
控制器处理程序.cpp
#include "ControllerHandler.h"
#include "Controller.h"
#include "PathHandler.h"
namespace HttpServer {
ControllerHandler::ControllerHandler(Config &conf)
: config(conf) {
}
void ControllerHandler::registerController(Controller &controller) {
controllers.push_back(&controller);
}
bool ControllerHandler::invokeController(std::string requestPath, Request &req, Reply &rep) {
PathHandler pathHandler(requestPath, ':');
for(Controller *controller : controllers) {
std::vector<ControllerCallback> callbacks = controller->getControllerCallbacks();
for(ControllerCallback controllerCallback : callbacks) {
if(pathHandler.compare(controllerCallback.pathTemplate)) {
rep = ((*controller).*(controllerCallback.handlerFunctionPtr))();
return true;
}
}
}
return false;
}
}