Server.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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.h"
15 #include "error/SysErrorDetail.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 
36 bool
38 {
39  // servers are not done while the connection is open
42 }
43 
44 void
46 {
47  // TODO: shuffle activity from ConnStateData
48 }
49 
50 void
52 {
55 
57 }
58 
59 void
61 {
62  if (reading()) {
64  reader = NULL;
65  }
66 }
67 
73 void
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 
88 void
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 
105 void
107 {
108  debugs(33,5, io.conn);
109  Must(reading());
110  reader = NULL;
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 
119  assert(io.conn->fd == clientConnection->fd);
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:
141  if (!receivedFirstByte_)
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 
183  afterClientRead();
184 }
185 
192 void
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 
void Read(const Comm::ConnectionPointer &conn, AsyncCall::Pointer &callback)
Definition: Read.cc:40
const char * xstrerr(int error)
Definition: xstrerror.cc:83
size_type idealSpace
if allocating anyway, provide this much space
Definition: SBuf.h:699
virtual bool handleReadData()=0
@ Error
Definition: ResultCode.h:19
SBuf inBuf
read I/O buffer for the client connection
Definition: Server.h:111
void fd_note(int fd, const char *s)
Definition: fd.cc:217
AsyncCall::Pointer reader
set when we are reading
Definition: Server.h:125
bool isEmpty() const
Definition: SBuf.h:424
virtual void swanSong()
Definition: AsyncJob.h:59
Comm::Flag ReadNow(CommIoCbParams &params, SBuf &buf)
Definition: Read.cc:81
size_type maxCapacity
do not allocate more than this
Definition: SBuf.h:701
@ INPROGRESS
Definition: Flag.h:22
virtual void receivedFirstByte()=0
Update flags and timeout after the first byte received.
Named SBuf::reserve() parameters. Defaults ask for and restrict nothing.
Definition: SBuf.h:690
virtual bool shouldCloseOnEof() const =0
whether to stop serving our client after reading EOF on its connection
size_type spaceSize() const
Definition: SBuf.h:386
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
@ OK
Definition: Flag.h:16
@ ENDFILE
Definition: Flag.h:27
bool allowShared
whether sharing our storage with others is OK
Definition: SBuf.h:702
void maybeMakeSpaceAvailable()
grows the available read buffer space (if possible)
Definition: Server.cc:74
@ ERR_CLOSING
Definition: Flag.h:25
virtual void terminateAll(const Error &, const LogTagsErrors &)=0
abort any pending transactions and prevent new ones (by closing)
int xerrno
The last errno to occur. non-zero if flag is Comm::COMM_ERROR.
Definition: CommCalls.h:88
static int port
Definition: ldap_backend.cc:69
Pipeline pipeline
set of requests waiting to be serviced
Definition: Server.h:116
virtual void afterClientWrite(size_t)
processing to sync state after a Comm::Write()
Definition: Server.h:84
size_type reserve(const SBufReservationRequirements &requirements)
Definition: SBuf.cc:112
void ReadCancel(int fd, AsyncCall::Pointer &callback)
Cancel the read pending on FD. No action if none pending.
Definition: Read.cc:219
virtual void swanSong()
Definition: Server.cc:51
ByteCounter kbytes_in
Definition: StatCounters.h:45
void leaveOrphanage()
resume relying on owner(s) to initiate an explicit connection closure
Definition: Connection.h:90
#define NULL
Definition: types.h:166
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:97
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
static ErrorDetail::Pointer NewIfAny(const int errorNo)
Http::StreamPointer front() const
get the first request context in the pipeline
Definition: Pipeline.cc:28
#define CLIENT_REQ_BUF_SZ
Definition: defines.h:88
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
virtual void start()
called by AsyncStart; do not call directly
Definition: Server.cc:45
virtual bool doneAll() const
whether positive goal has been reached
Definition: Server.cc:37
#define assert(EX)
Definition: assert.h:19
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
void readSomeData()
maybe grow the inBuf and schedule Comm::Read()
Definition: Server.cc:89
bool reading() const
whether Comm::Read() is scheduled
Definition: Server.h:58
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:69
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:408
Comm::ConnectionPointer clientConnection
Definition: Server.h:98
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition: SBuf.h:700
void doClientRead(const CommIoCbParams &io)
Definition: Server.cc:106
void stopReading()
cancels Comm::Read() if it is scheduled
Definition: Server.cc:60
#define fd_table
Definition: fde.h:189
virtual void afterClientRead()=0
processing to be done after a Comm::Read()
Server(const MasterXactionPointer &xact)
Definition: Server.cc:26
bool timedout
_TIMEDOUT: terminated due to a lifetime or I/O timeout
Definition: LogTags.h:25
AsyncCall::Pointer writer
set when we are writing
Definition: Server.h:126
@ ERR_CLIENT_GONE
Definition: forward.h:79
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:73
bool receivedFirstByte_
true if at least one byte received on this connection
Definition: Server.h:113
struct StatCounters::@128 client_http
size_t maxRequestBufferSize
Definition: SquidConfig.h:134
#define false
Definition: GnuRegex.c:233
void clientWriteDone(const CommIoCbParams &io)
Definition: Server.cc:193
bool aborted
_ABORTED: other abnormal termination (e.g., I/O error)
Definition: LogTags.h:26
class SquidConfig Config
Definition: SquidConfig.cc:12
virtual void writeSomeData()
maybe find some data to send and schedule a Comm::Write()
Definition: Server.h:67
void commMarkHalfClosed(int fd)
Definition: comm.h:96
StatCounters statCounter
Definition: StatCounters.cc:12

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors