]> git.ipfire.org Git - thirdparty/squid.git/blame - src/esi/Assign.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / esi / Assign.cc
CommitLineData
924f73bc 1/*
ef57eb7b 2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
924f73bc 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
924f73bc 7 */
8
bbc27441
AJ
9/* DEBUG: section 86 ESI processing */
10
582c2af2 11#include "squid.h"
3db19d36 12
454e8283 13/* MS Visual Studio Projects are monolithic, so we need the following
14 * #if to exclude the ESI code from compile process when not needed.
15 */
16#if (USE_SQUID_ESI == 1)
17
f99c2cfe
AR
18#include "esi/Assign.h"
19#include "esi/Context.h"
20#include "esi/Sequence.h"
924f73bc 21
924f73bc 22ESIAssign::~ESIAssign()
23{
24 if (value)
25 delete value;
26}
27
28ESIAssign::ESIAssign (ESIAssign const &old) : parent (NULL), varState (NULL), name (old.name), value (old.value ? new ESIVariableExpression (*old.value): NULL), variable (NULL), unevaluatedVariable(old.unevaluatedVariable)
29{}
30
31ESIAssign::ESIAssign (esiTreeParentPtr aParent, int attrcount, char const **attr, ESIContext *aContext) : parent (aParent), varState (NULL), name(), value (NULL), variable (NULL), unevaluatedVariable()
32{
33 /* TODO: grab content IFF no value was specified */
34 assert (aContext);
35
36 for (int i = 0; i < attrcount && attr[i]; i += 2) {
37 if (!strcmp(attr[i],"name")) {
38 /* the variables name is ... */
bf8fe701 39 debugs(86, 5, "ESIAssign::ESIAssign: Variable name '" << attr[i+1] << "'");
924f73bc 40 /* If there are duplicate name attributes, we simply use the
41 * last one
42 */
43 name = attr[i+1];
44 } else if (!strcmp(attr[i],"value")) {
45 /* short form assignment: */
bf8fe701 46 debugs(86, 5, "ESIAssign::ESIAssign: Unevaluated variable '" << attr[i+1] << "'");
924f73bc 47 /* Again, if there are duplicate attributes, we use the last */
48 unevaluatedVariable = attr[i+1];
49 } else {
50 /* ignore mistyped attributes. TODO:? error on these for user feedback - config parameter needed
51 */
52 }
53 }
54
55 varState = cbdataReference(aContext->varState);
56}
57
58void
59ESIAssign::evaluateVariable()
60{
61 if (variable.getRaw())
62 variable->process (false);
63
64 variable = NULL;
65
66 if (unevaluatedVariable.size()) {
bb790702 67 varState->feedData(unevaluatedVariable.rawBuf(), unevaluatedVariable.size());
924f73bc 68 char const *result = varState->extractChar ();
69
70 /* Consider activating this, when we want to evaluate variables to a
71 * value
72 */
73 // setTestResult(ESIExpression::Evaluate (expression));
74
75 value = new ESIVariableExpression (result);
76
77 safe_free (result);
78 }
79}
80
81void
82ESIAssign::provideData (ESISegment::Pointer data, ESIElement * source)
83{
84 assert (source == variable.getRaw());
85 char *result = data->listToChar();
86 unevaluatedVariable = result;
87 safe_free (result);
88}
89
90esiProcessResult_t
91ESIAssign::process (int dovars)
92{
93 assert (varState);
94
95 if (!value)
96 evaluateVariable();
97
98 if (!value)
99 return ESI_PROCESS_COMPLETE;
100
bb790702 101 varState->addVariable (name.rawBuf(), name.size(), value);
924f73bc 102
103 value = NULL;
104
bf8fe701 105 debugs(86, 5, "ESIAssign: Processed " << this);
924f73bc 106
107 return ESI_PROCESS_COMPLETE;
108}
109
110void
111ESIAssign::render(ESISegment::Pointer)
112{}
113
114ESIAssign::Pointer
115ESIAssign::makeCacheable() const
116{
117 ESIAssign *result = new ESIAssign (*this);
118
119 if (variable.getRaw())
120 result->variable = variable->makeCacheable();
121
122 return result;
123}
124
125ESIAssign::Pointer
126ESIAssign::makeUsable(esiTreeParentPtr aParent, ESIVarState &aVarState) const
127{
128 ESIAssign *result = new ESIAssign (*this);
129 result->parent = aParent;
130 result->varState = cbdataReference(&aVarState);
131
132 if (variable.getRaw())
133 result->variable = variable->makeUsable(result, aVarState);
134
135 return result;
136}
137
138void
139ESIAssign::finish()
140{
141 if (varState)
142 cbdataReferenceDone (varState);
143
144 if (parent.getRaw())
145 parent = NULL;
146}
147
148bool
149ESIAssign::addElement(ESIElement::Pointer anElement)
150{
151 /* we have a value, drop the element on the floor */
152
153 if (unevaluatedVariable.size())
154 return true;
155
156 if (!variable.getRaw())
157 variable = new esiSequence (this, false);
158
159 return variable->addElement (anElement);
160}
161
162ESIVariableExpression::~ESIVariableExpression()
163{}
164
30abd221 165ESIVariableExpression::ESIVariableExpression (String const &aString) : expression (aString)
924f73bc 166{}
167
168void
169ESIVariableExpression::eval (ESIVarState &state, char const *subref, char const *defaultOnEmpty) const
170{
171 /* XXX: Implement evaluation of the expression */
bb790702 172 ESISegment::ListAppend (state.getOutput(), expression.rawBuf(), expression.size());
924f73bc 173}
454e8283 174
175#endif /* USE_SQUID_ESI == 1 */
f53969cc 176