Server.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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
52{
55
57}
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
88void
90{
91 if (reading())
92 return;
93
94 debugs(33, 4, clientConnection << ": reading request...");
95
96 // we can only read if there is more than 1 byte of space free
98 return;
99
101 reader = JobCallback(33, 5, Dialer, this, Server::doClientRead);
103}
104
105void
107{
108 debugs(33,5, io.conn);
109 Must(reading());
110 reader = nullptr;
111
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.");
115 return;
116 }
117
120
121 /*
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
126 */
127
129 CommIoCbParams rd(this); // will be expanded with ReadNow results
130 rd.conn = io.conn;
131 switch (Comm::ReadNow(rd, inBuf)) {
132 case Comm::INPROGRESS:
133
134 if (inBuf.isEmpty())
135 debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
136 readSomeData();
137 return;
138
139 case Comm::OK:
143 // may comm_close or setReplyToError
144 if (!handleReadData())
145 return;
146
147 /* Continue to process previously read data */
148 break;
149
150 case Comm::ENDFILE: // close detected by 0-byte read
151 debugs(33, 5, io.conn << " closed?");
152
153 if (shouldCloseOnEof()) {
154 LogTagsErrors lte;
155 lte.aborted = true;
157 return;
158 }
159
160 /* It might be half-closed, we can't tell */
161 fd_table[io.conn->fd].flags.socket_eof = true;
163 fd_note(io.conn->fd, "half-closed");
164
165 /* There is one more close check at the end, to detect aborted
166 * (partial) requests. At this point we can't tell if the request
167 * is partial.
168 */
169
170 /* Continue to process previously read data */
171 break;
172
173 // case Comm::COMM_ERROR:
174 default: // no other flags should ever occur
175 debugs(33, 2, io.conn << ": got flag " << rd.flag << "; " << xstrerr(rd.xerrno));
176 LogTagsErrors lte;
177 lte.timedout = rd.xerrno == ETIMEDOUT;
178 lte.aborted = !lte.timedout; // intentionally true for zero rd.xerrno
180 return;
181 }
182
184}
185
192void
194{
195 debugs(33,5, io.conn);
196 Must(writer != nullptr);
197 writer = nullptr;
198
199 /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
201 debugs(33,5, io.conn << " closing Bailout.");
202 return;
203 }
204
205 Must(io.conn->fd == clientConnection->fd);
206
207 if (io.flag && pipeline.front())
208 pipeline.front()->initiateClose("write failure");
209
210 afterClientWrite(io.size); // update state
211 writeSomeData(); // maybe schedules another write
212}
213
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:69
class SquidConfig Config
Definition: SquidConfig.cc:12
StatCounters statCounter
Definition: StatCounters.cc:12
#define Must(condition)
Definition: TextException.h:75
#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
bool aborted
_ABORTED: other abnormal termination (e.g., I/O error)
Definition: LogTags.h:26
bool timedout
_TIMEDOUT: terminated due to a lifetime or I/O timeout
Definition: LogTags.h:25
Http::StreamPointer front() const
get the first request context in the pipeline
Definition: Pipeline.cc:28
Named SBuf::reserve() parameters. Defaults ask for and restrict nothing.
Definition: SBuf.h:697
bool allowShared
whether sharing our storage with others is OK
Definition: SBuf.h:709
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition: SBuf.h:707
size_type maxCapacity
do not allocate more than this
Definition: SBuf.h:708
size_type idealSpace
if allocating anyway, provide this much space
Definition: SBuf.h:706
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:415
size_type reserve(const SBufReservationRequirements &requirements)
Definition: SBuf.cc:112
size_type spaceSize() const
Definition: SBuf.h:393
bool isEmpty() const
Definition: SBuf.h:431
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:89
virtual void afterClientWrite(size_t)
processing to sync state after a Comm::Write()
Definition: Server.h:86
Pipeline pipeline
set of requests waiting to be serviced
Definition: Server.h:118
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:127
void doClientRead(const CommIoCbParams &io)
Definition: Server.cc:106
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:193
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)
void swanSong() override
Definition: Server.cc:51
AsyncCall::Pointer writer
set when we are writing
Definition: Server.h:128
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
Definition: SquidConfig.h:136
struct StatCounters::@122 client_http
ByteCounter kbytes_in
Definition: StatCounters.h:45
static ErrorDetail::Pointer NewIfAny(const int errorNo)
void commMarkHalfClosed(int fd)
Definition: comm.h:94
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
#define CLIENT_REQ_BUF_SZ
Definition: defines.h:88
static int port
Definition: ldap_backend.cc:70
@ ERR_CLIENT_GONE
Definition: forward.h:79
void fd_note(int fd, const char *s)
Definition: fd.cc:216
#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
@ Error
Definition: ResultCode.h:19
const char * xstrerr(int error)
Definition: xstrerror.cc:83

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors