From 12ac86943e2e8e9b2e530b1964e709a3b555ddb2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 26 Sep 2012 11:49:04 -0400 Subject: [PATCH] Changed to use snprintf. --- httpsvrkit.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/httpsvrkit.h b/httpsvrkit.h index 336e31d..14179bd 100644 --- a/httpsvrkit.h +++ b/httpsvrkit.h @@ -9,7 +9,7 @@ #define HTTPSVRKIT_H #ifdef _WIN32 -//#define _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS #define _CRT_NONSTDC_NO_DEPRECATE #ifndef SO_SYNCHRONOUS_NONALERT @@ -24,6 +24,7 @@ #include typedef SOCKET socket_t; +#define snprintf sprintf_s #else #include #include @@ -178,21 +179,21 @@ std::string dump_request(Context& cxt) s += "================================\n"; - sprintf(buf, "%s %s", req.method.c_str(), req.url.c_str()); + snprintf(buf, sizeof(buf), "%s %s", req.method.c_str(), req.url.c_str()); s += buf; std::string query; for (auto it = req.query.begin(); it != req.query.end(); ++it) { const auto& x = *it; - sprintf(buf, "%c%s=%s", (it == req.query.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str()); + snprintf(buf, sizeof(buf), "%c%s=%s", (it == req.query.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str()); query += buf; } - sprintf(buf, "%s\n", query.c_str()); + snprintf(buf, sizeof(buf), "%s\n", query.c_str()); s += buf; for (auto it = req.headers.begin(); it != req.headers.end(); ++it) { const auto& x = *it; - sprintf(buf, "%s: %s\n", x.first.c_str(), x.second.c_str()); + snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str()); s += buf; } @@ -279,8 +280,9 @@ inline bool Server::run() inline void Server::stop() { - close_socket(sock_); + auto sock = sock_; sock_ = -1; + close_socket(sock); } inline bool read_request_line(FILE* fp, Request& request)