Skip to content

Commit 597e15c

Browse files
authored
Add per-request compression control to HttpResponse (#2451)
1 parent 3495522 commit 597e15c

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

lib/inc/drogon/HttpResponse.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ class DROGON_EXPORT HttpResponse
161161
setCustomStatusCode(code, message.data(), message.length());
162162
}
163163

164+
/// Set whether the response should be compress.
165+
virtual void setAllowCompression(bool allow) = 0;
166+
167+
/// Get whether the response allow compression.
168+
virtual bool allowCompression() const = 0;
169+
164170
/// Get the creation timestamp of the response.
165171
virtual const trantor::Date &creationDate() const = 0;
166172

@@ -560,9 +566,9 @@ class DROGON_EXPORT HttpResponse
560566
virtual const std::string &sendfileName() const = 0;
561567

562568
/**
563-
* @brief Returns the range of the file response as a pair ot size_t
569+
* @brief Returns the range of the file response as a pair of size_t
564570
* (offset, length). Length of 0 means the entire file is sent. Behavior of
565-
* this function is undefined if the response if not a file response
571+
* this function is undefined if the response is not a file response
566572
*/
567573
using SendfileRange = std::pair<size_t, size_t>; // { offset, length }
568574
virtual const SendfileRange &sendfileRange() const = 0;

lib/src/HttpResponseImpl.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ static inline HttpResponsePtr genHttpResponse(const std::string &viewName,
5454
}
5555
} // namespace drogon
5656

57+
void HttpResponseImpl::setAllowCompression(bool allow)
58+
{
59+
allowCompression_ = allow;
60+
}
61+
62+
bool HttpResponseImpl::allowCompression() const
63+
{
64+
return allowCompression_;
65+
}
66+
5767
HttpResponsePtr HttpResponse::newHttpResponse()
5868
{
5969
auto res = std::make_shared<HttpResponseImpl>(k200OK, CT_TEXT_HTML);
@@ -960,6 +970,12 @@ void HttpResponseImpl::parseJson() const
960970

961971
bool HttpResponseImpl::shouldBeCompressed() const
962972
{
973+
// If the developer said "No" stop immediately.
974+
if (!allowCompression_)
975+
{
976+
return false;
977+
}
978+
963979
if (streamCallback_ || asyncStreamCallback_ || !sendfileName_.empty() ||
964980
contentType() >= CT_APPLICATION_OCTET_STREAM ||
965981
getBody().length() < 1024 ||

lib/src/HttpResponseImpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@ class DROGON_EXPORT HttpResponseImpl : public HttpResponse
463463
}
464464

465465
private:
466+
bool allowCompression_{true};
467+
468+
void setAllowCompression(bool allow) override;
469+
470+
bool allowCompression() const override;
471+
466472
void setBody(const char *body, size_t len) override
467473
{
468474
bodyPtr_ = std::make_shared<HttpMessageStringViewBody>(body, len);

0 commit comments

Comments
 (0)