Add changed response type based on Accept header

This commit is contained in:
Tyler Perkins 2022-04-28 22:29:43 -04:00
parent e8d3b5f2d8
commit 39b09d7495
3 changed files with 14 additions and 5 deletions

View File

@ -36,6 +36,7 @@ bool memory::getRawProcMem(std::string& ret){
if(f.is_open()){
while(std::getline(f, line)){
ret += line;
ret += "\n";
}
} else {
ret = "Failed to open proc filesystem";

View File

@ -9,13 +9,19 @@
void setRoutes(crow::SimpleApp& app){
CROW_ROUTE(app, "/proc/meminfo")([](const crow::request& req){
crow::json::wvalue json;
bool status;
std::string accept = req.get_header_value("Accept");
std::cerr << "Accept: " << req.get_header_value("Accept") << "\n";
std::transform(accept.begin(), accept.end(), accept.begin(), ::tolower);
if(memory::getProcMem(json))
return crow::response(200, json.dump());
else
return crow::response(503, json.dump());
if(accept == "text/plain"){
accept.clear();
status = memory::getRawProcMem(accept);
return crow::response(status ? 200 : 503, accept);
} else {
status = memory::getProcMem(json);
return crow::response(status ? 200 : 503, json.dump());
}
});
CROW_ROUTE(app, "/mem")([]{

View File

@ -6,6 +6,8 @@
#pragma once
#include <string>
#include <algorithm>
#include <crow.h>
#include "components/memory.hpp"