2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_SRC_CLIENTS_CLIENT_H
10 #define SQUID_SRC_CLIENTS_CLIENT_H
12 #include "base/AsyncJob.h"
14 #include "CommCalls.h"
16 #include "StoreIOBuffer.h"
18 #include "adaptation/forward.h"
19 #include "adaptation/Initiator.h"
26 * Client is a common base for classes such as HttpStateData and FtpStateData.
27 * All such classes must be able to consume request bodies from a BodyPipe
28 * or ICAP producer, adapt virgin responses using ICAP, and provide a
29 * consumer with responses.
33 public Adaptation::Initiator
,
43 /// \return primary or "request data connection"
44 virtual const Comm::ConnectionPointer
& dataConnection() const = 0;
46 // BodyConsumer: consume request body or adapted response body.
47 // The implementation just calls the corresponding HTTP or ICAP handle*()
48 // method, depending on the pipe.
49 virtual void noteMoreBodyDataAvailable(BodyPipe::Pointer
);
50 virtual void noteBodyProductionEnded(BodyPipe::Pointer
);
51 virtual void noteBodyProducerAborted(BodyPipe::Pointer
);
53 /// read response data from the network
54 virtual void maybeReadVirginBody() = 0;
56 /// abnormal transaction termination; reason is for debugging only
57 virtual void abortTransaction(const char *reason
) = 0;
59 /// a hack to reach HttpStateData::orignal_request
60 virtual HttpRequest
*originalRequest();
63 // Adaptation::Initiator API: start an ICAP transaction and receive adapted headers.
64 virtual void noteAdaptationAnswer(const Adaptation::Answer
&answer
);
65 virtual void noteAdaptationAclCheckDone(Adaptation::ServiceGroupPointer group
);
67 // BodyProducer: provide virgin response body to ICAP.
68 virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer
);
69 virtual void noteBodyConsumerAborted(BodyPipe::Pointer
);
71 virtual bool getMoreRequestBody(MemBuf
&buf
);
72 virtual void processReplyBody() = 0;
74 //AsyncJob virtual methods
75 virtual void swanSong();
76 virtual bool doneAll() const;
78 public: // should be protected
79 void serverComplete(); /**< call when no server communication is expected */
82 void serverComplete2(); /**< Continuation of serverComplete */
83 bool completed
; /**< serverComplete() has been called */
86 // kids customize these
87 virtual void haveParsedReplyHeaders(); /**< called when got final headers */
88 virtual void completeForwarding(); /**< default calls fwd->complete() */
90 // BodyConsumer for HTTP: consume request body.
91 bool startRequestBodyFlow();
92 void handleMoreRequestBodyAvailable();
93 void handleRequestBodyProductionEnded();
94 virtual void handleRequestBodyProducerAborted() = 0;
96 // sending of the request body to the server
97 void sendMoreRequestBody();
98 // has body; kids overwrite to increment I/O stats counters
99 virtual void sentRequestBody(const CommIoCbParams
&io
) = 0;
100 virtual void doneSendingRequestBody() = 0;
102 virtual void closeServer() = 0; /**< end communication with the server */
103 virtual bool doneWithServer() const = 0; /**< did we end communication? */
104 /// whether we may receive more virgin response body bytes
105 virtual bool mayReadVirginReplyBody() const = 0;
107 /// Entry-dependent callbacks use this check to quit if the entry went bad
108 bool abortOnBadEntry(const char *abortReason
);
113 void startAdaptation(const Adaptation::ServiceGroupPointer
&group
, HttpRequest
*cause
);
114 void adaptVirginReplyBody(const char *buf
, ssize_t len
);
115 void cleanAdaptation();
116 virtual bool doneWithAdaptation() const; /**< did we end ICAP communication? */
118 // BodyConsumer for ICAP: consume adapted response body.
119 void handleMoreAdaptedBodyAvailable();
120 void handleAdaptedBodyProductionEnded();
121 void handleAdaptedBodyProducerAborted();
123 void handleAdaptedHeader(HttpMsg
*msg
);
124 void handleAdaptationCompleted();
125 void handleAdaptationBlocked(const Adaptation::Answer
&answer
);
126 void handleAdaptationAborted(bool bypassable
= false);
128 /// called by StoreEntry when it has more buffer space available
129 void resumeBodyStorage();
130 /// called when the entire adapted response body is consumed
131 void endAdaptedBodyConsumption();
135 const HttpReply
*virginReply() const;
136 HttpReply
*virginReply();
137 HttpReply
*setVirginReply(HttpReply
*r
);
139 HttpReply
*finalReply();
140 HttpReply
*setFinalReply(HttpReply
*r
);
142 // Kids use these to stuff data into the response instead of messing with the entry directly
143 void adaptOrFinalizeReply();
144 void addVirginReplyBody(const char *buf
, ssize_t len
);
145 void storeReplyBody(const char *buf
, ssize_t len
);
146 /// \deprecated use SBuf I/O API and calcBufferSpaceToReserve() instead
147 size_t replyBodySpace(const MemBuf
&readBuf
, const size_t minSpace
) const;
148 /// determine how much space the buffer needs to reserve
149 size_t calcBufferSpaceToReserve(const size_t space
, const size_t wantSpace
) const;
151 void adjustBodyBytesRead(const int64_t delta
);
153 // These should be private
154 int64_t currentOffset
; /**< Our current offset in the StoreEntry */
155 MemBuf
*responseBodyBuffer
; /**< Data temporarily buffered for ICAP */
157 public: // should not be
159 FwdState::Pointer fwd
;
160 HttpRequest
*request
;
163 BodyPipe::Pointer requestBodySource
; /**< to consume request body */
164 AsyncCall::Pointer requestSender
; /**< set if we are expecting Comm::Write to call us back */
167 BodyPipe::Pointer virginBodyDestination
; /**< to provide virgin response body */
168 CbcPointer
<Adaptation::Initiate
> adaptedHeadSource
; /**< to get adapted response headers */
169 BodyPipe::Pointer adaptedBodySource
; /**< to consume adated response body */
171 bool adaptationAccessCheckPending
;
172 bool startedAdaptation
;
174 bool receivedWholeRequestBody
; ///< handleRequestBodyProductionEnded called
177 void sendBodyIsTooLargeError();
178 void maybePurgeOthers();
180 HttpReply
*theVirginReply
; /**< reply received from the origin server */
181 HttpReply
*theFinalReply
; /**< adapted reply from ICAP or virgin reply */
184 #endif /* SQUID_SRC_CLIENTS_CLIENT_H */