diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-07-27 21:22:25 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-07-27 21:22:25 +0100 |
commit | 95ae6c6a81d6ee204975247687308446ff89064d (patch) | |
tree | 1368df5916d6e2cb0d6fcf892a746dfdda67172c | |
parent | PathElements can be string_views (diff) | |
download | icespider-95ae6c6a81d6ee204975247687308446ff89064d.tar.bz2 icespider-95ae6c6a81d6ee204975247687308446ff89064d.tar.xz icespider-95ae6c6a81d6ee204975247687308446ff89064d.zip |
Remove magic number and string resize hack
Replace with a std::array sized according to the example date string.
-rw-r--r-- | icespider/core/ihttpRequest.cpp | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/icespider/core/ihttpRequest.cpp b/icespider/core/ihttpRequest.cpp index ed7fce6..e396b5a 100644 --- a/icespider/core/ihttpRequest.cpp +++ b/icespider/core/ihttpRequest.cpp @@ -156,7 +156,6 @@ namespace IceSpider { } // Set-Cookie: value[; expires=date][; domain=domain][; path=path][; secure] - // Sat, 02 May 2009 23:38:25 GMT void IHttpRequest::setCookie(const std::string_view & name, const std::string_view & value, const OptionalString & d, const OptionalString & p, bool s, std::optional<time_t> e) @@ -166,13 +165,13 @@ namespace IceSpider { o << '='; XWwwFormUrlEncoded::urlencodeto(o, value.begin(), value.end()); if (e) { - std::string buf(45, 0); - - struct tm tm { }; + static constexpr auto dateLength = std::string_view {"Sat, 02 May 2009 23:38:25 GMT"}.length() + 1; + std::array<char, dateLength> buf {}; + tm tm {}; gmtime_r(&*e, &tm); - buf.resize(strftime(buf.data(), buf.length(), "; expires=%a, %d %b %Y %T %Z", &tm)); - o << buf; + const auto len = strftime(buf.data(), buf.size(), "%a, %d %b %Y %T %Z", &tm); + o << "; expires=" << std::string_view {buf.data(), len}; } if (d) { "; domain=%?"_fmt(o, *d); |