static void OutputDirectoryContent(HttpOutput& output, const UriComponents& uri, const boost::filesystem::path& p) { namespace fs = boost::filesystem; output.SetContentType("text/html"); std::string s; s += "<html>"; s += " <body>"; s += " <h1>Subdirectories</h1>"; s += " <ul>"; if (uri.size() > 0) { std::string h = Toolbox::FlattenUri(uri) + "/.."; s += "<li><a href=\"" + h + "\">..</a></li>"; } fs::directory_iterator end; for (fs::directory_iterator it(p) ; it != end; ++it) { #if BOOST_HAS_FILESYSTEM_V3 == 1 std::string f = it->path().filename().string(); #else std::string f = it->path().filename(); #endif std::string h = Toolbox::FlattenUri(uri) + "/" + f; if (fs::is_directory(it->status())) s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; } s += " </ul>"; s += " <h1>Files</h1>"; s += " <ul>"; for (fs::directory_iterator it(p) ; it != end; ++it) { #if BOOST_HAS_FILESYSTEM_V3 == 1 std::string f = it->path().filename().string(); #else std::string f = it->path().filename(); #endif std::string h = Toolbox::FlattenUri(uri) + "/" + f; if (fs::is_regular_file(it->status())) s += "<li><a href=\"" + h + "\">" + f + "</a></li>"; } s += " </ul>"; s += " </body>"; s += "</html>"; output.SendBody(s); }
void RestApi::Handle(HttpOutput& httpOutput, const std::string& uri, const std::string& method, const std::string& body) { if (method == "GET") { bool foundHandler = false; for (GetHandlersType::iterator it = GetHandlers.begin(); it != GetHandlers.end(); ++it) { boost::regex ex(it->first); if (boost::regex_match(uri, ex)) { RestApiOutput restApiOutput(httpOutput); RestApiGetCall restApiGetCall(restApiOutput, *this); const int subs[] = {1}; // we just want to see group 1 boost::sregex_token_iterator i(uri.begin(), uri.end(), ex, subs); std::string id = *i; restApiGetCall.SetResourceID(id); it->second(restApiGetCall); foundHandler = true; break; } } if (!foundHandler) { httpOutput.SendBody("Handler not found\n"); return; } } else if (method == "POST") { bool foundHandler = false; for (PostHandlersType::iterator it = PostHandlers.begin(); it != PostHandlers.end(); ++it) { boost::regex ex(it->first); if (boost::regex_match(uri, ex)) { RestApiOutput restApiOutput(httpOutput); RestApiPostCall restApiPostCall(restApiOutput, *this, body); const int subs[] = {1}; // we just want to see group 1 boost::regex ex2(it->first); boost::sregex_token_iterator i(uri.begin(), uri.end(), ex2, subs); std::string id = *i; restApiPostCall.SetResourceID(id); it->second(restApiPostCall); foundHandler = true; break; } } if (!foundHandler) { httpOutput.SendBody("Handler not found"); return; } } else { httpOutput.SendBody("Not implemented"); return; } }