]> git.ipfire.org Git - thirdparty/squid.git/blame - src/whois.cc
Set default basic auth realm
[thirdparty/squid.git] / src / whois.cc
CommitLineData
77b32a34 1
af6691cc 2/*
b66315e4 3 * $Id: whois.cc,v 1.30 2005/09/10 19:31:31 serassio Exp $
af6691cc 4 *
5 * DEBUG: section 75 WHOIS protocol
6 * AUTHOR: Duane Wessels, Kostas Anagnostakis
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
af6691cc 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
af6691cc 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
af6691cc 34 */
35
36#include "squid.h"
e6ccf245 37#include "Store.h"
528b2c61 38#include "HttpReply.h"
924f73bc 39#include "HttpRequest.h"
e6546865 40#include "comm.h"
a2ac85d9 41#include "HttpRequest.h"
af6691cc 42
43#define WHOIS_PORT 43
44
62e76326 45class WhoisState
46{
47
528b2c61 48public:
49 void readReply (int fd, char *buf, size_t len, comm_err_t flag, int xerrno);
50 void setReplyToOK(StoreEntry *entry);
af6691cc 51 StoreEntry *entry;
190154cf 52 HttpRequest *request;
db1cd23c 53 FwdState *fwd;
c4b7a5a9 54 char buf[BUFSIZ];
528b2c61 55 bool dataWritten;
56};
af6691cc 57
58static PF whoisClose;
59static PF whoisTimeout;
c4b7a5a9 60static IOCB whoisReadReply;
af6691cc 61
62/* PUBLIC */
63
28c60158 64CBDATA_TYPE(WhoisState);
65
e6546865 66static void
67whoisWriteComplete(int fd, char *buf, size_t size, comm_err_t flag, int xerrno, void *data)
68{
62e76326 69 xfree(buf);
e6546865 70}
71
af6691cc 72void
db1cd23c 73whoisStart(FwdState * fwd)
af6691cc 74{
28c60158 75 WhoisState *p;
db1cd23c 76 int fd = fwd->server_fd;
af6691cc 77 char *buf;
78 size_t l;
28c60158 79 CBDATA_INIT_TYPE(WhoisState);
72711e31 80 p = cbdataAlloc(WhoisState);
db1cd23c 81 p->request = fwd->request;
82 p->entry = fwd->entry;
83 p->fwd = fwd;
528b2c61 84 p->dataWritten = 0;
af6691cc 85 storeLockObject(p->entry);
86 comm_add_close_handler(fd, whoisClose, p);
528b2c61 87 l = p->request->urlpath.size() + 3;
e6ccf245 88 buf = (char *)xmalloc(l);
528b2c61 89 snprintf(buf, l, "%s\r\n", p->request->urlpath.buf() + 1);
e6546865 90 comm_write(fd, buf, strlen(buf), whoisWriteComplete, p);
c4b7a5a9 91 comm_read(fd, p->buf, BUFSIZ, whoisReadReply, p);
af6691cc 92 commSetTimeout(fd, Config.Timeout.read, whoisTimeout, p);
93}
94
95/* PRIVATE */
96
97static void
98whoisTimeout(int fd, void *data)
99{
e6ccf245 100 WhoisState *p = (WhoisState *)data;
af6691cc 101 debug(75, 1) ("whoisTimeout: %s\n", storeUrl(p->entry));
102 whoisClose(fd, p);
103}
104
105static void
c4b7a5a9 106whoisReadReply(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
af6691cc 107{
e6ccf245 108 WhoisState *p = (WhoisState *)data;
528b2c61 109 p->readReply(fd, buf, len, flag, xerrno);
110}
111
112void
113WhoisState::setReplyToOK(StoreEntry *entry)
114{
62e76326 115 HttpReply *reply = httpReplyCreate();
b66315e4 116 storeBuffer(entry);
450e0c10 117 HttpVersion version(1, 0);
b66315e4 118 httpReplySetHeaders(reply, version, HTTP_OK, "Gatewaying", "text/plain", -1, -1, -2);
62e76326 119 storeEntryReplaceObject (entry, reply);
528b2c61 120}
121
122void
123WhoisState::readReply (int fd, char *buf, size_t len, comm_err_t flag, int xerrno)
124{
c4b7a5a9 125 int do_next_read = 0;
126
127 /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us */
62e76326 128
c4b7a5a9 129 if (flag == COMM_ERR_CLOSING) {
130 return;
131 }
132
af6691cc 133 buf[len] = '\0';
c4b7a5a9 134 debug(75, 3) ("whoisReadReply: FD %d read %d bytes\n", fd, (int)len);
af6691cc 135 debug(75, 5) ("{%s}\n", buf);
62e76326 136
c4b7a5a9 137 if (flag == COMM_OK && len > 0) {
62e76326 138 if (!dataWritten)
139 setReplyToOK(entry);
140
141 kb_incr(&statCounter.server.all.kbytes_in, len);
142
143 kb_incr(&statCounter.server.http.kbytes_in, len);
144
145 /* No range support, we always grab it all */
146 dataWritten = 1;
147
148 storeAppend(entry, buf, len);
149
b66315e4 150 storeBufferFlush(entry);
151
c4b7a5a9 152 do_next_read = 1;
153 } else if (flag != COMM_OK || len < 0) {
62e76326 154 debug(50, 2) ("whoisReadReply: FD %d: read failure: %s.\n",
155 fd, xstrerror());
156
157 if (ignoreErrno(errno)) {
c4b7a5a9 158 do_next_read = 1;
6cae5db1 159 } else {
62e76326 160 ErrorState *err;
161 err = errorCon(ERR_READ_ERROR, HTTP_INTERNAL_SERVER_ERROR);
162 err->xerrno = errno;
163 fwdFail(fwd, err);
164 comm_close(fd);
c4b7a5a9 165 do_next_read = 0;
62e76326 166 }
abdacbb5 167 } else {
62e76326 168 storeTimestampsSet(entry);
169 storeBufferFlush(entry);
170
171 if (!EBIT_TEST(entry->flags, RELEASE_REQUEST))
172 storeSetPublicKey(entry);
173
174 fwdComplete(fwd);
175
176 debug(75, 3) ("whoisReadReply: Done: %s\n", storeUrl(entry));
177
178 comm_close(fd);
179
c4b7a5a9 180 do_next_read = 0;
af6691cc 181 }
62e76326 182
c4b7a5a9 183 if (do_next_read)
528b2c61 184 comm_read(fd, buf, BUFSIZ, whoisReadReply, this);
af6691cc 185}
186
187static void
188whoisClose(int fd, void *data)
189{
e6ccf245 190 WhoisState *p = (WhoisState *)data;
af6691cc 191 debug(75, 3) ("whoisClose: FD %d\n", fd);
192 storeUnlockObject(p->entry);
193 cbdataFree(p);
194}