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