Also adjusted Cache-Control APIs to prevent similar bugs. These changes
also speed up processing a bit and simplify most of the affected code.
The now-gone "just remove the old CC" putCc() misfeature was unused.
The leak was introduced by commit
92a5adb: PutCommonResponseHeaders()
incorrectly assumed that putCc(pointerToX) takes ownership of X.
Detected by Coverity. CID
1534779: Resource leak (RESOURCE_LEAK).
}
void
-HttpHeader::putCc(const HttpHdrCc * cc)
+HttpHeader::putCc(const HttpHdrCc &cc)
{
- assert(cc);
/* remove old directives if any */
delById(Http::HdrType::CACHE_CONTROL);
/* pack into mb */
MemBuf mb;
mb.init();
- cc->packInto(&mb);
+ cc.packInto(&mb);
/* put */
addEntry(new HttpHeaderEntry(Http::HdrType::CACHE_CONTROL, SBuf(), mb.buf));
/* cleanup */
void putTime(Http::HdrType id, time_t htime);
void putStr(Http::HdrType id, const char *str);
void putAuth(const char *auth_scheme, const char *realm);
- void putCc(const HttpHdrCc * cc);
+ void putCc(const HttpHdrCc &cc);
void putContRange(const HttpHdrContRange * cr);
void putRange(const HttpHdrRange * range);
void putSc(HttpHdrSc *sc);
rv->header.addEntry(e->clone());
}
- rv->putCc(cache_control);
+ if (cache_control)
+ rv->putCc(*cache_control);
/* rv->body */
return rv;
response.header.putExt("Access-Control-Expose-Headers", "Server");
}
- std::unique_ptr<HttpHdrCc> cc(new HttpHdrCc());
+ HttpHdrCc cc;
// this is honored by more caches but allows pointless revalidation;
// revalidation will always fail because we do not support it (yet?)
- cc->noCache(String());
+ cc.noCache(String());
// this is honored by fewer caches but prohibits pointless revalidation
- cc->noStore(true);
- response.putCc(cc.release());
+ cc.noStore(true);
+ response.putCc(cc);
}
CacheManager*
{
HttpHdrCc cc;
cc.Private(String());
- header.putCc(&cc);
+ header.putCc(cc);
}
if (ftpStatus > 0)
header.putInt(Http::HdrType::FTP_STATUS, ftpStatus);
if (flags.only_if_cached)
cc->onlyIfCached(true);
- hdr_out->putCc(cc);
+ hdr_out->putCc(*cc);
delete cc;
}
}
void
-Http::Message::putCc(const HttpHdrCc *otherCc)
+Http::Message::putCc(const HttpHdrCc &otherCc)
{
- // get rid of the old CC, if any
- if (cache_control) {
- delete cache_control;
- cache_control = nullptr;
- if (!otherCc)
- header.delById(Http::HdrType::CACHE_CONTROL);
- // else it will be deleted inside putCc() below
- }
-
- // add new CC, if any
- if (otherCc) {
- cache_control = new HttpHdrCc(*otherCc);
- header.putCc(cache_control);
- }
+ delete cache_control;
+ cache_control = new HttpHdrCc(otherCc);
+ header.putCc(*cache_control);
}
/* find first CRLF */
uint32_t sources = 0; ///< The message sources
- /// copies Cache-Control header to this message
- void putCc(const HttpHdrCc *otherCc);
+ /// copies Cache-Control header to this message,
+ /// overwriting existing Cache-Control header(s), if any
+ void putCc(const HttpHdrCc &);
// returns true and sets hdr_sz on success
// returns false and sets *error to zero when needs more data
reply->setHeaders(status, nullptr, mimeGetContentType(icon_.c_str()), sb.st_size, sb.st_mtime, -1);
reply->cache_control = new HttpHdrCc();
reply->cache_control->maxAge(86400);
- reply->header.putCc(reply->cache_control);
+ reply->header.putCc(*reply->cache_control);
e->replaceHttpReply(reply.getRaw());
if (status == Http::scOkay) {
void HttpHeader::putTime(Http::HdrType, time_t) STUB
void HttpHeader::putStr(Http::HdrType, const char *) STUB
void HttpHeader::putAuth(const char *, const char *) STUB
-void HttpHeader::putCc(const HttpHdrCc *) STUB
+void HttpHeader::putCc(const HttpHdrCc &) STUB
void HttpHeader::putContRange(const HttpHdrContRange *) STUB
void HttpHeader::putRange(const HttpHdrRange *) STUB
void HttpHeader::putSc(HttpHdrSc *) STUB
void Message::packInto(Packable *, bool) const STUB
void Message::setContentLength(int64_t) STUB
bool Message::persistent() const STUB_RETVAL(false)
-void Message::putCc(const HttpHdrCc *) STUB
+void Message::putCc(const HttpHdrCc &) STUB
bool Message::parse(const char *, const size_t, bool, Http::StatusCode *) STUB_RETVAL(false)
bool Message::parseCharBuf(const char *, ssize_t) STUB_RETVAL(false)
int Message::httpMsgParseStep(const char *, int, int) STUB_RETVAL(-1)