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