You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.3 KiB
90 lines
2.3 KiB
#ifndef CONTROLLER_H
|
|
#define CONTROLLER_H
|
|
|
|
#include <hv/HttpServer.h>
|
|
#include <QString>
|
|
|
|
class Controller
|
|
{
|
|
public:
|
|
/**
|
|
* 析构函数
|
|
*/
|
|
virtual ~Controller() = default;
|
|
|
|
/**
|
|
* @brief className 类名称
|
|
* @return 类名
|
|
*/
|
|
virtual const QString className() const { return "Controller"; }
|
|
|
|
/**
|
|
* @brief get GET 请求处理
|
|
* @param req 请求requset
|
|
* @param resp 响应response
|
|
* @return http状态码
|
|
*/
|
|
virtual int get(HttpRequest *req, HttpResponse *resp) { return methodNotMatch(req, resp); };
|
|
|
|
/**
|
|
* @brief post POST 请求处理
|
|
* @param req 请求requset
|
|
* @param resp 响应response
|
|
* @return http状态码
|
|
*/
|
|
virtual int post(HttpRequest *req, HttpResponse *resp) { return methodNotMatch(req, resp); };
|
|
|
|
/**
|
|
* @brief put PUT 请求处理
|
|
* @param req 请求requset
|
|
* @param resp 响应response
|
|
* @return http状态码
|
|
*/
|
|
virtual int put(HttpRequest *req, HttpResponse *resp) { return methodNotMatch(req, resp); };
|
|
|
|
/**
|
|
* @brief del DEL 请求处理
|
|
* @param req 请求requset
|
|
* @param resp 响应response
|
|
* @return http状态码
|
|
*/
|
|
virtual int del(HttpRequest *req, HttpResponse *resp) { return methodNotMatch(req, resp); };
|
|
|
|
/**
|
|
* @brief service 请求入口
|
|
* @param req 请求requset
|
|
* @param resp 响应response
|
|
* @return http状态码
|
|
*/
|
|
int service(HttpRequest *req, HttpResponse *resp)
|
|
{
|
|
if (HTTP_GET == req->method) {
|
|
return get(req, resp);
|
|
} else if (HTTP_POST == req->method) {
|
|
return post(req, resp);
|
|
} else if (HTTP_PUT == req->method) {
|
|
return put(req, resp);
|
|
} else if (HTTP_DELETE == req->method) {
|
|
return del(req, resp);
|
|
} else {
|
|
return methodNotMatch(req, resp);
|
|
}
|
|
}
|
|
|
|
protected:
|
|
/**
|
|
* @brief methodNotMatch 方法不匹配处理
|
|
* @param req 请求requset
|
|
* @param resp 响应response
|
|
* @return 405-http状态码
|
|
*/
|
|
int methodNotMatch(HttpRequest *req, HttpResponse *resp)
|
|
{
|
|
Q_UNUSED(req);
|
|
Q_UNUSED(resp);
|
|
return HTTP_STATUS_METHOD_NOT_ALLOWED;
|
|
}
|
|
};
|
|
|
|
#endif // CONTROLLER_H
|