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