]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpReply.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / HttpReply.cc
CommitLineData
cb69b4c7 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
e25c139f 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
cb69b4c7 7 */
8
bbc27441
AJ
9/* DEBUG: section 58 HTTP Reply (Response) */
10
582c2af2 11#include "squid.h"
1328cfb7 12#include "acl/AclSizeLimit.h"
582c2af2 13#include "acl/FilledChecklist.h"
81ab22b6 14#include "base/EnumIterator.h"
582c2af2 15#include "globals.h"
4f1c93a7 16#include "http/ContentLengthInterpreter.h"
0521f8be 17#include "HttpBody.h"
7ebe76de 18#include "HttpHdrCc.h"
582c2af2 19#include "HttpHdrContRange.h"
25b6a907 20#include "HttpHdrSc.h"
582c2af2 21#include "HttpReply.h"
0667cbfb 22#include "HttpRequest.h"
0eb49b6d 23#include "MemBuf.h"
4d5904f7 24#include "SquidConfig.h"
582c2af2
FC
25#include "SquidTime.h"
26#include "Store.h"
28204b3b 27#include "StrList.h"
cb69b4c7 28
8341f96d
EB
29HttpReply::HttpReply():
30 Http::Message(hoReply),
31 date(0),
32 last_modified(0),
33 expires(0),
34 surrogate_control(nullptr),
35 keep_alive(0),
36 protoPrefix("HTTP/"),
37 bodySizeMax(-2),
38 content_range(nullptr)
cb69b4c7 39{
06a5ae20 40 init();
cb69b4c7 41}
42
06a5ae20 43HttpReply::~HttpReply()
cb69b4c7 44{
06a5ae20 45 if (do_clean)
46 clean();
cb69b4c7 47}
48
49void
06a5ae20 50HttpReply::init()
cb69b4c7 51{
06a5ae20 52 hdrCacheInit();
9b769c67 53 sline.init();
fb654382 54 pstate = Http::Message::psReadyToParseStartLine;
06a5ae20 55 do_clean = true;
cb69b4c7 56}
57
06a5ae20 58void HttpReply::reset()
cb69b4c7 59{
06a5ae20 60
8596962e 61 // reset should not reset the protocol; could have made protoPrefix a
62 // virtual function instead, but it is not clear whether virtual methods
63 // are allowed with MEMPROXY_CLASS() and whether some cbdata void*
64 // conversions are not going to kill virtual tables
30abd221 65 const String pfx = protoPrefix;
06a5ae20 66 clean();
67 init();
68 protoPrefix = pfx;
69}
70
71void
72HttpReply::clean()
73{
26ac0430 74 // we used to assert that the pipe is NULL, but now the message only
5f8252d2 75 // points to a pipe that is owned and initiated by another object.
76 body_pipe = NULL;
77
0521f8be 78 body.clear();
06a5ae20 79 hdrCacheClean();
519e0948 80 header.clean();
9b769c67 81 sline.clean();
0667cbfb 82 bodySizeMax = -2; // hack: make calculatedBodySizeMax() false
cb69b4c7 83}
84
cb69b4c7 85void
f8432432 86HttpReply::packHeadersUsingFastPacker(Packable &p) const
cb69b4c7 87{
f8432432
EB
88 sline.packInto(&p);
89 header.packInto(&p);
90 p.append("\r\n", 2);
528b2c61 91}
92
93void
f8432432 94HttpReply::packHeadersUsingSlowPacker(Packable &p) const
528b2c61 95{
f8432432
EB
96 MemBuf buf;
97 buf.init();
98 packHeadersUsingFastPacker(buf);
99 p.append(buf.content(), buf.contentSize());
100}
101
102void
103HttpReply::packInto(MemBuf &buf) const
104{
105 packHeadersUsingFastPacker(buf);
106 body.packInto(&buf);
cb69b4c7 107}
108
06a5ae20 109/* create memBuf, create mem-based packer, pack, destroy packer, return MemBuf */
032785bf 110MemBuf *
1f28a150 111HttpReply::pack() const
cb69b4c7 112{
032785bf 113 MemBuf *mb = new MemBuf;
2fe7eff9 114 mb->init();
f8432432 115 packInto(*mb);
cb69b4c7 116 return mb;
117}
118
ea55799d
EB
119HttpReplyPointer
120HttpReply::MakeConnectionEstablished() {
121
122 HttpReplyPointer rep(new HttpReply);
123 rep->sline.set(Http::ProtocolVersion(), Http::scOkay, "Connection established");
124 return rep;
125}
126
66d51f4f 127HttpReplyPointer
11992b6f 128HttpReply::make304() const
cb69b4c7 129{
789217a2 130 static const Http::HdrType ImsEntries[] = {Http::HdrType::DATE, Http::HdrType::CONTENT_TYPE, Http::HdrType::EXPIRES, Http::HdrType::LAST_MODIFIED, /* eof */ Http::HdrType::OTHER};
62e76326 131
66d51f4f 132 const HttpReplyPointer rv(new HttpReply);
728da2ee 133 int t;
de336bbe 134 HttpHeaderEntry *e;
cb69b4c7 135
528b2c61 136 /* rv->content_length; */
06a5ae20 137 rv->date = date;
138 rv->last_modified = last_modified;
139 rv->expires = expires;
140 rv->content_type = content_type;
528b2c61 141 /* rv->content_range */
142 /* rv->keep_alive */
2592bc70 143 rv->sline.set(Http::ProtocolVersion(), Http::scNotModified, NULL);
62e76326 144
81ab22b6 145 for (t = 0; ImsEntries[t] != Http::HdrType::OTHER; ++t) {
a9925b40 146 if ((e = header.findEntry(ImsEntries[t])))
eede25e7 147 rv->header.addEntry(e->clone());
81ab22b6 148 }
62e76326 149
0c90d3b1
DD
150 rv->putCc(cache_control);
151
528b2c61 152 /* rv->body */
153 return rv;
154}
155
032785bf 156MemBuf *
1f28a150 157HttpReply::packed304Reply() const
528b2c61 158{
159 /* Not as efficient as skipping the header duplication,
160 * but easier to maintain
161 */
66d51f4f 162 const auto temp = make304();
06a5ae20 163 MemBuf *rv = temp->pack();
528b2c61 164 return rv;
cb69b4c7 165}
166
167void
955394ce 168HttpReply::setHeaders(Http::StatusCode status, const char *reason,
350e2aec 169 const char *ctype, int64_t clen, time_t lmt, time_t expiresTime)
cb69b4c7 170{
171 HttpHeader *hdr;
2592bc70 172 sline.set(Http::ProtocolVersion(), status, reason);
06a5ae20 173 hdr = &header;
789217a2
FC
174 hdr->putStr(Http::HdrType::SERVER, visible_appname_string);
175 hdr->putStr(Http::HdrType::MIME_VERSION, "1.0");
176 hdr->putTime(Http::HdrType::DATE, squid_curtime);
62e76326 177
d8b249ef 178 if (ctype) {
789217a2 179 hdr->putStr(Http::HdrType::CONTENT_TYPE, ctype);
06a5ae20 180 content_type = ctype;
d8b249ef 181 } else
30abd221 182 content_type = String();
62e76326 183
de336bbe 184 if (clen >= 0)
789217a2 185 hdr->putInt64(Http::HdrType::CONTENT_LENGTH, clen);
62e76326 186
350e2aec 187 if (expiresTime >= 0)
789217a2 188 hdr->putTime(Http::HdrType::EXPIRES, expiresTime);
62e76326 189
f53969cc 190 if (lmt > 0) /* this used to be lmt != 0 @?@ */
789217a2 191 hdr->putTime(Http::HdrType::LAST_MODIFIED, lmt);
62e76326 192
06a5ae20 193 date = squid_curtime;
62e76326 194
06a5ae20 195 content_length = clen;
62e76326 196
350e2aec 197 expires = expiresTime;
62e76326 198
06a5ae20 199 last_modified = lmt;
cb69b4c7 200}
201
6d38ef86 202void
955394ce 203HttpReply::redirect(Http::StatusCode status, const char *loc)
6d38ef86 204{
205 HttpHeader *hdr;
2592bc70 206 sline.set(Http::ProtocolVersion(), status, NULL);
06a5ae20 207 hdr = &header;
789217a2
FC
208 hdr->putStr(Http::HdrType::SERVER, APP_FULLNAME);
209 hdr->putTime(Http::HdrType::DATE, squid_curtime);
210 hdr->putInt64(Http::HdrType::CONTENT_LENGTH, 0);
211 hdr->putStr(Http::HdrType::LOCATION, loc);
06a5ae20 212 date = squid_curtime;
213 content_length = 0;
6d38ef86 214}
215
528b2c61 216/* compare the validators of two replies.
217 * 1 = they match
218 * 0 = they do not match
219 */
220int
06a5ae20 221HttpReply::validatorsMatch(HttpReply const * otherRep) const
62e76326 222{
30abd221 223 String one,two;
06a5ae20 224 assert (otherRep);
528b2c61 225 /* Numbers first - easiest to check */
226 /* Content-Length */
227 /* TODO: remove -1 bypass */
62e76326 228
06a5ae20 229 if (content_length != otherRep->content_length
230 && content_length > -1 &&
62e76326 231 otherRep->content_length > -1)
232 return 0;
233
528b2c61 234 /* ETag */
789217a2 235 one = header.getStrOrList(Http::HdrType::ETAG);
62e76326 236
789217a2 237 two = otherRep->header.getStrOrList(Http::HdrType::ETAG);
62e76326 238
a1377698 239 if (one.size()==0 || two.size()==0 || one.caseCmp(two)!=0 ) {
30abd221 240 one.clean();
241 two.clean();
62e76326 242 return 0;
528b2c61 243 }
62e76326 244
06a5ae20 245 if (last_modified != otherRep->last_modified)
62e76326 246 return 0;
247
528b2c61 248 /* MD5 */
789217a2 249 one = header.getStrOrList(Http::HdrType::CONTENT_MD5);
62e76326 250
789217a2 251 two = otherRep->header.getStrOrList(Http::HdrType::CONTENT_MD5);
62e76326 252
a1377698 253 if (one.size()==0 || two.size()==0 || one.caseCmp(two)!=0 ) {
30abd221 254 one.clean();
255 two.clean();
62e76326 256 return 0;
528b2c61 257 }
62e76326 258
528b2c61 259 return 1;
260}
261
66d51f4f
AR
262HttpReply::Pointer
263HttpReply::recreateOnNotModified(const HttpReply &reply304) const
cb69b4c7 264{
66d51f4f
AR
265 // If enough 304s do not update, then this expensive checking is cheaper
266 // than blindly storing reply prefix identical to the already stored one.
267 if (!header.needUpdate(&reply304.header))
268 return nullptr;
269
270 const Pointer cloned = clone();
271 cloned->header.update(&reply304.header);
272 cloned->hdrCacheClean();
273 cloned->header.compact();
274 cloned->hdrCacheInit();
275 return cloned;
cb69b4c7 276}
277
d8b249ef 278/* internal routines */
cb69b4c7 279
06a5ae20 280time_t
281HttpReply::hdrExpirationTime()
d20b1cd0 282{
283 /* The s-maxage and max-age directive takes priority over Expires */
62e76326 284
06a5ae20 285 if (cache_control) {
810d879f
EB
286 int maxAge = -1;
287 /*
288 * Conservatively handle the case when we have a max-age
289 * header, but no Date for reference?
290 */
291 if (cache_control->hasSMaxAge(&maxAge) || cache_control->hasMaxAge(&maxAge))
292 return (date >= 0) ? date + maxAge : squid_curtime;
d20b1cd0 293 }
62e76326 294
f66a9ef4 295 if (Config.onoff.vary_ignore_expire &&
789217a2
FC
296 header.has(Http::HdrType::VARY)) {
297 const time_t d = header.getTime(Http::HdrType::DATE);
298 const time_t e = header.getTime(Http::HdrType::EXPIRES);
62e76326 299
300 if (d == e)
301 return -1;
f66a9ef4 302 }
62e76326 303
789217a2
FC
304 if (header.has(Http::HdrType::EXPIRES)) {
305 const time_t e = header.getTime(Http::HdrType::EXPIRES);
62e76326 306 /*
307 * HTTP/1.0 says that robust implementations should consider
308 * bad or malformed Expires header as equivalent to "expires
309 * immediately."
310 */
311 return e < 0 ? squid_curtime : e;
d20b1cd0 312 }
62e76326 313
d20b1cd0 314 return -1;
315}
316
d8b249ef 317/* sync this routine when you update HttpReply struct */
8596962e 318void
07947ad8 319HttpReply::hdrCacheInit()
cb69b4c7 320{
63df1d28 321 Http::Message::hdrCacheInit();
07947ad8 322
4a1acc56 323 http_ver = sline.version;
789217a2
FC
324 content_length = header.getInt64(Http::HdrType::CONTENT_LENGTH);
325 date = header.getTime(Http::HdrType::DATE);
326 last_modified = header.getTime(Http::HdrType::LAST_MODIFIED);
a9925b40 327 surrogate_control = header.getSc();
8341f96d
EB
328 content_range = (sline.status() == Http::scPartialContent) ?
329 header.getContRange() : nullptr;
4a1acc56 330 keep_alive = persistent() ? 1 : 0;
789217a2 331 const char *str = header.getStr(Http::HdrType::CONTENT_TYPE);
62e76326 332
d8b249ef 333 if (str)
2fe0439c 334 content_type.assign(str, strcspn(str, ";\t "));
d8b249ef 335 else
30abd221 336 content_type = String();
62e76326 337
d20b1cd0 338 /* be sure to set expires after date and cache-control */
06a5ae20 339 expires = hdrExpirationTime();
cb69b4c7 340}
341
8341f96d
EB
342const HttpHdrContRange *
343HttpReply::contentRange() const
344{
345 assert(!content_range || sline.status() == Http::scPartialContent);
346 return content_range;
347}
348
d8b249ef 349/* sync this routine when you update HttpReply struct */
06a5ae20 350void
351HttpReply::hdrCacheClean()
2ac76861 352{
30abd221 353 content_type.clean();
62e76326 354
06a5ae20 355 if (cache_control) {
3d7782c1 356 delete cache_control;
06a5ae20 357 cache_control = NULL;
07947ad8 358 }
62e76326 359
06a5ae20 360 if (surrogate_control) {
45a58345 361 delete surrogate_control;
06a5ae20 362 surrogate_control = NULL;
07947ad8 363 }
43ae1d95 364
06a5ae20 365 if (content_range) {
3c670b50 366 delete content_range;
06a5ae20 367 content_range = NULL;
07947ad8 368 }
63259c34 369}
cb69b4c7 370
35282fbf 371/*
372 * Returns the body size of a HTTP response
373 */
47f6e231 374int64_t
60745f24 375HttpReply::bodySize(const HttpRequestMethod& method) const
35282fbf 376{
06a5ae20 377 if (sline.version.major < 1)
1bda350e 378 return -1;
c2a7cefd 379 else if (method.id() == Http::METHOD_HEAD)
62e76326 380 return 0;
9b769c67 381 else if (sline.status() == Http::scOkay)
f53969cc 382 (void) 0; /* common case, continue */
9b769c67 383 else if (sline.status() == Http::scNoContent)
62e76326 384 return 0;
9b769c67 385 else if (sline.status() == Http::scNotModified)
62e76326 386 return 0;
9b769c67 387 else if (sline.status() < Http::scOkay)
62e76326 388 return 0;
389
06a5ae20 390 return content_length;
35282fbf 391}
8596962e 392
96ee497f
AJ
393/**
394 * Checks the first line of an HTTP Reply is valid.
395 * currently only checks "HTTP/" exists.
396 *
397 * NP: not all error cases are detected yet. Some are left for detection later in parse.
398 */
399bool
84ae6223 400HttpReply::sanityCheckStartLine(const char *buf, const size_t hdr_len, Http::StatusCode *error)
8596962e 401{
96ee497f
AJ
402 // hack warning: using psize instead of size here due to type mismatches with MemBuf.
403
404 // content is long enough to possibly hold a reply
405 // 4 being magic size of a 3-digit number plus space delimiter
84ae6223 406 if (hdr_len < (size_t)(protoPrefix.psize() + 4)) {
0246f6b8 407 if (hdr_len > 0) {
84ae6223 408 debugs(58, 3, "Too small reply header (" << hdr_len << " bytes)");
955394ce 409 *error = Http::scInvalidHeader;
0246f6b8 410 }
96ee497f
AJ
411 return false;
412 }
413
e77d7ef0 414 int pos;
96ee497f 415 // catch missing or mismatched protocol identifier
e77d7ef0 416 // allow special-case for ICY protocol (non-HTTP identifier) in response to faked HTTP request.
84ae6223 417 if (strncmp(buf, "ICY", 3) == 0) {
e77d7ef0
AJ
418 protoPrefix = "ICY";
419 pos = protoPrefix.psize();
dd20bfd3 420 } else {
8596962e 421
84ae6223
AJ
422 if (protoPrefix.cmp(buf, protoPrefix.size()) != 0) {
423 debugs(58, 3, "missing protocol prefix (" << protoPrefix << ") in '" << buf << "'");
955394ce 424 *error = Http::scInvalidHeader;
e77d7ef0
AJ
425 return false;
426 }
96ee497f 427
e77d7ef0
AJ
428 // catch missing or negative status value (negative '-' is not a digit)
429 pos = protoPrefix.psize();
dd20bfd3 430
e77d7ef0 431 // skip arbitrary number of digits and a dot in the verion portion
84ae6223 432 while ((size_t)pos <= hdr_len && (*(buf+pos) == '.' || xisdigit(*(buf+pos)) ) ) ++pos;
96ee497f 433
e77d7ef0
AJ
434 // catch missing version info
435 if (pos == protoPrefix.psize()) {
84ae6223 436 debugs(58, 3, "missing protocol version numbers (ie. " << protoPrefix << "/1.0) in '" << buf << "'");
955394ce 437 *error = Http::scInvalidHeader;
e77d7ef0
AJ
438 return false;
439 }
96ee497f
AJ
440 }
441
442 // skip arbitrary number of spaces...
84ae6223 443 while ((size_t)pos <= hdr_len && (char)*(buf+pos) == ' ') ++pos;
96ee497f 444
84ae6223
AJ
445 if ((size_t)pos < hdr_len && !xisdigit(*(buf+pos))) {
446 debugs(58, 3, "missing or invalid status number in '" << buf << "'");
955394ce 447 *error = Http::scInvalidHeader;
96ee497f
AJ
448 return false;
449 }
450
8596962e 451 return true;
452}
453
9b769c67
AJ
454bool
455HttpReply::parseFirstLine(const char *blk_start, const char *blk_end)
429f7150 456{
9b769c67 457 return sline.parse(protoPrefix, blk_start, blk_end);
429f7150 458}
5c09dcb8 459
4f1c93a7
EB
460void
461HttpReply::configureContentLengthInterpreter(Http::ContentLengthInterpreter &interpreter)
462{
463 interpreter.applyStatusCodeRules(sline.status());
464}
465
466bool
467HttpReply::parseHeader(Http1::Parser &hp)
468{
469 Http::ContentLengthInterpreter clen;
470 return Message::parseHeader(hp, clen);
471}
472
fb525683 473/* handy: resets and returns -1 */
474int
475HttpReply::httpMsgParseError()
476{
63df1d28 477 int result(Http::Message::httpMsgParseError());
fb525683 478 /* indicate an error in the status line */
2592bc70 479 sline.set(Http::ProtocolVersion(), Http::scInvalidHeader);
fb525683 480 return result;
481}
482
5c09dcb8 483/*
484 * Indicate whether or not we would usually expect an entity-body
485 * along with this response
486 */
487bool
60745f24 488HttpReply::expectingBody(const HttpRequestMethod& req_method, int64_t& theSize) const
5c09dcb8 489{
490 bool expectBody = true;
491
c2a7cefd 492 if (req_method == Http::METHOD_HEAD)
5c09dcb8 493 expectBody = false;
9b769c67 494 else if (sline.status() == Http::scNoContent)
5c09dcb8 495 expectBody = false;
9b769c67 496 else if (sline.status() == Http::scNotModified)
5c09dcb8 497 expectBody = false;
26e3e640 498 // TODO: Consider assuming that gray-area 0xx responses have bodies, like 9xx responses.
9b769c67 499 else if (sline.status() < Http::scOkay)
5c09dcb8 500 expectBody = false;
501 else
502 expectBody = true;
503
504 if (expectBody) {
c3d0ba0c 505 if (header.chunked())
5c09dcb8 506 theSize = -1;
507 else if (content_length >= 0)
508 theSize = content_length;
509 else
510 theSize = -1;
511 }
512
513 return expectBody;
514}
0667cbfb 515
516bool
517HttpReply::receivedBodyTooLarge(HttpRequest& request, int64_t receivedSize)
518{
519 calcMaxBodySize(request);
520 debugs(58, 3, HERE << receivedSize << " >? " << bodySizeMax);
521 return bodySizeMax >= 0 && receivedSize > bodySizeMax;
522}
523
524bool
525HttpReply::expectedBodyTooLarge(HttpRequest& request)
526{
527 calcMaxBodySize(request);
528 debugs(58, 7, HERE << "bodySizeMax=" << bodySizeMax);
529
530 if (bodySizeMax < 0) // no body size limit
531 return false;
532
533 int64_t expectedSize = -1;
534 if (!expectingBody(request.method, expectedSize))
535 return false;
26ac0430 536
0667cbfb 537 debugs(58, 6, HERE << expectedSize << " >? " << bodySizeMax);
538
539 if (expectedSize < 0) // expecting body of an unknown length
540 return false;
541
542 return expectedSize > bodySizeMax;
543}
544
545void
b248c2a3 546HttpReply::calcMaxBodySize(HttpRequest& request) const
0667cbfb 547{
548 // hack: -2 is used as "we have not calculated max body size yet" state
549 if (bodySizeMax != -2) // already tried
550 return;
551 bodySizeMax = -1;
552
4194f58d
AJ
553 // short-circuit ACL testing if there are none configured
554 if (!Config.ReplyBodySize)
555 return;
556
c0941a6a 557 ACLFilledChecklist ch(NULL, &request, NULL);
b248c2a3
AJ
558 // XXX: cont-cast becomes irrelevant when checklist is HttpReply::Pointer
559 ch.reply = const_cast<HttpReply *>(this);
560 HTTPMSGLOCK(ch.reply);
1328cfb7 561 for (AclSizeLimit *l = Config.ReplyBodySize; l; l = l -> next) {
b50e327b 562 /* if there is no ACL list or if the ACLs listed match use this size value */
06bf5384 563 if (!l->aclList || ch.fastCheck(l->aclList).allowed()) {
0667cbfb 564 debugs(58, 4, HERE << "bodySizeMax=" << bodySizeMax);
565 bodySizeMax = l->size; // may be -1
566 break;
567 }
568 }
569}
da33c835 570
fa0e6114 571// XXX: check that this is sufficient for eCAP cloning
da33c835
HN
572HttpReply *
573HttpReply::clone() const
574{
575 HttpReply *rep = new HttpReply();
66363092 576 rep->sline = sline; // used in hdrCacheInit() call below
da33c835
HN
577 rep->header.append(&header);
578 rep->hdrCacheInit();
579 rep->hdr_sz = hdr_sz;
f230832f
HN
580 rep->http_ver = http_ver;
581 rep->pstate = pstate;
fa0e6114
AR
582 rep->body_pipe = body_pipe;
583
66363092 584 // keep_alive is handled in hdrCacheInit()
da33c835
HN
585 return rep;
586}
d67acb4e 587
63df1d28
AJ
588bool
589HttpReply::inheritProperties(const Http::Message *aMsg)
d67acb4e
AJ
590{
591 const HttpReply *aRep = dynamic_cast<const HttpReply*>(aMsg);
26ac0430
AJ
592 if (!aRep)
593 return false;
d67acb4e 594 keep_alive = aRep->keep_alive;
88df846b 595 sources = aRep->sources;
d67acb4e
AJ
596 return true;
597}
c679653d
AR
598
599void HttpReply::removeStaleWarnings()
600{
601 String warning;
789217a2 602 if (header.getList(Http::HdrType::WARNING, &warning)) {
c679653d
AR
603 const String newWarning = removeStaleWarningValues(warning);
604 if (warning.size() && warning.size() == newWarning.size())
605 return; // some warnings are there and none changed
789217a2 606 header.delById(Http::HdrType::WARNING);
c679653d
AR
607 if (newWarning.size()) { // some warnings left
608 HttpHeaderEntry *const e =
d5f18517 609 new HttpHeaderEntry(Http::HdrType::WARNING, SBuf(), newWarning.termedBuf());
c679653d
AR
610 header.addEntry(e);
611 }
612 }
613}
614
615/**
616 * Remove warning-values with warn-date different from Date value from
617 * a single header entry. Returns a string with all valid warning-values.
618 */
619String HttpReply::removeStaleWarningValues(const String &value)
620{
621 String newValue;
622 const char *item = 0;
623 int len = 0;
624 const char *pos = 0;
625 while (strListGetItem(&value, ',', &item, &len, &pos)) {
626 bool keep = true;
627 // Does warning-value have warn-date (which contains quoted date)?
628 // We scan backwards, looking for two quoted strings.
629 // warning-value = warn-code SP warn-agent SP warn-text [SP warn-date]
630 const char *p = item + len - 1;
631
632 while (p >= item && xisspace(*p)) --p; // skip whitespace
633
634 // warning-value MUST end with quote
635 if (p >= item && *p == '"') {
636 const char *const warnDateEnd = p;
637 --p;
638 while (p >= item && *p != '"') --p; // find the next quote
639
640 const char *warnDateBeg = p + 1;
641 --p;
642 while (p >= item && xisspace(*p)) --p; // skip whitespace
643
644 if (p >= item && *p == '"' && warnDateBeg - p > 2) {
645 // found warn-text
646 String warnDate;
647 warnDate.append(warnDateBeg, warnDateEnd - warnDateBeg);
648 const time_t time = parse_rfc1123(warnDate.termedBuf());
649 keep = (time > 0 && time == date); // keep valid and matching date
650 }
651 }
652
653 if (keep) {
654 if (newValue.size())
655 newValue.append(", ");
656 newValue.append(item, len);
657 }
658 }
659
660 return newValue;
661}
f53969cc 662
eace013e
EB
663bool
664HttpReply::olderThan(const HttpReply *them) const
665{
666 if (!them || !them->date || !date)
667 return false;
668 return date < them->date;
669}
670
4f1c93a7
EB
671void
672HttpReply::removeIrrelevantContentLength() {
673 if (Http::ProhibitsContentLength(sline.status()))
674 if (header.delById(Http::HdrType::CONTENT_LENGTH))
675 debugs(58, 3, "Removing unexpected Content-Length header");
676}
677