4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 #ifndef SQUID_ICAPMODXACT_H
33 #define SQUID_ICAPMODXACT_H
36 #include "adaptation/icap/Xaction.h"
37 #include "adaptation/icap/InOut.h"
38 #include "adaptation/icap/Launcher.h"
41 * ICAPModXact implements ICAP REQMOD and RESPMOD transaction using
42 * ICAPXaction as the base. The ICAPModXact receives a virgin HTTP message
43 * from an ICAP vecoring point, (a.k.a., initiator), communicates with the
44 * ICAP server, and sends the adapted HTTP message headers back.
45 * Virgin/adapted HTTP message body is reveived/sent using BodyPipe
46 * interface. The initiator (or its associate) is expected to send and/or
47 * receive the HTTP body.
50 class ChunkedCodingParser
;
57 // estimated future presence and size of something (e.g., HTTP body)
63 SizedEstimate(); // not expected by default
64 void expect(int64_t aSize
); // expect with any, even unknown size
65 bool expected() const;
67 /* other members can be accessed iff expected() */
69 bool knownSize() const;
70 uint64_t size() const; // can be accessed iff knownSize()
73 enum { dtUnexpected
= -2, dtUnknown
= -1 };
74 int64_t theData
; // combines expectation and size info to save RAM
77 // Virgin body may be used for two activities: (a) writing preview or prime
78 // body to the ICAP server and (b) sending the body back in the echo mode.
79 // Both activities use the same BodyPipe and may be active at the same time.
80 // This class is used to maintain the state of body writing or sending
81 // activity and to coordinate consumption of the shared virgin body buffer.
88 void plan(); // the activity may happen; do not consume at or above offset
89 void disable(); // the activity wont continue; no consumption restrictions
91 bool active() const { return theState
== stActive
; }
92 bool disabled() const { return theState
== stDisabled
; }
94 // methods below require active()
96 uint64_t offset() const; // the absolute beginning of not-yet-acted-on data
97 void progress(size_t size
); // note processed body bytes
100 int64_t theStart
; // unprocessed virgin body data offset
102 typedef enum { stUndecided
, stActive
, stDisabled
} State
;
106 // maintains preview-related sizes
112 Preview(); // disabled
113 void enable(size_t anAd
); // enabled with advertised size
114 bool enabled() const;
116 /* other members can be accessed iff enabled() */
118 size_t ad() const; // advertised preview size
119 size_t debt() const; // remains to write
120 bool done() const; // wrote everything
121 bool ieof() const; // premature EOF
123 void wrote(size_t size
, bool wroteEof
);
128 enum State
{ stDisabled
, stWriting
, stIeof
, stDone
} theState
;
131 class ModXact
: public Xaction
, public BodyProducer
, public BodyConsumer
135 ModXact(HttpMsg
*virginHeader
, HttpRequest
*virginCause
, ServiceRep::Pointer
&s
);
138 // BodyProducer methods
139 virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer
);
140 virtual void noteBodyConsumerAborted(BodyPipe::Pointer
);
142 // BodyConsumer methods
143 virtual void noteMoreBodyDataAvailable(BodyPipe::Pointer
);
144 virtual void noteBodyProductionEnded(BodyPipe::Pointer
);
145 virtual void noteBodyProducerAborted(BodyPipe::Pointer
);
148 virtual void handleCommConnected();
149 virtual void handleCommWrote(size_t size
);
150 virtual void handleCommRead(size_t size
);
151 void handleCommWroteHeaders();
152 void handleCommWroteBody();
155 void noteServiceReady();
156 void noteServiceAvailable();
162 // bypasses exceptions if needed and possible
163 virtual void callException(const std::exception
&e
);
165 /// record error detail in the virgin request if possible
166 virtual void detailError(int errDetail
);
168 virtual void clearError();
171 virtual void start();
173 /// locates the request, either as a cause or as a virgin message itself
174 const HttpRequest
&virginRequest() const; // Must always be available
176 void estimateVirginBody();
177 void makeAdaptedBodyPipe(const char *what
);
179 void waitForService();
181 // will not send anything [else] on the adapted pipe
182 bool doneSending() const;
186 void writePreviewBody();
187 void writePrimeBody();
188 void writeSomeBody(const char *label
, size_t size
);
189 void decideWritingAfterPreview(const char *previewKind
);
193 virtual bool doneReading() const { return commEof
|| state
.doneParsing(); }
194 virtual bool doneWriting() const { return state
.doneWriting(); }
196 size_t virginContentSize(const VirginBodyAct
&act
) const;
197 const char *virginContentData(const VirginBodyAct
&act
) const;
198 bool virginBodyEndReached(const VirginBodyAct
&act
) const;
200 void makeRequestHeaders(MemBuf
&buf
);
201 void makeAllowHeader(MemBuf
&buf
);
202 void makeUsernameHeader(const HttpRequest
*request
, MemBuf
&buf
);
203 void addLastRequestChunk(MemBuf
&buf
);
204 void openChunk(MemBuf
&buf
, size_t chunkSize
, bool ieof
);
205 void closeChunk(MemBuf
&buf
);
206 void virginConsume();
207 void finishNullOrEmptyBodyPreview(MemBuf
&buf
);
209 void decideOnPreview();
210 void decideOnRetries();
211 bool shouldAllow204();
212 bool shouldAllow206any();
213 bool shouldAllow206in();
214 bool shouldAllow206out();
215 bool canBackupEverything() const;
217 void prepBackup(size_t expectedSize
);
218 void backup(const MemBuf
&buf
);
223 void parseIcapHead();
224 void parseHttpHead();
225 bool parseHead(HttpMsg
*head
);
227 void decideOnParsingBody();
229 void maybeAllocateHttpMsg();
231 void handle100Continue();
232 bool validate200Ok();
234 void handle204NoContent();
235 void handle206PartialContent();
236 void handleUnknownScode();
238 void bypassFailure();
241 void disableBypass(const char *reason
, bool includeGroupBypass
);
244 void prepPartialBodyEchoing(uint64_t pos
);
247 virtual bool doneAll() const;
248 virtual void swanSong();
250 void stopReceiving();
251 void stopSending(bool nicely
);
252 void stopWriting(bool nicely
);
256 virtual void fillPendingStatus(MemBuf
&buf
) const;
257 virtual void fillDoneStatus(MemBuf
&buf
) const;
258 virtual bool fillVirginHttpHeader(MemBuf
&) const;
261 void packHead(MemBuf
&httpBuf
, const HttpMsg
*head
);
262 void encapsulateHead(MemBuf
&icapBuf
, const char *section
, MemBuf
&httpBuf
, const HttpMsg
*head
);
263 bool gotEncapsulated(const char *section
) const;
264 void checkConsuming();
266 virtual void finalizeLogInfo();
268 SizedEstimate virginBody
;
269 VirginBodyAct virginBodyWriting
; // virgin body writing state
270 VirginBodyAct virginBodySending
; // virgin body sending state
271 uint64_t virginConsumed
; // virgin data consumed so far
272 Preview preview
; // use for creating (writing) the preview
274 ChunkedCodingParser
*bodyParser
; // ICAP response body parser
276 bool canStartBypass
; // enables bypass of transaction failures
277 bool protectGroupBypass
; // protects ServiceGroup-wide bypass of failures
280 * size of HTTP header in ICAP reply or -1 if there is not any encapsulated
283 int64_t replyHttpHeaderSize
;
285 * size of dechunked HTTP body in ICAP reply or -1 if there is not any
286 * encapsulated message data
288 int64_t replyHttpBodySize
;
290 int adaptHistoryId
; ///< adaptation history slot reservation
300 bool serviceWaiting
; // waiting for ICAP service options
301 bool allowedPostview204
; // mmust handle 204 No Content outside preview
302 bool allowedPostview206
; // must handle 206 Partial Content outside preview
303 bool allowedPreview206
; // must handle 206 Partial Content inside preview
304 bool readyForUob
; ///< got a 206 response and expect a use-origin-body
305 bool waitedForService
; ///< true if was queued at least once
307 // will not write anything [else] to the ICAP server connection
308 bool doneWriting() const { return writing
== writingReallyDone
; }
310 // will not use virgin.body_pipe
311 bool doneConsumingVirgin() const {
312 return writing
>= writingAlmostDone
313 && ((sending
== sendingAdapted
&& !readyForUob
) ||
314 sending
== sendingDone
);
317 // parsed entire ICAP response from the ICAP server
318 bool doneParsing() const { return parsing
== psDone
; }
320 // is parsing ICAP or HTTP headers read from the ICAP server
321 bool parsingHeaders() const {
322 return parsing
== psIcapHeader
||
323 parsing
== psHttpHeader
;
326 enum Parsing
{ psIcapHeader
, psHttpHeader
, psBody
, psDone
} parsing
;
328 // measures ICAP request writing progress
329 enum Writing
{ writingInit
, writingConnect
, writingHeaders
,
330 writingPreview
, writingPaused
, writingPrime
,
331 writingAlmostDone
, // waiting for the last write() call to finish
335 enum Sending
{ sendingUndecided
, sendingVirgin
, sendingAdapted
,
340 CBDATA_CLASS2(ModXact
);
343 // An Launcher that stores ModXact construction info and
344 // creates ModXact when needed
345 class ModXactLauncher
: public Launcher
348 ModXactLauncher(HttpMsg
*virginHeader
, HttpRequest
*virginCause
, Adaptation::ServicePointer s
);
351 virtual Xaction
*createXaction();
353 virtual void swanSong();
355 /// starts or stops transaction accounting in ICAP history
356 void updateHistory(bool start
);
361 CBDATA_CLASS2(ModXactLauncher
);
365 } // namespace Adaptation
367 #endif /* SQUID_ICAPMOD_XACT_H */