]> git.ipfire.org Git - thirdparty/squid.git/blame - src/adaptation/ecap/MessageRep.cc
Updated stale "Ssl" text to make the comment match the code again.
[thirdparty/squid.git] / src / adaptation / ecap / MessageRep.cc
CommitLineData
fdc96a39 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
bbc27441
AJ
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.
fdc96a39 7 */
bbc27441
AJ
8
9/* DEBUG: section 93 eCAP Interface */
10
582c2af2 11#include "squid.h"
cf331c99 12#include "BodyPipe.h"
602d9612
A
13#include "HttpReply.h"
14#include "HttpRequest.h"
cf331c99
AR
15#include <libecap/common/names.h>
16#include <libecap/common/area.h>
17#include <libecap/common/version.h>
12a410c3 18#include <libecap/common/named_values.h>
602d9612 19#include "adaptation/ecap/Host.h" /* for protocol constants */
1f3c65fc
AR
20#include "adaptation/ecap/MessageRep.h"
21#include "adaptation/ecap/XactionRep.h"
3d93a84d 22#include "base/TextException.h"
b1bd952a 23#include "URL.h"
fdc96a39 24
cf331c99
AR
25/* HeaderRep */
26
574b508c 27Adaptation::Ecap::HeaderRep::HeaderRep(HttpMsg &aMessage): theHeader(aMessage.header),
f53969cc 28 theMessage(aMessage)
cf331c99
AR
29{
30}
31
cf331c99 32bool
574b508c 33Adaptation::Ecap::HeaderRep::hasAny(const Name &name) const
cf331c99 34{
789217a2 35 const Http::HdrType squidId = TranslateHeaderId(name);
cf331c99 36 // XXX: optimize to remove getByName: we do not need the value here
789217a2 37 return squidId == Http::HdrType::OTHER ?
26ac0430
AJ
38 theHeader.getByName(name.image().c_str()).size() > 0:
39 (bool)theHeader.has(squidId);
cf331c99
AR
40}
41
574b508c
AR
42Adaptation::Ecap::HeaderRep::Value
43Adaptation::Ecap::HeaderRep::value(const Name &name) const
cf331c99 44{
789217a2
FC
45 const Http::HdrType squidId = TranslateHeaderId(name);
46 const String value = squidId == Http::HdrType::OTHER ?
26ac0430
AJ
47 theHeader.getByName(name.image().c_str()) :
48 theHeader.getStrOrList(squidId);
b38b26cb 49 return value.size() > 0 ?
001eaf80 50 Value::FromTempString(value.termedBuf()) : Value();
cf331c99
AR
51}
52
53void
574b508c 54Adaptation::Ecap::HeaderRep::add(const Name &name, const Value &value)
cf331c99 55{
789217a2 56 const Http::HdrType squidId = TranslateHeaderId(name); // Http::HdrType::OTHER OK
cf331c99 57 HttpHeaderEntry *e = new HttpHeaderEntry(squidId, name.image().c_str(),
26ac0430 58 value.toString().c_str());
cf331c99 59 theHeader.addEntry(e);
809fd5d3 60
789217a2
FC
61 if (squidId == Http::HdrType::CONTENT_LENGTH)
62 theMessage.content_length = theHeader.getInt64(Http::HdrType::CONTENT_LENGTH);
cf331c99
AR
63}
64
65void
574b508c 66Adaptation::Ecap::HeaderRep::removeAny(const Name &name)
cf331c99 67{
789217a2
FC
68 const Http::HdrType squidId = TranslateHeaderId(name);
69 if (squidId == Http::HdrType::OTHER)
cf331c99
AR
70 theHeader.delByName(name.image().c_str());
71 else
72 theHeader.delById(squidId);
809fd5d3 73
789217a2
FC
74 if (squidId == Http::HdrType::CONTENT_LENGTH)
75 theMessage.content_length = theHeader.getInt64(Http::HdrType::CONTENT_LENGTH);
cf331c99
AR
76}
77
12a410c3
AR
78void
79Adaptation::Ecap::HeaderRep::visitEach(libecap::NamedValueVisitor &visitor) const
80{
81 HttpHeaderPos pos = HttpHeaderInitPos;
82 while (HttpHeaderEntry *e = theHeader.getEntry(&pos)) {
83 const Name name(e->name.termedBuf()); // optimize: find std Names
84 name.assignHostId(e->id);
85 visitor.visit(name, Value(e->value.rawBuf(), e->value.size()));
86 }
87}
88
cf331c99 89libecap::Area
574b508c 90Adaptation::Ecap::HeaderRep::image() const
cf331c99
AR
91{
92 MemBuf mb;
93 mb.init();
10201568 94 theMessage.packInto(&mb, true);
cf331c99
AR
95 return Area::FromTempBuffer(mb.content(), mb.contentSize());
96}
97
98// throws on failures
99void
574b508c 100Adaptation::Ecap::HeaderRep::parse(const Area &buf)
cf331c99 101{
955394ce 102 Http::StatusCode error;
9b491ddf 103 Must(theMessage.parse(buf.start, buf.size, true, &error));
cf331c99
AR
104}
105
789217a2 106Http::HdrType
574b508c 107Adaptation::Ecap::HeaderRep::TranslateHeaderId(const Name &name)
77773405
AR
108{
109 if (name.assignedHostId())
789217a2
FC
110 return static_cast<Http::HdrType>(name.hostId());
111 return Http::HdrType::OTHER;
77773405
AR
112}
113
77773405
AR
114/* FirstLineRep */
115
574b508c 116Adaptation::Ecap::FirstLineRep::FirstLineRep(HttpMsg &aMessage): theMessage(aMessage)
77773405
AR
117{
118}
119
cf331c99 120libecap::Version
574b508c 121Adaptation::Ecap::FirstLineRep::version() const
cf331c99
AR
122{
123 return libecap::Version(theMessage.http_ver.major,
26ac0430 124 theMessage.http_ver.minor);
cf331c99
AR
125}
126
127void
574b508c 128Adaptation::Ecap::FirstLineRep::version(const libecap::Version &aVersion)
cf331c99
AR
129{
130 theMessage.http_ver.major = aVersion.majr;
131 theMessage.http_ver.minor = aVersion.minr;
132}
133
134libecap::Name
574b508c 135Adaptation::Ecap::FirstLineRep::protocol() const
cf331c99
AR
136{
137 // TODO: optimize?
7b167513 138 switch (theMessage.http_ver.protocol) {
39a19cb7 139 case AnyP::PROTO_HTTP:
26ac0430 140 return libecap::protocolHttp;
39a19cb7 141 case AnyP::PROTO_HTTPS:
26ac0430 142 return libecap::protocolHttps;
39a19cb7 143 case AnyP::PROTO_FTP:
26ac0430 144 return libecap::protocolFtp;
39a19cb7 145 case AnyP::PROTO_GOPHER:
26ac0430 146 return libecap::protocolGopher;
39a19cb7 147 case AnyP::PROTO_WAIS:
26ac0430 148 return libecap::protocolWais;
39a19cb7 149 case AnyP::PROTO_WHOIS:
26ac0430 150 return libecap::protocolWhois;
39a19cb7 151 case AnyP::PROTO_URN:
26ac0430 152 return libecap::protocolUrn;
39a19cb7 153 case AnyP::PROTO_ICP:
26ac0430 154 return protocolIcp;
cf331c99 155#if USE_HTCP
39a19cb7 156 case AnyP::PROTO_HTCP:
26ac0430 157 return protocolHtcp;
cf331c99 158#endif
39a19cb7 159 case AnyP::PROTO_CACHE_OBJECT:
26ac0430 160 return protocolCacheObj;
39a19cb7 161 case AnyP::PROTO_ICY:
555aedbf 162 return protocolIcy;
a90ea541
AJ
163 case AnyP::PROTO_COAP:
164 case AnyP::PROTO_COAPS: // use 'unknown' until libecap supports coap:// and coaps://
555aedbf
AR
165 case AnyP::PROTO_UNKNOWN:
166 return protocolUnknown; // until we remember the protocol image
39a19cb7 167 case AnyP::PROTO_NONE:
26ac0430
AJ
168 return Name();
169
39a19cb7 170 case AnyP::PROTO_MAX:
26ac0430 171 break; // should not happen
39a19cb7 172 // no default to catch AnyP::PROTO_ additions
cf331c99
AR
173 }
174 Must(false); // not reached
175 return Name();
176}
177
178void
574b508c 179Adaptation::Ecap::FirstLineRep::protocol(const Name &p)
cf331c99
AR
180{
181 // TODO: what happens if we fail to translate some protocol?
7b167513 182 theMessage.http_ver.protocol = TranslateProtocolId(p);
cf331c99
AR
183}
184
555aedbf 185AnyP::ProtocolType
574b508c 186Adaptation::Ecap::FirstLineRep::TranslateProtocolId(const Name &name)
77773405
AR
187{
188 if (name.assignedHostId())
555aedbf
AR
189 return static_cast<AnyP::ProtocolType>(name.hostId());
190 return AnyP::PROTO_UNKNOWN;
77773405
AR
191}
192
cf331c99
AR
193/* RequestHeaderRep */
194
574b508c 195Adaptation::Ecap::RequestLineRep::RequestLineRep(HttpRequest &aMessage):
f53969cc 196 FirstLineRep(aMessage), theMessage(aMessage)
cf331c99
AR
197{
198}
199
200void
574b508c 201Adaptation::Ecap::RequestLineRep::uri(const Area &aUri)
cf331c99
AR
202{
203 // TODO: if method is not set, urlPath will assume it is not connect;
204 // Can we change urlParse API to remove the method parameter?
205 // TODO: optimize: urlPath should take constant URL buffer
206 char *buf = xstrdup(aUri.toString().c_str());
207 const bool ok = urlParse(theMessage.method, buf, &theMessage);
208 xfree(buf);
209 Must(ok);
210}
211
574b508c
AR
212Adaptation::Ecap::RequestLineRep::Area
213Adaptation::Ecap::RequestLineRep::uri() const
cf331c99 214{
851feda6
AJ
215 const SBuf &fullUrl = theMessage.effectiveRequestUri();
216 // XXX: effectiveRequestUri() cannot return NULL or even empty string, some other problem?
217 Must(!fullUrl.isEmpty());
dd31ab8f 218 // optimize: avoid copying by having an Area::Detail that locks theMessage
851feda6 219 return Area::FromTempBuffer(fullUrl.rawContent(), fullUrl.length());
cf331c99
AR
220}
221
222void
574b508c 223Adaptation::Ecap::RequestLineRep::method(const Name &aMethod)
cf331c99
AR
224{
225 if (aMethod.assignedHostId()) {
226 const int id = aMethod.hostId();
23ef3d70
FC
227 Must(Http::METHOD_NONE < id && id < Http::METHOD_ENUM_END);
228 Must(id != Http::METHOD_OTHER);
c2a7cefd 229 theMessage.method = HttpRequestMethod(static_cast<Http::MethodType>(id));
cf331c99
AR
230 } else {
231 const std::string &image = aMethod.image();
f42a67c5 232 theMessage.method.HttpRequestMethodXXX(image.c_str());
cf331c99
AR
233 }
234}
235
574b508c
AR
236Adaptation::Ecap::RequestLineRep::Name
237Adaptation::Ecap::RequestLineRep::method() const
cf331c99
AR
238{
239 switch (theMessage.method.id()) {
23ef3d70 240 case Http::METHOD_GET:
26ac0430 241 return libecap::methodGet;
23ef3d70 242 case Http::METHOD_POST:
26ac0430 243 return libecap::methodPost;
23ef3d70 244 case Http::METHOD_PUT:
26ac0430 245 return libecap::methodPut;
23ef3d70 246 case Http::METHOD_HEAD:
26ac0430 247 return libecap::methodHead;
23ef3d70 248 case Http::METHOD_CONNECT:
26ac0430 249 return libecap::methodConnect;
23ef3d70 250 case Http::METHOD_DELETE:
26ac0430 251 return libecap::methodDelete;
23ef3d70 252 case Http::METHOD_TRACE:
26ac0430
AJ
253 return libecap::methodTrace;
254 default:
e44adabd 255 return Name(theMessage.method.image().toStdString());
cf331c99
AR
256 }
257}
258
77773405 259libecap::Version
574b508c 260Adaptation::Ecap::RequestLineRep::version() const
77773405
AR
261{
262 return FirstLineRep::version();
263}
264
265void
574b508c 266Adaptation::Ecap::RequestLineRep::version(const libecap::Version &aVersion)
77773405
AR
267{
268 FirstLineRep::version(aVersion);
269}
270
271libecap::Name
574b508c 272Adaptation::Ecap::RequestLineRep::protocol() const
77773405
AR
273{
274 return FirstLineRep::protocol();
275}
276
277void
574b508c 278Adaptation::Ecap::RequestLineRep::protocol(const Name &p)
77773405
AR
279{
280 FirstLineRep::protocol(p);
281}
282
cf331c99
AR
283/* ReplyHeaderRep */
284
574b508c 285Adaptation::Ecap::StatusLineRep::StatusLineRep(HttpReply &aMessage):
f53969cc 286 FirstLineRep(aMessage), theMessage(aMessage)
cf331c99
AR
287{
288}
289
290void
574b508c 291Adaptation::Ecap::StatusLineRep::statusCode(int code)
cf331c99 292{
9b769c67 293 theMessage.sline.set(theMessage.sline.version, static_cast<Http::StatusCode>(code), theMessage.sline.reason());
cf331c99
AR
294}
295
296int
574b508c 297Adaptation::Ecap::StatusLineRep::statusCode() const
cf331c99 298{
9b769c67
AJ
299 // TODO: remove cast when possible
300 return static_cast<int>(theMessage.sline.status());
cf331c99
AR
301}
302
303void
9b769c67 304Adaptation::Ecap::StatusLineRep::reasonPhrase(const Area &str)
cf331c99 305{
f9632290 306 theMessage.sline.set(theMessage.sline.version, theMessage.sline.status(), str.toString().c_str());
cf331c99
AR
307}
308
574b508c
AR
309Adaptation::Ecap::StatusLineRep::Area
310Adaptation::Ecap::StatusLineRep::reasonPhrase() const
cf331c99 311{
9b769c67 312 return Area::FromTempString(std::string(theMessage.sline.reason()));
cf331c99
AR
313}
314
77773405 315libecap::Version
574b508c 316Adaptation::Ecap::StatusLineRep::version() const
77773405
AR
317{
318 return FirstLineRep::version();
319}
320
321void
574b508c 322Adaptation::Ecap::StatusLineRep::version(const libecap::Version &aVersion)
77773405
AR
323{
324 FirstLineRep::version(aVersion);
325}
326
327libecap::Name
574b508c 328Adaptation::Ecap::StatusLineRep::protocol() const
77773405
AR
329{
330 return FirstLineRep::protocol();
331}
332
333void
574b508c 334Adaptation::Ecap::StatusLineRep::protocol(const Name &p)
77773405
AR
335{
336 FirstLineRep::protocol(p);
337}
cf331c99
AR
338
339/* BodyRep */
340
574b508c 341Adaptation::Ecap::BodyRep::BodyRep(const BodyPipe::Pointer &aBody): theBody(aBody)
cf331c99
AR
342{
343}
344
77773405 345void
574b508c 346Adaptation::Ecap::BodyRep::tie(const BodyPipe::Pointer &aBody)
77773405
AR
347{
348 Must(!theBody);
349 Must(aBody != NULL);
350 theBody = aBody;
351}
352
574b508c
AR
353Adaptation::Ecap::BodyRep::BodySize
354Adaptation::Ecap::BodyRep::bodySize() const
cf331c99 355{
e4a88e44 356 return (theBody != nullptr && theBody->bodySizeKnown()) ? BodySize(theBody->bodySize()) : BodySize();
cf331c99
AR
357}
358
cf331c99
AR
359/* MessageRep */
360
574b508c 361Adaptation::Ecap::MessageRep::MessageRep(HttpMsg *rawHeader):
f53969cc
SM
362 theMessage(rawHeader), theFirstLineRep(NULL),
363 theHeaderRep(NULL), theBodyRep(NULL)
cf331c99
AR
364{
365 Must(theMessage.header); // we do not want to represent a missing message
366
367 if (HttpRequest *req = dynamic_cast<HttpRequest*>(theMessage.header))
77773405 368 theFirstLineRep = new RequestLineRep(*req);
e1381638
AJ
369 else if (HttpReply *rep = dynamic_cast<HttpReply*>(theMessage.header))
370 theFirstLineRep = new StatusLineRep(*rep);
cf331c99 371 else
e1381638 372 Must(false); // unknown message header type
cf331c99 373
77773405
AR
374 theHeaderRep = new HeaderRep(*theMessage.header);
375
cf331c99
AR
376 if (theMessage.body_pipe != NULL)
377 theBodyRep = new BodyRep(theMessage.body_pipe);
378}
379
574b508c 380Adaptation::Ecap::MessageRep::~MessageRep()
cf331c99 381{
77773405 382 delete theBodyRep;
80031342
AR
383 delete theHeaderRep;
384 delete theFirstLineRep;
77773405
AR
385}
386
387libecap::shared_ptr<libecap::Message>
574b508c 388Adaptation::Ecap::MessageRep::clone() const
77773405
AR
389{
390 HttpMsg *hdr = theMessage.header->clone();
391 hdr->body_pipe = NULL; // if any; TODO: remove pipe cloning from ::clone?
392 libecap::shared_ptr<libecap::Message> res(new MessageRep(hdr));
393
394 // restore indication of a body if needed, but not the pipe
395 if (theMessage.header->body_pipe != NULL)
396 res->addBody();
397
398 return res;
399}
400
401libecap::FirstLine &
574b508c 402Adaptation::Ecap::MessageRep::firstLine()
77773405
AR
403{
404 return *theFirstLineRep;
405}
406
407const libecap::FirstLine &
574b508c 408Adaptation::Ecap::MessageRep::firstLine() const
77773405
AR
409{
410 return *theFirstLineRep;
cf331c99
AR
411}
412
413libecap::Header &
574b508c 414Adaptation::Ecap::MessageRep::header()
cf331c99
AR
415{
416 return *theHeaderRep;
417}
418
419const libecap::Header &
574b508c 420Adaptation::Ecap::MessageRep::header() const
cf331c99
AR
421{
422 return *theHeaderRep;
423}
424
425libecap::Body *
574b508c 426Adaptation::Ecap::MessageRep::body()
cf331c99
AR
427{
428 return theBodyRep;
429}
430
431void
574b508c 432Adaptation::Ecap::MessageRep::addBody()
cf331c99 433{
cf331c99 434 Must(!theBodyRep);
77773405
AR
435 Must(!theMessage.body_pipe); // set in tieBody()
436 theBodyRep = new BodyRep(NULL);
437}
438
439void
574b508c 440Adaptation::Ecap::MessageRep::tieBody(Adaptation::Ecap::XactionRep *x)
77773405
AR
441{
442 Must(theBodyRep != NULL); // addBody must be called first
ce367911 443 Must(!theMessage.header->body_pipe);
cf331c99 444 Must(!theMessage.body_pipe);
ce367911
AR
445 theMessage.header->body_pipe = new BodyPipe(x);
446 theMessage.body_pipe = theMessage.header->body_pipe;
77773405 447 theBodyRep->tie(theMessage.body_pipe);
cf331c99
AR
448}
449
574b508c 450const libecap::Body *Adaptation::Ecap::MessageRep::body() const
cf331c99
AR
451{
452 return theBodyRep;
453}
f53969cc 454