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