From 95ae6c6a81d6ee204975247687308446ff89064d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 27 Jul 2023 21:22:25 +0100 Subject: Remove magic number and string resize hack Replace with a std::array sized according to the example date string. --- icespider/core/ihttpRequest.cpp | 11 +++++------ 1 file 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 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 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); -- cgit v1.2.3