OptXact.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 93 ICAP (RFC 3507) Client */
10 
11 #include "squid.h"
12 #include "adaptation/Answer.h"
13 #include "adaptation/icap/Config.h"
16 #include "base/TextException.h"
17 #include "comm.h"
18 #include "HttpHeaderTools.h"
19 #include "HttpReply.h"
20 #include "HttpRequest.h"
21 #include "SquidTime.h"
22 
25 
27  AsyncJob("Adaptation::Icap::OptXact"),
28  Adaptation::Icap::Xaction("Adaptation::Icap::OptXact", aService),
29  readAll(false)
30 {
31 }
32 
34 {
36 
38 }
39 
41 {
42  scheduleRead();
43 
44  MemBuf requestBuf;
45  requestBuf.init();
46  makeRequest(requestBuf);
47  debugs(93, 9, HERE << "request " << status() << ":\n" <<
48  (requestBuf.terminate(), requestBuf.content()));
50  scheduleWrite(requestBuf);
51 }
52 
54 {
55  const Adaptation::Service &s = service();
56  const String uri = s.cfg().uri;
57  buf.appendf("OPTIONS " SQUIDSTRINGPH " ICAP/1.0\r\n", SQUIDSTRINGPRINT(uri));
58  const String host = s.cfg().host;
59  buf.appendf("Host: " SQUIDSTRINGPH ":%d\r\n", SQUIDSTRINGPRINT(host), s.cfg().port);
60 
62  buf.append("Connection: close\r\n", 19);
63 
64  buf.append("Allow: ", 7);
66  buf.append("206, ", 5);
67  buf.append("trailers\r\n", 10);
68  buf.append(ICAP::crlf, 2);
69 
70  // XXX: HttpRequest cannot fully parse ICAP Request-Line
71  Http::StatusCode reqStatus;
72  buf.terminate(); // Http::Message::parse requires terminated buffer
73  Must(icapRequest->parse(buf.content(), buf.contentSize(), true, &reqStatus) > 0);
74 }
75 
77 {
78  debugs(93, 9, HERE << "finished writing " << size <<
79  "-byte request " << status());
80 }
81 
82 // comm module read a portion of the ICAP response for us
84 {
85  if (parseResponse()) {
86  Must(icapReply != NULL);
87  // We read everything if there is no response body. If there is a body,
88  // we cannot parse it because we do not support any opt-body-types, so
89  // we leave readAll false which forces connection closure.
90  readAll = icapReply->header.getByNameListMember("Encapsulated",
91  "opt-body", ',').isEmpty();
92  debugs(93, 7, HERE << "readAll=" << readAll);
96  Must(done()); // there should be nothing else to do
97  return;
98  }
99 
100  scheduleRead();
101 }
102 
104 {
105  debugs(93, 5, "have " << readBuf.length() << " bytes to parse" << status());
106  debugs(93, DBG_DATA, "\n" << readBuf);
107 
109  r->protoPrefix = "ICAP/"; // TODO: make an IcapReply class?
110 
111  if (!parseHttpMsg(r.getRaw())) // throws on errors
112  return false;
113 
114  static SBuf close("close", 5);
115  if (httpHeaderHasConnDir(&r->header, close))
116  reuseConnection = false;
117 
118  icapReply = r;
119  return true;
120 }
121 
123 {
125 }
126 
128 {
129  // al.cache.caddr = 0;
131 
134 
136 }
137 
138 /* Adaptation::Icap::OptXactLauncher */
139 
141  AsyncJob("Adaptation::Icap::OptXactLauncher"),
142  Adaptation::Icap::Launcher("Adaptation::Icap::OptXactLauncher", aService)
143 {
144 }
145 
147 {
150  Must(s != NULL);
151  return new Adaptation::Icap::OptXact(s);
152 }
153 
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:404
#define SQUIDSTRINGPH
Definition: SquidString.h:21
#define SQUIDSTRINGPRINT(s)
Definition: SquidString.h:22
class AccessLogEntry::IcapLogEntry icap
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:216
static Answer Forward(Http::Message *aMsg)
create an akForward answer
Definition: Answer.cc:26
virtual void handleCommWrote(size_t size)
Definition: OptXact.cc:76
virtual void start()
called by AsyncStart; do not call directly
Definition: Xaction.cc:143
int hdr_sz
Definition: Message.h:82
Definition: SBuf.h:86
HttpReply::Pointer icapReply
received ICAP reply, if any
Definition: Xaction.h:58
virtual void finalizeLogInfo()
Definition: OptXact.cc:127
Config TheConfig
Definition: Config.cc:19
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
bool isEmpty() const
Definition: SBuf.h:420
String protoPrefix
Definition: HttpReply.h:64
struct timeval current_time
Definition: stub_time.cc:15
virtual const char * status() const
internal cleanup; do not call directly
Definition: Xaction.cc:663
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:96
bool parseHttpMsg(Http::Message *msg)
Definition: Xaction.cc:523
bool httpHeaderHasConnDir(const HttpHeader *hdr, const SBuf &directive)
OptXactLauncher(Adaptation::ServicePointer aService)
Definition: OptXact.cc:140
const XactOutcome xoOpt
OPTION transaction.
Definition: Elements.cc:22
ServiceRep & service()
Definition: Xaction.cc:123
virtual void swanSong()
Definition: Xaction.cc:598
StatusCode
Definition: StatusCode.h:20
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
virtual void handleCommConnected()
Definition: OptXact.cc:40
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
OptXact(ServiceRep::Pointer &aService)
Definition: OptXact.cc:26
SBuf getByNameListMember(const char *name, const char *member, const char separator) const
Definition: HttpHeader.cc:957
void makeRequest(MemBuf &buf)
Definition: OptXact.cc:53
virtual Xaction * createXaction()
Definition: OptXact.cc:146
virtual void start()
called by AsyncStart; do not call directly
Definition: OptXact.cc:33
char * content()
start of the added data
Definition: MemBuf.h:41
void const char * buf
Definition: stub_helper.cc:16
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:153
HttpHeader header
Definition: Message.h:75
bool readAll
read the entire OPTIONS response
Definition: OptXact.h:49
const CharacterSet crlf("crlf","\")
Definition: Elements.cc:12
void sendAnswer(const Answer &answer)
Definition: Initiate.cc:79
void scheduleWrite(MemBuf &buf)
Definition: Xaction.cc:331
HttpRequest * icapRequest
sent (or at least created) ICAP request
Definition: Xaction.h:57
Adaptation::ServicePointer theService
ICAP service for all launches.
Definition: Launcher.h:75
C * getRaw() const
Definition: RefCount.h:74
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
void setOutcome(const XactOutcome &xo)
Definition: Xaction.cc:585
Definition: MemBuf.h:23
AccessLogEntry & al
short for *alep
Definition: Xaction.h:149
bool parse(const char *buf, const size_t sz, bool eol, Http::StatusCode *error)
Definition: Message.cc:80
CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, OptXact)
virtual void finalizeLogInfo()
Definition: Xaction.cc:640
const ServiceConfig & cfg() const
Definition: Service.h:51
#define DBG_DATA
Definition: Debug.h:48
Adaptation::Icap::ICAP::Method reqMethod
ICAP request method.
virtual void handleCommRead(size_t size)
Definition: OptXact.cc:83
bool done() const
the job is destroyed in callEnd() when done()
Definition: AsyncJob.cc:90
#define NULL
Definition: types.h:166
virtual void swanSong()
Definition: OptXact.cc:122
int size
Definition: ModDevPoll.cc:77
#define false
Definition: GnuRegex.c:233
int64_t bytesRead
number of bytes read from ICAP server so far
void terminate()
Definition: MemBuf.cc:250

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors