Squid Web Cache master
Loading...
Searching...
No Matches
Server.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2026 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9#include "squid.h"
10#include "anyp/PortCfg.h"
11#include "client_side.h"
12#include "comm.h"
13#include "comm/Read.h"
14#include "debug/Stream.h"
16#include "fd.h"
17#include "fde.h"
18#include "http/Stream.h"
19#include "LogTags.h"
20#include "MasterXaction.h"
21#include "servers/Server.h"
22#include "SquidConfig.h"
23#include "StatCounters.h"
24#include "tools.h"
25
27 AsyncJob("::Server"), // kids overwrite
28 clientConnection(xact->tcpClient),
29 transferProtocol(xact->squidPort->transport),
30 port(xact->squidPort),
31 receivedFirstByte_(false)
32{
34}
35
36bool
38{
39 // servers are not done while the connection is open
42}
43
44void
46{
47 // TODO: shuffle activity from ConnStateData
48}
49
50void
58
59void
61{
62 if (reading()) {
64 reader = nullptr;
65 }
66}
67
73void
75{
76 // The hard-coded parameters are arbitrary but seem reasonable.
77 // A careful study of Squid I/O and parsing patterns is needed to tune them.
78 SBufReservationRequirements requirements;
79 requirements.minSpace = 1024; // smaller I/Os are not worth their overhead
80 requirements.idealSpace = CLIENT_REQ_BUF_SZ; // we expect few larger I/Os
82 requirements.allowShared = true; // allow because inBuf is used immediately
83 inBuf.reserve(requirements);
84 if (!inBuf.spaceSize())
85 debugs(33, 4, "request buffer full: client_request_buffer_max_size=" << Config.maxRequestBufferSize);
86}
87
88bool
90{
91 // TODO: Account for bodyPipe buffering as well.
93 debugs(33, 4, "no: " << inBuf.length() << '-' << Config.maxRequestBufferSize << '=' << (inBuf.length() - Config.maxRequestBufferSize));
94 return false;
95 }
96 debugs(33, 7, "yes: " << Config.maxRequestBufferSize << '-' << inBuf.length() << '=' << (Config.maxRequestBufferSize - inBuf.length()));
97 return true;
98}
99
100void
102{
103 if (reading())
104 return;
105
108 // XXX: We cannot tell whether the caller requires more bytes (and,
109 // hence, we should fail here), is done with processing (and, hence, we
110 // should just close here), or may still write something to the client
111 // (and, hence, we should do nothing here). TODO: Add a parameter?
112 debugs(33, 3, "WARNING: Reading from a half-closed " << clientConnection);
113 }
114
116 return;
117
119 reader = JobCallback(33, 5, Dialer, this, Server::doClientRead);
121}
122
123void
125{
126 debugs(33,5, io.conn);
127 Must(reading());
128 reader = nullptr;
129
130 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
131 if (io.flag == Comm::ERR_CLOSING) {
132 debugs(33,5, io.conn << " closing Bailout.");
133 return;
134 }
135
138
139 /*
140 * Don't reset the timeout value here. The value should be
141 * counting Config.Timeout.request and applies to the request
142 * as a whole, not individual read() calls.
143 * Plus, it breaks our lame *HalfClosed() detection
144 */
145
146 // mayBufferMoreRequestBytes() was true during readSomeData(), but variables
147 // like Config.maxRequestBufferSize may have changed since that check
149 // XXX: If we avoid Comm::ReadNow(), we should not Comm::Read() again
150 // when the wait is over; resume these doClientRead() checks instead.
151 return; // wait for noteMoreBodySpaceAvailable() or a similar inBuf draining event
152 }
155
156 CommIoCbParams rd(this); // will be expanded with ReadNow results
157 rd.conn = io.conn;
160
161 switch (Comm::ReadNow(rd, inBuf)) {
162 case Comm::INPROGRESS:
163
164 if (inBuf.isEmpty())
165 debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
166 readSomeData();
167 return;
168
169 case Comm::OK:
173 // may comm_close or setReplyToError
174 if (!handleReadData())
175 return;
176
177 /* Continue to process previously read data */
178 break;
179
180 case Comm::ENDFILE: // close detected by 0-byte read
181 debugs(33, 5, io.conn << " closed?");
182
183 if (shouldCloseOnEof()) {
184 LogTagsErrors lte;
185 lte.aborted = true;
187 return;
188 }
189
190 /* It might be half-closed, we can't tell */
191 fd_table[io.conn->fd].flags.socket_eof = true;
193 fd_note(io.conn->fd, "half-closed");
194
195 /* There is one more close check at the end, to detect aborted
196 * (partial) requests. At this point we can't tell if the request
197 * is partial.
198 */
199
200 /* Continue to process previously read data */
201 break;
202
203 // case Comm::COMM_ERROR:
204 default: // no other flags should ever occur
205 debugs(33, 2, io.conn << ": got flag " << rd.flag << "; " << xstrerr(rd.xerrno));
207 return;
208 }
209
211}
212
219void
221{
222 debugs(33,5, io.conn);
223 Must(writer != nullptr);
224 writer = nullptr;
225
226 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
228 debugs(33,5, io.conn << " closing Bailout.");
229 return;
230 }
231
232 Must(io.conn->fd == clientConnection->fd);
233
234 if (io.flag) {
235 debugs(33, 2, "bailing after a write failure: " << xstrerr(io.xerrno));
237 return;
238 }
239
240 afterClientWrite(io.size); // update state
241 writeSomeData(); // maybe schedules another write
242}
243
#define Assure(condition)
Definition Assure.h:35
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
class SquidConfig Config
StatCounters statCounter
#define Must(condition)
#define assert(EX)
Definition assert.h:17
virtual bool doneAll() const
whether positive goal has been reached
Definition AsyncJob.cc:112
virtual void swanSong()
Definition AsyncJob.h:61
int xerrno
The last errno to occur. non-zero if flag is Comm::COMM_ERROR.
Definition CommCalls.h:83
Comm::Flag flag
comm layer result status.
Definition CommCalls.h:82
Comm::ConnectionPointer conn
Definition CommCalls.h:80
void leaveOrphanage()
resume relying on owner(s) to initiate an explicit connection closure
Definition Connection.h:92
a transaction problem
Definition Error.h:27
bool aborted
_ABORTED: other abnormal termination (e.g., I/O error)
Definition LogTags.h:29
static LogTagsErrors FromErrno(int errNo)
constructs an object matching errno(3) of a failed I/O call
Definition LogTags.cc:22
Named SBuf::reserve() parameters. Defaults ask for and restrict nothing.
Definition SBuf.h:701
bool allowShared
whether sharing our storage with others is OK
Definition SBuf.h:713
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition SBuf.h:711
size_type maxCapacity
do not allocate more than this
Definition SBuf.h:712
size_type idealSpace
if allocating anyway, provide this much space
Definition SBuf.h:710
size_type length() const
Returns the number of bytes stored in SBuf.
Definition SBuf.h:419
size_type reserve(const SBufReservationRequirements &requirements)
Definition SBuf.cc:112
size_type spaceSize() const
Definition SBuf.h:397
bool isEmpty() const
Definition SBuf.h:435
virtual void afterClientRead()=0
processing to be done after a Comm::Read()
void readSomeData()
maybe grow the inBuf and schedule Comm::Read()
Definition Server.cc:101
virtual void afterClientWrite(size_t)
processing to sync state after a Comm::Write()
Definition Server.h:86
bool doneAll() const override
whether positive goal has been reached
Definition Server.cc:37
AsyncCall::Pointer reader
set when we are reading
Definition Server.h:130
void doClientRead(const CommIoCbParams &io)
Definition Server.cc:124
void maybeMakeSpaceAvailable()
grows the available read buffer space (if possible)
Definition Server.cc:74
virtual void writeSomeData()
maybe find some data to send and schedule a Comm::Write()
Definition Server.h:69
Comm::ConnectionPointer clientConnection
Definition Server.h:100
void clientWriteDone(const CommIoCbParams &io)
Definition Server.cc:220
bool receivedFirstByte_
true if at least one byte received on this connection
Definition Server.h:115
void start() override
called by AsyncStart; do not call directly
Definition Server.cc:45
void stopReading()
cancels Comm::Read() if it is scheduled
Definition Server.cc:60
virtual bool handleReadData()=0
Server(const MasterXactionPointer &xact)
Definition Server.cc:26
SBuf inBuf
read I/O buffer for the client connection
Definition Server.h:113
virtual void receivedFirstByte()=0
Update flags and timeout after the first byte received.
virtual void terminateAll(const Error &, const LogTagsErrors &)=0
abort any pending transactions and prevent new ones (by closing)
bool mayBufferMoreRequestBytes() const
whether client_request_buffer_max_size allows inBuf.length() increase
Definition Server.cc:89
void swanSong() override
Definition Server.cc:51
AsyncCall::Pointer writer
set when we are writing
Definition Server.h:131
bool reading() const
whether Comm::Read() is scheduled
Definition Server.h:60
virtual bool shouldCloseOnEof() const =0
whether to stop serving our client after reading EOF on its connection
size_t maxRequestBufferSize
ByteCounter kbytes_in
struct StatCounters::@104 client_http
static ErrorDetail::Pointer NewIfAny(const int errorNo)
void commMarkHalfClosed(int fd)
Definition comm.h:103
bool commIsHalfClosed(int fd)
Definition comm.h:104
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define CLIENT_REQ_BUF_SZ
Definition defines.h:86
static int port
@ ERR_CLIENT_GONE
Definition forward.h:79
@ ERR_WRITE_ERROR
Definition forward.h:29
@ ERR_READ_ERROR
Definition forward.h:28
void fd_note(int fd, const char *s)
Definition fd.cc:211
#define fd_table
Definition fde.h:189
void ReadCancel(int fd, AsyncCall::Pointer &callback)
Cancel the read pending on FD. No action if none pending.
Definition Read.cc:219
void Read(const Comm::ConnectionPointer &conn, AsyncCall::Pointer &callback)
Definition Read.cc:40
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition Connection.cc:27
@ OK
Definition Flag.h:16
@ ENDFILE
Definition Flag.h:26
@ ERR_CLOSING
Definition Flag.h:24
@ INPROGRESS
Definition Flag.h:21
Comm::Flag ReadNow(CommIoCbParams &params, SBuf &buf)
Definition Read.cc:81
const char * xstrerr(int error)
Definition xstrerror.cc:83