]> git.ipfire.org Git - thirdparty/squid.git/blame - src/whois.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / whois.cc
CommitLineData
af6691cc 1/*
f70aedc4 2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
e25c139f 3 *
bbc27441
AJ
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.
af6691cc 7 */
8
bbc27441
AJ
9/* DEBUG: section 75 WHOIS protocol */
10
582c2af2
FC
11#include "squid.h"
12#include "comm.h"
7e66d5e2 13#include "comm/Read.h"
ec41b64c 14#include "comm/Write.h"
aa839030 15#include "errorpage.h"
eb13c21e 16#include "FwdState.h"
528b2c61 17#include "HttpReply.h"
924f73bc 18#include "HttpRequest.h"
4d5904f7 19#include "SquidConfig.h"
e4f1fdae 20#include "StatCounters.h"
582c2af2 21#include "Store.h"
4e540555 22#include "tools.h"
af6691cc 23
1a30fdf5 24#include <cerrno>
21d845b1 25
62e76326 26class WhoisState
27{
5c2f68b7
AJ
28 CBDATA_CLASS(WhoisState);
29
528b2c61 30public:
c8407295 31 void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno);
e053c141 32 void setReplyToOK(StoreEntry *sentry);
af6691cc 33 StoreEntry *entry;
c31bb519 34 HttpRequest::Pointer request;
b6b6f466 35 FwdState::Pointer fwd;
f53969cc 36 char buf[BUFSIZ+1]; /* readReply adds terminating NULL */
528b2c61 37 bool dataWritten;
38};
af6691cc 39
c31bb519
AJ
40CBDATA_CLASS_INIT(WhoisState);
41
575d05c4 42static CLCB whoisClose;
8d77a37c 43static CTCB whoisTimeout;
c4b7a5a9 44static IOCB whoisReadReply;
af6691cc 45
46/* PUBLIC */
47
e6546865 48static void
ced8def3 49whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
e6546865 50{
62e76326 51 xfree(buf);
e6546865 52}
53
af6691cc 54void
db1cd23c 55whoisStart(FwdState * fwd)
af6691cc 56{
c31bb519 57 WhoisState *p = new WhoisState;
db1cd23c 58 p->request = fwd->request;
59 p->entry = fwd->entry;
60 p->fwd = fwd;
5bac8e33 61 p->dataWritten = false;
34266cde 62
1bfe9ade 63 p->entry->lock("whoisStart");
e0d28505 64 comm_add_close_handler(fwd->serverConnection()->fd, whoisClose, p);
34266cde 65
51b5dcf5
AJ
66 size_t l = p->request->url.path().length() + 3;
67 char *buf = (char *)xmalloc(l);
34266cde 68
51b5dcf5
AJ
69 const SBuf str_print = p->request->url.path().substr(1);
70 snprintf(buf, l, SQUIDSBUFPH "\r\n", SQUIDSBUFPRINT(str_print));
34266cde 71
abd8f140 72 AsyncCall::Pointer writeCall = commCbCall(5,5, "whoisWriteComplete",
dc49061a 73 CommIoCbPtrFun(whoisWriteComplete, p));
abd8f140
AJ
74 Comm::Write(fwd->serverConnection(), buf, strlen(buf), writeCall, NULL);
75 AsyncCall::Pointer readCall = commCbCall(5,4, "whoisReadReply",
dc49061a 76 CommIoCbPtrFun(whoisReadReply, p));
abd8f140 77 comm_read(fwd->serverConnection(), p->buf, BUFSIZ, readCall);
8d77a37c 78 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "whoisTimeout",
dc49061a 79 CommTimeoutCbPtrFun(whoisTimeout, p));
8d77a37c 80 commSetConnTimeout(fwd->serverConnection(), Config.Timeout.read, timeoutCall);
af6691cc 81}
82
83/* PRIVATE */
84
85static void
8d77a37c 86whoisTimeout(const CommTimeoutCbParams &io)
af6691cc 87{
8d77a37c
AJ
88 WhoisState *p = static_cast<WhoisState *>(io.data);
89 debugs(75, 3, HERE << io.conn << ", URL " << p->entry->url());
90 io.conn->close();
af6691cc 91}
92
93static void
c8407295 94whoisReadReply(const Comm::ConnectionPointer &conn, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data)
af6691cc 95{
e6ccf245 96 WhoisState *p = (WhoisState *)data;
e0d28505 97 p->readReply(conn, buf, len, flag, xerrno);
528b2c61 98}
99
100void
e053c141 101WhoisState::setReplyToOK(StoreEntry *sentry)
528b2c61 102{
06a5ae20 103 HttpReply *reply = new HttpReply;
e053c141 104 sentry->buffer();
955394ce 105 reply->setHeaders(Http::scOkay, "Gatewaying", "text/plain", -1, -1, -2);
63df1d28 106 reply->sources |= Http::Message::srcWhois;
e053c141 107 sentry->replaceHttpReply(reply);
528b2c61 108}
109
110void
c8407295 111WhoisState::readReply(const Comm::ConnectionPointer &conn, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
528b2c61 112{
c8407295
AJ
113 /* Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us */
114 if (flag == Comm::ERR_CLOSING)
c4b7a5a9 115 return;
c4b7a5a9 116
e053c141 117 aBuffer[aBufferLength] = '\0';
e0d28505 118 debugs(75, 3, HERE << conn << " read " << aBufferLength << " bytes");
e053c141 119 debugs(75, 5, "{" << aBuffer << "}");
62e76326 120
d2b604ec
AR
121 // TODO: Honor delay pools.
122
123 // XXX: Update statCounter before bailing
124 if (!entry->isAccepting()) {
125 debugs(52, 3, "terminating due to bad " << *entry);
126 // TODO: Do not abuse connection for triggering cleanup.
127 conn->close();
128 return;
129 }
130
c8407295 131 if (flag != Comm::OK) {
b69e9ffa 132 debugs(50, 2, conn << ": read failure: " << xstrerr(xerrno));
62e76326 133
b69e9ffa 134 if (ignoreErrno(xerrno)) {
1b76e6c1
AJ
135 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
136 CommIoCbPtrFun(whoisReadReply, this));
137 comm_read(conn, aBuffer, BUFSIZ, call);
6cae5db1 138 } else {
7e6eabbc 139 const auto err = new ErrorState(ERR_READ_ERROR, Http::scInternalServerError, fwd->request, fwd->al);
129fe2a1 140 err->xerrno = xerrno;
b6b6f466 141 fwd->fail(err);
80463bb4 142 conn->close();
62e76326 143 }
58c0b17d
AJ
144 return;
145 }
62e76326 146
58c0b17d
AJ
147 if (aBufferLength > 0) {
148 if (!dataWritten)
149 setReplyToOK(entry);
62e76326 150
a0864754
AJ
151 statCounter.server.all.kbytes_in += aBufferLength;
152 statCounter.server.http.kbytes_in += aBufferLength;
62e76326 153
58c0b17d
AJ
154 /* No range support, we always grab it all */
155 dataWritten = true;
156 entry->append(aBuffer, aBufferLength);
157 entry->flush();
62e76326 158
abd8f140
AJ
159 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
160 CommIoCbPtrFun(whoisReadReply, this));
161 comm_read(conn, aBuffer, BUFSIZ, call);
58c0b17d 162 return;
abd8f140 163 }
62e76326 164
58c0b17d
AJ
165 /* no bytes read. stop reading */
166 entry->timestampsSet();
167 entry->flush();
168
4310f8b0
EB
169 if (!entry->makePublic())
170 entry->makePrivate(true);
58c0b17d
AJ
171
172 fwd->complete();
173 debugs(75, 3, "whoisReadReply: Done: " << entry->url());
1b76e6c1 174 conn->close();
af6691cc 175}
176
177static void
575d05c4 178whoisClose(const CommCloseCbParams &params)
af6691cc 179{
575d05c4
AJ
180 WhoisState *p = (WhoisState *)params.data;
181 debugs(75, 3, "whoisClose: FD " << params.fd);
1bfe9ade 182 p->entry->unlock("whoisClose");
c31bb519 183 delete p;
af6691cc 184}
f53969cc 185