]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/servers/Server.cc
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.
10 #include "anyp/PortCfg.h"
11 #include "client_side.h"
13 #include "comm/Read.h"
17 #include "MasterXaction.h"
18 #include "servers/Server.h"
19 #include "SquidConfig.h"
20 #include "StatCounters.h"
23 Server::Server(const MasterXaction::Pointer
&xact
) :
24 AsyncJob("::Server"), // kids overwrite
25 clientConnection(xact
->tcpClient
),
26 transferProtocol(xact
->squidPort
->transport
),
27 port(xact
->squidPort
),
28 receivedFirstByte_(false)
32 Server::doneAll() const
34 // servers are not done while the connection is open
35 return !Comm::IsConnOpen(clientConnection
) &&
36 BodyProducer::doneAll();
42 // TODO: shuffle activity from ConnStateData
48 if (Comm::IsConnOpen(clientConnection
))
49 clientConnection
->close();
51 BodyProducer::swanSong();
58 Comm::ReadCancel(clientConnection
->fd
, reader
);
64 Server::maybeMakeSpaceAvailable()
66 if (inBuf
.spaceSize() < 2) {
67 const SBuf::size_type haveCapacity
= inBuf
.length() + inBuf
.spaceSize();
68 if (haveCapacity
>= Config
.maxRequestBufferSize
) {
69 debugs(33, 4, "request buffer full: client_request_buffer_max_size=" << Config
.maxRequestBufferSize
);
72 if (haveCapacity
== 0) {
73 // haveCapacity is based on the SBuf visible window of the MemBlob buffer, which may fill up.
74 // at which point bump the buffer back to default. This allocates a new MemBlob with any un-parsed bytes.
75 inBuf
.reserveCapacity(CLIENT_REQ_BUF_SZ
);
77 const SBuf::size_type wantCapacity
= min(static_cast<SBuf::size_type
>(Config
.maxRequestBufferSize
), haveCapacity
*2);
78 inBuf
.reserveCapacity(wantCapacity
);
80 debugs(33, 2, "growing request buffer: available=" << inBuf
.spaceSize() << " used=" << inBuf
.length());
82 return (inBuf
.spaceSize() >= 2);
86 Server::readSomeData()
91 debugs(33, 4, clientConnection
<< ": reading request...");
93 // we can only read if there is more than 1 byte of space free
94 if (Config
.maxRequestBufferSize
- inBuf
.length() < 2)
97 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
98 reader
= JobCallback(33, 5, Dialer
, this, Server::doClientRead
);
99 Comm::Read(clientConnection
, reader
);
103 Server::doClientRead(const CommIoCbParams
&io
)
105 debugs(33,5, io
.conn
);
109 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
110 if (io
.flag
== Comm::ERR_CLOSING
) {
111 debugs(33,5, io
.conn
<< " closing Bailout.");
115 assert(Comm::IsConnOpen(clientConnection
));
116 assert(io
.conn
->fd
== clientConnection
->fd
);
119 * Don't reset the timeout value here. The value should be
120 * counting Config.Timeout.request and applies to the request
121 * as a whole, not individual read() calls.
122 * Plus, it breaks our lame *HalfClosed() detection
125 maybeMakeSpaceAvailable();
126 CommIoCbParams
rd(this); // will be expanded with ReadNow results
128 switch (Comm::ReadNow(rd
, inBuf
)) {
129 case Comm::INPROGRESS
:
132 debugs(33, 2, io
.conn
<< ": no data to process, " << xstrerr(rd
.xerrno
));
137 statCounter
.client_http
.kbytes_in
+= rd
.size
;
138 if (!receivedFirstByte_
)
140 // may comm_close or setReplyToError
141 if (!handleReadData())
144 /* Continue to process previously read data */
147 case Comm::ENDFILE
: // close detected by 0-byte read
148 debugs(33, 5, io
.conn
<< " closed?");
150 if (connFinishedWithConn(rd
.size
)) {
151 clientConnection
->close();
155 /* It might be half-closed, we can't tell */
156 fd_table
[io
.conn
->fd
].flags
.socket_eof
= true;
157 commMarkHalfClosed(io
.conn
->fd
);
158 fd_note(io
.conn
->fd
, "half-closed");
160 /* There is one more close check at the end, to detect aborted
161 * (partial) requests. At this point we can't tell if the request
165 /* Continue to process previously read data */
168 // case Comm::COMM_ERROR:
169 default: // no other flags should ever occur
170 debugs(33, 2, io
.conn
<< ": got flag " << rd
.flag
<< "; " << xstrerr(rd
.xerrno
));
171 pipeline
.terminateAll(rd
.xerrno
);
180 Server::clientWriteDone(const CommIoCbParams
&io
)
182 debugs(33,5, io
.conn
);
183 Must(writer
!= NULL
);
186 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
187 if (io
.flag
== Comm::ERR_CLOSING
) {
188 debugs(33,5, io
.conn
<< " closing Bailout.");
192 assert(Comm::IsConnOpen(clientConnection
));
193 assert(io
.conn
->fd
== clientConnection
->fd
);
195 writeSomeData(); // maybe schedules another write