]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeader.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / HttpHeader.cc
CommitLineData
cb69b4c7 1/*
123abbe1 2 * DEBUG: section 55 HTTP Header
cb69b4c7 3 * AUTHOR: Alex Rousskov
4 *
2b6662ba 5 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 6 * ----------------------------------------------------------
cb69b4c7 7 *
2b6662ba 8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
cb69b4c7 16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
26ac0430 21 *
cb69b4c7 22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26ac0430 26 *
cb69b4c7 27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
cbdec147 29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
cb69b4c7 30 */
31
582c2af2 32#include "squid.h"
25f98340 33#include "base64.h"
67679543 34#include "globals.h"
7ebe76de 35#include "HttpHdrCc.h"
582c2af2 36#include "HttpHdrContRange.h"
25b6a907 37#include "HttpHdrSc.h"
8822ebee 38#include "HttpHeader.h"
79cb238d 39#include "HttpHeaderFieldInfo.h"
e1656dc4 40#include "HttpHeaderStat.h"
a5bac1d2 41#include "HttpHeaderTools.h"
0eb49b6d 42#include "MemBuf.h"
8822ebee 43#include "mgr/Registration.h"
582c2af2 44#include "profiler/Profiler.h"
25f98340 45#include "rfc1123.h"
602d9612
A
46#include "SquidConfig.h"
47#include "SquidString.h"
00a7574e 48#include "StatHist.h"
8822ebee 49#include "Store.h"
28204b3b 50#include "StrList.h"
81a94152 51#include "TimeOrTag.h"
cb69b4c7 52
42865d25
FC
53#include <algorithm>
54
6fda1515
FC
55/* XXX: the whole set of API managing the entries vector should be rethought
56 * after the parse4r-ng effort is complete.
57 */
58
cb69b4c7 59/*
2ac76861 60 * On naming conventions:
26ac0430
AJ
61 *
62 * HTTP/1.1 defines message-header as
63 *
2ac76861 64 * message-header = field-name ":" [ field-value ] CRLF
65 * field-name = token
66 * field-value = *( field-content | LWS )
26ac0430 67 *
2ac76861 68 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
69 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
26ac0430 70 *
2ac76861 71 * HttpHeader is an object that represents all message-headers in a message.
72 * HttpHeader does not manage start-line.
26ac0430 73 *
d8b249ef 74 * HttpHeader is implemented as a collection of header "entries".
75 * An entry is a (field_id, field_name, field_value) triplet.
2ac76861 76 */
cb69b4c7 77
cb69b4c7 78/*
79 * local constants and vars
80 */
81
82/*
26ac0430
AJ
83 * A table with major attributes for every known field.
84 * We calculate name lengths and reorganize this array on start up.
cb69b4c7 85 * After reorganization, field id can be used as an index to the table.
86 */
26ac0430
AJ
87static const HttpHeaderFieldAttrs HeadersAttrs[] = {
88 {"Accept", HDR_ACCEPT, ftStr},
89
90 {"Accept-Charset", HDR_ACCEPT_CHARSET, ftStr},
91 {"Accept-Encoding", HDR_ACCEPT_ENCODING, ftStr},
92 {"Accept-Language", HDR_ACCEPT_LANGUAGE, ftStr},
93 {"Accept-Ranges", HDR_ACCEPT_RANGES, ftStr},
94 {"Age", HDR_AGE, ftInt},
95 {"Allow", HDR_ALLOW, ftStr},
96 {"Authorization", HDR_AUTHORIZATION, ftStr}, /* for now */
97 {"Cache-Control", HDR_CACHE_CONTROL, ftPCc},
98 {"Connection", HDR_CONNECTION, ftStr},
99 {"Content-Base", HDR_CONTENT_BASE, ftStr},
a585e13d 100 {"Content-Disposition", HDR_CONTENT_DISPOSITION, ftStr}, /* for now */
26ac0430
AJ
101 {"Content-Encoding", HDR_CONTENT_ENCODING, ftStr},
102 {"Content-Language", HDR_CONTENT_LANGUAGE, ftStr},
103 {"Content-Length", HDR_CONTENT_LENGTH, ftInt64},
104 {"Content-Location", HDR_CONTENT_LOCATION, ftStr},
105 {"Content-MD5", HDR_CONTENT_MD5, ftStr}, /* for now */
106 {"Content-Range", HDR_CONTENT_RANGE, ftPContRange},
107 {"Content-Type", HDR_CONTENT_TYPE, ftStr},
108 {"Cookie", HDR_COOKIE, ftStr},
f89d4012 109 {"Cookie2", HDR_COOKIE2, ftStr},
26ac0430
AJ
110 {"Date", HDR_DATE, ftDate_1123},
111 {"ETag", HDR_ETAG, ftETag},
52b601ff 112 {"Expect", HDR_EXPECT, ftStr},
debd58cb 113 {"Expires", HDR_EXPIRES, ftDate_1123},
26ac0430
AJ
114 {"From", HDR_FROM, ftStr},
115 {"Host", HDR_HOST, ftStr},
c2819396 116 {"HTTP2-Settings", HDR_HTTP2_SETTINGS, ftStr}, /* for now */
26ac0430
AJ
117 {"If-Match", HDR_IF_MATCH, ftStr}, /* for now */
118 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE, ftDate_1123},
119 {"If-None-Match", HDR_IF_NONE_MATCH, ftStr}, /* for now */
120 {"If-Range", HDR_IF_RANGE, ftDate_1123_or_ETag},
121 {"Keep-Alive", HDR_KEEP_ALIVE, ftStr},
f44e9341 122 {"Key", HDR_KEY, ftStr},
26ac0430
AJ
123 {"Last-Modified", HDR_LAST_MODIFIED, ftDate_1123},
124 {"Link", HDR_LINK, ftStr},
125 {"Location", HDR_LOCATION, ftStr},
ef064e01 126 {"Max-Forwards", HDR_MAX_FORWARDS, ftInt64},
26ac0430 127 {"Mime-Version", HDR_MIME_VERSION, ftStr}, /* for now */
debd58cb 128 {"Negotiate", HDR_NEGOTIATE, ftStr},
3865965d 129 {"Origin", HDR_ORIGIN, ftStr},
26ac0430
AJ
130 {"Pragma", HDR_PRAGMA, ftStr},
131 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE, ftStr},
132 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO, ftStr},
133 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION, ftStr},
134 {"Proxy-Connection", HDR_PROXY_CONNECTION, ftStr},
135 {"Proxy-support", HDR_PROXY_SUPPORT, ftStr},
136 {"Public", HDR_PUBLIC, ftStr},
137 {"Range", HDR_RANGE, ftPRange},
138 {"Referer", HDR_REFERER, ftStr},
139 {"Request-Range", HDR_REQUEST_RANGE, ftPRange}, /* usually matches HDR_RANGE */
140 {"Retry-After", HDR_RETRY_AFTER, ftStr}, /* for now (ftDate_1123 or ftInt!) */
141 {"Server", HDR_SERVER, ftStr},
142 {"Set-Cookie", HDR_SET_COOKIE, ftStr},
f89d4012 143 {"Set-Cookie2", HDR_SET_COOKIE2, ftStr},
26ac0430
AJ
144 {"TE", HDR_TE, ftStr},
145 {"Title", HDR_TITLE, ftStr},
a1651bad 146 {"Trailer", HDR_TRAILER, ftStr},
26ac0430 147 {"Transfer-Encoding", HDR_TRANSFER_ENCODING, ftStr},
af48ea68
AJ
148 {"Translate", HDR_TRANSLATE, ftStr}, /* for now. may need to crop */
149 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE, ftStr}, /* for now ignore. may need to crop */
26ac0430
AJ
150 {"Upgrade", HDR_UPGRADE, ftStr}, /* for now */
151 {"User-Agent", HDR_USER_AGENT, ftStr},
152 {"Vary", HDR_VARY, ftStr}, /* for now */
153 {"Via", HDR_VIA, ftStr}, /* for now */
154 {"Warning", HDR_WARNING, ftStr}, /* for now */
155 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE, ftStr},
156 {"Authentication-Info", HDR_AUTHENTICATION_INFO, ftStr},
157 {"X-Cache", HDR_X_CACHE, ftStr},
158 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP, ftStr},
159 {"X-Forwarded-For", HDR_X_FORWARDED_FOR, ftStr},
160 {"X-Request-URI", HDR_X_REQUEST_URI, ftStr},
161 {"X-Squid-Error", HDR_X_SQUID_ERROR, ftStr},
f66a9ef4 162#if X_ACCELERATOR_VARY
26ac0430 163 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY, ftStr},
a22e6cd3
AR
164#endif
165#if USE_ADAPTATION
166 {"X-Next-Services", HDR_X_NEXT_SERVICES, ftStr},
f66a9ef4 167#endif
26ac0430
AJ
168 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY, ftStr},
169 {"Surrogate-Control", HDR_SURROGATE_CONTROL, ftPSc},
170 {"Front-End-Https", HDR_FRONT_END_HTTPS, ftStr},
171 {"Other:", HDR_OTHER, ftStr} /* ':' will not allow matches */
172};
62e76326 173
de336bbe 174static HttpHeaderFieldInfo *Headers = NULL;
cb69b4c7 175
e6ccf245 176http_hdr_type &operator++ (http_hdr_type &aHeader)
177{
1f1ae50a 178 int tmp = (int)aHeader;
179 aHeader = (http_hdr_type)(++tmp);
e6ccf245 180 return aHeader;
181}
182
cb69b4c7 183/*
184 * headers with field values defined as #(values) in HTTP/1.1
d8b249ef 185 * Headers that are currently not recognized, are commented out.
cb69b4c7 186 */
b644367b 187static HttpHeaderMask ListHeadersMask; /* set run-time using ListHeadersArr */
26ac0430
AJ
188static http_hdr_type ListHeadersArr[] = {
189 HDR_ACCEPT,
190 HDR_ACCEPT_CHARSET, HDR_ACCEPT_ENCODING, HDR_ACCEPT_LANGUAGE,
191 HDR_ACCEPT_RANGES, HDR_ALLOW,
192 HDR_CACHE_CONTROL,
193 HDR_CONTENT_ENCODING,
194 HDR_CONTENT_LANGUAGE,
195 HDR_CONNECTION,
52b601ff 196 HDR_EXPECT,
26ac0430 197 HDR_IF_MATCH, HDR_IF_NONE_MATCH,
a0a17114 198 HDR_KEY,
26ac0430
AJ
199 HDR_LINK, HDR_PRAGMA,
200 HDR_PROXY_CONNECTION,
201 HDR_PROXY_SUPPORT,
202 HDR_TRANSFER_ENCODING,
203 HDR_UPGRADE,
204 HDR_VARY,
205 HDR_VIA,
c679653d 206 HDR_WARNING,
26ac0430
AJ
207 HDR_WWW_AUTHENTICATE,
208 HDR_AUTHENTICATION_INFO,
209 HDR_PROXY_AUTHENTICATION_INFO,
52b601ff 210 /* HDR_TE, HDR_TRAILER */
f66a9ef4 211#if X_ACCELERATOR_VARY
26ac0430 212 HDR_X_ACCELERATOR_VARY,
a22e6cd3
AR
213#endif
214#if USE_ADAPTATION
215 HDR_X_NEXT_SERVICES,
f66a9ef4 216#endif
26ac0430
AJ
217 HDR_SURROGATE_CAPABILITY,
218 HDR_SURROGATE_CONTROL,
219 HDR_X_FORWARDED_FOR
220};
99edd1c3 221
222/* general-headers */
26ac0430
AJ
223static http_hdr_type GeneralHeadersArr[] = {
224 HDR_CACHE_CONTROL, HDR_CONNECTION, HDR_DATE, HDR_PRAGMA,
225 HDR_TRANSFER_ENCODING,
226 HDR_UPGRADE,
227 /* HDR_TRAILER, */
228 HDR_VIA,
229};
99edd1c3 230
231/* entity-headers */
26ac0430
AJ
232static http_hdr_type EntityHeadersArr[] = {
233 HDR_ALLOW, HDR_CONTENT_BASE, HDR_CONTENT_ENCODING, HDR_CONTENT_LANGUAGE,
234 HDR_CONTENT_LENGTH, HDR_CONTENT_LOCATION, HDR_CONTENT_MD5,
235 HDR_CONTENT_RANGE, HDR_CONTENT_TYPE, HDR_ETAG, HDR_EXPIRES, HDR_LAST_MODIFIED, HDR_LINK,
236 HDR_OTHER
237};
cb69b4c7 238
b644367b 239static HttpHeaderMask ReplyHeadersMask; /* set run-time using ReplyHeaders */
26ac0430
AJ
240static http_hdr_type ReplyHeadersArr[] = {
241 HDR_ACCEPT, HDR_ACCEPT_CHARSET, HDR_ACCEPT_ENCODING, HDR_ACCEPT_LANGUAGE,
242 HDR_ACCEPT_RANGES, HDR_AGE,
a0a17114 243 HDR_KEY,
26ac0430 244 HDR_LOCATION, HDR_MAX_FORWARDS,
f89d4012 245 HDR_MIME_VERSION, HDR_PUBLIC, HDR_RETRY_AFTER, HDR_SERVER, HDR_SET_COOKIE, HDR_SET_COOKIE2,
3865965d 246 HDR_ORIGIN,
26ac0430
AJ
247 HDR_VARY,
248 HDR_WARNING, HDR_PROXY_CONNECTION, HDR_X_CACHE,
249 HDR_X_CACHE_LOOKUP,
250 HDR_X_REQUEST_URI,
f66a9ef4 251#if X_ACCELERATOR_VARY
26ac0430 252 HDR_X_ACCELERATOR_VARY,
a22e6cd3
AR
253#endif
254#if USE_ADAPTATION
255 HDR_X_NEXT_SERVICES,
f66a9ef4 256#endif
26ac0430
AJ
257 HDR_X_SQUID_ERROR,
258 HDR_SURROGATE_CONTROL
259};
cb69b4c7 260
b644367b 261static HttpHeaderMask RequestHeadersMask; /* set run-time using RequestHeaders */
26ac0430
AJ
262static http_hdr_type RequestHeadersArr[] = {
263 HDR_AUTHORIZATION, HDR_FROM, HDR_HOST,
c2819396 264 HDR_HTTP2_SETTINGS,
26ac0430 265 HDR_IF_MATCH, HDR_IF_MODIFIED_SINCE, HDR_IF_NONE_MATCH,
3865965d
AJ
266 HDR_IF_RANGE, HDR_MAX_FORWARDS,
267 HDR_ORIGIN,
268 HDR_PROXY_CONNECTION,
26ac0430
AJ
269 HDR_PROXY_AUTHORIZATION, HDR_RANGE, HDR_REFERER, HDR_REQUEST_RANGE,
270 HDR_USER_AGENT, HDR_X_FORWARDED_FOR, HDR_SURROGATE_CAPABILITY
271};
cb69b4c7 272
dcf3665b 273static HttpHeaderMask HopByHopHeadersMask;
26ac0430 274static http_hdr_type HopByHopHeadersArr[] = {
c2819396 275 HDR_CONNECTION, HDR_HTTP2_SETTINGS, HDR_KEEP_ALIVE, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION,
a1651bad 276 HDR_TE, HDR_TRAILER, HDR_TRANSFER_ENCODING, HDR_UPGRADE, HDR_PROXY_CONNECTION
26ac0430 277};
2cdeea82 278
12cf1be2 279/* header accounting */
26ac0430
AJ
280static HttpHeaderStat HttpHeaderStats[] = {
281 {"all"},
9f4d4f65 282#if USE_HTCP
26ac0430 283 {"HTCP reply"},
9f4d4f65 284#endif
26ac0430
AJ
285 {"request"},
286 {"reply"}
287};
9bea1d5b 288static int HttpHeaderStatCount = countof(HttpHeaderStats);
12cf1be2 289
7faf2bdb 290static int HeaderEntryParsedCount = 0;
cb69b4c7 291
12cf1be2 292/*
f734cda2 293 * forward declarations and local routines
12cf1be2 294 */
cb69b4c7 295
f734cda2 296class StoreEntry;
cb69b4c7 297#define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
298
30abd221 299static void httpHeaderNoteParsedEntry(http_hdr_type id, String const &value, int error);
cb69b4c7 300
9bea1d5b 301static void httpHeaderStatInit(HttpHeaderStat * hs, const char *label);
302static void httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e);
7c525cc2 303
f734cda2
FC
304/** store report about current header usage and other stats */
305static void httpHeaderStoreReport(StoreEntry * e);
306
cb69b4c7 307/*
308 * Module initialization routines
309 */
310
6b7d87bb
FC
311static void
312httpHeaderRegisterWithCacheManager(void)
313{
8822ebee 314 Mgr::RegisterAction("http_headers",
d9fc6862
A
315 "HTTP Header Statistics",
316 httpHeaderStoreReport, 0, 1);
6b7d87bb
FC
317}
318
cb69b4c7 319void
9bea1d5b 320httpHeaderInitModule(void)
cb69b4c7 321{
9bea1d5b 322 int i;
323 /* check that we have enough space for masks */
324 assert(8 * sizeof(HttpHeaderMask) >= HDR_ENUM_END);
325 /* all headers must be described */
326 assert(countof(HeadersAttrs) == HDR_ENUM_END);
62e76326 327
9bea1d5b 328 if (!Headers)
62e76326 329 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
330
9bea1d5b 331 /* create masks */
332 httpHeaderMaskInit(&ListHeadersMask, 0);
62e76326 333
8abf232c 334 httpHeaderCalcMask(&ListHeadersMask, ListHeadersArr, countof(ListHeadersArr));
62e76326 335
9bea1d5b 336 httpHeaderMaskInit(&ReplyHeadersMask, 0);
62e76326 337
8abf232c 338 httpHeaderCalcMask(&ReplyHeadersMask, ReplyHeadersArr, countof(ReplyHeadersArr));
62e76326 339
8abf232c 340 httpHeaderCalcMask(&ReplyHeadersMask, GeneralHeadersArr, countof(GeneralHeadersArr));
62e76326 341
8abf232c 342 httpHeaderCalcMask(&ReplyHeadersMask, EntityHeadersArr, countof(EntityHeadersArr));
62e76326 343
9bea1d5b 344 httpHeaderMaskInit(&RequestHeadersMask, 0);
62e76326 345
8abf232c 346 httpHeaderCalcMask(&RequestHeadersMask, RequestHeadersArr, countof(RequestHeadersArr));
62e76326 347
8abf232c 348 httpHeaderCalcMask(&RequestHeadersMask, GeneralHeadersArr, countof(GeneralHeadersArr));
62e76326 349
8abf232c 350 httpHeaderCalcMask(&RequestHeadersMask, EntityHeadersArr, countof(EntityHeadersArr));
62e76326 351
c90e2fcb 352 httpHeaderMaskInit(&HopByHopHeadersMask, 0);
e1381638 353
dcf3665b
AJ
354 httpHeaderCalcMask(&HopByHopHeadersMask, HopByHopHeadersArr, countof(HopByHopHeadersArr));
355
9bea1d5b 356 /* init header stats */
357 assert(HttpHeaderStatCount == hoReply + 1);
62e76326 358
95dc7ff4 359 for (i = 0; i < HttpHeaderStatCount; ++i)
62e76326 360 httpHeaderStatInit(HttpHeaderStats + i, HttpHeaderStats[i].label);
361
9bea1d5b 362 HttpHeaderStats[hoRequest].owner_mask = &RequestHeadersMask;
62e76326 363
9bea1d5b 364 HttpHeaderStats[hoReply].owner_mask = &ReplyHeadersMask;
62e76326 365
86aebcda 366#if USE_HTCP
62e76326 367
9bea1d5b 368 HttpHeaderStats[hoHtcpReply].owner_mask = &ReplyHeadersMask;
62e76326 369
86aebcda 370#endif
9bea1d5b 371 /* init dependent modules */
372 httpHdrCcInitModule();
62e76326 373
43ae1d95 374 httpHdrScInitModule();
ea391f18
FC
375
376 httpHeaderRegisterWithCacheManager();
62ee09ca 377}
43ae1d95 378
7021844c 379void
9bea1d5b 380httpHeaderCleanModule(void)
7021844c 381{
9bea1d5b 382 httpHeaderDestroyFieldsInfo(Headers, HDR_ENUM_END);
383 Headers = NULL;
384 httpHdrCcCleanModule();
43ae1d95 385 httpHdrScCleanModule();
7021844c 386}
387
12cf1be2 388static void
9bea1d5b 389httpHeaderStatInit(HttpHeaderStat * hs, const char *label)
12cf1be2 390{
9bea1d5b 391 assert(hs);
392 assert(label);
393 memset(hs, 0, sizeof(HttpHeaderStat));
394 hs->label = label;
e161cb08
FC
395 hs->hdrUCountDistr.enumInit(32); /* not a real enum */
396 hs->fieldTypeDistr.enumInit(HDR_ENUM_END);
397 hs->ccTypeDistr.enumInit(CC_ENUM_END);
398 hs->scTypeDistr.enumInit(SC_ENUM_END);
cb69b4c7 399}
400
cb69b4c7 401/*
402 * HttpHeader Implementation
403 */
404
75faaa7a 405HttpHeader::HttpHeader() : owner (hoNone), len (0)
cb69b4c7 406{
75faaa7a 407 httpHeaderMaskInit(&mask, 0);
408}
409
5e5fa5b1 410HttpHeader::HttpHeader(const http_hdr_owner_type anOwner): owner(anOwner), len(0)
75faaa7a 411{
02259ff8 412 assert(anOwner > hoNone && anOwner < hoEnd);
efc26e8e 413 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner);
75faaa7a 414 httpHeaderMaskInit(&mask, 0);
415}
416
5e5fa5b1
AR
417HttpHeader::HttpHeader(const HttpHeader &other): owner(other.owner), len(other.len)
418{
419 httpHeaderMaskInit(&mask, 0);
420 update(&other, NULL); // will update the mask as well
421}
422
75faaa7a 423HttpHeader::~HttpHeader()
424{
519e0948 425 clean();
cb69b4c7 426}
427
5e5fa5b1
AR
428HttpHeader &
429HttpHeader::operator =(const HttpHeader &other)
430{
431 if (this != &other) {
432 // we do not really care, but the caller probably does
433 assert(owner == other.owner);
434 clean();
435 update(&other, NULL); // will update the mask as well
436 len = other.len;
437 }
438 return *this;
439}
440
cb69b4c7 441void
519e0948 442HttpHeader::clean()
cb69b4c7 443{
9bea1d5b 444
02259ff8 445 assert(owner > hoNone && owner < hoEnd);
efc26e8e 446 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner);
9bea1d5b 447
3a9cf2d5 448 PROF_start(HttpHeaderClean);
449
0de73a38 450 if (owner <= hoReply) {
c594d76c
A
451 /*
452 * An unfortunate bug. The entries array is initialized
453 * such that count is set to zero. httpHeaderClean() seems to
454 * be called both when 'hdr' is created, and destroyed. Thus,
455 * we accumulate a large number of zero counts for 'hdr' before
456 * it is ever used. Can't think of a good way to fix it, except
457 * adding a state variable that indicates whether or not 'hdr'
458 * has been used. As a hack, just never count zero-sized header
459 * arrays.
460 */
4c9eadc2
FC
461 if (!entries.empty())
462 HttpHeaderStats[owner].hdrUCountDistr.count(entries.size());
51328da8 463
95dc7ff4 464 ++ HttpHeaderStats[owner].destroyedCount;
51328da8 465
4c9eadc2 466 HttpHeaderStats[owner].busyDestroyedCount += entries.size() > 0;
0de73a38 467 } // if (owner <= hoReply)
62e76326 468
6fda1515
FC
469 for (std::vector<HttpHeaderEntry *>::iterator i = entries.begin(); i != entries.end(); ++i) {
470 HttpHeaderEntry *e = *i;
471 if (e == NULL)
472 continue;
c594d76c 473 if (e->id < 0 || e->id >= HDR_ENUM_END) {
6fda1515 474 debugs(55, DBG_CRITICAL, "BUG: invalid entry (" << e->id << "). Ignored.");
c594d76c
A
475 } else {
476 if (owner <= hoReply)
f30f7998 477 HttpHeaderStats[owner].fieldTypeDistr.count(e->id);
c594d76c 478 delete e;
62e76326 479 }
c594d76c 480 }
6fda1515 481
c33a88ca 482 entries.clear();
519e0948 483 httpHeaderMaskInit(&mask, 0);
5e5fa5b1 484 len = 0;
3a9cf2d5 485 PROF_stop(HttpHeaderClean);
cb69b4c7 486}
487
2246b732 488/* append entries (also see httpHeaderUpdate) */
489void
a9925b40 490HttpHeader::append(const HttpHeader * src)
2246b732 491{
9bea1d5b 492 const HttpHeaderEntry *e;
493 HttpHeaderPos pos = HttpHeaderInitPos;
a9925b40 494 assert(src);
495 assert(src != this);
efc26e8e 496 debugs(55, 7, "appending hdr: " << this << " += " << src);
9bea1d5b 497
a9925b40 498 while ((e = src->getEntry(&pos))) {
eede25e7 499 addEntry(e->clone());
2246b732 500 }
501}
502
d8b249ef 503/* use fresh entries to replace old ones */
cb69b4c7 504void
9bea1d5b 505httpHeaderUpdate(HttpHeader * old, const HttpHeader * fresh, const HttpHeaderMask * denied_mask)
924f73bc 506{
507 assert (old);
508 old->update (fresh, denied_mask);
509}
510
511void
512HttpHeader::update (HttpHeader const *fresh, HttpHeaderMask const *denied_mask)
cb69b4c7 513{
9bea1d5b 514 const HttpHeaderEntry *e;
515 HttpHeaderPos pos = HttpHeaderInitPos;
a9925b40 516 assert(fresh);
924f73bc 517 assert(this != fresh);
9bea1d5b 518
a9925b40 519 while ((e = fresh->getEntry(&pos))) {
62e76326 520 /* deny bad guys (ok to check for HDR_OTHER) here */
521
522 if (denied_mask && CBIT_TEST(*denied_mask, e->id))
523 continue;
524
ba9fb01d 525 if (e->id != HDR_OTHER)
526 delById(e->id);
527 else
9d7a89a5 528 delByName(e->name.termedBuf());
9917847f 529 }
530
531 pos = HttpHeaderInitPos;
532 while ((e = fresh->getEntry(&pos))) {
533 /* deny bad guys (ok to check for HDR_OTHER) here */
534
535 if (denied_mask && CBIT_TEST(*denied_mask, e->id))
536 continue;
537
538 debugs(55, 7, "Updating header '" << HeadersAttrs[e->id].name << "' in cached entry");
62e76326 539
eede25e7 540 addEntry(e->clone());
cb69b4c7 541 }
cb69b4c7 542}
543
544/* just handy in parsing: resets and returns false */
9f4d4f65 545int
a9925b40 546HttpHeader::reset()
2ac76861 547{
a9925b40 548 clean();
9bea1d5b 549 return 0;
cb69b4c7 550}
551
cb69b4c7 552int
a9925b40 553HttpHeader::parse(const char *header_start, const char *header_end)
cb69b4c7 554{
52d3f198 555 const char *field_ptr = header_start;
556 HttpHeaderEntry *e, *e2;
56cd29e6 557 int warnOnError = (Config.onoff.relaxed_header_parser <= 0 ? DBG_IMPORTANT : 2);
9bea1d5b 558
3a9cf2d5 559 PROF_start(HttpHeaderParse);
560
9bea1d5b 561 assert(header_start && header_end);
efc26e8e 562 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl << getStringPrefix(header_start, header_end));
95dc7ff4 563 ++ HttpHeaderStats[owner].parsedCount;
62e76326 564
1d8a896b 565 char *nulpos;
6d8633ea 566 if ((nulpos = (char*)memchr(header_start, '\0', header_end - header_start))) {
e0236918 567 debugs(55, DBG_IMPORTANT, "WARNING: HTTP header contains NULL characters {" <<
1d8a896b 568 getStringPrefix(header_start, nulpos) << "}\nNULL\n{" << getStringPrefix(nulpos+1, header_end));
6d7bc97b
AJ
569 PROF_stop(HttpHeaderParse);
570 return reset();
52d3f198 571 }
572
573 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
574 * continuation lines start with a (single) space or tab */
575 while (field_ptr < header_end) {
576 const char *field_start = field_ptr;
62e76326 577 const char *field_end;
62e76326 578
579 do {
52d3f198 580 const char *this_line = field_ptr;
581 field_ptr = (const char *)memchr(field_ptr, '\n', header_end - field_ptr);
582
6d7bc97b
AJ
583 if (!field_ptr) {
584 // missing <LF>
585 PROF_stop(HttpHeaderParse);
586 return reset();
587 }
52d3f198 588
589 field_end = field_ptr;
590
95dc7ff4 591 ++field_ptr; /* Move to next line */
52d3f198 592
593 if (field_end > this_line && field_end[-1] == '\r') {
5e263176 594 --field_end; /* Ignore CR LF */
52d3f198 595
e10272fc
AR
596 if (owner == hoRequest && field_end > this_line) {
597 bool cr_only = true;
598 for (const char *p = this_line; p < field_end && cr_only; ++p) {
599 if (*p != '\r')
600 cr_only = false;
601 }
602 if (cr_only) {
6e09ed7b 603 debugs(55, DBG_IMPORTANT, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
e10272fc
AR
604 "header field to prevent request smuggling attacks: {" <<
605 getStringPrefix(header_start, header_end) << "}");
6d7bc97b
AJ
606 PROF_stop(HttpHeaderParse);
607 return reset();
e10272fc 608 }
52d3f198 609 }
610 }
611
612 /* Barf on stray CR characters */
613 if (memchr(this_line, '\r', field_end - this_line)) {
6e09ed7b 614 debugs(55, warnOnError, "WARNING: suspicious CR characters in HTTP header {" <<
26ac0430 615 getStringPrefix(field_start, field_end) << "}");
52d3f198 616
617 if (Config.onoff.relaxed_header_parser) {
618 char *p = (char *) this_line; /* XXX Warning! This destroys original header content and violates specifications somewhat */
619
a38ec4b1
FC
620 while ((p = (char *)memchr(p, '\r', field_end - p)) != NULL) {
621 *p = ' ';
622 ++p;
623 }
6d7bc97b
AJ
624 } else {
625 PROF_stop(HttpHeaderParse);
626 return reset();
627 }
52d3f198 628 }
629
630 if (this_line + 1 == field_end && this_line > field_start) {
6e09ed7b 631 debugs(55, warnOnError, "WARNING: Blank continuation line in HTTP header {" <<
26ac0430 632 getStringPrefix(header_start, header_end) << "}");
6d7bc97b
AJ
633 PROF_stop(HttpHeaderParse);
634 return reset();
52d3f198 635 }
636 } while (field_ptr < header_end && (*field_ptr == ' ' || *field_ptr == '\t'));
637
638 if (field_start == field_end) {
639 if (field_ptr < header_end) {
6e09ed7b 640 debugs(55, warnOnError, "WARNING: unparseable HTTP header field near {" <<
26ac0430 641 getStringPrefix(field_start, header_end) << "}");
6d7bc97b
AJ
642 PROF_stop(HttpHeaderParse);
643 return reset();
52d3f198 644 }
645
646 break; /* terminating blank line */
647 }
62e76326 648
cdce6c61 649 if ((e = HttpHeaderEntry::parse(field_start, field_end)) == NULL) {
6e09ed7b 650 debugs(55, warnOnError, "WARNING: unparseable HTTP header field {" <<
26ac0430 651 getStringPrefix(field_start, field_end) << "}");
6e09ed7b 652 debugs(55, warnOnError, " in {" << getStringPrefix(header_start, header_end) << "}");
52d3f198 653
654 if (Config.onoff.relaxed_header_parser)
655 continue;
6e09ed7b 656
6d7bc97b
AJ
657 PROF_stop(HttpHeaderParse);
658 return reset();
47ac2ebe 659 }
62e76326 660
a9925b40 661 if (e->id == HDR_CONTENT_LENGTH && (e2 = findEntry(e->id)) != NULL) {
9d7a89a5 662 if (e->value != e2->value) {
3e62bd58 663 int64_t l1, l2;
6e09ed7b
AJ
664 debugs(55, warnOnError, "WARNING: found two conflicting content-length headers in {" <<
665 getStringPrefix(header_start, header_end) << "}");
9331e4b4 666
667 if (!Config.onoff.relaxed_header_parser) {
eede25e7 668 delete e;
6d7bc97b
AJ
669 PROF_stop(HttpHeaderParse);
670 return reset();
9331e4b4 671 }
672
9d7a89a5 673 if (!httpHeaderParseOffset(e->value.termedBuf(), &l1)) {
e0236918 674 debugs(55, DBG_IMPORTANT, "WARNING: Unparseable content-length '" << e->value << "'");
eede25e7 675 delete e;
9331e4b4 676 continue;
9d7a89a5 677 } else if (!httpHeaderParseOffset(e2->value.termedBuf(), &l2)) {
e0236918 678 debugs(55, DBG_IMPORTANT, "WARNING: Unparseable content-length '" << e2->value << "'");
a9925b40 679 delById(e2->id);
9331e4b4 680 } else if (l1 > l2) {
a9925b40 681 delById(e2->id);
9331e4b4 682 } else {
eede25e7 683 delete e;
9331e4b4 684 continue;
685 }
52d3f198 686 } else {
6e09ed7b
AJ
687 debugs(55, warnOnError, "NOTICE: found double content-length header");
688 delete e;
b3123159 689
6e09ed7b 690 if (Config.onoff.relaxed_header_parser)
b3123159 691 continue;
6e09ed7b 692
6d7bc97b
AJ
693 PROF_stop(HttpHeaderParse);
694 return reset();
52d3f198 695 }
696 }
62e76326 697
9d7a89a5 698 if (e->id == HDR_OTHER && stringHasWhitespace(e->name.termedBuf())) {
6e09ed7b 699 debugs(55, warnOnError, "WARNING: found whitespace in HTTP header name {" <<
bf8fe701 700 getStringPrefix(field_start, field_end) << "}");
47ac2ebe 701
52d3f198 702 if (!Config.onoff.relaxed_header_parser) {
eede25e7 703 delete e;
6d7bc97b
AJ
704 PROF_stop(HttpHeaderParse);
705 return reset();
52d3f198 706 }
707 }
62e76326 708
a9925b40 709 addEntry(e);
cb69b4c7 710 }
62e76326 711
c3d0ba0c
AR
712 if (chunked()) {
713 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
714 delById(HDR_CONTENT_LENGTH);
715 }
716
3a9cf2d5 717 PROF_stop(HttpHeaderParse);
9bea1d5b 718 return 1; /* even if no fields where found, it is a valid header */
cb69b4c7 719}
720
99edd1c3 721/* packs all the entries using supplied packer */
cb69b4c7 722void
d8f6c79c 723HttpHeader::packInto(Packer * p, bool mask_sensitive_info) const
cb69b4c7 724{
9bea1d5b 725 HttpHeaderPos pos = HttpHeaderInitPos;
726 const HttpHeaderEntry *e;
a9925b40 727 assert(p);
efc26e8e 728 debugs(55, 7, "packing hdr: (" << this << ")");
9bea1d5b 729 /* pack all entries one by one */
d8f6c79c
FC
730 while ((e = getEntry(&pos))) {
731 if (!mask_sensitive_info) {
732 e->packInto(p);
733 continue;
734 }
735 switch (e->id) {
d3823ea7
A
736 case HDR_AUTHORIZATION:
737 case HDR_PROXY_AUTHORIZATION:
738 packerAppend(p, e->name.rawBuf(), e->name.size());
739 packerAppend(p, ": ** NOT DISPLAYED **\r\n", 23);
740 break;
741 default:
742 e->packInto(p);
743 break;
d8f6c79c
FC
744 }
745 }
35b88ed2 746 /* Pack in the "special" entries */
747
748 /* Cache-Control */
cb69b4c7 749}
750
751/* returns next valid entry */
99edd1c3 752HttpHeaderEntry *
a9925b40 753HttpHeader::getEntry(HttpHeaderPos * pos) const
cb69b4c7 754{
a9925b40 755 assert(pos);
c0abe2ec 756 assert(*pos >= HttpHeaderInitPos && *pos < static_cast<ssize_t>(entries.size()));
62e76326 757
c0abe2ec 758 for (++(*pos); *pos < static_cast<ssize_t>(entries.size()); ++(*pos)) {
4c9eadc2 759 if (entries[*pos])
c0abe2ec 760 return static_cast<HttpHeaderEntry*>(entries[*pos]);
cb69b4c7 761 }
62e76326 762
9bea1d5b 763 return NULL;
cb69b4c7 764}
765
766/*
26ac0430 767 * returns a pointer to a specified entry if any
d8b249ef 768 * note that we return one entry so it does not make much sense to ask for
769 * "list" headers
cb69b4c7 770 */
de336bbe 771HttpHeaderEntry *
a9925b40 772HttpHeader::findEntry(http_hdr_type id) const
cb69b4c7 773{
9bea1d5b 774 HttpHeaderPos pos = HttpHeaderInitPos;
775 HttpHeaderEntry *e;
9bea1d5b 776 assert_eid(id);
777 assert(!CBIT_TEST(ListHeadersMask, id));
778
779 /* check mask first */
62e76326 780
a9925b40 781 if (!CBIT_TEST(mask, id))
62e76326 782 return NULL;
783
9bea1d5b 784 /* looks like we must have it, do linear search */
a9925b40 785 while ((e = getEntry(&pos))) {
62e76326 786 if (e->id == id)
787 return e;
cb69b4c7 788 }
62e76326 789
9bea1d5b 790 /* hm.. we thought it was there, but it was not found */
791 assert(0);
62e76326 792
9bea1d5b 793 return NULL; /* not reached */
cb69b4c7 794}
795
a622fff0 796/*
797 * same as httpHeaderFindEntry
798 */
a9925b40 799HttpHeaderEntry *
800HttpHeader::findLastEntry(http_hdr_type id) const
a622fff0 801{
9bea1d5b 802 HttpHeaderPos pos = HttpHeaderInitPos;
803 HttpHeaderEntry *e;
804 HttpHeaderEntry *result = NULL;
9bea1d5b 805 assert_eid(id);
806 assert(!CBIT_TEST(ListHeadersMask, id));
807
808 /* check mask first */
62e76326 809
a9925b40 810 if (!CBIT_TEST(mask, id))
62e76326 811 return NULL;
812
9bea1d5b 813 /* looks like we must have it, do linear search */
a9925b40 814 while ((e = getEntry(&pos))) {
62e76326 815 if (e->id == id)
816 result = e;
a622fff0 817 }
62e76326 818
9bea1d5b 819 assert(result); /* must be there! */
820 return result;
a622fff0 821}
822
cb69b4c7 823/*
26ac0430 824 * deletes all fields with a given name if any, returns #fields deleted;
cb69b4c7 825 */
2ac76861 826int
a9925b40 827HttpHeader::delByName(const char *name)
cb69b4c7 828{
9bea1d5b 829 int count = 0;
830 HttpHeaderPos pos = HttpHeaderInitPos;
831 HttpHeaderEntry *e;
a9925b40 832 httpHeaderMaskInit(&mask, 0); /* temporal inconsistency */
efc26e8e 833 debugs(55, 9, "deleting '" << name << "' fields in hdr " << this);
62e76326 834
a9925b40 835 while ((e = getEntry(&pos))) {
30abd221 836 if (!e->name.caseCmp(name))
ba9fb01d 837 delAt(pos, count);
838 else
a9925b40 839 CBIT_SET(mask, e->id);
cb69b4c7 840 }
62e76326 841
9bea1d5b 842 return count;
cb69b4c7 843}
844
2246b732 845/* deletes all entries with a given id, returns the #entries deleted */
846int
a9925b40 847HttpHeader::delById(http_hdr_type id)
d8b249ef 848{
9bea1d5b 849 int count = 0;
850 HttpHeaderPos pos = HttpHeaderInitPos;
851 HttpHeaderEntry *e;
efc26e8e 852 debugs(55, 8, this << " del-by-id " << id);
9bea1d5b 853 assert_eid(id);
e6ca7a9b 854 assert(id != HDR_OTHER); /* does not make sense */
62e76326 855
a9925b40 856 if (!CBIT_TEST(mask, id))
62e76326 857 return 0;
858
a9925b40 859 while ((e = getEntry(&pos))) {
ba9fb01d 860 if (e->id == id)
861 delAt(pos, count);
d8b249ef 862 }
62e76326 863
a9925b40 864 CBIT_CLR(mask, id);
9bea1d5b 865 assert(count);
866 return count;
d8b249ef 867}
d8b249ef 868
cb69b4c7 869/*
870 * deletes an entry at pos and leaves a gap; leaving a gap makes it
871 * possible to iterate(search) and delete fields at the same time
ba9fb01d 872 * NOTE: Does not update the header mask. Caller must follow up with
873 * a call to refreshMask() if headers_deleted was incremented.
cb69b4c7 874 */
2246b732 875void
ba9fb01d 876HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted)
cb69b4c7 877{
9bea1d5b 878 HttpHeaderEntry *e;
c0abe2ec
FC
879 assert(pos >= HttpHeaderInitPos && pos < static_cast<ssize_t>(entries.size()));
880 e = static_cast<HttpHeaderEntry*>(entries[pos]);
4c9eadc2 881 entries[pos] = NULL;
9bea1d5b 882 /* decrement header length, allow for ": " and crlf */
a9925b40 883 len -= e->name.size() + 2 + e->value.size() + 2;
884 assert(len >= 0);
eede25e7 885 delete e;
ba9fb01d 886 ++headers_deleted;
cb69b4c7 887}
888
394499bd 889/*
890 * Compacts the header storage
891 */
892void
893HttpHeader::compact()
894{
6e1410f9
FC
895 // TODO: optimize removal, or possibly make it so that's not needed.
896 std::vector<HttpHeaderEntry *>::iterator newend;
897 newend = std::remove(entries.begin(), entries.end(), static_cast<HttpHeaderEntry *>(NULL));
898 entries.resize(newend-entries.begin());
394499bd 899}
900
ba9fb01d 901/*
902 * Refreshes the header mask. Required after delAt() calls.
903 */
904void
905HttpHeader::refreshMask()
906{
907 httpHeaderMaskInit(&mask, 0);
908 debugs(55, 7, "refreshing the mask in hdr " << this);
909 HttpHeaderPos pos = HttpHeaderInitPos;
910 while (HttpHeaderEntry *e = getEntry(&pos)) {
26ac0430 911 CBIT_SET(mask, e->id);
ba9fb01d 912 }
913}
99edd1c3 914
62e76326 915/* appends an entry;
eede25e7 916 * does not call e->clone() so one should not reuse "*e"
cb69b4c7 917 */
99edd1c3 918void
a9925b40 919HttpHeader::addEntry(HttpHeaderEntry * e)
cb69b4c7 920{
a9925b40 921 assert(e);
9bea1d5b 922 assert_eid(e->id);
59c50530 923 assert(e->name.size());
9bea1d5b 924
4c9eadc2 925 debugs(55, 7, this << " adding entry: " << e->id << " at " << entries.size());
62e76326 926
a9925b40 927 if (CBIT_TEST(mask, e->id))
95dc7ff4 928 ++ Headers[e->id].stat.repCount;
9bea1d5b 929 else
a9925b40 930 CBIT_SET(mask, e->id);
62e76326 931
a9925b40 932 entries.push_back(e);
62e76326 933
9bea1d5b 934 /* increment header length, allow for ": " and crlf */
a9925b40 935 len += e->name.size() + 2 + e->value.size() + 2;
cb69b4c7 936}
937
bbe58ab5 938/* inserts an entry;
eede25e7 939 * does not call e->clone() so one should not reuse "*e"
bbe58ab5 940 */
941void
a9925b40 942HttpHeader::insertEntry(HttpHeaderEntry * e)
bbe58ab5 943{
a9925b40 944 assert(e);
bbe58ab5 945 assert_eid(e->id);
946
f9514ae8 947 debugs(55, 7, this << " adding entry: " << e->id << " at " << entries.size());
bbe58ab5 948
a9925b40 949 if (CBIT_TEST(mask, e->id))
95dc7ff4 950 ++ Headers[e->id].stat.repCount;
bbe58ab5 951 else
a9925b40 952 CBIT_SET(mask, e->id);
bbe58ab5 953
42865d25 954 entries.insert(entries.begin(),e);
bbe58ab5 955
956 /* increment header length, allow for ": " and crlf */
a9925b40 957 len += e->name.size() + 2 + e->value.size() + 2;
bbe58ab5 958}
959
35b88ed2 960bool
30abd221 961HttpHeader::getList(http_hdr_type id, String *s) const
35b88ed2 962{
963 HttpHeaderEntry *e;
964 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 965 debugs(55, 9, this << " joining for id " << id);
35b88ed2 966 /* only fields from ListHeaders array can be "listed" */
967 assert(CBIT_TEST(ListHeadersMask, id));
968
969 if (!CBIT_TEST(mask, id))
970 return false;
971
972 while ((e = getEntry(&pos))) {
973 if (e->id == id)
9d7a89a5 974 strListAdd(s, e->value.termedBuf(), ',');
35b88ed2 975 }
976
977 /*
978 * note: we might get an empty (size==0) string if there was an "empty"
979 * header. This results in an empty length String, which may have a NULL
980 * buffer.
981 */
982 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
983 if (!s->size())
984 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
985 else
986 debugs(55, 6, this << ": joined for id " << id << ": " << s);
987
988 return true;
989}
990
99edd1c3 991/* return a list of entries with the same id separated by ',' and ws */
30abd221 992String
a9925b40 993HttpHeader::getList(http_hdr_type id) const
cb69b4c7 994{
9bea1d5b 995 HttpHeaderEntry *e;
996 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 997 debugs(55, 9, this << "joining for id " << id);
9bea1d5b 998 /* only fields from ListHeaders array can be "listed" */
999 assert(CBIT_TEST(ListHeadersMask, id));
62e76326 1000
a9925b40 1001 if (!CBIT_TEST(mask, id))
30abd221 1002 return String();
650c4b88 1003
30abd221 1004 String s;
62e76326 1005
a9925b40 1006 while ((e = getEntry(&pos))) {
62e76326 1007 if (e->id == id)
9d7a89a5 1008 strListAdd(&s, e->value.termedBuf(), ',');
d35b9a94 1009 }
62e76326 1010
9bea1d5b 1011 /*
948078e3 1012 * note: we might get an empty (size==0) string if there was an "empty"
1013 * header. This results in an empty length String, which may have a NULL
1014 * buffer.
9bea1d5b 1015 */
948078e3 1016 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
528b2c61 1017 if (!s.size())
948078e3 1018 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
1019 else
1020 debugs(55, 6, this << ": joined for id " << id << ": " << s);
62e76326 1021
9bea1d5b 1022 return s;
cb69b4c7 1023}
1024
f66a9ef4 1025/* return a string or list of entries with the same id separated by ',' and ws */
30abd221 1026String
a9925b40 1027HttpHeader::getStrOrList(http_hdr_type id) const
f66a9ef4 1028{
c7327fa0 1029 HttpHeaderEntry *e;
9bea1d5b 1030
1031 if (CBIT_TEST(ListHeadersMask, id))
a9925b40 1032 return getList(id);
62e76326 1033
a9925b40 1034 if ((e = findEntry(id)))
62e76326 1035 return e->value;
1036
30abd221 1037 return String();
f66a9ef4 1038}
1039
1040/*
b2c44718 1041 * Returns the value of the specified header and/or an undefined String.
f66a9ef4 1042 */
30abd221 1043String
a9925b40 1044HttpHeader::getByName(const char *name) const
b2c44718
AR
1045{
1046 String result;
1047 // ignore presence: return undefined string if an empty header is present
1048 (void)getByNameIfPresent(name, result);
1049 return result;
1050}
1051
1052bool
1053HttpHeader::getByNameIfPresent(const char *name, String &result) const
f66a9ef4 1054{
9bea1d5b 1055 http_hdr_type id;
1056 HttpHeaderPos pos = HttpHeaderInitPos;
1057 HttpHeaderEntry *e;
9bea1d5b 1058
9bea1d5b 1059 assert(name);
1060
1061 /* First try the quick path */
1062 id = httpHeaderIdByNameDef(name, strlen(name));
62e76326 1063
b2c44718
AR
1064 if (id != -1) {
1065 if (!has(id))
1066 return false;
1067 result = getStrOrList(id);
1068 return true;
1069 }
650c4b88 1070
9bea1d5b 1071 /* Sorry, an unknown header name. Do linear search */
b2c44718 1072 bool found = false;
a9925b40 1073 while ((e = getEntry(&pos))) {
30abd221 1074 if (e->id == HDR_OTHER && e->name.caseCmp(name) == 0) {
b2c44718 1075 found = true;
9d7a89a5 1076 strListAdd(&result, e->value.termedBuf(), ',');
62e76326 1077 }
f66a9ef4 1078 }
62e76326 1079
b2c44718 1080 return found;
f66a9ef4 1081}
cb69b4c7 1082
14b463aa 1083/*
372fdfbf 1084 * Returns a the value of the specified list member, if any.
14b463aa 1085 */
30abd221 1086String
a9925b40 1087HttpHeader::getByNameListMember(const char *name, const char *member, const char separator) const
14b463aa 1088{
30abd221 1089 String header;
14b463aa 1090 const char *pos = NULL;
1091 const char *item;
1092 int ilen;
1093 int mlen = strlen(member);
1094
14b463aa 1095 assert(name);
1096
a9925b40 1097 header = getByName(name);
14b463aa 1098
30abd221 1099 String result;
650c4b88 1100
14b463aa 1101 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 1102 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
1103 result.append(item + mlen + 1, ilen - mlen - 1);
1104 break;
1105 }
14b463aa 1106 }
62e76326 1107
14b463aa 1108 return result;
1109}
1110
1111/*
1112 * returns a the value of the specified list member, if any.
1113 */
30abd221 1114String
a9925b40 1115HttpHeader::getListMember(http_hdr_type id, const char *member, const char separator) const
14b463aa 1116{
30abd221 1117 String header;
14b463aa 1118 const char *pos = NULL;
1119 const char *item;
1120 int ilen;
1121 int mlen = strlen(member);
1122
14b463aa 1123 assert(id >= 0);
1124
a9925b40 1125 header = getStrOrList(id);
30abd221 1126 String result;
14b463aa 1127
1128 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 1129 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
1130 result.append(item + mlen + 1, ilen - mlen - 1);
1131 break;
1132 }
14b463aa 1133 }
62e76326 1134
30abd221 1135 header.clean();
14b463aa 1136 return result;
1137}
1138
cb69b4c7 1139/* test if a field is present */
2ac76861 1140int
a9925b40 1141HttpHeader::has(http_hdr_type id) const
cb69b4c7 1142{
9bea1d5b 1143 assert_eid(id);
1144 assert(id != HDR_OTHER);
efc26e8e 1145 debugs(55, 9, this << " lookup for " << id);
a9925b40 1146 return CBIT_TEST(mask, id);
cb69b4c7 1147}
1148
1149void
a9925b40 1150HttpHeader::putInt(http_hdr_type id, int number)
cb69b4c7 1151{
9bea1d5b 1152 assert_eid(id);
1153 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
1154 assert(number >= 0);
eede25e7 1155 addEntry(new HttpHeaderEntry(id, NULL, xitoa(number)));
cb69b4c7 1156}
1157
47f6e231 1158void
1159HttpHeader::putInt64(http_hdr_type id, int64_t number)
1160{
1161 assert_eid(id);
1162 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1163 assert(number >= 0);
1164 addEntry(new HttpHeaderEntry(id, NULL, xint64toa(number)));
1165}
1166
cb69b4c7 1167void
a9925b40 1168HttpHeader::putTime(http_hdr_type id, time_t htime)
cb69b4c7 1169{
9bea1d5b 1170 assert_eid(id);
1171 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
a1d6870f 1172 assert(htime >= 0);
eede25e7 1173 addEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
cb69b4c7 1174}
2ac76861 1175
bbe58ab5 1176void
a9925b40 1177HttpHeader::insertTime(http_hdr_type id, time_t htime)
bbe58ab5 1178{
1179 assert_eid(id);
1180 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
1181 assert(htime >= 0);
eede25e7 1182 insertEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
bbe58ab5 1183}
1184
cb69b4c7 1185void
a9925b40 1186HttpHeader::putStr(http_hdr_type id, const char *str)
cb69b4c7 1187{
9bea1d5b 1188 assert_eid(id);
1189 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
1190 assert(str);
eede25e7 1191 addEntry(new HttpHeaderEntry(id, NULL, str));
cb69b4c7 1192}
1193
63259c34 1194void
a9925b40 1195HttpHeader::putAuth(const char *auth_scheme, const char *realm)
63259c34 1196{
a9925b40 1197 assert(auth_scheme && realm);
1198 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE, "%s realm=\"%s\"", auth_scheme, realm);
63259c34 1199}
1200
99edd1c3 1201void
a9925b40 1202HttpHeader::putCc(const HttpHdrCc * cc)
99edd1c3 1203{
9bea1d5b 1204 MemBuf mb;
1205 Packer p;
a9925b40 1206 assert(cc);
9bea1d5b 1207 /* remove old directives if any */
a9925b40 1208 delById(HDR_CACHE_CONTROL);
9bea1d5b 1209 /* pack into mb */
2fe7eff9 1210 mb.init();
9bea1d5b 1211 packerToMemInit(&p, &mb);
4ce6e3b5 1212 cc->packInto(&p);
9bea1d5b 1213 /* put */
eede25e7 1214 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL, NULL, mb.buf));
9bea1d5b 1215 /* cleanup */
1216 packerClean(&p);
2fe7eff9 1217 mb.clean();
99edd1c3 1218}
1219
d192d11f 1220void
a9925b40 1221HttpHeader::putContRange(const HttpHdrContRange * cr)
d192d11f 1222{
9bea1d5b 1223 MemBuf mb;
1224 Packer p;
a9925b40 1225 assert(cr);
9bea1d5b 1226 /* remove old directives if any */
a9925b40 1227 delById(HDR_CONTENT_RANGE);
9bea1d5b 1228 /* pack into mb */
2fe7eff9 1229 mb.init();
9bea1d5b 1230 packerToMemInit(&p, &mb);
1231 httpHdrContRangePackInto(cr, &p);
1232 /* put */
eede25e7 1233 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE, NULL, mb.buf));
9bea1d5b 1234 /* cleanup */
1235 packerClean(&p);
2fe7eff9 1236 mb.clean();
d192d11f 1237}
1238
1239void
a9925b40 1240HttpHeader::putRange(const HttpHdrRange * range)
d192d11f 1241{
9bea1d5b 1242 MemBuf mb;
1243 Packer p;
a9925b40 1244 assert(range);
9bea1d5b 1245 /* remove old directives if any */
a9925b40 1246 delById(HDR_RANGE);
9bea1d5b 1247 /* pack into mb */
2fe7eff9 1248 mb.init();
9bea1d5b 1249 packerToMemInit(&p, &mb);
528b2c61 1250 range->packInto(&p);
9bea1d5b 1251 /* put */
eede25e7 1252 addEntry(new HttpHeaderEntry(HDR_RANGE, NULL, mb.buf));
9bea1d5b 1253 /* cleanup */
1254 packerClean(&p);
2fe7eff9 1255 mb.clean();
d192d11f 1256}
1257
43ae1d95 1258void
a9925b40 1259HttpHeader::putSc(HttpHdrSc *sc)
43ae1d95 1260{
1261 MemBuf mb;
1262 Packer p;
a9925b40 1263 assert(sc);
43ae1d95 1264 /* remove old directives if any */
f48978e6 1265 delById(HDR_SURROGATE_CONTROL);
43ae1d95 1266 /* pack into mb */
2fe7eff9 1267 mb.init();
43ae1d95 1268 packerToMemInit(&p, &mb);
45a58345 1269 sc->packInto(&p);
43ae1d95 1270 /* put */
eede25e7 1271 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL, NULL, mb.buf));
43ae1d95 1272 /* cleanup */
1273 packerClean(&p);
2fe7eff9 1274 mb.clean();
43ae1d95 1275}
1276
bcfba8bd
AR
1277void
1278HttpHeader::putWarning(const int code, const char *const text)
1279{
1280 char buf[512];
1281 snprintf(buf, sizeof(buf), "%i %s \"%s\"", code, visible_appname_string, text);
1282 putStr(HDR_WARNING, buf);
1283}
1284
cb69b4c7 1285/* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1286void
a9925b40 1287HttpHeader::putExt(const char *name, const char *value)
cb69b4c7 1288{
9bea1d5b 1289 assert(name && value);
efc26e8e 1290 debugs(55, 8, this << " adds ext entry " << name << " : " << value);
eede25e7 1291 addEntry(new HttpHeaderEntry(HDR_OTHER, name, value));
528b2c61 1292}
1293
1294int
a9925b40 1295HttpHeader::getInt(http_hdr_type id) const
528b2c61 1296{
9bea1d5b 1297 assert_eid(id);
1298 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
528b2c61 1299 HttpHeaderEntry *e;
62e76326 1300
a9925b40 1301 if ((e = findEntry(id)))
eede25e7 1302 return e->getInt();
62e76326 1303
528b2c61 1304 return -1;
7c525cc2 1305}
1306
47f6e231 1307int64_t
1308HttpHeader::getInt64(http_hdr_type id) const
1309{
1310 assert_eid(id);
1311 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1312 HttpHeaderEntry *e;
1313
1314 if ((e = findEntry(id)))
1315 return e->getInt64();
1316
1317 return -1;
1318}
1319
de336bbe 1320time_t
a9925b40 1321HttpHeader::getTime(http_hdr_type id) const
cb69b4c7 1322{
9bea1d5b 1323 HttpHeaderEntry *e;
1324 time_t value = -1;
1325 assert_eid(id);
1326 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
62e76326 1327
a9925b40 1328 if ((e = findEntry(id))) {
9d7a89a5 1329 value = parse_rfc1123(e->value.termedBuf());
62e76326 1330 httpHeaderNoteParsedEntry(e->id, e->value, value < 0);
d8b249ef 1331 }
62e76326 1332
9bea1d5b 1333 return value;
cb69b4c7 1334}
1335
99edd1c3 1336/* sync with httpHeaderGetLastStr */
de336bbe 1337const char *
a9925b40 1338HttpHeader::getStr(http_hdr_type id) const
cb69b4c7 1339{
9bea1d5b 1340 HttpHeaderEntry *e;
1341 assert_eid(id);
1342 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1343
a9925b40 1344 if ((e = findEntry(id))) {
62e76326 1345 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
9d7a89a5 1346 return e->value.termedBuf();
d8b249ef 1347 }
62e76326 1348
9bea1d5b 1349 return NULL;
cb69b4c7 1350}
1351
a622fff0 1352/* unusual */
1353const char *
a9925b40 1354HttpHeader::getLastStr(http_hdr_type id) const
a622fff0 1355{
9bea1d5b 1356 HttpHeaderEntry *e;
1357 assert_eid(id);
1358 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1359
a9925b40 1360 if ((e = findLastEntry(id))) {
62e76326 1361 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
9d7a89a5 1362 return e->value.termedBuf();
a622fff0 1363 }
62e76326 1364
9bea1d5b 1365 return NULL;
a622fff0 1366}
1367
7faf2bdb 1368HttpHdrCc *
a9925b40 1369HttpHeader::getCc() const
cb69b4c7 1370{
a9925b40 1371 if (!CBIT_TEST(mask, HDR_CACHE_CONTROL))
62e76326 1372 return NULL;
35b88ed2 1373 PROF_start(HttpHeader_getCc);
62e76326 1374
c57b8f05
FC
1375 String s;
1376 getList(HDR_CACHE_CONTROL, &s);
62e76326 1377
c57b8f05 1378 HttpHdrCc *cc=new HttpHdrCc();
7ebe76de
FC
1379
1380 if (!cc->parse(s)) {
cf66f10a
FC
1381 delete cc;
1382 cc = NULL;
1383 }
62e76326 1384
95dc7ff4 1385 ++ HttpHeaderStats[owner].ccParsedCount;
62e76326 1386
9bea1d5b 1387 if (cc)
a9925b40 1388 httpHdrCcUpdateStats(cc, &HttpHeaderStats[owner].ccTypeDistr);
62e76326 1389
9bea1d5b 1390 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL, s, !cc);
62e76326 1391
35b88ed2 1392 PROF_stop(HttpHeader_getCc);
62e76326 1393
9bea1d5b 1394 return cc;
cb69b4c7 1395}
1396
02922e76 1397HttpHdrRange *
a9925b40 1398HttpHeader::getRange() const
02922e76 1399{
9bea1d5b 1400 HttpHdrRange *r = NULL;
1401 HttpHeaderEntry *e;
1402 /* some clients will send "Request-Range" _and_ *matching* "Range"
1403 * who knows, some clients might send Request-Range only;
1404 * this "if" should work correctly in both cases;
1405 * hopefully no clients send mismatched headers! */
62e76326 1406
a9925b40 1407 if ((e = findEntry(HDR_RANGE)) ||
1408 (e = findEntry(HDR_REQUEST_RANGE))) {
62e76326 1409 r = HttpHdrRange::ParseCreate(&e->value);
1410 httpHeaderNoteParsedEntry(e->id, e->value, !r);
d192d11f 1411 }
62e76326 1412
9bea1d5b 1413 return r;
02922e76 1414}
1415
43ae1d95 1416HttpHdrSc *
a9925b40 1417HttpHeader::getSc() const
43ae1d95 1418{
a9925b40 1419 if (!CBIT_TEST(mask, HDR_SURROGATE_CONTROL))
43ae1d95 1420 return NULL;
1421
30abd221 1422 String s;
26ac0430 1423
35b88ed2 1424 (void) getList(HDR_SURROGATE_CONTROL, &s);
43ae1d95 1425
45a58345 1426 HttpHdrSc *sc = httpHdrScParseCreate(s);
43ae1d95 1427
95dc7ff4 1428 ++ HttpHeaderStats[owner].ccParsedCount;
43ae1d95 1429
1430 if (sc)
45a58345 1431 sc->updateStats(&HttpHeaderStats[owner].scTypeDistr);
43ae1d95 1432
1433 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL, s, !sc);
1434
1435 return sc;
1436}
1437
d76fcfa7 1438HttpHdrContRange *
a9925b40 1439HttpHeader::getContRange() const
d76fcfa7 1440{
9bea1d5b 1441 HttpHdrContRange *cr = NULL;
1442 HttpHeaderEntry *e;
62e76326 1443
a9925b40 1444 if ((e = findEntry(HDR_CONTENT_RANGE))) {
9d7a89a5 1445 cr = httpHdrContRangeParseCreate(e->value.termedBuf());
62e76326 1446 httpHeaderNoteParsedEntry(e->id, e->value, !cr);
d8b249ef 1447 }
62e76326 1448
9bea1d5b 1449 return cr;
cb69b4c7 1450}
1451
99edd1c3 1452const char *
a9925b40 1453HttpHeader::getAuth(http_hdr_type id, const char *auth_scheme) const
cb69b4c7 1454{
9bea1d5b 1455 const char *field;
1456 int l;
a9925b40 1457 assert(auth_scheme);
1458 field = getStr(id);
62e76326 1459
9bea1d5b 1460 if (!field) /* no authorization field */
62e76326 1461 return NULL;
1462
9bea1d5b 1463 l = strlen(auth_scheme);
62e76326 1464
9bea1d5b 1465 if (!l || strncasecmp(field, auth_scheme, l)) /* wrong scheme */
62e76326 1466 return NULL;
1467
9bea1d5b 1468 field += l;
62e76326 1469
9bea1d5b 1470 if (!xisspace(*field)) /* wrong scheme */
62e76326 1471 return NULL;
1472
9bea1d5b 1473 /* skip white space */
95dc7ff4 1474 for (; field && xisspace(*field); ++field);
62e76326 1475
9bea1d5b 1476 if (!*field) /* no authorization cookie */
62e76326 1477 return NULL;
1478
8bdd0cec
AJ
1479 static char decodedAuthToken[8192];
1480 const int decodedLen = base64_decode(decodedAuthToken, sizeof(decodedAuthToken)-1, field);
1481 decodedAuthToken[decodedLen] = '\0';
1482 return decodedAuthToken;
cb69b4c7 1483}
1484
a9771e51 1485ETag
a9925b40 1486HttpHeader::getETag(http_hdr_type id) const
a9771e51 1487{
26ac0430 1488 ETag etag = {NULL, -1};
9bea1d5b 1489 HttpHeaderEntry *e;
1490 assert(Headers[id].type == ftETag); /* must be of an appropriate type */
62e76326 1491
a9925b40 1492 if ((e = findEntry(id)))
9d7a89a5 1493 etagParseInit(&etag, e->value.termedBuf());
62e76326 1494
9bea1d5b 1495 return etag;
a9771e51 1496}
1497
1498TimeOrTag
a9925b40 1499HttpHeader::getTimeOrTag(http_hdr_type id) const
a9771e51 1500{
9bea1d5b 1501 TimeOrTag tot;
1502 HttpHeaderEntry *e;
1503 assert(Headers[id].type == ftDate_1123_or_ETag); /* must be of an appropriate type */
1504 memset(&tot, 0, sizeof(tot));
62e76326 1505
a9925b40 1506 if ((e = findEntry(id))) {
9d7a89a5 1507 const char *str = e->value.termedBuf();
62e76326 1508 /* try as an ETag */
1509
1510 if (etagParseInit(&tot.tag, str)) {
1511 tot.valid = tot.tag.str != NULL;
1512 tot.time = -1;
1513 } else {
1514 /* or maybe it is time? */
1515 tot.time = parse_rfc1123(str);
1516 tot.valid = tot.time >= 0;
1517 tot.tag.str = NULL;
1518 }
a9771e51 1519 }
62e76326 1520
9bea1d5b 1521 assert(tot.time < 0 || !tot.tag.str); /* paranoid */
1522 return tot;
a9771e51 1523}
1524
cb69b4c7 1525/*
1526 * HttpHeaderEntry
1527 */
1528
eede25e7 1529HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId, const char *aName, const char *aValue)
cb69b4c7 1530{
eede25e7 1531 assert_eid(anId);
1532 id = anId;
62e76326 1533
9bea1d5b 1534 if (id != HDR_OTHER)
eede25e7 1535 name = Headers[id].name;
9bea1d5b 1536 else
eede25e7 1537 name = aName;
62e76326 1538
eede25e7 1539 value = aValue;
62e76326 1540
95dc7ff4 1541 ++ Headers[id].stat.aliveCount;
62e76326 1542
9d7a89a5 1543 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name << " : " << value );
eede25e7 1544}
62e76326 1545
eede25e7 1546HttpHeaderEntry::~HttpHeaderEntry()
2ac76861 1547{
eede25e7 1548 assert_eid(id);
9d7a89a5 1549 debugs(55, 9, "destroying entry " << this << ": '" << name << ": " << value << "'");
30abd221 1550 /* clean name if needed */
1551
1552 if (id == HDR_OTHER)
1553 name.clean();
1554
1555 value.clean();
62e76326 1556
eede25e7 1557 assert(Headers[id].stat.aliveCount);
62e76326 1558
5e263176 1559 -- Headers[id].stat.aliveCount;
62e76326 1560
eede25e7 1561 id = HDR_BAD_HDR;
cb69b4c7 1562}
1563
eede25e7 1564/* parses and inits header entry, returns true/false */
cdce6c61 1565HttpHeaderEntry *
eede25e7 1566HttpHeaderEntry::parse(const char *field_start, const char *field_end)
cb69b4c7 1567{
9bea1d5b 1568 /* note: name_start == field_start */
52d3f198 1569 const char *name_end = (const char *)memchr(field_start, ':', field_end - field_start);
26ac0430 1570 int name_len = name_end ? name_end - field_start :0;
9bea1d5b 1571 const char *value_start = field_start + name_len + 1; /* skip ':' */
1572 /* note: value_end == field_end */
1573
95dc7ff4 1574 ++ HeaderEntryParsedCount;
9bea1d5b 1575
1576 /* do we have a valid field name within this field? */
62e76326 1577
9bea1d5b 1578 if (!name_len || name_end > field_end)
cdce6c61 1579 return NULL;
62e76326 1580
67746eea 1581 if (name_len > 65534) {
1582 /* String must be LESS THAN 64K and it adds a terminating NULL */
e0236918 1583 debugs(55, DBG_IMPORTANT, "WARNING: ignoring header name of " << name_len << " bytes");
cdce6c61 1584 return NULL;
25acfb53 1585 }
62e76326 1586
52d3f198 1587 if (Config.onoff.relaxed_header_parser && xisspace(field_start[name_len - 1])) {
bf8fe701 1588 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
1589 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start, field_end) << "'");
52d3f198 1590
1591 while (name_len > 0 && xisspace(field_start[name_len - 1]))
5e263176 1592 --name_len;
52d3f198 1593
1594 if (!name_len)
cdce6c61 1595 return NULL;
52d3f198 1596 }
1597
9bea1d5b 1598 /* now we know we can parse it */
62e76326 1599
bf8fe701 1600 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start, field_end) << "'");
62e76326 1601
9bea1d5b 1602 /* is it a "known" field? */
cdce6c61 1603 http_hdr_type id = httpHeaderIdByName(field_start, name_len, Headers, HDR_ENUM_END);
1604
30abd221 1605 String name;
cdce6c61 1606
30abd221 1607 String value;
62e76326 1608
9bea1d5b 1609 if (id < 0)
62e76326 1610 id = HDR_OTHER;
1611
9bea1d5b 1612 assert_eid(id);
62e76326 1613
9bea1d5b 1614 /* set field name */
1615 if (id == HDR_OTHER)
eede25e7 1616 name.limitInit(field_start, name_len);
9bea1d5b 1617 else
eede25e7 1618 name = Headers[id].name;
62e76326 1619
9bea1d5b 1620 /* trim field value */
1621 while (value_start < field_end && xisspace(*value_start))
95dc7ff4 1622 ++value_start;
62e76326 1623
52d3f198 1624 while (value_start < field_end && xisspace(field_end[-1]))
5e263176 1625 --field_end;
52d3f198 1626
67746eea 1627 if (field_end - value_start > 65534) {
1628 /* String must be LESS THAN 64K and it adds a terminating NULL */
e0236918 1629 debugs(55, DBG_IMPORTANT, "WARNING: ignoring '" << name << "' header of " << (field_end - value_start) << " bytes");
62e76326 1630
eede25e7 1631 if (id == HDR_OTHER)
30abd221 1632 name.clean();
62e76326 1633
cdce6c61 1634 return NULL;
25acfb53 1635 }
62e76326 1636
9bea1d5b 1637 /* set field value */
eede25e7 1638 value.limitInit(value_start, field_end - value_start);
62e76326 1639
95dc7ff4 1640 ++ Headers[id].stat.seenCount;
62e76326 1641
9d7a89a5 1642 debugs(55, 9, "parsed HttpHeaderEntry: '" << name << ": " << value << "'");
62e76326 1643
9d7a89a5 1644 return new HttpHeaderEntry(id, name.termedBuf(), value.termedBuf());
cb69b4c7 1645}
1646
99edd1c3 1647HttpHeaderEntry *
eede25e7 1648HttpHeaderEntry::clone() const
cb69b4c7 1649{
9d7a89a5 1650 return new HttpHeaderEntry(id, name.termedBuf(), value.termedBuf());
cb69b4c7 1651}
1652
de336bbe 1653void
eede25e7 1654HttpHeaderEntry::packInto(Packer * p) const
cb69b4c7 1655{
eede25e7 1656 assert(p);
9d7a89a5 1657 packerAppend(p, name.rawBuf(), name.size());
9bea1d5b 1658 packerAppend(p, ": ", 2);
9d7a89a5 1659 packerAppend(p, value.rawBuf(), value.size());
9bea1d5b 1660 packerAppend(p, "\r\n", 2);
cb69b4c7 1661}
1662
eede25e7 1663int
1664HttpHeaderEntry::getInt() const
1665{
1666 assert_eid (id);
1667 assert (Headers[id].type == ftInt);
1668 int val = -1;
9d7a89a5 1669 int ok = httpHeaderParseInt(value.termedBuf(), &val);
eede25e7 1670 httpHeaderNoteParsedEntry(id, value, !ok);
1671 /* XXX: Should we check ok - ie
1672 * return ok ? -1 : value;
1673 */
1674 return val;
1675}
1676
47f6e231 1677int64_t
1678HttpHeaderEntry::getInt64() const
1679{
1680 assert_eid (id);
1681 assert (Headers[id].type == ftInt64);
1682 int64_t val = -1;
9d7a89a5 1683 int ok = httpHeaderParseOffset(value.termedBuf(), &val);
47f6e231 1684 httpHeaderNoteParsedEntry(id, value, !ok);
1685 /* XXX: Should we check ok - ie
1686 * return ok ? -1 : value;
1687 */
1688 return val;
1689}
1690
cb69b4c7 1691static void
30abd221 1692httpHeaderNoteParsedEntry(http_hdr_type id, String const &context, int error)
cb69b4c7 1693{
95dc7ff4 1694 ++ Headers[id].stat.parsCount;
62e76326 1695
9bea1d5b 1696 if (error) {
95dc7ff4 1697 ++ Headers[id].stat.errCount;
9d7a89a5 1698 debugs(55, 2, "cannot parse hdr field: '" << Headers[id].name << ": " << context << "'");
cb69b4c7 1699 }
cb69b4c7 1700}
1701
cb69b4c7 1702/*
12cf1be2 1703 * Reports
cb69b4c7 1704 */
cb69b4c7 1705
fcd2d3ef 1706/* tmp variable used to pass stat info to dumpers */
0cdcddb9 1707extern const HttpHeaderStat *dump_stat; /* argh! */
fcd2d3ef 1708const HttpHeaderStat *dump_stat = NULL;
1709
59a09b98 1710void
9bea1d5b 1711httpHeaderFieldStatDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1712{
9bea1d5b 1713 const int id = (int) val;
1714 const int valid_id = id >= 0 && id < HDR_ENUM_END;
9d7a89a5 1715 const char *name = valid_id ? Headers[id].name.termedBuf() : "INVALID";
9bea1d5b 1716 int visible = count > 0;
1717 /* for entries with zero count, list only those that belong to current type of message */
62e76326 1718
9bea1d5b 1719 if (!visible && valid_id && dump_stat->owner_mask)
62e76326 1720 visible = CBIT_TEST(*dump_stat->owner_mask, id);
1721
9bea1d5b 1722 if (visible)
62e76326 1723 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
1724 id, name, count, xdiv(count, dump_stat->busyDestroyedCount));
cb69b4c7 1725}
1726
12cf1be2 1727static void
9bea1d5b 1728httpHeaderFldsPerHdrDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1729{
9bea1d5b 1730 if (count)
62e76326 1731 storeAppendPrintf(sentry, "%2d\t %5d\t %5d\t %6.2f\n",
1732 idx, (int) val, count,
1733 xpercent(count, dump_stat->destroyedCount));
cb69b4c7 1734}
1735
cb69b4c7 1736static void
9bea1d5b 1737httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e)
12cf1be2 1738{
9bea1d5b 1739 assert(hs && e);
1740
1741 dump_stat = hs;
1742 storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label);
1743 storeAppendPrintf(e, "\nField type distribution\n");
1744 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1745 "id", "name", "count", "#/header");
96886986 1746 hs->fieldTypeDistr.dump(e, httpHeaderFieldStatDumper);
9bea1d5b 1747 storeAppendPrintf(e, "\nCache-control directives distribution\n");
1748 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1749 "id", "name", "count", "#/cc_field");
96886986 1750 hs->ccTypeDistr.dump(e, httpHdrCcStatDumper);
43ae1d95 1751 storeAppendPrintf(e, "\nSurrogate-control directives distribution\n");
1752 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1753 "id", "name", "count", "#/sc_field");
96886986 1754 hs->scTypeDistr.dump(e, httpHdrScStatDumper);
9bea1d5b 1755 storeAppendPrintf(e, "\nNumber of fields per header distribution\n");
1756 storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n",
62e76326 1757 "id", "#flds", "count", "%total");
96886986 1758 hs->hdrUCountDistr.dump(e, httpHeaderFldsPerHdrDumper);
41b43e70 1759 storeAppendPrintf(e, "\n");
9bea1d5b 1760 dump_stat = NULL;
cb69b4c7 1761}
1762
12cf1be2 1763void
9bea1d5b 1764httpHeaderStoreReport(StoreEntry * e)
cb69b4c7 1765{
9bea1d5b 1766 int i;
1767 http_hdr_type ht;
1768 assert(e);
1769
1770 HttpHeaderStats[0].parsedCount =
62e76326 1771 HttpHeaderStats[hoRequest].parsedCount + HttpHeaderStats[hoReply].parsedCount;
9bea1d5b 1772 HttpHeaderStats[0].ccParsedCount =
62e76326 1773 HttpHeaderStats[hoRequest].ccParsedCount + HttpHeaderStats[hoReply].ccParsedCount;
9bea1d5b 1774 HttpHeaderStats[0].destroyedCount =
62e76326 1775 HttpHeaderStats[hoRequest].destroyedCount + HttpHeaderStats[hoReply].destroyedCount;
9bea1d5b 1776 HttpHeaderStats[0].busyDestroyedCount =
62e76326 1777 HttpHeaderStats[hoRequest].busyDestroyedCount + HttpHeaderStats[hoReply].busyDestroyedCount;
9bea1d5b 1778
95dc7ff4 1779 for (i = 1; i < HttpHeaderStatCount; ++i) {
62e76326 1780 httpHeaderStatDump(HttpHeaderStats + i, e);
12cf1be2 1781 }
62e76326 1782
9bea1d5b 1783 /* field stats for all messages */
1784 storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n");
62e76326 1785
077fe581 1786 storeAppendPrintf(e, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
62e76326 1787 "id", "name", "#alive", "%err", "%repeat");
1788
e6ccf245 1789 for (ht = (http_hdr_type)0; ht < HDR_ENUM_END; ++ht) {
62e76326 1790 HttpHeaderFieldInfo *f = Headers + ht;
077fe581 1791 storeAppendPrintf(e, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
9d7a89a5 1792 f->id, f->name.termedBuf(), f->stat.aliveCount,
62e76326 1793 xpercent(f->stat.errCount, f->stat.parsCount),
1794 xpercent(f->stat.repCount, f->stat.seenCount));
12cf1be2 1795 }
62e76326 1796
9bea1d5b 1797 storeAppendPrintf(e, "Headers Parsed: %d + %d = %d\n",
62e76326 1798 HttpHeaderStats[hoRequest].parsedCount,
1799 HttpHeaderStats[hoReply].parsedCount,
1800 HttpHeaderStats[0].parsedCount);
9bea1d5b 1801 storeAppendPrintf(e, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount);
cb69b4c7 1802}
97474590 1803
e6ccf245 1804http_hdr_type
9b558d8a 1805httpHeaderIdByName(const char *name, size_t name_len, const HttpHeaderFieldInfo * info, int end)
97474590 1806{
1545d2d1
AJ
1807 if (name_len > 0) {
1808 for (int i = 0; i < end; ++i) {
1809 if (name_len != info[i].name.size())
1810 continue;
62e76326 1811
1545d2d1
AJ
1812 if (!strncasecmp(name, info[i].name.rawBuf(), name_len))
1813 return info[i].id;
1814 }
97474590 1815 }
62e76326 1816
e6ccf245 1817 return HDR_BAD_HDR;
97474590 1818}
1819
e6ccf245 1820http_hdr_type
9bea1d5b 1821httpHeaderIdByNameDef(const char *name, int name_len)
97474590 1822{
9bea1d5b 1823 if (!Headers)
62e76326 1824 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1825
9bea1d5b 1826 return httpHeaderIdByName(name, name_len, Headers, HDR_ENUM_END);
97474590 1827}
efd900cb 1828
1829const char *
9bea1d5b 1830httpHeaderNameById(int id)
efd900cb 1831{
9bea1d5b 1832 if (!Headers)
62e76326 1833 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1834
9bea1d5b 1835 assert(id >= 0 && id < HDR_ENUM_END);
62e76326 1836
9d7a89a5 1837 return Headers[id].name.termedBuf();
efd900cb 1838}
0353e724 1839
924f73bc 1840int
a9925b40 1841HttpHeader::hasListMember(http_hdr_type id, const char *member, const char separator) const
924f73bc 1842{
1843 int result = 0;
1844 const char *pos = NULL;
1845 const char *item;
1846 int ilen;
1847 int mlen = strlen(member);
1848
924f73bc 1849 assert(id >= 0);
1850
30abd221 1851 String header (getStrOrList(id));
924f73bc 1852
9d0cdbb9 1853 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
380e7faf 1854 if (strncasecmp(item, member, mlen) == 0
9d0cdbb9 1855 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1856 result = 1;
1857 break;
1858 }
1859 }
1860
1861 return result;
1862}
1863
1864int
a9925b40 1865HttpHeader::hasByNameListMember(const char *name, const char *member, const char separator) const
9d0cdbb9 1866{
1867 int result = 0;
1868 const char *pos = NULL;
1869 const char *item;
1870 int ilen;
1871 int mlen = strlen(member);
1872
9d0cdbb9 1873 assert(name);
1874
30abd221 1875 String header (getByName(name));
9d0cdbb9 1876
924f73bc 1877 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
380e7faf 1878 if (strncasecmp(item, member, mlen) == 0
924f73bc 1879 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1880 result = 1;
1881 break;
1882 }
1883 }
1884
1885 return result;
1886}
1887
2cdeea82 1888void
1889HttpHeader::removeHopByHopEntries()
1890{
1891 removeConnectionHeaderEntries();
26ac0430 1892
dcf3665b
AJ
1893 const HttpHeaderEntry *e;
1894 HttpHeaderPos pos = HttpHeaderInitPos;
1895 int headers_deleted = 0;
1896 while ((e = getEntry(&pos))) {
26ac0430
AJ
1897 int id = e->id;
1898 if (CBIT_TEST(HopByHopHeadersMask, id)) {
1899 delAt(pos, headers_deleted);
1900 CBIT_CLR(mask, id);
1901 }
dcf3665b 1902 }
2cdeea82 1903}
1904
924f73bc 1905void
1906HttpHeader::removeConnectionHeaderEntries()
1907{
a9925b40 1908 if (has(HDR_CONNECTION)) {
924f73bc 1909 /* anything that matches Connection list member will be deleted */
30abd221 1910 String strConnection;
26ac0430
AJ
1911
1912 (void) getList(HDR_CONNECTION, &strConnection);
924f73bc 1913 const HttpHeaderEntry *e;
1914 HttpHeaderPos pos = HttpHeaderInitPos;
1915 /*
1916 * think: on-average-best nesting of the two loops (hdrEntry
1917 * and strListItem) @?@
1918 */
1919 /*
1920 * maybe we should delete standard stuff ("keep-alive","close")
1921 * from strConnection first?
1922 */
1923
ba9fb01d 1924 int headers_deleted = 0;
a9925b40 1925 while ((e = getEntry(&pos))) {
9d7a89a5 1926 if (strListIsMember(&strConnection, e->name.termedBuf(), ','))
ba9fb01d 1927 delAt(pos, headers_deleted);
924f73bc 1928 }
ba9fb01d 1929 if (headers_deleted)
1930 refreshMask();
924f73bc 1931 }
1932}