]> git.ipfire.org Git - thirdparty/squid.git/blob - src/esi/Context.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / esi / Context.h
1 /*
2 *
3 * SQUID Web Proxy Cache http://www.squid-cache.org/
4 * ----------------------------------------------------------
5 *
6 * Squid is the result of efforts by numerous individuals from
7 * the Internet community; see the CONTRIBUTORS file for full
8 * details. Many organizations have provided support for Squid's
9 * development; see the SPONSORS file for full details. Squid is
10 * Copyrighted (C) 2001 by the Regents of the University of
11 * California; see the COPYRIGHT file for full details. Squid
12 * incorporates software developed and/or copyrighted by other
13 * sources; see the CREDITS file for full details.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
28 *
29 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
30 */
31
32 #ifndef SQUID_ESICONTEXT_H
33 #define SQUID_ESICONTEXT_H
34
35 #include "clientStream.h"
36 #include "err_type.h"
37 #include "esi/Element.h"
38 #include "esi/Parser.h"
39 #include "http/StatusCode.h"
40
41 class ESIVarState;
42 class ClientHttpRequest;
43
44 /* ESIContext */
45
46 class ESIContext : public esiTreeParent, public ESIParserClient
47 {
48
49 public:
50 typedef RefCount<ESIContext> Pointer;
51 ESIContext() :
52 thisNode(NULL),
53 http(NULL),
54 errorpage(ERR_NONE),
55 errorstatus(Http::scNone),
56 errormessage(NULL),
57 rep(NULL),
58 outbound_offset(0),
59 readpos(0),
60 pos(0),
61 varState(NULL),
62 cachedASTInUse(false),
63 reading_(true),
64 processing(false) {
65 memset(&flags, 0, sizeof(flags));
66 }
67
68 ~ESIContext();
69
70 enum esiKick_t {
71 ESI_KICK_FAILED,
72 ESI_KICK_PENDING,
73 ESI_KICK_SENT,
74 ESI_KICK_INPROGRESS
75 };
76
77 /* when esi processing completes */
78 void provideData(ESISegment::Pointer, ESIElement *source);
79 void fail (ESIElement *source, char const*anError = NULL);
80 void startRead();
81 void finishRead();
82 bool reading() const;
83 void setError();
84 void setErrorMessage(char const *);
85
86 void addStackElement (ESIElement::Pointer element);
87 void addLiteral (const char *s, int len);
88
89 void finishChildren ();
90
91 clientStreamNode *thisNode; /* our stream node */
92 /* the request we are processing. HMM: cbdataReferencing this will result
93 * in a circular reference, so we don't. Note: we are automatically freed
94 * when it is, so thats ok. */
95 ClientHttpRequest *http;
96
97 struct {
98 int passthrough:1;
99 int oktosend:1;
100 int finished:1;
101
102 /* an error has occured, send full body replies
103 * regardless. Note that we don't fail midstream
104 * because we buffer until we can not fail
105 */
106 int error:1;
107
108 int finishedtemplate:1; /* we've read the entire template */
109 int clientwantsdata:1; /* we need to satisfy a read request */
110 int kicked:1; /* note on reentering the kick routine */
111 int detached:1; /* our downstream has detached */
112 } flags;
113
114 err_type errorpage; /* if we error what page to use */
115 Http::StatusCode errorstatus; /* if we error, what code to return */
116 char *errormessage; /* error to pass to error page */
117 HttpReply *rep; /* buffered until we pass data downstream */
118 ESISegment::Pointer buffered; /* unprocessed data - for whatever reason */
119 ESISegment::Pointer incoming;
120 /* processed data we are waiting to send, or for
121 * potential errors to be resolved
122 */
123 ESISegment::Pointer outbound;
124 ESISegment::Pointer outboundtail; /* our write segment */
125 /* the offset to the next character to send -
126 * non zero if we haven't sent the entire segment
127 * for some reason
128 */
129 size_t outbound_offset;
130 int64_t readpos; /* the logical position we are reading from */
131 int64_t pos; /* the logical position of outbound_offset in the data stream */
132
133 class ParserState
134 {
135
136 public:
137 ESIElement::Pointer stack[10]; /* a stack of esi elements that are open */
138 int stackdepth; /* self explanatory */
139 ESIParser::Pointer theParser;
140 ESIElement::Pointer top();
141 void init (ESIParserClient *);
142 bool inited() const;
143 ParserState();
144 void freeResources();
145 void popAll();
146 int parsing:1; /* libexpat is not reentrant on the same context */
147
148 private:
149 bool inited_;
150 }
151
152 parserState; /* todo factor this off somewhere else; */
153 ESIVarState *varState;
154 ESIElement::Pointer tree;
155
156 esiKick_t kick ();
157 RefCount<ESIContext> cbdataLocker;
158 bool failed() const {return flags.error != 0;}
159
160 bool cachedASTInUse;
161
162 private:
163 void fail ();
164 void freeResources();
165 void fixupOutboundTail();
166 void trimBlanks();
167 size_t send ();
168 bool reading_;
169 void appendOutboundData(ESISegment::Pointer theData);
170 esiProcessResult_t process ();
171 void parse();
172 void parseOneBuffer();
173 void updateCachedAST();
174 bool hasCachedAST() const;
175 void getCachedAST();
176 virtual void start(const char *el, const char **attr, size_t attrCount);
177 virtual void end(const char *el);
178 virtual void parserDefault (const char *s, int len);
179 virtual void parserComment (const char *s);
180 bool processing;
181
182 CBDATA_CLASS2(ESIContext);
183 };
184
185 #endif /* SQUID_ESICONTEXT_H */