I wrote a simple http server using served. Found two issues.
Hello !
I wrote a simple http server using served. Found two issues.
1、Visual studio mode debugging, chrome open url http://localhost:8123/ The program will crash Crash location: asio\include\asio\buffer.hpp
~buffer_debug_check() { #if defined(ASIO_MSVC) && (ASIO_MSVC == 1400) // MSVC 8's string iterator checking may crash in a std::string::iterator // object's destructor when the iterator points to an already-destroyed // std::string object, unless the iterator is cleared first. Iter_ = Iterator(); #endif //defined(ASIO_MSVC) && (ASIO_MSVC == 1400) }
Void operator()() { (void)*iter_; }
2、Use release to run normally, but opening the page is very slow, sometimes loading resources fails
Sample code is as follows:
`
#include <served/served.hpp>
#include <served/plugins.hpp>
#include <io.h>
#include <stdio.h>
#include <direct.h>
#include
inline const char * mime_type(const std::string & path) { std::string ext; size_t pos = path.rfind("."); if (pos != std::string::npos) { ext = path.substr(pos, path.size() - pos); }
if (ext == ".txt") {
return "text/plain";
}
else if (ext == ".html" || ext == ".htm") {
return "text/html";
}
else if (ext == ".css") {
return "text/css";
}
else if (ext == ".jpeg" || ext == ".jpg") {
return "image/jpg";
}
else if (ext == ".png") {
return "image/png";
}
else if (ext == ".bmp") {
return "image/bmp";
}
else if (ext == ".gif") {
return "image/gif";
}
else if (ext == ".svg" || ext == ".svgz") {
return "image/svg+xml";
}
else if (ext == ".ico") {
return "image/x-icon";
}
else if (ext == ".json") {
return "application/json";
}
else if (ext == ".pdf") {
return "application/pdf";
}
else if (ext == ".js") {
return "application/javascript";
}
else if (ext == ".xml") {
return "application/xml";
}
else if (ext == ".xhtml") {
return "application/xhtml+xml";
}
else if (ext == ".ttf") {
return "application/x-font-truetype";
}
return nullptr;
}
int main(int argc, char *argv[]) { served::multiplexer mux; mux.use_after(served::plugin::access_log); std::string htdocs = argv[1];
if ((_access(htdocs.c_str(), 0)) == -1) {
printf("args: tinyhttpd c:\wwwroot");
return 0;
}
mux.handle("/").get([&](served::response & res, const served::request & req) {
std::string path = htdocs + req.url().path();
if (!path.empty() && path.back() == '/') { path += "index.html"; }
bool is_exist = false;
if ((_access(path.c_str(), 0)) != -1) {
std::string out;
std::ifstream fs(path, std::ios_base::binary);
fs.seekg(0, std::ios_base::end);
auto size = fs.tellg();
fs.seekg(0);
out.resize(static_cast<size_t>(size));
fs.read(&out[0], size);
res.set_body(out);
is_exist = true;
}
const char *type = mime_type(path);
if (type) {
res.set_header("content-type", type);
}
if (is_exist) {
res.set_status(200);
} else {
res.set_status(404);
}
});
std::cout << "Try this example by opening http://localhost:8123 in a browser" << std::endl;
served::net::server server("0.0.0.0", "8123", mux);
server.run(1);
return (EXIT_SUCCESS);
} `