Skip to content

Commit

Permalink
extract c_str for estring_view
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuiba committed Dec 19, 2024
1 parent 8cf7203 commit dcb245b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
37 changes: 37 additions & 0 deletions common/estring.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
#include <limits>
#include <string>
#include <bitset>
#include <memory>
#include <tuple>
#include <type_traits>

Expand Down Expand Up @@ -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<size_t N>
class Extraction {
char _buf[N];
std::unique_ptr<char> _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<size_t N = 256>
Extraction<N> extract_c_str() const {
return {*this};
}
operator std::string ()
{
return to_string();
Expand Down
2 changes: 1 addition & 1 deletion net/http/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions net/http/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dcb245b

Please sign in to comment.