whois.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 
StatCounters statCounter
Definition: StatCounters.cc:12
void makePrivate(const bool shareable)
Definition: store.cc:152
int unlock(const char *context)
Definition: store.cc:463
void CTCB(const CommTimeoutCbParams &params)
Definition: CommCalls.h:39
static IOCB whoisReadReply
Definition: whois.cc:44
void path(const char *p)
Definition: Uri.h:88
bool timestampsSet()
Definition: store.cc:1460
Definition: SBuf.h:86
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:829
void setReplyToOK(StoreEntry *sentry)
Definition: whois.cc:101
struct StatCounters::@128::@137 http
virtual void buffer()
Definition: store.cc:1641
char buf[BUFSIZ+1]
Definition: whois.cc:36
Definition: Flag.h:16
virtual void flush()
Definition: store.cc:1652
static CLCB whoisClose
Definition: whois.cc:42
HttpRequest * request
Definition: FwdState.h:157
char * p
Definition: membanger.c:43
int conn
the current server connection FD
Definition: Transport.cc:26
Comm::ConnectionPointer const & serverConnection() const
Definition: FwdState.h:108
void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
Definition: whois.cc:111
void replaceHttpReply(HttpReply *, bool andStartWriting=true)
Definition: store.cc:1788
StoreEntry * entry
Definition: whois.cc:33
struct StatCounters::@128 server
Whois server.
Definition: Message.h:44
void whoisStart(FwdState *fwd)
Definition: whois.cc:55
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
const char * xstrerr(int error)
Definition: xstrerror.cc:83
void const char HLPCB void * data
Definition: stub_helper.cc:16
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:161
const char * url() const
Definition: store.cc:1606
void complete()
Definition: FwdState.cc:492
HttpRequest::Pointer request
Definition: whois.cc:34
void comm_read(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer &callback)
Definition: Read.h:59
#define CBDATA_CLASS(type)
Definition: cbdata.h:302
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
static CTCB whoisTimeout
Definition: whois.cc:43
bool dataWritten
Definition: whois.cc:37
#define BUFSIZ
Definition: defines.h:20
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void CLCB(const CommCloseCbParams &params)
Definition: CommCalls.h:42
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:153
void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data)
Definition: CommCalls.h:36
Flag
Definition: Flag.h:15
bool makePublic(const KeyScope keyScope=ksDefault)
Definition: store.cc:145
int ignoreErrno(int ierrno)
Definition: comm.cc:1477
AccessLogEntryPointer al
info for the future access.log entry
Definition: FwdState.h:158
#define xmalloc
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
struct SquidConfig::@97 Timeout
FwdState::Pointer fwd
Definition: whois.cc:35
void fail(ErrorState *err)
Definition: FwdState.cc:438
CommCbFunPtrCallT< Dialer > * commCbCall(int debugSection, int debugLevel, const char *callName, const Dialer &dialer)
Definition: CommCalls.h:342
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:961
static void whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
Definition: whois.cc:49
#define SQUIDSBUFPH
Definition: SBuf.h:31
#define xfree
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
struct StatCounters::@128::@137 all
StoreEntry * entry
Definition: FwdState.h:156
int commSetConnTimeout(const Comm::ConnectionPointer &conn, int timeout, AsyncCall::Pointer &callback)
Definition: comm.cc:552
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
uint32_t sources
The message sources.
Definition: Message.h:100
time_t read
Definition: SquidConfig.h:106
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:35
void lock(const char *context)
Definition: store.cc:439

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors