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