2 * Copyright (C) 1996-2017 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 transferProtocol(xact
->squidPort
->transport
),
28 port(xact
->squidPort
),
29 receivedFirstByte_(false)
33 Server::doneAll() const
35 // servers are not done while the connection is open
36 return !Comm::IsConnOpen(clientConnection
) &&
37 BodyProducer::doneAll();
43 // TODO: shuffle activity from ConnStateData
49 if (Comm::IsConnOpen(clientConnection
))
50 clientConnection
->close();
52 BodyProducer::swanSong();
59 Comm::ReadCancel(clientConnection
->fd
, reader
);
64 /// Prepare inBuf for I/O. This method balances several conflicting desires:
65 /// 1. Do not read too few bytes at a time.
66 /// 2. Do not waste too much buffer space.
67 /// 3. Do not [re]allocate or memmove the buffer too much.
68 /// 4. Obey Config.maxRequestBufferSize limit.
70 Server::maybeMakeSpaceAvailable()
72 // The hard-coded parameters are arbitrary but seem reasonable.
73 // A careful study of Squid I/O and parsing patterns is needed to tune them.
74 SBufReservationRequirements requirements
;
75 requirements
.minSpace
= 1024; // smaller I/Os are not worth their overhead
76 requirements
.idealSpace
= CLIENT_REQ_BUF_SZ
; // we expect few larger I/Os
77 requirements
.maxCapacity
= Config
.maxRequestBufferSize
;
78 requirements
.allowShared
= true; // allow because inBuf is used immediately
79 inBuf
.reserve(requirements
);
80 if (!inBuf
.spaceSize())
81 debugs(33, 4, "request buffer full: client_request_buffer_max_size=" << Config
.maxRequestBufferSize
);
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
));
171 pipeline
.terminateAll(rd
.xerrno
);
179 /** callback handling the Comm::Write completion
181 * Will call afterClientWrite(size_t) to sync the I/O state.
182 * Then writeSomeData() to initiate any followup writes that
183 * could be immediately done.
186 Server::clientWriteDone(const CommIoCbParams
&io
)
188 debugs(33,5, io
.conn
);
189 Must(writer
!= nullptr);
192 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
193 if (io
.flag
== Comm::ERR_CLOSING
|| !Comm::IsConnOpen(clientConnection
)) {
194 debugs(33,5, io
.conn
<< " closing Bailout.");
198 Must(io
.conn
->fd
== clientConnection
->fd
);
200 if (io
.flag
&& pipeline
.front())
201 pipeline
.front()->initiateClose("write failure");
203 afterClientWrite(io
.size
); // update state
204 writeSomeData(); // maybe schedules another write