From dcb245b610de1a3216ef08f6e73bcb69451c4b99 Mon Sep 17 00:00:00 2001 From: lihuiba Date: Thu, 19 Dec 2024 12:01:42 +0800 Subject: [PATCH] extract c_str for estring_view --- common/estring.h | 37 +++++++++++++++++++++++++++++++++++++ net/http/client.cpp | 2 +- net/http/server.cpp | 11 ++++++----- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/common/estring.h b/common/estring.h index 4a562fe9..e42b34e6 100644 --- a/common/estring.h +++ b/common/estring.h @@ -22,6 +22,7 @@ limitations under the License. #include #include #include +#include #include #include @@ -75,6 +76,42 @@ class estring_view : public std::string_view size_t find_last_of(const charset& set) const; size_t find_last_not_of(const charset& set) const; + template + class Extraction { + char _buf[N]; + std::unique_ptr _s; + bool _ownership; + public: + Extraction(std::string_view sv, bool strict = false) { + //for regular strings, sv[sv.size()] should be accessible + if (!strict && sv[sv.size()] == '\0') { + _s.reset((char*)sv.data()); + _ownership = false; + } else if (sv.size() < N) { + memcpy(_buf, sv.data(), sv.size()); + _buf[sv.size()] = '\0'; + _s.reset(_buf); + _ownership = false; + } else { + auto ptr = (char*)malloc(sv.size() + 1); + memcpy(ptr, sv.data(), sv.size()); + ptr[sv.size()] = '\0'; + _s.reset(ptr); + _ownership = true; + } + } + operator const char* () const { + return _s.get(); + } + ~Extraction() { + if (!_ownership) + _s.release(); + } + }; + template + Extraction extract_c_str() const { + return {*this}; + } operator std::string () { return to_string(); diff --git a/net/http/client.cpp b/net/http/client.cpp index 20b7add8..be708f66 100644 --- a/net/http/client.cpp +++ b/net/http/client.cpp @@ -107,7 +107,7 @@ ISocketStream* PooledDialer::dial(std::string_view host, uint16_t port, bool sec if (secure) { tlssock->timeout(timeout); sock = tlssock->connect(ep); - tls_stream_set_hostname(sock, host.data()); + tls_stream_set_hostname(sock, estring_view(host).extract_c_str()); } else { tcpsock->timeout(timeout); sock = tcpsock->connect(ep); diff --git a/net/http/server.cpp b/net/http/server.cpp index a2818f0b..ec1c6f6f 100644 --- a/net/http/server.cpp +++ b/net/http/server.cpp @@ -196,24 +196,25 @@ class FsHandler : public HTTPHandler { DEFER(LOG_DEBUG("leave fs handler")); auto target = req.target(); auto pos = target.find("?"); - std::string query; + estring_view query; if (pos != std::string_view::npos) { - query = std::string(target.substr(pos + 1)); + query = target.substr(pos + 1); target = target.substr(0, pos); } - estring filename(target); + estring_view filename(target); if (!prefix.empty()) filename = filename.substr(prefix.size() - 1); LOG_DEBUG(VALUE(filename)); - auto file = m_fs->open(filename.c_str(), O_RDONLY); + auto file = m_fs->open(filename.extract_c_str(), O_RDONLY); if (!file) { failed_resp(resp); LOG_ERROR_RETURN(0, 0, "open file ` failed", target); } DEFER(delete file); - if (!query.empty()) file->ioctl(fs::HTTP_URL_PARAM, query.c_str()); + if (!query.empty()) + file->ioctl(fs::HTTP_URL_PARAM, (const char*) query.extract_c_str()); struct stat buf; if (file->fstat(&buf) < 0) {