Squid Web Cache master
Loading...
Searching...
No Matches
WhoisGateway.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 75 WHOIS protocol */
10
11#include "squid.h"
13#include "comm.h"
14#include "comm/Read.h"
15#include "comm/Write.h"
16#include "errorpage.h"
17#include "FwdState.h"
18#include "HttpReply.h"
19#include "HttpRequest.h"
20#include "SquidConfig.h"
21#include "StatCounters.h"
22#include "Store.h"
23#include "tools.h"
24
25#include <cerrno>
26
28{
30
31public:
32 void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno);
33 void setReplyToOK(StoreEntry *sentry);
37 char buf[BUFSIZ+1]; /* readReply adds terminating NULL */
39};
40
42
46
47/* PUBLIC */
48
49static void
50whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
51{
52 xfree(buf);
53}
54
55void
57{
58 WhoisState *p = new WhoisState;
59 p->request = fwd->request;
60 p->entry = fwd->entry;
61 p->fwd = fwd;
62 p->dataWritten = false;
63
64 p->entry->lock("whoisStart");
66
67 size_t l = p->request->url.absolutePath().length() + 3;
68 char *buf = (char *)xmalloc(l);
69
70 const SBuf str_print = p->request->url.absolutePath().substr(1);
71 snprintf(buf, l, SQUIDSBUFPH "\r\n", SQUIDSBUFPRINT(str_print));
72
73 AsyncCall::Pointer writeCall = commCbCall(5,5, "whoisWriteComplete",
75 Comm::Write(fwd->serverConnection(), buf, strlen(buf), writeCall, nullptr);
76 AsyncCall::Pointer readCall = commCbCall(5,4, "whoisReadReply",
78 comm_read(fwd->serverConnection(), p->buf, BUFSIZ, readCall);
79 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "whoisTimeout",
82}
83
84/* PRIVATE */
85
86static void
88{
89 WhoisState *p = static_cast<WhoisState *>(io.data);
90 debugs(75, 3, io.conn << ", URL " << p->entry->url());
91 io.conn->close();
92}
93
94static void
95whoisReadReply(const Comm::ConnectionPointer &conn, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data)
96{
97 WhoisState *p = (WhoisState *)data;
98 p->readReply(conn, buf, len, flag, xerrno);
99}
100
101void
103{
104 HttpReply *reply = new HttpReply;
105 sentry->buffer();
106 reply->setHeaders(Http::scOkay, "Gatewaying", "text/plain", -1, -1, -2);
108 sentry->replaceHttpReply(reply);
109}
110
111void
112WhoisState::readReply(const Comm::ConnectionPointer &conn, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
113{
114 /* Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us */
115 if (flag == Comm::ERR_CLOSING)
116 return;
117
118 aBuffer[aBufferLength] = '\0';
119 debugs(75, 3, conn << " read " << aBufferLength << " bytes");
120 debugs(75, 5, "{" << aBuffer << "}");
121
122 // TODO: Honor delay pools.
123
124 // XXX: Update statCounter before bailing
125 if (!entry->isAccepting()) {
126 debugs(52, 3, "terminating due to bad " << *entry);
127 // TODO: Do not abuse connection for triggering cleanup.
128 conn->close();
129 return;
130 }
131
132 if (flag != Comm::OK) {
133 debugs(50, 2, conn << ": read failure: " << xstrerr(xerrno));
134
135 if (ignoreErrno(xerrno)) {
136 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
138 comm_read(conn, aBuffer, BUFSIZ, call);
139 } else {
141 err->xerrno = xerrno;
142 fwd->fail(err);
143 conn->close();
144 }
145 return;
146 }
147
148 if (aBufferLength > 0) {
149 if (!dataWritten)
151
152 statCounter.server.all.kbytes_in += aBufferLength;
153 statCounter.server.http.kbytes_in += aBufferLength;
154
155 /* No range support, we always grab it all */
156 dataWritten = true;
157 entry->append(aBuffer, aBufferLength);
158 entry->flush();
159
160 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
162 comm_read(conn, aBuffer, BUFSIZ, call);
163 return;
164 }
165
166 /* no bytes read. stop reading */
168 entry->flush();
169
170 if (!entry->makePublic())
171 entry->makePrivate(true);
172
173 if (dataWritten) // treat zero-length responses as incomplete
174 fwd->markStoredReplyAsWhole("whois received/stored the entire response");
175 else
177
178 fwd->complete();
179 debugs(75, 3, "whoisReadReply: Done: " << entry->url());
180 conn->close();
181}
182
183static void
185{
186 WhoisState *p = (WhoisState *)params.data;
187 debugs(75, 3, "whoisClose: FD " << params.fd);
188 // We do not own a Connection. Assume that FwdState is also monitoring.
189 p->entry->unlock("whoisClose");
190 delete p;
191}
192
CommCbFunPtrCallT< Dialer > * commCbCall(int debugSection, int debugLevel, const char *callName, const Dialer &dialer)
Definition CommCalls.h:312
void CTCB(const CommTimeoutCbParams &params)
Definition CommCalls.h:37
void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data)
Definition CommCalls.h:34
void CLCB(const CommCloseCbParams &params)
Definition CommCalls.h:40
void comm_read(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer &callback)
Definition Read.h:59
#define SQUIDSBUFPH
Definition SBuf.h:31
#define SQUIDSBUFPRINT(s)
Definition SBuf.h:32
class SquidConfig Config
StatCounters statCounter
static void whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
static CTCB whoisTimeout
static IOCB whoisReadReply
static CLCB whoisClose
#define CBDATA_CLASS_INIT(type)
Definition cbdata.h:325
SBuf & absolutePath() const
RFC 3986 section 4.2 relative reference called 'absolute-path'.
Definition Uri.cc:775
int fd
FD which the call was about. Set by the async call creator.
Definition CommCalls.h:85
Comm::ConnectionPointer conn
Definition CommCalls.h:80
HttpRequest * request
Definition FwdState.h:203
void complete()
Definition FwdState.cc:526
Comm::ConnectionPointer const & serverConnection() const
Definition FwdState.h:138
void fail(ErrorState *err)
Definition FwdState.cc:458
StoreEntry * entry
Definition FwdState.h:202
void markStoredReplyAsWhole(const char *whyWeAreSure)
Definition FwdState.cc:575
AccessLogEntryPointer al
info for the future access.log entry
Definition FwdState.h:204
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition HttpReply.cc:170
AnyP::Uri url
the request URI
@ srcWhois
Whois server.
Definition Message.h:43
uint32_t sources
The message sources.
Definition Message.h:99
Definition SBuf.h:94
size_type length() const
Returns the number of bytes stored in SBuf.
Definition SBuf.h:419
SBuf substr(size_type pos, size_type n=npos) const
Definition SBuf.cc:576
struct SquidConfig::@77 Timeout
struct StatCounters::@105::@115 http
struct StatCounters::@105 server
ByteCounter kbytes_in
struct StatCounters::@105::@115 all
bool isAccepting() const
Definition store.cc:1988
int unlock(const char *context)
Definition store.cc:469
const char * url() const
Definition store.cc:1566
void lock(const char *context)
Definition store.cc:445
void flush() override
Definition store.cc:1612
bool makePublic(const KeyScope keyScope=ksDefault)
Definition store.cc:167
bool timestampsSet()
Definition store.cc:1387
void makePrivate(const bool shareable)
Definition store.cc:174
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition store.cc:1705
void append(char const *, int) override
Appends a c-string to existing packed data.
Definition store.cc:803
void buffer() override
Definition store.cc:1601
void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
StoreEntry * entry
char buf[BUFSIZ+1]
CBDATA_CLASS(WhoisState)
void setReplyToOK(StoreEntry *sentry)
HttpRequest::Pointer request
FwdState::Pointer fwd
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition comm.cc:942
void commSetConnTimeout(const Comm::ConnectionPointer &conn, time_t timeout, AsyncCall::Pointer &callback)
Definition comm.cc:594
int ignoreErrno(int ierrno)
Definition comm.cc:1407
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define BUFSIZ
Definition defines.h:20
@ ERR_ZERO_SIZE_OBJECT
Definition forward.h:46
@ ERR_READ_ERROR
Definition forward.h:28
void whoisStart(FwdState *fwd)
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition Write.cc:33
Flag
Definition Flag.h:15
@ OK
Definition Flag.h:16
@ ERR_CLOSING
Definition Flag.h:24
@ scInternalServerError
Definition StatusCode.h:73
@ scOkay
Definition StatusCode.h:27
@ scBadGateway
Definition StatusCode.h:75
#define xfree
#define xmalloc
const char * xstrerr(int error)
Definition xstrerror.cc:83