Server.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 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 {}
33 
34 bool
36 {
37  // servers are not done while the connection is open
40 }
41 
42 void
44 {
45  // TODO: shuffle activity from ConnStateData
46 }
47 
48 void
50 {
53 
55 }
56 
57 void
59 {
60  if (reading()) {
62  reader = NULL;
63  }
64 }
65 
71 void
73 {
74  // The hard-coded parameters are arbitrary but seem reasonable.
75  // A careful study of Squid I/O and parsing patterns is needed to tune them.
76  SBufReservationRequirements requirements;
77  requirements.minSpace = 1024; // smaller I/Os are not worth their overhead
78  requirements.idealSpace = CLIENT_REQ_BUF_SZ; // we expect few larger I/Os
80  requirements.allowShared = true; // allow because inBuf is used immediately
81  inBuf.reserve(requirements);
82  if (!inBuf.spaceSize())
83  debugs(33, 4, "request buffer full: client_request_buffer_max_size=" << Config.maxRequestBufferSize);
84 }
85 
86 void
88 {
89  if (reading())
90  return;
91 
92  debugs(33, 4, clientConnection << ": reading request...");
93 
94  // we can only read if there is more than 1 byte of space free
96  return;
97 
99  reader = JobCallback(33, 5, Dialer, this, Server::doClientRead);
101 }
102 
103 void
105 {
106  debugs(33,5, io.conn);
107  Must(reading());
108  reader = NULL;
109 
110  /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
111  if (io.flag == Comm::ERR_CLOSING) {
112  debugs(33,5, io.conn << " closing Bailout.");
113  return;
114  }
115 
117  assert(io.conn->fd == clientConnection->fd);
118 
119  /*
120  * Don't reset the timeout value here. The value should be
121  * counting Config.Timeout.request and applies to the request
122  * as a whole, not individual read() calls.
123  * Plus, it breaks our lame *HalfClosed() detection
124  */
125 
127  CommIoCbParams rd(this); // will be expanded with ReadNow results
128  rd.conn = io.conn;
129  switch (Comm::ReadNow(rd, inBuf)) {
130  case Comm::INPROGRESS:
131 
132  if (inBuf.isEmpty())
133  debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
134  readSomeData();
135  return;
136 
137  case Comm::OK:
139  if (!receivedFirstByte_)
141  // may comm_close or setReplyToError
142  if (!handleReadData())
143  return;
144 
145  /* Continue to process previously read data */
146  break;
147 
148  case Comm::ENDFILE: // close detected by 0-byte read
149  debugs(33, 5, io.conn << " closed?");
150 
151  if (shouldCloseOnEof()) {
152  LogTagsErrors lte;
153  lte.aborted = true;
155  return;
156  }
157 
158  /* It might be half-closed, we can't tell */
159  fd_table[io.conn->fd].flags.socket_eof = true;
161  fd_note(io.conn->fd, "half-closed");
162 
163  /* There is one more close check at the end, to detect aborted
164  * (partial) requests. At this point we can't tell if the request
165  * is partial.
166  */
167 
168  /* Continue to process previously read data */
169  break;
170 
171  // case Comm::COMM_ERROR:
172  default: // no other flags should ever occur
173  debugs(33, 2, io.conn << ": got flag " << rd.flag << "; " << xstrerr(rd.xerrno));
174  LogTagsErrors lte;
175  lte.timedout = rd.xerrno == ETIMEDOUT;
176  lte.aborted = !lte.timedout; // intentionally true for zero rd.xerrno
178  return;
179  }
180 
181  afterClientRead();
182 }
183 
190 void
192 {
193  debugs(33,5, io.conn);
194  Must(writer != nullptr);
195  writer = nullptr;
196 
197  /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
199  debugs(33,5, io.conn << " closing Bailout.");
200  return;
201  }
202 
203  Must(io.conn->fd == clientConnection->fd);
204 
205  if (io.flag && pipeline.front())
206  pipeline.front()->initiateClose("write failure");
207 
208  afterClientWrite(io.size); // update state
209  writeSomeData(); // maybe schedules another write
210 }
211 
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:695
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:246
AsyncCall::Pointer reader
set when we are reading
Definition: Server.h:125
bool isEmpty() const
Definition: SBuf.h:420
virtual void swanSong()
Definition: AsyncJob.h:54
Comm::Flag ReadNow(CommIoCbParams &params, SBuf &buf)
Definition: Read.cc:81
size_type maxCapacity
do not allocate more than this
Definition: SBuf.h:697
@ 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:686
virtual bool shouldCloseOnEof() const =0
whether to stop serving our client after reading EOF on its connection
size_type spaceSize() const
Definition: SBuf.h:382
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:26
@ OK
Definition: Flag.h:16
@ ENDFILE
Definition: Flag.h:27
bool allowShared
whether sharing our storage with others is OK
Definition: SBuf.h:698
void maybeMakeSpaceAvailable()
grows the available read buffer space (if possible)
Definition: Server.cc:72
@ 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:49
ByteCounter kbytes_in
Definition: StatCounters.h:45
struct StatCounters::@130 client_http
#define NULL
Definition: types.h:166
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:96
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
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:130
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
virtual void start()
called by AsyncStart; do not call directly
Definition: Server.cc:43
virtual bool doneAll() const
whether positive goal has been reached
Definition: Server.cc:35
#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:87
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:404
Comm::ConnectionPointer clientConnection
Definition: Server.h:98
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition: SBuf.h:696
void doClientRead(const CommIoCbParams &io)
Definition: Server.cc:104
void stopReading()
cancels Comm::Read() if it is scheduled
Definition: Server.cc:58
#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:72
bool receivedFirstByte_
true if at least one byte received on this connection
Definition: Server.h:113
size_t maxRequestBufferSize
Definition: SquidConfig.h:133
#define false
Definition: GnuRegex.c:233
void clientWriteDone(const CommIoCbParams &io)
Definition: Server.cc:191
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:100
StatCounters statCounter
Definition: StatCounters.cc:12

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors