]> git.ipfire.org Git - thirdparty/squid.git/blame - src/whois.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / whois.cc
CommitLineData
af6691cc 1/*
77b1029d 2 * Copyright (C) 1996-2020 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
c8407295 121 if (flag != Comm::OK) {
b69e9ffa 122 debugs(50, 2, conn << ": read failure: " << xstrerr(xerrno));
62e76326 123
b69e9ffa 124 if (ignoreErrno(xerrno)) {
1b76e6c1
AJ
125 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
126 CommIoCbPtrFun(whoisReadReply, this));
127 comm_read(conn, aBuffer, BUFSIZ, call);
6cae5db1 128 } else {
7e6eabbc 129 const auto err = new ErrorState(ERR_READ_ERROR, Http::scInternalServerError, fwd->request, fwd->al);
129fe2a1 130 err->xerrno = xerrno;
b6b6f466 131 fwd->fail(err);
80463bb4 132 conn->close();
62e76326 133 }
58c0b17d
AJ
134 return;
135 }
62e76326 136
58c0b17d
AJ
137 if (aBufferLength > 0) {
138 if (!dataWritten)
139 setReplyToOK(entry);
62e76326 140
a0864754
AJ
141 statCounter.server.all.kbytes_in += aBufferLength;
142 statCounter.server.http.kbytes_in += aBufferLength;
62e76326 143
58c0b17d
AJ
144 /* No range support, we always grab it all */
145 dataWritten = true;
146 entry->append(aBuffer, aBufferLength);
147 entry->flush();
62e76326 148
abd8f140
AJ
149 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
150 CommIoCbPtrFun(whoisReadReply, this));
151 comm_read(conn, aBuffer, BUFSIZ, call);
58c0b17d 152 return;
abd8f140 153 }
62e76326 154
58c0b17d
AJ
155 /* no bytes read. stop reading */
156 entry->timestampsSet();
157 entry->flush();
158
4310f8b0
EB
159 if (!entry->makePublic())
160 entry->makePrivate(true);
58c0b17d
AJ
161
162 fwd->complete();
163 debugs(75, 3, "whoisReadReply: Done: " << entry->url());
1b76e6c1 164 conn->close();
af6691cc 165}
166
167static void
575d05c4 168whoisClose(const CommCloseCbParams &params)
af6691cc 169{
575d05c4
AJ
170 WhoisState *p = (WhoisState *)params.data;
171 debugs(75, 3, "whoisClose: FD " << params.fd);
1bfe9ade 172 p->entry->unlock("whoisClose");
c31bb519 173 delete p;
af6691cc 174}
f53969cc 175