]> git.ipfire.org Git - thirdparty/squid.git/blob - src/servers/Server.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / servers / Server.h
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 /* DEBUG: section 33 Client-side Routines */
10
11 #ifndef SQUID_SERVERS_SERVER_H
12 #define SQUID_SERVERS_SERVER_H
13
14 #include "anyp/forward.h"
15 #include "anyp/ProtocolVersion.h"
16 #include "base/AsyncJob.h"
17 #include "BodyPipe.h"
18 #include "comm/Write.h"
19 #include "CommCalls.h"
20 #include "log/forward.h"
21 #include "Pipeline.h"
22 #include "sbuf/SBuf.h"
23 #include "servers/forward.h"
24
25 /**
26 * Common base for all Server classes used
27 * to manage connections from clients.
28 */
29 class Server : virtual public AsyncJob, public BodyProducer
30 {
31 public:
32 Server(const MasterXactionPointer &xact);
33 virtual ~Server() {}
34
35 /* AsyncJob API */
36 virtual void start();
37 virtual bool doneAll() const;
38 virtual void swanSong();
39
40 /// whether to stop serving our client after reading EOF on its connection
41 virtual bool shouldCloseOnEof() const = 0;
42
43 /// maybe grow the inBuf and schedule Comm::Read()
44 void readSomeData();
45
46 /**
47 * called when new request data has been read from the socket
48 *
49 * \retval false called comm_close or setReplyToError (the caller should bail)
50 * \retval true we did not call comm_close or setReplyToError
51 */
52 virtual bool handleReadData() = 0;
53
54 /// processing to be done after a Comm::Read()
55 virtual void afterClientRead() = 0;
56
57 /// whether Comm::Read() is scheduled
58 bool reading() const {return reader != NULL;}
59
60 /// cancels Comm::Read() if it is scheduled
61 void stopReading();
62
63 /// Update flags and timeout after the first byte received
64 virtual void receivedFirstByte() = 0;
65
66 /// maybe find some data to send and schedule a Comm::Write()
67 virtual void writeSomeData() {}
68
69 /// schedule some data for a Comm::Write()
70 void write(MemBuf *mb) {
71 typedef CommCbMemFunT<Server, CommIoCbParams> Dialer;
72 writer = JobCallback(33, 5, Dialer, this, Server::clientWriteDone);
73 Comm::Write(clientConnection, mb, writer);
74 }
75
76 /// schedule some data for a Comm::Write()
77 void write(char *buf, int len) {
78 typedef CommCbMemFunT<Server, CommIoCbParams> Dialer;
79 writer = JobCallback(33, 5, Dialer, this, Server::clientWriteDone);
80 Comm::Write(clientConnection, buf, len, writer, nullptr);
81 }
82
83 /// processing to sync state after a Comm::Write()
84 virtual void afterClientWrite(size_t) {}
85
86 /// whether Comm::Write() is scheduled
87 bool writing() const {return writer != NULL;}
88
89 // XXX: should be 'protected:' for child access only,
90 // but all sorts of code likes to play directly
91 // with the I/O buffers and socket.
92 public:
93
94 /// grows the available read buffer space (if possible)
95 void maybeMakeSpaceAvailable();
96
97 // Client TCP connection details from comm layer.
98 Comm::ConnectionPointer clientConnection;
99
100 /**
101 * The transfer protocol currently being spoken on this connection.
102 * HTTP/1.x CONNECT, HTTP/1.1 Upgrade and HTTP/2 SETTINGS offer the
103 * ability to change protocols on the fly.
104 */
105 AnyP::ProtocolVersion transferProtocol;
106
107 /// Squid listening port details where this connection arrived.
108 AnyP::PortCfgPointer port;
109
110 /// read I/O buffer for the client connection
111 SBuf inBuf;
112
113 bool receivedFirstByte_; ///< true if at least one byte received on this connection
114
115 /// set of requests waiting to be serviced
116 Pipeline pipeline;
117
118 protected:
119 /// abort any pending transactions and prevent new ones (by closing)
120 virtual void terminateAll(const Error &, const LogTagsErrors &) = 0;
121
122 void doClientRead(const CommIoCbParams &io);
123 void clientWriteDone(const CommIoCbParams &io);
124
125 AsyncCall::Pointer reader; ///< set when we are reading
126 AsyncCall::Pointer writer; ///< set when we are writing
127 };
128
129 #endif /* SQUID_SERVERS_SERVER_H */
130