Skip to content

Commit

Permalink
Fix compilation warning of sprintf function (drogonframework#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
an-tao authored Aug 14, 2020
1 parent 3a10db9 commit c4d727c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/src/HttpRequestImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void HttpRequestImpl::appendToBuffer(trantor::MsgBuffer *output) const
char buf[64];
auto len = snprintf(buf,
sizeof(buf),
"Content-Length: %lu\r\n",
contentLengthFormatString<decltype(content.length())>(),
content.length() + content_.length());
output->append(buf, len);
if (contentTypeString_.empty())
Expand Down
5 changes: 3 additions & 2 deletions lib/src/HttpResponseImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ using namespace drogon;

namespace drogon
{

// "Fri, 23 Aug 2019 12:58:03 GMT" length = 29
static const size_t httpFullDateStringLength = 29;
static inline void doResponseCreateAdvices(
Expand Down Expand Up @@ -272,7 +273,7 @@ void HttpResponseImpl::makeHeaderString(trantor::MsgBuffer &buffer)
auto bodyLength = bodyPtr_ ? bodyPtr_->length() : 0;
len = snprintf(buffer.beginWrite(),
buffer.writableBytes(),
"Content-Length: %lu\r\n",
contentLengthFormatString<decltype(bodyLength)>(),
bodyLength);
}
else
Expand All @@ -285,7 +286,7 @@ void HttpResponseImpl::makeHeaderString(trantor::MsgBuffer &buffer)
}
len = snprintf(buffer.beginWrite(),
buffer.writableBytes(),
"Content-Length: %lld\r\n",
contentLengthFormatString<decltype(filestat.st_size)>(),
filestat.st_size);
}
buffer.hasWritten(len);
Expand Down
30 changes: 30 additions & 0 deletions lib/src/HttpUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,34 @@ ContentType parseContentType(const string_view &contentType);
const string_view &webContentTypeToString(ContentType contenttype);
const string_view &statusCodeToString(int code);
ContentType getContentType(const std::string &fileName);
template <typename T>
inline constexpr const char *contentLengthFormatString()
{
return "Content-Length: %d\r\n";
}
template <>
inline constexpr const char *contentLengthFormatString<unsigned int>()
{
return "Content-Length: %u\r\n";
}
template <>
inline constexpr const char *contentLengthFormatString<long>()
{
return "Content-Length: %ld\r\n";
}
template <>
inline constexpr const char *contentLengthFormatString<unsigned long>()
{
return "Content-Length: %lu\r\n";
}
template <>
inline constexpr const char *contentLengthFormatString<long long>()
{
return "Content-Length: %lld\r\n";
}
template <>
inline constexpr const char *contentLengthFormatString<unsigned long long>()
{
return "Content-Length: %llu\r\n";
}
} // namespace drogon

0 comments on commit c4d727c

Please sign in to comment.