12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- //
- // Created by xcbosa on 2023/1/28.
- //
- #include "../processor/processor.h"
- #include "../webuiconf.h"
- #include <sys/stat.h>
- using namespace std;
- using namespace xc::processor;
- namespace xc::controller {
- ContentGeneratorDefine({
- if (request.getMethod() != "GET") { return false; }
- if (request.getURL().length() == 0) { return false; }
- struct stat buffer;
- string filePath = "html";
- if (request.getURL()[0] == '/') {
- filePath += request.getURL();
- } else {
- filePath += "/" + request.getURL();
- }
- if (stat(filePath.c_str(), &buffer) == 0) {
- if (S_ISREG(buffer.st_mode)) {
- return true;
- } else {
- for (auto file: conf::defaultFiles) {
- string newFilePath = filePath;
- if (filePath[filePath.length() - 1] == '/') {
- newFilePath += file;
- } else {
- newFilePath += "/" + file;
- }
- if (stat(newFilePath.c_str(), &buffer) == 0) {
- return S_ISREG(buffer.st_mode);
- }
- }
- }
- }
- return false;
- }, {
- struct stat buffer;
- string filePath = "html";
- if (request.getURL()[0] == '/') {
- filePath += request.getURL();
- } else {
- filePath += "/" + request.getURL();
- }
- if (stat(filePath.c_str(), &buffer) == 0) {
- if (S_ISREG(buffer.st_mode)) {
- return (ResponseData *) new BinaryResponseData(200, filePath,
- mimeTypeOfFile(filePath));
- } else {
- for (auto file: conf::defaultFiles) {
- string newFilePath = filePath;
- if (filePath[filePath.length() - 1] == '/') {
- newFilePath += file;
- } else {
- newFilePath += "/" + file;
- }
- if (stat(newFilePath.c_str(), &buffer) == 0) {
- if (S_ISREG(buffer.st_mode)) {
- return (ResponseData *) new BinaryResponseData(200, newFilePath,
- mimeTypeOfFile(
- newFilePath));
- }
- }
- }
- }
- }
- return (ResponseData *) new FileResponseData(conf::errorPage404);
- })
- ContentGeneratorDefine(return request.getURL() == "/test1", {
- return new TextResponseData(200, "test1 controller response");
- })
- ContentGeneratorDefine(return request.getURL() == "/test2", {
- return new TextResponseData(200, "test2 controller response");
- })
- }
|