]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/Read.cc
Parser-NG: HTTP Response Parser upgrade
[thirdparty/squid.git] / src / comm / Read.cc
1 /*
2 * Copyright (C) 1996-2015 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 05 Socket Functions */
10
11 #include "squid.h"
12 #include "comm.h"
13 #include "comm/IoCallback.h"
14 #include "comm/Loops.h"
15 #include "comm/Read.h"
16 #include "comm_internal.h"
17 #include "CommCalls.h"
18 #include "Debug.h"
19 #include "fd.h"
20 #include "fde.h"
21 #include "SBuf.h"
22 #include "StatCounters.h"
23 //#include "tools.h"
24
25 // Does comm check this fd for read readiness?
26 // Note that when comm is not monitoring, there can be a pending callback
27 // call, which may resume comm monitoring once fired.
28 bool
29 Comm::MonitorsRead(int fd)
30 {
31 assert(isOpen(fd) && COMMIO_FD_READCB(fd) != NULL);
32 // Being active is usually the same as monitoring because we always
33 // start monitoring the FD when we configure Comm::IoCallback for I/O
34 // and we usually configure Comm::IoCallback for I/O when we starting
35 // monitoring a FD for reading.
36 return COMMIO_FD_READCB(fd)->active();
37 }
38
39 void
40 Comm::Read(const Comm::ConnectionPointer &conn, AsyncCall::Pointer &callback)
41 {
42 // TODO: move comm_read_base() internals into here
43 // when comm_read() char* API is no longer needed
44 comm_read_base(conn, NULL, 0, callback);
45 }
46
47 /**
48 * Queue a read.
49 * If a buffer is given the callback is scheduled when the read
50 * completes, on error, or on file descriptor close.
51 *
52 * If no buffer (NULL) is given the callback is scheduled when
53 * the socket FD is ready for a read(2)/recv(2).
54 */
55 void
56 comm_read_base(const Comm::ConnectionPointer &conn, char *buf, int size, AsyncCall::Pointer &callback)
57 {
58 debugs(5, 5, "comm_read, queueing read for " << conn << "; asynCall " << callback);
59
60 /* Make sure we are open and not closing */
61 assert(Comm::IsConnOpen(conn));
62 assert(!fd_table[conn->fd].closing());
63 Comm::IoCallback *ccb = COMMIO_FD_READCB(conn->fd);
64
65 // Make sure we are either not reading or just passively monitoring.
66 // Active/passive conflicts are OK and simply cancel passive monitoring.
67 if (ccb->active()) {
68 // if the assertion below fails, we have an active comm_read conflict
69 assert(fd_table[conn->fd].halfClosedReader != NULL);
70 commStopHalfClosedMonitor(conn->fd);
71 assert(!ccb->active());
72 }
73 ccb->conn = conn;
74
75 /* Queue the read */
76 ccb->setCallback(Comm::IOCB_READ, callback, (char *)buf, NULL, size);
77 Comm::SetSelect(conn->fd, COMM_SELECT_READ, Comm::HandleRead, ccb, 0);
78 }
79
80 Comm::Flag
81 Comm::ReadNow(CommIoCbParams &params, SBuf &buf)
82 {
83 /* Attempt a read */
84 ++ statCounter.syscalls.sock.reads;
85 SBuf::size_type sz = buf.spaceSize();
86 if (params.size > 0 && params.size < sz)
87 sz = params.size;
88 char *inbuf = buf.rawSpace(sz);
89 errno = 0;
90 const int retval = FD_READ_METHOD(params.conn->fd, inbuf, sz);
91 params.xerrno = errno;
92
93 debugs(5, 3, params.conn << ", size " << sz << ", retval " << retval << ", errno " << params.xerrno);
94
95 if (retval > 0) { // data read most common case
96 buf.append(inbuf, retval);
97 fd_bytes(params.conn->fd, retval, FD_READ);
98 params.flag = Comm::OK;
99 params.size = retval;
100
101 } else if (retval == 0) { // remote closure (somewhat less) common
102 // Note - read 0 == socket EOF, which is a valid read.
103 params.flag = Comm::ENDFILE;
104
105 } else if (retval < 0) { // connection errors are worst-case
106 debugs(5, 3, params.conn << " Comm::COMM_ERROR: " << xstrerr(params.xerrno));
107 if (ignoreErrno(params.xerrno))
108 params.flag = Comm::INPROGRESS;
109 else
110 params.flag = Comm::COMM_ERROR;
111 }
112
113 return params.flag;
114 }
115
116 /**
117 * Handle an FD which is ready for read(2).
118 *
119 * If there is no provided buffer to fill call the callback.
120 *
121 * Otherwise attempt a read into the provided buffer.
122 * If the read attempt succeeds or fails, call the callback.
123 * Else, wait for another IO notification.
124 */
125 void
126 Comm::HandleRead(int fd, void *data)
127 {
128 Comm::IoCallback *ccb = (Comm::IoCallback *) data;
129
130 assert(data == COMMIO_FD_READCB(fd));
131 assert(ccb->active());
132
133 // Without a buffer, just call back.
134 // The callee may ReadMore() to get the data.
135 if (!ccb->buf) {
136 ccb->finish(Comm::OK, 0);
137 return;
138 }
139
140 /* For legacy callers : Attempt a read */
141 // Keep in sync with Comm::ReadNow()!
142 ++ statCounter.syscalls.sock.reads;
143 errno = 0;
144 int retval = FD_READ_METHOD(fd, ccb->buf, ccb->size);
145 debugs(5, 3, "FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << errno);
146
147 /* See if we read anything */
148 /* Note - read 0 == socket EOF, which is a valid read */
149 if (retval >= 0) {
150 fd_bytes(fd, retval, FD_READ);
151 ccb->offset = retval;
152 ccb->finish(Comm::OK, errno);
153 return;
154
155 } else if (retval < 0 && !ignoreErrno(errno)) {
156 debugs(5, 3, "comm_read_try: scheduling Comm::COMM_ERROR");
157 ccb->offset = 0;
158 ccb->finish(Comm::COMM_ERROR, errno);
159 return;
160 };
161
162 /* Nope, register for some more IO */
163 Comm::SetSelect(fd, COMM_SELECT_READ, Comm::HandleRead, data, 0);
164 }
165
166 /**
167 * Cancel a pending read. Assert that we have the right parameters,
168 * and that there are no pending read events!
169 *
170 * XXX: We do not assert that there are no pending read events and
171 * with async calls it becomes even more difficult.
172 * The whole interface should be reworked to do callback->cancel()
173 * instead of searching for places where the callback may be stored and
174 * updating the state of those places.
175 *
176 * AHC Don't call the comm handlers?
177 */
178 void
179 comm_read_cancel(int fd, IOCB *callback, void *data)
180 {
181 if (!isOpen(fd)) {
182 debugs(5, 4, "fails: FD " << fd << " closed");
183 return;
184 }
185
186 Comm::IoCallback *cb = COMMIO_FD_READCB(fd);
187 // TODO: is "active" == "monitors FD"?
188 if (!cb->active()) {
189 debugs(5, 4, "fails: FD " << fd << " inactive");
190 return;
191 }
192
193 typedef CommCbFunPtrCallT<CommIoCbPtrFun> Call;
194 Call *call = dynamic_cast<Call*>(cb->callback.getRaw());
195 if (!call) {
196 debugs(5, 4, "fails: FD " << fd << " lacks callback");
197 return;
198 }
199
200 call->cancel("old comm_read_cancel");
201
202 typedef CommIoCbParams Params;
203 const Params &params = GetCommParams<Params>(cb->callback);
204
205 /* Ok, we can be reasonably sure we won't lose any data here! */
206 assert(call->dialer.handler == callback);
207 assert(params.data == data);
208
209 /* Delete the callback */
210 cb->cancel("old comm_read_cancel");
211
212 /* And the IO event */
213 Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
214 }
215
216 void
217 Comm::ReadCancel(int fd, AsyncCall::Pointer &callback)
218 {
219 callback->cancel("comm_read_cancel");
220
221 if (!isOpen(fd)) {
222 debugs(5, 4, "fails: FD " << fd << " closed");
223 return;
224 }
225
226 Comm::IoCallback *cb = COMMIO_FD_READCB(fd);
227
228 if (!cb->active()) {
229 debugs(5, 4, "fails: FD " << fd << " inactive");
230 return;
231 }
232
233 AsyncCall::Pointer call = cb->callback;
234
235 /* Ok, we can be reasonably sure we won't lose any data here! */
236 assert(call == callback);
237
238 /* Delete the callback */
239 cb->cancel("comm_read_cancel");
240
241 /* And the IO event */
242 Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
243 }
244