]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/servers/Server.cc
2 * Copyright (C) 1996-2016 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.
10 #include "anyp/PortCfg.h"
11 #include "client_side.h"
13 #include "comm/Read.h"
17 #include "http/Stream.h"
18 #include "MasterXaction.h"
19 #include "servers/Server.h"
20 #include "SquidConfig.h"
21 #include "StatCounters.h"
24 Server::Server(const MasterXaction::Pointer
&xact
) :
25 AsyncJob("::Server"), // kids overwrite
26 clientConnection(xact
->tcpClient
),
27 port(xact
->squidPort
),
28 receivedFirstByte_(false)
31 transferProtocol
= xact
->squidPort
->transport
;
35 Server::doneAll() const
37 // servers are not done while the connection is open
38 return !Comm::IsConnOpen(clientConnection
) &&
39 BodyProducer::doneAll();
45 // TODO: shuffle activity from ConnStateData
51 if (Comm::IsConnOpen(clientConnection
))
52 clientConnection
->close();
54 BodyProducer::swanSong();
61 Comm::ReadCancel(clientConnection
->fd
, reader
);
67 Server::maybeMakeSpaceAvailable()
69 if (inBuf
.spaceSize() < 2) {
70 const SBuf::size_type haveCapacity
= inBuf
.length() + inBuf
.spaceSize();
71 if (haveCapacity
>= Config
.maxRequestBufferSize
) {
72 debugs(33, 4, "request buffer full: client_request_buffer_max_size=" << Config
.maxRequestBufferSize
);
75 if (haveCapacity
== 0) {
76 // haveCapacity is based on the SBuf visible window of the MemBlob buffer, which may fill up.
77 // at which point bump the buffer back to default. This allocates a new MemBlob with any un-parsed bytes.
78 inBuf
.reserveCapacity(CLIENT_REQ_BUF_SZ
);
80 const SBuf::size_type wantCapacity
= min(static_cast<SBuf::size_type
>(Config
.maxRequestBufferSize
), haveCapacity
*2);
81 inBuf
.reserveCapacity(wantCapacity
);
83 debugs(33, 2, "growing request buffer: available=" << inBuf
.spaceSize() << " used=" << inBuf
.length());
85 return (inBuf
.spaceSize() >= 2);
89 Server::readSomeData()
94 debugs(33, 4, clientConnection
<< ": reading request...");
96 // we can only read if there is more than 1 byte of space free
97 if (Config
.maxRequestBufferSize
- inBuf
.length() < 2)
100 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
101 reader
= JobCallback(33, 5, Dialer
, this, Server::doClientRead
);
102 Comm::Read(clientConnection
, reader
);
106 Server::doClientRead(const CommIoCbParams
&io
)
108 debugs(33,5, io
.conn
);
112 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
113 if (io
.flag
== Comm::ERR_CLOSING
) {
114 debugs(33,5, io
.conn
<< " closing Bailout.");
118 assert(Comm::IsConnOpen(clientConnection
));
119 assert(io
.conn
->fd
== clientConnection
->fd
);
122 * Don't reset the timeout value here. The value should be
123 * counting Config.Timeout.request and applies to the request
124 * as a whole, not individual read() calls.
125 * Plus, it breaks our lame *HalfClosed() detection
128 maybeMakeSpaceAvailable();
129 CommIoCbParams
rd(this); // will be expanded with ReadNow results
131 switch (Comm::ReadNow(rd
, inBuf
)) {
132 case Comm::INPROGRESS
:
135 debugs(33, 2, io
.conn
<< ": no data to process, " << xstrerr(rd
.xerrno
));
140 statCounter
.client_http
.kbytes_in
+= rd
.size
;
141 if (!receivedFirstByte_
)
143 // may comm_close or setReplyToError
144 if (!handleReadData())
147 /* Continue to process previously read data */
150 case Comm::ENDFILE
: // close detected by 0-byte read
151 debugs(33, 5, io
.conn
<< " closed?");
153 if (connFinishedWithConn(rd
.size
)) {
154 clientConnection
->close();
158 /* It might be half-closed, we can't tell */
159 fd_table
[io
.conn
->fd
].flags
.socket_eof
= true;
160 commMarkHalfClosed(io
.conn
->fd
);
161 fd_note(io
.conn
->fd
, "half-closed");
163 /* There is one more close check at the end, to detect aborted
164 * (partial) requests. At this point we can't tell if the request
168 /* Continue to process previously read data */
171 // case Comm::COMM_ERROR:
172 default: // no other flags should ever occur
173 debugs(33, 2, io
.conn
<< ": got flag " << rd
.flag
<< "; " << xstrerr(rd
.xerrno
));
174 pipeline
.terminateAll(rd
.xerrno
);
182 /** callback handling the Comm::Write completion
184 * Will call afterClientWrite(size_t) to sync the I/O state.
185 * Then writeSomeData() to initiate any followup writes that
186 * could be immediately done.
189 Server::clientWriteDone(const CommIoCbParams
&io
)
191 debugs(33,5, io
.conn
);
192 Must(writer
!= nullptr);
195 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
196 if (io
.flag
== Comm::ERR_CLOSING
|| !Comm::IsConnOpen(clientConnection
)) {
197 debugs(33,5, io
.conn
<< " closing Bailout.");
201 Must(io
.conn
->fd
== clientConnection
->fd
);
203 if (io
.flag
&& pipeline
.front())
204 pipeline
.front()->initiateClose("write failure");
206 afterClientWrite(io
.size
); // update state
207 writeSomeData(); // maybe schedules another write