Server.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "fd.h"
16 #include "fde.h"
17 #include "http/Stream.h"
18 #include "MasterXaction.h"
19 #include "servers/Server.h"
20 #include "SquidConfig.h"
21 #include "StatCounters.h"
22 #include "tools.h"
23 
25  AsyncJob("::Server"), // kids overwrite
26  clientConnection(xact->tcpClient),
27  transferProtocol(xact->squidPort->transport),
28  port(xact->squidPort),
29  receivedFirstByte_(false)
30 {}
31 
32 bool
34 {
35  // servers are not done while the connection is open
38 }
39 
40 void
42 {
43  // TODO: shuffle activity from ConnStateData
44 }
45 
46 void
48 {
51 
53 }
54 
55 void
57 {
58  if (reading()) {
60  reader = NULL;
61  }
62 }
63 
69 void
71 {
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
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);
82 }
83 
84 void
86 {
87  if (reading())
88  return;
89 
90  debugs(33, 4, clientConnection << ": reading request...");
91 
92  // we can only read if there is more than 1 byte of space free
94  return;
95 
97  reader = JobCallback(33, 5, Dialer, this, Server::doClientRead);
99 }
100 
101 void
103 {
104  debugs(33,5, io.conn);
105  Must(reading());
106  reader = NULL;
107 
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.");
111  return;
112  }
113 
115  assert(io.conn->fd == clientConnection->fd);
116 
117  /*
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
122  */
123 
125  CommIoCbParams rd(this); // will be expanded with ReadNow results
126  rd.conn = io.conn;
127  switch (Comm::ReadNow(rd, inBuf)) {
128  case Comm::INPROGRESS:
129 
130  if (inBuf.isEmpty())
131  debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
132  readSomeData();
133  return;
134 
135  case Comm::OK:
137  if (!receivedFirstByte_)
139  // may comm_close or setReplyToError
140  if (!handleReadData())
141  return;
142 
143  /* Continue to process previously read data */
144  break;
145 
146  case Comm::ENDFILE: // close detected by 0-byte read
147  debugs(33, 5, io.conn << " closed?");
148 
149  if (connFinishedWithConn(rd.size)) {
151  return;
152  }
153 
154  /* It might be half-closed, we can't tell */
155  fd_table[io.conn->fd].flags.socket_eof = true;
157  fd_note(io.conn->fd, "half-closed");
158 
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
161  * is partial.
162  */
163 
164  /* Continue to process previously read data */
165  break;
166 
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));
170  checkLogging();
172  io.conn->close();
173  return;
174  }
175 
176  afterClientRead();
177 }
178 
185 void
187 {
188  debugs(33,5, io.conn);
189  Must(writer != nullptr);
190  writer = nullptr;
191 
192  /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
194  debugs(33,5, io.conn << " closing Bailout.");
195  return;
196  }
197 
198  Must(io.conn->fd == clientConnection->fd);
199 
200  if (io.flag && pipeline.front())
201  pipeline.front()->initiateClose("write failure");
202 
203  afterClientWrite(io.size); // update state
204  writeSomeData(); // maybe schedules another write
205 }
206 
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:96
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:68
#define fd_table
Definition: fde.h:157
Server(const MasterXactionPointer &xact)
Definition: Server.cc:24
StatCounters statCounter
Definition: StatCounters.cc:12
#define assert(EX)
Definition: assert.h:17
Http::StreamPointer front() const
get the first request context in the pipeline
Definition: Pipeline.cc:28
void commMarkHalfClosed(int fd)
Definition: comm.h:100
size_type idealSpace
if allocating anyway, provide this much space
Definition: SBuf.h:697
Comm::Flag ReadNow(CommIoCbParams &params, SBuf &buf)
Definition: Read.cc:80
void terminateAll(const int xerrno)
tell everybody about the err, and abort all waiting requests
Definition: Pipeline.cc:52
void fd_note(int fd, const char *s)
Definition: fd.cc:251
void maybeMakeSpaceAvailable()
grows the available read buffer space (if possible)
Definition: Server.cc:70
virtual void swanSong()
Definition: Server.cc:47
struct StatCounters::@128 client_http
Definition: Flag.h:16
virtual void afterClientRead()=0
processing to be done after a Comm::Read()
bool isEmpty() const
Definition: SBuf.h:422
ByteCounter kbytes_in
Definition: StatCounters.h:43
void doClientRead(const CommIoCbParams &io)
Definition: Server.cc:102
void Read(const Comm::ConnectionPointer &conn, AsyncCall::Pointer &callback)
Definition: Read.cc:39
Named SBuf::reserve() parameters. Defaults ask for and restrict nothing.
Definition: SBuf.h:687
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
const char * xstrerr(int error)
Definition: xstrerror.cc:83
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:405
size_t maxRequestBufferSize
Definition: SquidConfig.h:126
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
size_type maxCapacity
do not allocate more than this
Definition: SBuf.h:699
void stopReading()
cancels Comm::Read() if it is scheduled
Definition: Server.cc:56
void readSomeData()
maybe grow the inBuf and schedule Comm::Read()
Definition: Server.cc:85
virtual void swanSong()
Definition: AsyncJob.h:55
AsyncCall::Pointer reader
set when we are reading
Definition: Server.h:124
virtual void checkLogging()=0
Log the current [attempt at] transaction if nobody else will.
int xerrno
The last errno to occur. non-zero if flag is Comm::COMM_ERROR.
Definition: CommCalls.h:88
bool receivedFirstByte_
true if at least one byte received on this connection
Definition: Server.h:112
virtual void afterClientWrite(size_t)
processing to sync state after a Comm::Write()
Definition: Server.h:83
virtual bool handleReadData()=0
bool allowShared
whether sharing our storage with others is OK
Definition: SBuf.h:700
static int port
Definition: ldap_backend.cc:69
#define Must(cond)
Definition: TextException.h:89
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition: SBuf.h:698
virtual void receivedFirstByte()=0
Update flags and timeout after the first byte received.
virtual bool doneAll() const
whether positive goal has been reached
Definition: Server.cc:33
void clientWriteDone(const CommIoCbParams &io)
Definition: Server.cc:186
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
SBuf inBuf
read I/O buffer for the client connection
Definition: Server.h:110
Comm::ConnectionPointer clientConnection
Definition: Server.h:97
virtual void start()
called by AsyncStart; do not call directly
Definition: Server.cc:41
virtual void writeSomeData()
maybe find some data to send and schedule a Comm::Write()
Definition: Server.h:66
void ReadCancel(int fd, AsyncCall::Pointer &callback)
Cancel the read pending on FD. No action if none pending.
Definition: Read.cc:216
size_type reserve(const SBufReservationRequirements &requirements)
Definition: SBuf.cc:123
AsyncCall::Pointer writer
set when we are writing
Definition: Server.h:125
class SquidConfig Config
Definition: SquidConfig.cc:12
virtual bool connFinishedWithConn(int size)=0
??
#define NULL
Definition: types.h:166
size_type spaceSize() const
Definition: SBuf.h:383
#define false
Definition: GnuRegex.c:233
bool reading() const
whether Comm::Read() is scheduled
Definition: Server.h:57
Pipeline pipeline
set of requests waiting to be serviced
Definition: Server.h:115
#define CLIENT_REQ_BUF_SZ
Definition: defines.h:130

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors