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