]> git.ipfire.org Git - thirdparty/squid.git/blame - src/whois.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / whois.cc
CommitLineData
af6691cc 1/*
bde978a6 2 * Copyright (C) 1996-2015 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
af6691cc 26#define WHOIS_PORT 43
27
62e76326 28class WhoisState
29{
5c2f68b7
AJ
30 CBDATA_CLASS(WhoisState);
31
528b2c61 32public:
c8407295 33 void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno);
e053c141 34 void setReplyToOK(StoreEntry *sentry);
af6691cc 35 StoreEntry *entry;
c31bb519 36 HttpRequest::Pointer request;
b6b6f466 37 FwdState::Pointer fwd;
f53969cc 38 char buf[BUFSIZ+1]; /* readReply adds terminating NULL */
528b2c61 39 bool dataWritten;
40};
af6691cc 41
c31bb519
AJ
42CBDATA_CLASS_INIT(WhoisState);
43
575d05c4 44static CLCB whoisClose;
8d77a37c 45static CTCB whoisTimeout;
c4b7a5a9 46static IOCB whoisReadReply;
af6691cc 47
48/* PUBLIC */
49
e6546865 50static void
ced8def3 51whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
e6546865 52{
62e76326 53 xfree(buf);
e6546865 54}
55
af6691cc 56void
db1cd23c 57whoisStart(FwdState * fwd)
af6691cc 58{
af6691cc 59 char *buf;
60 size_t l;
c31bb519 61 WhoisState *p = new WhoisState;
db1cd23c 62 p->request = fwd->request;
63 p->entry = fwd->entry;
64 p->fwd = fwd;
5bac8e33 65 p->dataWritten = false;
34266cde 66
1bfe9ade 67 p->entry->lock("whoisStart");
e0d28505 68 comm_add_close_handler(fwd->serverConnection()->fd, whoisClose, p);
34266cde 69
528b2c61 70 l = p->request->urlpath.size() + 3;
34266cde 71
e6ccf245 72 buf = (char *)xmalloc(l);
34266cde 73
2c1fd837
FC
74 String str_print=p->request->urlpath.substr(1,p->request->urlpath.size());
75 snprintf(buf, l, SQUIDSTRINGPH"\r\n", SQUIDSTRINGPRINT(str_print));
34266cde 76
abd8f140 77 AsyncCall::Pointer writeCall = commCbCall(5,5, "whoisWriteComplete",
dc49061a 78 CommIoCbPtrFun(whoisWriteComplete, p));
abd8f140
AJ
79 Comm::Write(fwd->serverConnection(), buf, strlen(buf), writeCall, NULL);
80 AsyncCall::Pointer readCall = commCbCall(5,4, "whoisReadReply",
dc49061a 81 CommIoCbPtrFun(whoisReadReply, p));
abd8f140 82 comm_read(fwd->serverConnection(), p->buf, BUFSIZ, readCall);
8d77a37c 83 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "whoisTimeout",
dc49061a 84 CommTimeoutCbPtrFun(whoisTimeout, p));
8d77a37c 85 commSetConnTimeout(fwd->serverConnection(), Config.Timeout.read, timeoutCall);
af6691cc 86}
87
88/* PRIVATE */
89
90static void
8d77a37c 91whoisTimeout(const CommTimeoutCbParams &io)
af6691cc 92{
8d77a37c
AJ
93 WhoisState *p = static_cast<WhoisState *>(io.data);
94 debugs(75, 3, HERE << io.conn << ", URL " << p->entry->url());
95 io.conn->close();
af6691cc 96}
97
98static void
c8407295 99whoisReadReply(const Comm::ConnectionPointer &conn, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data)
af6691cc 100{
e6ccf245 101 WhoisState *p = (WhoisState *)data;
e0d28505 102 p->readReply(conn, buf, len, flag, xerrno);
528b2c61 103}
104
105void
e053c141 106WhoisState::setReplyToOK(StoreEntry *sentry)
528b2c61 107{
06a5ae20 108 HttpReply *reply = new HttpReply;
e053c141 109 sentry->buffer();
955394ce 110 reply->setHeaders(Http::scOkay, "Gatewaying", "text/plain", -1, -1, -2);
e053c141 111 sentry->replaceHttpReply(reply);
528b2c61 112}
113
114void
c8407295 115WhoisState::readReply(const Comm::ConnectionPointer &conn, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
528b2c61 116{
c8407295
AJ
117 /* Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us */
118 if (flag == Comm::ERR_CLOSING)
c4b7a5a9 119 return;
c4b7a5a9 120
e053c141 121 aBuffer[aBufferLength] = '\0';
e0d28505 122 debugs(75, 3, HERE << conn << " read " << aBufferLength << " bytes");
e053c141 123 debugs(75, 5, "{" << aBuffer << "}");
62e76326 124
c8407295 125 if (flag != Comm::OK) {
e0d28505 126 debugs(50, 2, HERE << conn << ": read failure: " << xstrerror() << ".");
62e76326 127
128 if (ignoreErrno(errno)) {
1b76e6c1
AJ
129 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
130 CommIoCbPtrFun(whoisReadReply, this));
131 comm_read(conn, aBuffer, BUFSIZ, call);
6cae5db1 132 } else {
955394ce 133 ErrorState *err = new ErrorState(ERR_READ_ERROR, Http::scInternalServerError, fwd->request);
129fe2a1 134 err->xerrno = xerrno;
b6b6f466 135 fwd->fail(err);
80463bb4 136 conn->close();
62e76326 137 }
58c0b17d
AJ
138 return;
139 }
62e76326 140
58c0b17d
AJ
141 if (aBufferLength > 0) {
142 if (!dataWritten)
143 setReplyToOK(entry);
62e76326 144
e4f1fdae
FC
145 kb_incr(&(statCounter.server.all.kbytes_in), aBufferLength);
146 kb_incr(&(statCounter.server.http.kbytes_in), aBufferLength);
62e76326 147
58c0b17d
AJ
148 /* No range support, we always grab it all */
149 dataWritten = true;
150 entry->append(aBuffer, aBufferLength);
151 entry->flush();
62e76326 152
abd8f140
AJ
153 AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
154 CommIoCbPtrFun(whoisReadReply, this));
155 comm_read(conn, aBuffer, BUFSIZ, call);
58c0b17d 156 return;
abd8f140 157 }
62e76326 158
58c0b17d
AJ
159 /* no bytes read. stop reading */
160 entry->timestampsSet();
161 entry->flush();
162
6919be24 163 entry->makePublic();
58c0b17d
AJ
164
165 fwd->complete();
166 debugs(75, 3, "whoisReadReply: Done: " << entry->url());
1b76e6c1 167 conn->close();
af6691cc 168}
169
170static void
575d05c4 171whoisClose(const CommCloseCbParams &params)
af6691cc 172{
575d05c4
AJ
173 WhoisState *p = (WhoisState *)params.data;
174 debugs(75, 3, "whoisClose: FD " << params.fd);
1bfe9ade 175 p->entry->unlock("whoisClose");
c31bb519 176 delete p;
af6691cc 177}
f53969cc 178