OptXact.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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 
24 
26  AsyncJob("Adaptation::Icap::OptXact"),
27  Adaptation::Icap::Xaction("Adaptation::Icap::OptXact", aService),
28  readAll(false)
29 {
30 }
31 
33 {
35 
36  openConnection();
37 }
38 
40 {
41  scheduleRead();
42 
43  MemBuf requestBuf;
44  requestBuf.init();
45  makeRequest(requestBuf);
46  debugs(93, 9, "request " << status() << ":\n" <<
47  (requestBuf.terminate(), requestBuf.content()));
48  icap_tio_start = current_time;
49  scheduleWrite(requestBuf);
50 }
51 
53 {
54  const Adaptation::Service &s = service();
55  const String uri = s.cfg().uri;
56  buf.appendf("OPTIONS " SQUIDSTRINGPH " ICAP/1.0\r\n", SQUIDSTRINGPRINT(uri));
57  const String host = s.cfg().host;
58  buf.appendf("Host: " SQUIDSTRINGPH ":%d\r\n", SQUIDSTRINGPRINT(host), s.cfg().port);
59 
61  buf.append("Connection: close\r\n", 19);
62 
63  buf.append("Allow: ", 7);
65  buf.append("206, ", 5);
66  buf.append("trailers\r\n", 10);
67  buf.append(ICAP::crlf, 2);
68 
69  // XXX: HttpRequest cannot fully parse ICAP Request-Line
70  Http::StatusCode reqStatus;
71  buf.terminate(); // Http::Message::parse requires terminated buffer
72  Must(icapRequest->parse(buf.content(), buf.contentSize(), true, &reqStatus) > 0);
73 }
74 
76 {
77  debugs(93, 9, "finished writing " << size <<
78  "-byte request " << status());
79 }
80 
81 // comm module read a portion of the ICAP response for us
83 {
84  if (parseResponse()) {
85  Must(icapReply != NULL);
86  // We read everything if there is no response body. If there is a body,
87  // we cannot parse it because we do not support any opt-body-types, so
88  // we leave readAll false which forces connection closure.
89  readAll = icapReply->header.getByNameListMember("Encapsulated",
90  "opt-body", ',').isEmpty();
91  debugs(93, 7, "readAll=" << readAll);
92  icap_tio_finish = current_time;
93  setOutcome(xoOpt);
94  sendAnswer(Answer::Forward(icapReply.getRaw()));
95  Must(done()); // there should be nothing else to do
96  return;
97  }
98 
99  scheduleRead();
100 }
101 
103 {
104  debugs(93, 5, "have " << readBuf.length() << " bytes to parse" << status());
105  debugs(93, DBG_DATA, "\n" << readBuf);
106 
108  r->protoPrefix = "ICAP/"; // TODO: make an IcapReply class?
109 
110  if (!parseHttpMsg(r.getRaw())) // throws on errors
111  return false;
112 
113  static SBuf close("close", 5);
114  if (httpHeaderHasConnDir(&r->header, close))
115  reuseConnection = false;
116 
117  icapReply = r;
118  return true;
119 }
120 
122 {
124 }
125 
127 {
128  // al.cache.caddr = 0;
129  al.icap.reqMethod = Adaptation::methodOptions;
130 
131  if (icapReply != NULL && al.icap.bytesRead > icapReply->hdr_sz)
132  al.icap.bodyBytesRead = al.icap.bytesRead - icapReply->hdr_sz;
133 
135 }
136 
137 /* Adaptation::Icap::OptXactLauncher */
138 
140  AsyncJob("Adaptation::Icap::OptXactLauncher"),
141  Adaptation::Icap::Launcher("Adaptation::Icap::OptXactLauncher", aService)
142 {
143 }
144 
146 {
148  dynamic_cast<Adaptation::Icap::ServiceRep*>(theService.getRaw());
149  Must(s != NULL);
150  return new Adaptation::Icap::OptXact(s);
151 }
152 
CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, OptXact)
virtual Xaction * createXaction()
Definition: OptXact.cc:145
void terminate()
Definition: MemBuf.cc:241
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
virtual void handleCommRead(size_t size)
Definition: OptXact.cc:82
OptXactLauncher(Adaptation::ServicePointer aService)
Definition: OptXact.cc:139
Config TheConfig
Definition: Config.cc:19
virtual void startShoveling()
starts sending/receiving ICAP messages
Definition: OptXact.cc:39
OptXact(ServiceRep::Pointer &aService)
Definition: OptXact.cc:25
HttpHeader header
Definition: Message.h:75
void makeRequest(MemBuf &buf)
Definition: OptXact.cc:52
#define SQUIDSTRINGPRINT(s)
Definition: SquidString.h:22
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:93
Definition: SBuf.h:94
C * getRaw() const
Definition: RefCount.h:80
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:209
virtual void start()
called by AsyncStart; do not call directly
Definition: OptXact.cc:32
StatusCode
Definition: StatusCode.h:20
#define SQUIDSTRINGPH
Definition: SquidString.h:21
#define DBG_DATA
Definition: Stream.h:43
bool httpHeaderHasConnDir(const HttpHeader *hdr, const SBuf &directive)
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
int size
Definition: ModDevPoll.cc:75
struct timeval current_time
the current UNIX time in timeval {seconds, microseconds} format
Definition: gadgets.cc:17
#define NULL
Definition: types.h:166
virtual void handleCommWrote(size_t size)
Definition: OptXact.cc:75
Definition: MemBuf.h:24
virtual void swanSong()
Definition: OptXact.cc:121
static Answer Forward(Http::Message *aMsg)
create an akForward answer
Definition: Answer.cc:26
virtual void finalizeLogInfo()
Definition: Xaction.cc:609
@ methodOptions
Definition: Elements.h:17
String protoPrefix
Definition: HttpReply.h:60
char * content()
start of the added data
Definition: MemBuf.h:41
#define Must(condition)
Definition: TextException.h:71
virtual void swanSong()
Definition: Xaction.cc:570
const ServiceConfig & cfg() const
Definition: Service.h:51
#define false
Definition: GnuRegex.c:233
virtual void finalizeLogInfo()
Definition: OptXact.cc:126
const XactOutcome xoOpt
OPTION transaction.
Definition: Elements.cc:22
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
virtual void start()
called by AsyncStart; do not call directly
Definition: Xaction.cc:140
const CharacterSet crlf("crlf","\r\n")

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors