]>
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"
12 #include "comm/Read.h"
16 #include "MasterXaction.h"
17 #include "servers/Server.h"
18 #include "SquidConfig.h"
19 #include "StatCounters.h"
22 Server::Server(const MasterXaction::Pointer
&xact
) :
23 AsyncJob("::Server"), // kids overwrite
24 clientConnection(xact
->tcpClient
),
25 transferProtocol(xact
->squidPort
->transport
),
26 port(xact
->squidPort
),
27 receivedFirstByte_(false)
31 Server::doneAll() const
33 // servers are not done while the connection is open
34 return !Comm::IsConnOpen(clientConnection
) &&
35 BodyProducer::doneAll();
41 // TODO: shuffle activity from ConnStateData
47 if (Comm::IsConnOpen(clientConnection
))
48 clientConnection
->close();
50 BodyProducer::swanSong();
57 Comm::ReadCancel(clientConnection
->fd
, reader
);
63 Server::maybeMakeSpaceAvailable()
65 if (inBuf
.spaceSize() < 2) {
66 const SBuf::size_type haveCapacity
= inBuf
.length() + inBuf
.spaceSize();
67 if (haveCapacity
>= Config
.maxRequestBufferSize
) {
68 debugs(33, 4, "request buffer full: client_request_buffer_max_size=" << Config
.maxRequestBufferSize
);
71 if (haveCapacity
== 0) {
72 // haveCapacity is based on the SBuf visible window of the MemBlob buffer, which may fill up.
73 // at which point bump the buffer back to default. This allocates a new MemBlob with any un-parsed bytes.
74 inBuf
.reserveCapacity(CLIENT_REQ_BUF_SZ
);
76 const SBuf::size_type wantCapacity
= min(static_cast<SBuf::size_type
>(Config
.maxRequestBufferSize
), haveCapacity
*2);
77 inBuf
.reserveCapacity(wantCapacity
);
79 debugs(33, 2, "growing request buffer: available=" << inBuf
.spaceSize() << " used=" << inBuf
.length());
81 return (inBuf
.spaceSize() >= 2);
85 Server::readSomeData()
90 debugs(33, 4, clientConnection
<< ": reading request...");
92 // we can only read if there is more than 1 byte of space free
93 if (Config
.maxRequestBufferSize
- inBuf
.length() < 2)
96 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
97 reader
= JobCallback(33, 5, Dialer
, this, Server::doClientRead
);
98 Comm::Read(clientConnection
, reader
);
102 Server::doClientRead(const CommIoCbParams
&io
)
104 debugs(33,5, io
.conn
);
108 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
109 if (io
.flag
== Comm::ERR_CLOSING
) {
110 debugs(33,5, io
.conn
<< " closing Bailout.");
114 assert(Comm::IsConnOpen(clientConnection
));
115 assert(io
.conn
->fd
== clientConnection
->fd
);
118 * Don't reset the timeout value here. The value should be
119 * counting Config.Timeout.request and applies to the request
120 * as a whole, not individual read() calls.
121 * Plus, it breaks our lame *HalfClosed() detection
124 maybeMakeSpaceAvailable();
125 CommIoCbParams
rd(this); // will be expanded with ReadNow results
127 switch (Comm::ReadNow(rd
, inBuf
)) {
128 case Comm::INPROGRESS
:
131 debugs(33, 2, io
.conn
<< ": no data to process, " << xstrerr(rd
.xerrno
));
136 statCounter
.client_http
.kbytes_in
+= rd
.size
;
137 if (!receivedFirstByte_
)
139 // may comm_close or setReplyToError
140 if (!handleReadData())
143 /* Continue to process previously read data */
146 case Comm::ENDFILE
: // close detected by 0-byte read
147 debugs(33, 5, io
.conn
<< " closed?");
149 if (connFinishedWithConn(rd
.size
)) {
150 clientConnection
->close();
154 /* It might be half-closed, we can't tell */
155 fd_table
[io
.conn
->fd
].flags
.socket_eof
= true;
156 commMarkHalfClosed(io
.conn
->fd
);
157 fd_note(io
.conn
->fd
, "half-closed");
159 /* There is one more close check at the end, to detect aborted
160 * (partial) requests. At this point we can't tell if the request
164 /* Continue to process previously read data */
167 // case Comm::COMM_ERROR:
168 default: // no other flags should ever occur
169 debugs(33, 2, io
.conn
<< ": got flag " << rd
.flag
<< "; " << xstrerr(rd
.xerrno
));
170 notifyAllContexts(rd
.xerrno
);
179 Server::clientWriteDone(const CommIoCbParams
&io
)
181 debugs(33,5, io
.conn
);
182 Must(writer
!= NULL
);
185 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
186 if (io
.flag
== Comm::ERR_CLOSING
) {
187 debugs(33,5, io
.conn
<< " closing Bailout.");
191 assert(Comm::IsConnOpen(clientConnection
));
192 assert(io
.conn
->fd
== clientConnection
->fd
);
194 writeSomeData(); // maybe schedules another write