]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/esi/VarState.cc
5 * DEBUG: section 86 ESI processing
6 * AUTHOR: Robert Collins
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
38 #include "esi/VarState.h"
39 #include "HttpReply.h"
41 CBDATA_TYPE (ESIVarState
);
44 char const *ESIVariableUserAgent::esiUserOs
[]= {
51 char const * esiBrowsers
[]= {"MSIE",
58 ESIVarState::Variable::eval (ESIVarState
&state
, char const *subref
, char const *found_default
) const
60 /* No-op. We swallow it */
63 ESISegment::ListAppend (state
.getOutput(), found_default
, strlen (found_default
));
67 ESIVarState::hostUsed()
73 ESIVarState::cookieUsed()
79 ESIVarState::languageUsed()
85 ESIVarState::refererUsed()
91 ESIVarState::useragentUsed()
102 ESISegment::Pointer
&
103 ESIVarState::getOutput()
109 ESIVariableQuery::queryString() const
114 struct _query_elem
const *
115 ESIVariableQuery::queryVector() const {
120 ESIVariableQuery::queryElements() const
122 return query_elements
;
126 ESIVarState::feedData (const char *buf
, size_t len
)
128 /* TODO: if needed - tune to skip segment iteration */
129 debugs (86,6, "esiVarState::feedData: accepting " << len
<< " bytes");
130 ESISegment::ListAppend (input
, buf
, len
);
134 ESIVarState::extractList()
137 ESISegment::Pointer rv
= output
;
139 debugs(86, 6, "ESIVarStateExtractList: Extracted list");
144 ESIVarState::extractChar ()
147 fatal ("Attempt to extract variable state with no data fed in \n");
151 char *rv
= output
->listToChar();
153 ESISegmentFreeList (output
);
155 debugs(86, 6, "ESIVarStateExtractList: Extracted char");
162 esiVarStateFree (void *data
)
164 ESIVarState
*thisNode
= (ESIVarState
*)data
;
165 thisNode
->freeResources();
168 ESIVarState::~ESIVarState()
172 while (variablesForCleanup
.size())
173 delete variablesForCleanup
.pop_back();
175 delete defaultVariable
;
179 ESIVarState::freeResources()
182 ESISegmentFreeList (output
);
187 ESIVarState::operator new(size_t byteCount
)
189 assert (byteCount
== sizeof (ESIVarState
));
191 CBDATA_INIT_TYPE_FREECB(ESIVarState
, esiVarStateFree
);
192 rv
= (void *)cbdataAlloc (ESIVarState
);
197 ESIVarState::operator delete (void *address
)
199 cbdataFree (address
);
203 ESIVariableUserAgent::getProductVersion (char const *s
)
212 len
= strcspn (t
, " \r\n()<>@,;:\\\"/[]?={}");
214 return xstrndup (t
, len
+ 1);
217 ESIVariableQuery::ESIVariableQuery(char const *uri
) : query (NULL
), query_sz (0), query_elements (0), query_string (NULL
)
219 /* Count off the query elements */
220 char const *query_start
= strchr (uri
, '?');
222 if (query_start
&& query_start
[1] != '\0' ) {
224 query_string
= xstrdup (query_start
+ 1);
226 char const *query_pos
= query_start
+ 1;
228 while ((query_pos
= strchr (query_pos
, '&'))) {
233 query
= (_query_elem
*)memReallocBuf(query
, query_elements
* sizeof (struct _query_elem
),
235 query_pos
= query_start
+ 1;
239 char const *next
= strchr (query_pos
, '&');
240 char const *div
= strchr (query_pos
, '=');
245 assert (n
< query_elements
);
250 if (!(div
- query_pos
+ 1))
251 /* zero length between & and = or & and & */
254 query
[n
].var
= xstrndup (query_pos
, div
- query_pos
+ 1) ;
257 query
[n
].val
= xstrdup ("");
259 query
[n
].val
= xstrndup (div
+ 1, next
- div
- 1);
266 query_string
= xstrdup ("");
271 debugs(86, 6, "esiVarStateNew: Parsed Query string: '" << uri
<< "'");
273 while (n
< query_elements
) {
274 debugs(86, 6, "esiVarStateNew: Parsed Query element " << n
+ 1 << " '" << query
[n
].var
<< "'='" << query
[n
].val
<< "'");
280 ESIVariableQuery::~ESIVariableQuery()
285 for (i
= 0; i
< query_elements
; ++i
) {
286 safe_free(query
[i
].var
);
287 safe_free(query
[i
].val
);
290 memFreeBuf (query_sz
, query
);
293 safe_free (query_string
);
296 ESIVarState::ESIVarState (HttpHeader
const *aHeader
, char const *uri
)
297 : output (NULL
), hdr(hoReply
)
299 /* TODO: only grab the needed headers */
300 /* Note that as we pass these through to included requests, we
301 * cannot trim them */
304 /* populate our variables trie with the available variables.
305 * Additional ones can be added during the parsing.
306 * If there is a lazy evaluation approach to this, consider it!
308 defaultVariable
= new Variable
;
309 addVariable ("HTTP_ACCEPT_LANGUAGE", 20, new ESIVariableLanguage
);
310 addVariable ("HTTP_COOKIE", 11, new ESIVariableCookie
);
311 addVariable ("HTTP_HOST", 9, new ESIVariableHost
);
312 addVariable ("HTTP_REFERER", 12, new ESIVariableReferer
);
313 addVariable ("HTTP_USER_AGENT", 15, new ESIVariableUserAgent(*this));
314 addVariable ("QUERY_STRING", 12, new ESIVariableQuery(uri
));
318 ESIVarState::removeVariable (String
const &name
)
320 Variable
*candidate
= static_cast <Variable
*>(variables
.find (name
.rawBuf(), name
.size()));
324 /* Note - this involves:
325 * extend libTrie to have a remove() call.
326 * delete from the vector.
333 ESIVarState::addVariable(char const *name
, size_t len
, Variable
*aVariable
)
336 temp
.limitInit (name
, len
);
337 removeVariable (temp
);
338 variables
.add(name
, len
, aVariable
);
339 variablesForCleanup
.push_back(aVariable
);
342 ESIVariableUserAgent::~ESIVariableUserAgent()
344 safe_free (browserversion
);
347 ESIVariableUserAgent::ESIVariableUserAgent(ESIVarState
&state
)
350 * User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705) */
351 /* Grr this Node is painful - RFC 2616 specifies that 'by convention' the tokens are in order of importance
352 * in identifying the product. According to the RFC the above should be interpreted as:
353 * Product - Mozilla version 4.0
354 * in comments - compatible; .... 3705
356 * Useing the RFC a more appropriate header would be
357 * User-Agent: MSIE/6.0 Mozilla/4.0 Windows-NT/5.1 .NET-CLR/1.0.3705
358 * or something similar.
360 * Because we can't parse under those rules and get real-world useful answers, we follow the following
362 * if the string Windows appears in the header, the OS is WIN.
363 * If the string Mac appears in the header, the OS is MAC.
364 * If the string nix, or BSD appears in the header, the OS is UNIX.
365 * If the string MSIE appears in the header, the BROWSER is MSIE, and the version is the string from
366 * MSIE<sp> to the first ;, or end of string.
367 * If the String MSIE does not appear in the header, and MOZILLA does, we use the version from the
369 * if MOZILLA doesn't appear, the browser is set to OTHER.
370 * In future, this may be better implemented as a regexp.
373 if (state
.header().has(HDR_USER_AGENT
)) {
374 char const *s
= state
.header().getStr(HDR_USER_AGENT
);
375 UserOs
= identifyOs(s
);
378 /* Now the browser and version */
380 if ((t
= strstr (s
, "MSIE"))) {
381 browser
= ESI_BROWSER_MSIE
;
385 browserversion
= xstrdup ("");
390 browserversion
= xstrdup (t
+ 1);
392 browserversion
= xstrndup (t
+ 1, t1
-t
);
394 } else if (strstr (s
, "Mozilla")) {
395 browser
= ESI_BROWSER_MOZILLA
;
396 browserversion
= getProductVersion(s
);
398 browser
= ESI_BROWSER_OTHER
;
399 browserversion
= getProductVersion(s
);
402 UserOs
= ESI_OS_OTHER
;
403 browser
= ESI_BROWSER_OTHER
;
404 browserversion
= xstrdup ("");
408 ESIVariableUserAgent::esiUserOs_t
409 ESIVariableUserAgent::identifyOs(char const *s
) const
414 if (strstr (s
, "Windows"))
416 else if (strstr (s
, "Mac"))
418 else if (strstr (s
, "nix") || strstr (s
, "BSD"))
425 ESIVariableCookie::eval (ESIVarState
&state
, char const *subref
, char const *found_default
) const
427 const char *s
= NULL
;
430 if (state
.header().has(HDR_COOKIE
)) {
432 s
= state
.header().getStr (HDR_COOKIE
);
434 String S
= state
.header().getListMember (HDR_COOKIE
, subref
, ';');
437 ESISegment::ListAppend (state
.getOutput(), S
.rawBuf(), S
.size());
438 else if (found_default
)
439 ESISegment::ListAppend (state
.getOutput(), found_default
, strlen (found_default
));
445 ESISegment::ListAppend (state
.getOutput(), s
, strlen (s
));
449 ESIVariableHost::eval (ESIVarState
&state
, char const *subref
, char const *found_default
) const
451 const char *s
= NULL
;
454 if (!subref
&& state
.header().has(HDR_HOST
)) {
455 s
= state
.header().getStr (HDR_HOST
);
459 ESISegment::ListAppend (state
.getOutput(), s
, strlen (s
));
463 ESIVariableLanguage::eval (ESIVarState
&state
, char const *subref
, char const *found_default
) const
465 char const *s
= NULL
;
466 state
.languageUsed();
468 if (state
.header().has(HDR_ACCEPT_LANGUAGE
)) {
470 String
S (state
.header().getList (HDR_ACCEPT_LANGUAGE
));
471 ESISegment::ListAppend (state
.getOutput(), S
.rawBuf(), S
.size());
473 if (state
.header().hasListMember (HDR_ACCEPT_LANGUAGE
, subref
, ',')) {
479 ESISegment::ListAppend (state
.getOutput(), s
, strlen (s
));
483 ESISegment::ListAppend (state
.getOutput(), s
, strlen (s
));
488 ESIVariableQuery::eval (ESIVarState
&state
, char const *subref
, char const *found_default
) const
490 char const *s
= NULL
;
497 while (i
< queryElements() && !s
) {
498 if (!strcmp (subref
, queryVector()[i
].var
))
499 s
= queryVector()[i
].val
;
508 ESISegment::ListAppend (state
.getOutput(), s
, strlen (s
));
512 ESIVariableReferer::eval (ESIVarState
&state
, char const *subref
, char const *found_default
) const
514 const char *s
= NULL
;
517 if (!subref
&& state
.header().has(HDR_REFERER
))
518 s
= state
.header().getStr (HDR_REFERER
);
522 ESISegment::ListAppend (state
.getOutput(), s
, strlen (s
));
526 ESIVariableUserAgent::eval (ESIVarState
&state
, char const *subref
, char const *found_default
) const
528 char const *s
= NULL
;
529 state
.useragentUsed();
531 if (state
.header().has(HDR_USER_AGENT
)) {
533 s
= state
.header().getStr (HDR_USER_AGENT
);
535 if (!strcmp (subref
, "os")) {
536 s
= esiUserOs
[UserOs
];
537 } else if (!strcmp (subref
, "browser")) {
538 s
= esiBrowsers
[browser
];
539 } else if (!strcmp (subref
, "version")) {
540 s
= browserVersion();
547 ESISegment::ListAppend (state
.getOutput(), s
, strlen (s
));
550 /* thoughts on long term:
553 * hand off to handler.
554 * one handler for variables.
555 * one handler for each function.
558 class ESIVariableProcessor
;
564 static ESIFunction
*GetFunction (char const *symbol
, ESIVariableProcessor
&);
565 ESIFunction(ESIVariableProcessor
&);
569 ESIVariableProcessor
&processor
;
573 ESIFunction::ESIFunction(ESIVariableProcessor
&aProcessor
) : processor(aProcessor
)
577 ESIFunction::GetFunction(char const *symbol
, ESIVariableProcessor
&aProcessor
)
580 return new ESIFunction(aProcessor
);
585 class ESIVariableProcessor
589 ESIVariableProcessor(char *, ESISegment::Pointer
&, Trie
&, ESIVarState
*);
590 ~ESIVariableProcessor();
594 bool validChar (char c
);
595 void eval (ESIVarState::Variable
*var
, char const *subref
, char const *found_default
);
597 void identifyFunction();
599 ESISegment::Pointer
&output
;
601 ESIVarState
*varState
;
609 ESIVarState::Variable
*vartype
;
610 ESIFunction
*currentFunction
;
614 ESIVariableProcessor::eval (ESIVarState::Variable
*var
, char const *subref
, char const *found_default
)
621 var
->eval (*varState
, subref
, found_default
);
625 ESIVariableProcessor::validChar (char c
)
627 if (('A' <= c
&& c
<= 'Z') ||
628 ('a' <= c
&& c
<= 'z') ||
629 '_' == c
|| '-' == c
)
635 ESIVarState::Variable
*
636 ESIVarState::GetVar(char const *symbol
, int len
)
640 void *result
= variables
.find (symbol
, len
);
643 return static_cast<Variable
*>(result
);
645 return defaultVariable
;
651 char *string
= input
->listToChar();
652 ESISegmentFreeList (input
);
653 ESIVariableProcessor
theProcessor(string
, output
, variables
, this);
658 #define LOOKFORSTART 0
659 ESIVariableProcessor::ESIVariableProcessor(char *aString
, ESISegment::Pointer
&aSegment
, Trie
&aTrie
, ESIVarState
*aState
) :
660 string(aString
), output (aSegment
), variables(aTrie
), varState (aState
),
661 state(LOOKFORSTART
), pos(0), var_pos(0), done_pos(0), found_subref (NULL
),
662 found_default (NULL
), currentFunction(NULL
)
664 len
= strlen (string
);
665 vartype
= varState
->GetVar("",0);
672 /* because we are only used to process:
676 * buffering is ok - we won't delay the start of async activity, or
677 * of output data preparation
679 /* Should make these an enum or something...
682 ESIVariableProcessor::doFunction()
684 if (!currentFunction
)
687 /* stay in here whilst operating */
688 while (pos
< len
&& state
)
691 case 2: /* looking for variable name */
693 if (!validChar(string
[pos
])) {
694 /* not a variable name char */
697 vartype
= varState
->GetVar (string
+ var_pos
, pos
- var_pos
);
707 case 3: /* looking for variable subref, end bracket or default indicator */
709 if (string
[pos
] == ')') {
711 eval(vartype
, found_subref
, found_default
);
713 safe_free(found_subref
);
714 safe_free(found_default
);
715 state
= LOOKFORSTART
;
716 } else if (!found_subref
&& !found_default
&& string
[pos
] == '{') {
717 debugs(86, 6, "ESIVarStateDoIt: Subref of some sort");
718 /* subreference of some sort */
719 /* look for the entry name */
722 } else if (!found_default
&& string
[pos
] == '|') {
723 debugs(86, 6, "esiVarStateDoIt: Default present");
724 /* extract default value */
728 /* unexpected char, not a variable after all */
729 debugs(86, 6, "esiVarStateDoIt: unexpected char after varname");
730 state
= LOOKFORSTART
;
736 case 4: /* looking for variable subref */
738 if (string
[pos
] == '}') {
740 found_subref
= xstrndup (&string
[var_pos
], pos
- var_pos
+ 1);
741 debugs(86, 6, "esiVarStateDoIt: found end of variable subref '" << found_subref
<< "'");
744 } else if (!validChar (string
[pos
])) {
745 debugs(86, 6, "esiVarStateDoIt: found invalid char in variable subref");
746 /* not a valid subref */
747 safe_free(found_subref
);
748 state
= LOOKFORSTART
;
756 case 5: /* looking for a default value */
758 if (string
[pos
] == '\'') {
759 /* begins with a quote */
760 debugs(86, 6, "esiVarStateDoIt: found quoted default");
765 debugs(86, 6, "esiVarStateDoIt: found unquoted default");
772 case 6: /* looking for a quote terminate default value */
774 if (string
[pos
] == '\'') {
776 found_default
= xstrndup (&string
[var_pos
], pos
- var_pos
+ 1);
777 debugs(86, 6, "esiVarStateDoIt: found end of quoted default '" << found_default
<< "'");
784 case 7: /* looking for } terminate default value */
786 if (string
[pos
] == ')') {
787 /* end of default - end of variable*/
788 found_default
= xstrndup (&string
[var_pos
], pos
- var_pos
+ 1);
789 debugs(86, 6, "esiVarStateDoIt: found end of variable (w/ unquoted default) '" << found_default
<< "'");
790 eval(vartype
,found_subref
, found_default
);
792 safe_free(found_default
);
793 safe_free(found_subref
);
794 state
= LOOKFORSTART
;
801 fatal("esiVarStateDoIt: unexpected state\n");
806 ESIVariableProcessor::identifyFunction()
808 delete currentFunction
;
809 currentFunction
= ESIFunction::GetFunction (&string
[pos
], *this);
811 if (!currentFunction
) {
812 state
= LOOKFORSTART
;
814 state
= 2; /* process a function */
815 /* advance past function name */
821 ESIVariableProcessor::doIt()
823 assert (output
== NULL
);
826 /* skipping pre-variables */
828 if (string
[pos
] != '$') {
832 /* extract known plain text */
833 ESISegment::ListAppend (output
, string
+ done_pos
, pos
- done_pos
);
845 /* pos-done_pos chars are ready to copy */
847 ESISegment::ListAppend (output
, string
+done_pos
, pos
- done_pos
);
849 safe_free (found_default
);
851 safe_free (found_subref
);
854 ESIVariableProcessor::~ESIVariableProcessor()
856 delete currentFunction
;
860 /* XXX FIXME: this should be comma delimited, no? */
862 ESIVarState::buildVary (HttpReply
*rep
)
868 strcat (tempstr
, "Accept-Language ");
871 strcat (tempstr
, "Cookie ");
874 strcat (tempstr
, "Host ");
877 strcat (tempstr
, "Referer ");
880 strcat (tempstr
, "User-Agent ");
885 String
strVary (rep
->header
.getList (HDR_VARY
));
887 if (!strVary
.size() || strVary
[0] != '*') {
888 rep
->header
.putStr (HDR_VARY
, tempstr
);