Moved SimpleWeb::string_view to utility.hpp, and made use of string_view in Server::Response::write

This commit is contained in:
eidheim 2018-07-12 08:43:07 +02:00
commit 56b1df1ee5
5 changed files with 20 additions and 20 deletions

View file

@ -29,22 +29,6 @@ namespace SimpleWeb {
} // namespace SimpleWeb } // namespace SimpleWeb
#endif #endif
#if __cplusplus > 201402L || (defined(_MSC_VER) && _MSC_VER >= 1910)
#include <string_view>
namespace SimpleWeb {
using string_view = std::string_view;
}
#elif !defined(USE_STANDALONE_ASIO)
#include <boost/utility/string_ref.hpp>
namespace SimpleWeb {
using string_view = boost::string_ref;
}
#else
namespace SimpleWeb {
using string_view = const std::string &;
}
#endif
namespace SimpleWeb { namespace SimpleWeb {
template <class socket_type> template <class socket_type>
class Client; class Client;

View file

@ -108,7 +108,7 @@ int main() {
// GET-example for the path /match/[number], responds with the matched string in path (number) // GET-example for the path /match/[number], responds with the matched string in path (number)
// For instance a request GET /match/123 will receive: 123 // For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
response->write(request->path_match[1]); response->write(request->path_match[1].str());
}; };
// GET-example simulating heavy work in a separate thread // GET-example simulating heavy work in a separate thread

View file

@ -106,7 +106,7 @@ int main() {
// GET-example for the path /match/[number], responds with the matched string in path (number) // GET-example for the path /match/[number], responds with the matched string in path (number)
// For instance a request GET /match/123 will receive: 123 // For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) { server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
response->write(request->path_match[1]); response->write(request->path_match[1].str());
}; };
// GET-example simulating heavy work in a separate thread // GET-example simulating heavy work in a separate thread

View file

@ -161,7 +161,7 @@ namespace SimpleWeb {
} }
/// Convenience function for writing status line, header fields, and content /// Convenience function for writing status line, header fields, and content
void write(StatusCode status_code, const std::string &content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) { void write(StatusCode status_code, string_view content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
*this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n"; *this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n";
write_header(header, content.size()); write_header(header, content.size());
if(!content.empty()) if(!content.empty())
@ -180,7 +180,7 @@ namespace SimpleWeb {
} }
/// Convenience function for writing success status line, header fields, and content /// Convenience function for writing success status line, header fields, and content
void write(const std::string &content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) { void write(string_view content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
write(StatusCode::success_ok, content, header); write(StatusCode::success_ok, content, header);
} }

View file

@ -8,6 +8,22 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#if __cplusplus > 201402L || (defined(_MSC_VER) && _MSC_VER >= 1910)
#include <string_view>
namespace SimpleWeb {
using string_view = std::string_view;
}
#elif !defined(USE_STANDALONE_ASIO)
#include <boost/utility/string_ref.hpp>
namespace SimpleWeb {
using string_view = boost::string_ref;
}
#else
namespace SimpleWeb {
using string_view = const std::string &;
}
#endif
namespace SimpleWeb { namespace SimpleWeb {
inline bool case_insensitive_equal(const std::string &str1, const std::string &str2) noexcept { inline bool case_insensitive_equal(const std::string &str1, const std::string &str2) noexcept {
return str1.size() == str2.size() && return str1.size() == str2.size() &&