whois.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 75 WHOIS protocol */
10 
11 #include "squid.h"
12 #include "comm.h"
13 #include "comm/Read.h"
14 #include "comm/Write.h"
15 #include "errorpage.h"
16 #include "FwdState.h"
17 #include "HttpReply.h"
18 #include "HttpRequest.h"
19 #include "SquidConfig.h"
20 #include "StatCounters.h"
21 #include "Store.h"
22 #include "tools.h"
23 
24 #include <cerrno>
25 
27 {
29 
30 public:
31  void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno);
32  void setReplyToOK(StoreEntry *sentry);
36  char buf[BUFSIZ+1]; /* readReply adds terminating NULL */
38 };
39 
41 
45 
46 /* PUBLIC */
47 
48 static void
49 whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
50 {
51  xfree(buf);
52 }
53 
54 void
56 {
57  WhoisState *p = new WhoisState;
58  p->request = fwd->request;
59  p->entry = fwd->entry;
60  p->fwd = fwd;
61  p->dataWritten = false;
62 
63  p->entry->lock("whoisStart");
65 
66  size_t l = p->request->url.path().length() + 3;
67  char *buf = (char *)xmalloc(l);
68 
69  const SBuf str_print = p->request->url.path().substr(1);
70  snprintf(buf, l, SQUIDSBUFPH "\r\n", SQUIDSBUFPRINT(str_print));
71 
72  AsyncCall::Pointer writeCall = commCbCall(5,5, "whoisWriteComplete",
74  Comm::Write(fwd->serverConnection(), buf, strlen(buf), writeCall, NULL);
75  AsyncCall::Pointer readCall = commCbCall(5,4, "whoisReadReply",
77  comm_read(fwd->serverConnection(), p->buf, BUFSIZ, readCall);
78  AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "whoisTimeout",
81 }
82 
83 /* PRIVATE */
84 
85 static void
87 {
88  WhoisState *p = static_cast<WhoisState *>(io.data);
89  debugs(75, 3, HERE << io.conn << ", URL " << p->entry->url());
90  io.conn->close();
91 }
92 
93 static void
94 whoisReadReply(const Comm::ConnectionPointer &conn, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data)
95 {
96  WhoisState *p = (WhoisState *)data;
97  p->readReply(conn, buf, len, flag, xerrno);
98 }
99 
100 void
102 {
103  HttpReply *reply = new HttpReply;
104  sentry->buffer();
105  reply->setHeaders(Http::scOkay, "Gatewaying", "text/plain", -1, -1, -2);
107  sentry->replaceHttpReply(reply);
108 }
109 
110 void
111 WhoisState::readReply(const Comm::ConnectionPointer &conn, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
112 {
113  /* Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us */
114  if (flag == Comm::ERR_CLOSING)
115  return;
116 
117  aBuffer[aBufferLength] = '\0';
118  debugs(75, 3, HERE << conn << " read " << aBufferLength << " bytes");
119  debugs(75, 5, "{" << aBuffer << "}");
120 
121  if (flag != Comm::OK) {
122  debugs(50, 2, conn << ": read failure: " << xstrerr(xerrno));
123 
124  if (ignoreErrno(xerrno)) {
125  AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
127  comm_read(conn, aBuffer, BUFSIZ, call);
128  } else {
130  err->xerrno = xerrno;
131  fwd->fail(err);
132  conn->close();
133  }
134  return;
135  }
136 
137  if (aBufferLength > 0) {
138  if (!dataWritten)
140 
141  statCounter.server.all.kbytes_in += aBufferLength;
142  statCounter.server.http.kbytes_in += aBufferLength;
143 
144  /* No range support, we always grab it all */
145  dataWritten = true;
146  entry->append(aBuffer, aBufferLength);
147  entry->flush();
148 
149  AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
151  comm_read(conn, aBuffer, BUFSIZ, call);
152  return;
153  }
154 
155  /* no bytes read. stop reading */
156  entry->timestampsSet();
157  entry->flush();
158 
159  if (!entry->makePublic())
160  entry->makePrivate(true);
161 
162  fwd->complete();
163  debugs(75, 3, "whoisReadReply: Done: " << entry->url());
164  conn->close();
165 }
166 
167 static void
169 {
170  WhoisState *p = (WhoisState *)params.data;
171  debugs(75, 3, "whoisClose: FD " << params.fd);
172  p->entry->unlock("whoisClose");
173  delete p;
174 }
175 
const char * xstrerr(int error)
Definition: xstrerror.cc:83
void makePrivate(const bool shareable)
Definition: store.cc:172
struct StatCounters::@130::@140 all
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:983
static CTCB whoisTimeout
Definition: whois.cc:43
virtual void buffer()
Definition: store.cc:1657
#define BUFSIZ
Definition: defines.h:20
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
#define xmalloc
void whoisStart(FwdState *fwd)
Definition: whois.cc:55
bool makePublic(const KeyScope keyScope=ksDefault)
Definition: store.cc:165
virtual void flush()
Definition: store.cc:1668
const char * url() const
Definition: store.cc:1622
void lock(const char *context)
Definition: store.cc:459
AccessLogEntryPointer al
info for the future access.log entry
Definition: FwdState.h:174
#define CBDATA_CLASS(type)
Definition: cbdata.h:302
Definition: SBuf.h:86
int commSetConnTimeout(const Comm::ConnectionPointer &conn, int timeout, AsyncCall::Pointer &callback)
Definition: comm.cc:567
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
@ OK
Definition: Flag.h:16
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1782
void CLCB(const CommCloseCbParams &params)
Definition: CommCalls.h:42
void fail(ErrorState *err)
Definition: FwdState.cc:471
@ ERR_CLOSING
Definition: Flag.h:25
static CLCB whoisClose
Definition: whois.cc:42
static void whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
Definition: whois.cc:49
void comm_read(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer &callback)
Definition: Read.h:59
void setReplyToOK(StoreEntry *sentry)
Definition: whois.cc:101
#define NULL
Definition: types.h:166
time_t read
Definition: SquidConfig.h:109
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
void CTCB(const CommTimeoutCbParams &params)
Definition: CommCalls.h:39
struct StatCounters::@130::@140 http
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
const Comm::ConnectionPointer & serverConnection() const
Definition: FwdState.h:110
StoreEntry * entry
Definition: whois.cc:33
HttpRequest::Pointer request
Definition: whois.cc:34
FwdState::Pointer fwd
Definition: whois.cc:35
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:157
void const char HLPCB void * data
Definition: stub_helper.cc:16
void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data)
Definition: CommCalls.h:36
int unlock(const char *context)
Definition: store.cc:483
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
CommCbFunPtrCallT< Dialer > * commCbCall(int debugSection, int debugLevel, const char *callName, const Dialer &dialer)
Definition: CommCalls.h:342
int conn
the current server connection FD
Definition: Transport.cc:26
@ srcWhois
Whois server.
Definition: Message.h:44
@ ERR_READ_ERROR
Definition: err_type.h:26
void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
Definition: whois.cc:111
@ scInternalServerError
Definition: StatusCode.h:71
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:35
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
#define xfree
char buf[BUFSIZ+1]
Definition: whois.cc:36
static IOCB whoisReadReply
Definition: whois.cc:44
uint32_t sources
The message sources.
Definition: Message.h:100
Flag
Definition: Flag.h:15
struct StatCounters::@130 server
int ignoreErrno(int ierrno)
Definition: comm.cc:1507
void path(const char *p)
Definition: Uri.h:99
void complete()
Definition: FwdState.cc:530
bool timestampsSet()
Definition: store.cc:1449
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
StoreEntry * entry
Definition: FwdState.h:172
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:849
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:168
struct SquidConfig::@98 Timeout
bool dataWritten
Definition: whois.cc:37
@ scOkay
Definition: StatusCode.h:26
HttpRequest * request
Definition: FwdState.h:173
void const char * buf
Definition: stub_helper.cc:16
#define SQUIDSBUFPH
Definition: SBuf.h:31
class SquidConfig Config
Definition: SquidConfig.cc:12
StatCounters statCounter
Definition: StatCounters.cc:12

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors